Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various bugfixes due to humans being humans #54

Merged
merged 4 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading