Move magic type checking into functions.php

This commit is contained in:
Junior 2026-11-21 16:00:42 -05:00
parent 93235c92ab
commit ebb5325f99
2 changed files with 17 additions and 10 deletions

View File

@ -75,13 +75,8 @@ if ( !is_dir($comicoutputfull) ) {
} }
if ( !$extracted ) { if ( !$extracted ) {
$magic = file_get_contents($comicfull, false, null, 0, 4); $comtype = getComicType($comicfull);
if ( substr($magic, 0, 2) === "PK" ) { if ( $comtype === COMICZIP ) {
$comtype = "cbz";
} elseif ( $magic === "Rar!" ) {
$comtype = "cbr";
}
if ( $comtype == "cbz" ) {
$zip = new ZipArchive(); $zip = new ZipArchive();
$opened = $zip->open($comicfull); $opened = $zip->open($comicfull);
if ( !$opened ) { if ( !$opened ) {
@ -101,7 +96,7 @@ if ( !$extracted ) {
} }
$zip->close(); $zip->close();
} }
} elseif ( $comtype == "cbr" ) { } elseif ( $comtype === COMICRAR ) {
$rar = RarArchive::open($comicfull); $rar = RarArchive::open($comicfull);
if ( $rar === false ) { if ( $rar === false ) {
$data["error"] = true; $data["error"] = true;

View File

@ -19,6 +19,17 @@ function safeFileName($file = "") {
return str_replace($to_remove, "", $file); return str_replace($to_remove, "", $file);
} }
function getComicType(string $file = null): String|false {
$magic = file_get_contents($file, false, null, 0, 4);
$type = false;
if ( substr($magic, 0, 2) === "PK" ) {
$type = "cbz";
} elseif ( $magic === "Rar!" ) {
$type = "cbr";
}
return $type;
}
function makeThumb($item = "") { function makeThumb($item = "") {
if ( $item == "" ) { return false; } if ( $item == "" ) { return false; }
$prefix = (substr(getcwd(), -4) == "ajax") ? "../" : ""; $prefix = (substr(getcwd(), -4) == "ajax") ? "../" : "";
@ -51,7 +62,8 @@ function makeThumb($item = "") {
} else { } else {
return false; return false;
} }
if ( substr($comic, -3) == "cbz" ) { $comtype = getComicType($comic);
if ( $comtype === COMICZIP ) {
$zip = new ZipArchive(); $zip = new ZipArchive();
$opened = $zip->open($comic); $opened = $zip->open($comic);
if ( !$opened ) { return false; } if ( !$opened ) { return false; }
@ -72,7 +84,7 @@ function makeThumb($item = "") {
if ( is_null($imgdata) ) return false; if ( is_null($imgdata) ) return false;
$didwrite = file_put_contents($outfile, $imgdata); $didwrite = file_put_contents($outfile, $imgdata);
if ( !$didwrite ) return false; if ( !$didwrite ) return false;
} elseif ( substr($comic, -3) == "cbr" ) { } elseif ( $comtype === COMICRAR ) {
$rar_file = RarArchive::open($comic); $rar_file = RarArchive::open($comic);
if ( $rar_file === false ) { return false; } if ( $rar_file === false ) { return false; }
$entries = $rar_file->getEntries(); $entries = $rar_file->getEntries();