2023-11-12 18:42:35 +08:00
|
|
|
<?php
|
2023-11-12 23:18:32 +08:00
|
|
|
include_once __DIR__ . "/logging.php";
|
|
|
|
|
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/");
|
|
|
|
define("THUMBNAIL_DIR", __DIR__ . "../thumbnails");
|
2023-11-12 18:42:35 +08:00
|
|
|
|
|
|
|
main();
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if ($category != "") $file = $category.'/'.$file;
|
2023-11-12 23:18:32 +08:00
|
|
|
$is_dir = is_dir(ROOT_DIR.IMG_DIR.$file);
|
2023-11-12 22:44:35 +08:00
|
|
|
if (!$is_dir && !is_valid_file_type($file)) continue;
|
|
|
|
|
|
|
|
$file_item = [];
|
|
|
|
$file_item["is_dir"] = $is_dir;
|
2023-11-12 23:18:32 +08:00
|
|
|
$file_item["url"] = ROOT_URL.IMG_DIR.$file;
|
|
|
|
$file_item["thumbnail_url"] = create_thumbnail($file);
|
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-12 23:18:32 +08:00
|
|
|
function create_thumbnail(string $file): string {
|
|
|
|
return ROOT_URL.IMG_DIR.$file;
|
|
|
|
}
|
|
|
|
|
2023-11-12 18:42:35 +08:00
|
|
|
function is_valid_file_type(string $filename): bool {
|
2023-11-12 22:44:35 +08:00
|
|
|
$file_extensions = [
|
|
|
|
// Image extensions
|
|
|
|
'jpg',
|
|
|
|
'jpeg',
|
|
|
|
'png',
|
|
|
|
'gif',
|
|
|
|
'tiff',
|
|
|
|
'bmp',
|
|
|
|
'webp',
|
|
|
|
'svg',
|
|
|
|
// Video extensions
|
|
|
|
'mp4',
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($file_extensions as $file_extension) {
|
|
|
|
if (str_ends_with($filename, $file_extension)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
2023-11-12 18:42:35 +08:00
|
|
|
}
|
|
|
|
?>
|
|
|
|
|