Skip to content

Commit 4470f17

Browse files
Merge pull request #54 from onigoetz/main
Various bugfixes due to humans being humans
2 parents cd75239 + 39b3b5d commit 4470f17

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

src/Archive.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,31 @@ public static function read(string $path, ?string $password = null): BaseArchive
4343
ArchiveEnum::pdf => ArchivePdf::class,
4444
};
4545

46-
return $archive::read($self->path, $self->password);
46+
try {
47+
return $archive::read($self->path, $self->password);
48+
} catch (\Throwable $originalException) {
49+
if ($self->type === ArchiveEnum::zip && $extension === 'cbz') {
50+
try {
51+
// Sometimes files with cbz extension are actually misnamed rar files
52+
return ArchiveRar::read($self->path, $self->password);
53+
} catch (\Throwable) {
54+
// If it's not a rar file, throw the original exception
55+
throw $originalException;
56+
}
57+
}
58+
59+
if ($self->type === ArchiveEnum::rar && $extension === 'cbr') {
60+
try {
61+
// Sometimes files with cbr extension are actually misnamed zip files
62+
return ArchiveZip::read($self->path, $self->password);
63+
} catch (\Throwable) {
64+
// If it's not a zip file, throw the original exception
65+
throw $originalException;
66+
}
67+
}
68+
69+
throw $originalException;
70+
}
4771
}
4872

4973
/**

src/Processes/SevenZipProcess.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,28 @@ public static function test(bool $exception = true): bool
6666
return true;
6767
}
6868

69+
/**
70+
* Escapes a string to be used as a shell argument.
71+
*/
72+
private function escapeArgument(?string $argument): string
73+
{
74+
if ('' === $argument || null === $argument) {
75+
return '""';
76+
}
77+
if ('\\' !== \DIRECTORY_SEPARATOR) {
78+
return "'".str_replace("'", "'\\''", $argument)."'";
79+
}
80+
if (str_contains($argument, "\0")) {
81+
$argument = str_replace("\0", '?', $argument);
82+
}
83+
if (!preg_match('/[()%!^"<>&|\s]/', $argument)) {
84+
return $argument;
85+
}
86+
$argument = preg_replace('/(\\\\+)$/', '$1$1', $argument);
87+
88+
return '"'.str_replace(['"', '^', '%', '!', "\n"], ['""', '"^^"', '"^%"', '"^!"', '!LF!'], $argument).'"';
89+
}
90+
6991
/**
7092
* @param string[] $args
7193
* @return string[]
@@ -82,7 +104,7 @@ public function execute(string $command, array $args): array
82104
$command = $this->binaryPath;
83105
}
84106

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

87109
try {
88110
exec($command, $output, $res);

src/Readers/ArchivePdf.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ public function getContents(?ArchiveItem $file, bool $toBase64 = false): ?string
8484
$imagick->clear();
8585
$imagick->destroy();
8686
} catch (\Throwable $th) {
87-
// throw new \Exception("Error, {$file->getFilename()} is not an image");
88-
error_log("Error, {$file->getFilename()} is not an image");
87+
error_log("Error, {$file->getFilename()} Failed to extract page: {$th->getMessage()}");
8988
}
9089

9190
if (! $content) {

0 commit comments

Comments
 (0)