2023-11-12 18:42:35 +08:00
|
|
|
<?php
|
2023-11-12 23:18:32 +08:00
|
|
|
include_once __DIR__ . "/logging.php";
|
2023-11-13 22:50:32 +08:00
|
|
|
include_once __DIR__ . "/cors.php";
|
|
|
|
cors();
|
2023-11-12 23:18:32 +08:00
|
|
|
|
2023-11-12 18:42:35 +08:00
|
|
|
define("ROOT_URL", "http://localhost/");
|
2023-11-12 23:18:32 +08:00
|
|
|
define("ROOT_DIR", "/var/www/localhost/");
|
|
|
|
define("IMG_DIR", "img/");
|
2023-11-13 02:03:04 +08:00
|
|
|
define("THUMBNAIL_DIR", "thumbnails/");
|
|
|
|
define("THUMBNAIL_SCRIPT", __DIR__ . "/../scripts/resize-img");
|
|
|
|
|
|
|
|
define("IMAGE_EXTENSIONS", [
|
|
|
|
'jpg',
|
|
|
|
'jpeg',
|
|
|
|
'png',
|
|
|
|
'gif',
|
|
|
|
'tiff',
|
|
|
|
'bmp',
|
|
|
|
'webp',
|
|
|
|
'svg',
|
|
|
|
]);
|
|
|
|
|
|
|
|
define("VIDEO_EXTENSIONS", [
|
|
|
|
'mp4',
|
|
|
|
]);
|
|
|
|
|
|
|
|
define("FILE_EXTENSIONS", array_merge(IMAGE_EXTENSIONS, VIDEO_EXTENSIONS));
|
2023-11-12 18:42:35 +08:00
|
|
|
|
|
|
|
function main(): void {
|
2023-11-12 22:44:35 +08:00
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
$array = [];
|
|
|
|
|
|
|
|
$category = $data["category"];
|
2023-11-12 23:18:32 +08:00
|
|
|
$directory = ROOT_DIR.IMG_DIR.$category;
|
|
|
|
|
|
|
|
if (!is_dir($directory)) goto encode;
|
2023-11-12 22:44:35 +08:00
|
|
|
foreach (scandir($directory) as $file) {
|
|
|
|
if ($file === ".") continue;
|
|
|
|
if ($category === "" && $file === "..") continue;
|
|
|
|
|
2023-11-13 02:03:04 +08:00
|
|
|
$path = add_seperator_ab("/", $category, $file);
|
|
|
|
$is_dir = is_dir(ROOT_DIR.IMG_DIR.$path);
|
2023-11-12 22:44:35 +08:00
|
|
|
if (!$is_dir && !is_valid_file_type($file)) continue;
|
|
|
|
|
2023-11-13 22:47:14 +08:00
|
|
|
$url = ROOT_URL.IMG_DIR.$path;
|
|
|
|
$thumbnail_url = null;
|
|
|
|
|
|
|
|
if (!$is_dir) {
|
|
|
|
$thumbnail_url = create_thumbnail($category, $file);
|
|
|
|
}
|
|
|
|
|
2023-11-12 22:44:35 +08:00
|
|
|
$file_item = [];
|
|
|
|
$file_item["is_dir"] = $is_dir;
|
2023-11-13 22:47:14 +08:00
|
|
|
$file_item["url"] = $url;
|
|
|
|
$file_item["thumbnail_url"] = $thumbnail_url;
|
2023-11-12 22:44:35 +08:00
|
|
|
array_push($array, $file_item);
|
|
|
|
}
|
2023-11-12 23:18:32 +08:00
|
|
|
|
|
|
|
encode:
|
2023-11-12 22:44:35 +08:00
|
|
|
echo json_encode($array);
|
2023-11-12 18:42:35 +08:00
|
|
|
}
|
|
|
|
|
2023-11-13 02:03:04 +08:00
|
|
|
function create_thumbnail(string $category, string $file): string | null {
|
|
|
|
if (!is_image_file_type($file)) return null;
|
|
|
|
|
|
|
|
$path = add_seperator_ab("/", $category, $file);
|
|
|
|
$original_file = ROOT_DIR.IMG_DIR.$path;
|
2023-11-19 13:25:21 +08:00
|
|
|
# use fast hashing algo, see: https://github.com/Cyan4973/xxHash
|
|
|
|
$hash = hash_file("xxh3", $original_file);
|
2023-11-18 15:56:05 +08:00
|
|
|
|
|
|
|
$thumbnail_directory = ROOT_DIR.THUMBNAIL_DIR;
|
|
|
|
$thumbnail_name = "$hash.jpg";
|
|
|
|
$thumbnail_file = $thumbnail_directory.$thumbnail_name;
|
2023-11-13 02:03:04 +08:00
|
|
|
|
|
|
|
if (file_exists($thumbnail_file)) {
|
2023-11-18 15:56:05 +08:00
|
|
|
log_print("thumbnail for $original_file exists, skipping generation");
|
2023-11-13 02:03:04 +08:00
|
|
|
goto return_thumbnail;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!is_dir($thumbnail_directory)) mkdir($thumbnail_directory, 0777, true);
|
|
|
|
}
|
|
|
|
catch(Exception $e) {
|
|
|
|
log_error($e->getMessage());
|
|
|
|
}
|
|
|
|
|
2023-11-18 15:56:05 +08:00
|
|
|
log_print("generating thumbnail for $original_file");
|
2023-11-13 02:03:04 +08:00
|
|
|
$output = shell_exec(THUMBNAIL_SCRIPT." '$original_file' '$thumbnail_file' 2>&1");
|
2023-11-18 15:56:05 +08:00
|
|
|
if ($output) log_print($output);
|
2023-11-18 16:42:33 +08:00
|
|
|
if (substr_count(strtolower((string) $output), "error")) {
|
2023-11-13 02:03:04 +08:00
|
|
|
log_error("Thumbnail failed");
|
2023-11-18 16:42:33 +08:00
|
|
|
log_print(ROOT_URL.IMG_DIR.$path);
|
2023-11-13 02:03:04 +08:00
|
|
|
return ROOT_URL.IMG_DIR.$path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return_thumbnail:
|
2023-11-18 15:56:05 +08:00
|
|
|
return ROOT_URL.THUMBNAIL_DIR.$thumbnail_name;
|
2023-11-13 02:03:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function add_seperator_ab(string $separator, string $a, string $b): string {
|
|
|
|
if ($a === "") return $b;
|
|
|
|
return $a.$separator.$b;
|
2023-11-12 23:18:32 +08:00
|
|
|
}
|
|
|
|
|
2023-11-12 18:42:35 +08:00
|
|
|
function is_valid_file_type(string $filename): bool {
|
2023-11-13 02:03:04 +08:00
|
|
|
foreach (FILE_EXTENSIONS as $file_extension) {
|
2023-11-12 22:44:35 +08:00
|
|
|
if (str_ends_with($filename, $file_extension)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
2023-11-12 18:42:35 +08:00
|
|
|
}
|
2023-11-13 02:03:04 +08:00
|
|
|
|
|
|
|
function is_image_file_type(string $filename): bool {
|
|
|
|
foreach(IMAGE_EXTENSIONS as $file_extension) {
|
|
|
|
if(str_ends_with($filename, $file_extension)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-12 18:42:35 +08:00
|
|
|
|
2023-11-13 22:47:14 +08:00
|
|
|
main();
|
|
|
|
?>
|