photo-viewer-backend/php/get.php

158 lines
4.1 KiB
PHP

<?php
include_once __DIR__ . "/logging.php";
include_once __DIR__ . "/cors.php";
cors();
$config_data = get_config();
define("ROOT_URL", $config_data["ROOT_URL"]);
define("ROOT_DIR", $config_data["ROOT_DIR"]);
define("IMG_DIR", $config_data["IMG_DIR"]);
define("THUMBNAIL_DIR", $config_data["THUMBNAIL_DIR"]);
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));
function main(): void {
$data = json_decode(file_get_contents('php://input'), true);
$array = [];
$category = $data["category"];
$directory = ROOT_DIR.IMG_DIR.$category;
if (!is_dir($directory)) goto encode;
foreach (scandir($directory) as $file) {
if ($file === ".") continue;
if ($category === "" && $file === "..") continue;
if (substr($file, 0, 1) === "." && $file !== "..") continue;
$path = add_seperator_ab("/", $category, $file);
$is_dir = is_dir(ROOT_DIR.IMG_DIR.$path);
if (!$is_dir && !is_valid_file_type($file)) continue;
$url = ROOT_URL.IMG_DIR.$path;
$thumbnail_url = null;
if (!$is_dir) {
$thumbnail_url = create_thumbnail($category, $file);
}
$file_item = [];
$file_item["is_dir"] = $is_dir;
$file_item["url"] = $url;
$file_item["thumbnail_url"] = $thumbnail_url;
array_push($array, $file_item);
}
encode:
echo json_encode($array);
}
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;
# use fast hashing algo, see: https://github.com/Cyan4973/xxHash
$hash = hash_file("xxh3", $original_file);
$thumbnail_directory = ROOT_DIR.THUMBNAIL_DIR;
$thumbnail_name = "$hash.jpg";
$thumbnail_file = $thumbnail_directory.$thumbnail_name;
if (file_exists($thumbnail_file)) {
log_print("thumbnail for $original_file exists, skipping generation");
goto return_thumbnail;
}
try {
if (!is_dir($thumbnail_directory)) mkdir($thumbnail_directory, 0777, true);
}
catch(Exception $e) {
log_error($e->getMessage());
}
log_print("generating thumbnail for $original_file");
$output = shell_exec(THUMBNAIL_SCRIPT." '$original_file' '$thumbnail_file' 2>&1");
if ($output) log_print($output);
if (substr_count(strtolower((string) $output), "error")) {
log_error("Thumbnail failed");
log_print(ROOT_URL.IMG_DIR.$path);
return ROOT_URL.IMG_DIR.$path;
}
return_thumbnail:
return ROOT_URL.THUMBNAIL_DIR.$thumbnail_name;
}
function add_seperator_ab(string $separator, string $a, string $b): string {
if ($a === "") return $b;
return $a.$separator.$b;
}
function is_valid_file_type(string $filename): bool {
foreach (FILE_EXTENSIONS as $file_extension) {
if (str_ends_with($filename, $file_extension)) return true;
}
return false;
}
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;
}
function get_config(): array {
$config_data = file_exists(__DIR__ . "/../config.json") ?
json_decode(file_get_contents(__DIR__ . "/../config.json"), true) : null;
if (!is_valid_config_data($config_data)) {
log_error("Config invalid, falling back to default. Please check config.json");
$config_data = json_decode(file_get_contents(__DIR__ . "/../config.default.json"), true);
if (!is_valid_config_data($config_data))
log_error("Default config invalid. Please check or create config.json");
}
return $config_data;
}
function is_valid_config_data(?array $data): bool {
if (!$data) return false;
$keys = ["ROOT_URL", "ROOT_DIR", "IMG_DIR", "THUMBNAIL_DIR"];
foreach ($keys as $key) {
if (!isset($data[$key])) return false;
switch ($key) {
case "ROOT_URL":
case "THUMBNAIL_DIR":
break;
case "ROOT_DIR":
if (!is_dir($data[$key])) return false;
break;
case "IMG_DIR":
if (!is_dir($data["ROOT_DIR"].$data[$key])) return false;
break;
}
}
return true;
}
main();
?>