Skip to content

Commit

Permalink
Merge pull request #54 from onigoetz/main
Browse files Browse the repository at this point in the history
Various bugfixes due to humans being humans
  • Loading branch information
ewilan-riviere authored Jan 18, 2025
2 parents cd75239 + 39b3b5d commit 4470f17
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
26 changes: 25 additions & 1 deletion src/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,31 @@ public static function read(string $path, ?string $password = null): BaseArchive
ArchiveEnum::pdf => ArchivePdf::class,
};

return $archive::read($self->path, $self->password);
try {
return $archive::read($self->path, $self->password);
} catch (\Throwable $originalException) {
if ($self->type === ArchiveEnum::zip && $extension === 'cbz') {
try {
// Sometimes files with cbz extension are actually misnamed rar files
return ArchiveRar::read($self->path, $self->password);
} catch (\Throwable) {
// If it's not a rar file, throw the original exception
throw $originalException;
}
}

if ($self->type === ArchiveEnum::rar && $extension === 'cbr') {
try {
// Sometimes files with cbr extension are actually misnamed zip files
return ArchiveZip::read($self->path, $self->password);
} catch (\Throwable) {
// If it's not a zip file, throw the original exception
throw $originalException;
}
}

throw $originalException;
}
}

/**
Expand Down
24 changes: 23 additions & 1 deletion src/Processes/SevenZipProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@ public static function test(bool $exception = true): bool
return true;
}

/**
* Escapes a string to be used as a shell argument.
*/
private function escapeArgument(?string $argument): string
{
if ('' === $argument || null === $argument) {
return '""';
}
if ('\\' !== \DIRECTORY_SEPARATOR) {
return "'".str_replace("'", "'\\''", $argument)."'";
}
if (str_contains($argument, "\0")) {
$argument = str_replace("\0", '?', $argument);
}
if (!preg_match('/[()%!^"<>&|\s]/', $argument)) {
return $argument;
}
$argument = preg_replace('/(\\\\+)$/', '$1$1', $argument);

return '"'.str_replace(['"', '^', '%', '!', "\n"], ['""', '"^^"', '"^%"', '"^!"', '!LF!'], $argument).'"';
}

/**
* @param string[] $args
* @return string[]
Expand All @@ -82,7 +104,7 @@ public function execute(string $command, array $args): array
$command = $this->binaryPath;
}

$command = "{$command} ".implode(' ', $args);
$command = "{$command} ".implode(' ', array_map($this->escapeArgument(...), $args));

try {
exec($command, $output, $res);
Expand Down
3 changes: 1 addition & 2 deletions src/Readers/ArchivePdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public function getContents(?ArchiveItem $file, bool $toBase64 = false): ?string
$imagick->clear();
$imagick->destroy();
} catch (\Throwable $th) {
// throw new \Exception("Error, {$file->getFilename()} is not an image");
error_log("Error, {$file->getFilename()} is not an image");
error_log("Error, {$file->getFilename()} Failed to extract page: {$th->getMessage()}");
}

if (! $content) {
Expand Down

0 comments on commit 4470f17

Please sign in to comment.