Compare commits

..

No commits in common. "a14e2972c62fda79f5aafa95337b3bc97b3fa788" and "fbd10d6b11e1a29ce787bbb1f5dff67fac9b94cd" have entirely different histories.

3 changed files with 36 additions and 78 deletions

2
.gitignore vendored
View File

@ -1,5 +1,3 @@
php/composer* php/composer*
php/psalm.xml php/psalm.xml
php/vendor php/vendor
php.log

View File

@ -1,63 +1,52 @@
<?php <?php
include_once __DIR__ . "/logging.php";
define("ROOT_URL", "http://localhost/"); define("ROOT_URL", "http://localhost/");
define("ROOT_DIR", "/var/www/localhost/"); define("IMG_DIR", "/var/www/localhost/img/");
define("IMG_DIR", "img/"); define("IMG_URL", "img/");
define("THUMBNAIL_DIR", __DIR__ . "../thumbnails");
main(); main();
function main(): void { function main(): void {
$data = json_decode(file_get_contents('php://input'), true); $data = json_decode(file_get_contents('php://input'), true);
$array = []; $array = [];
$category = $data["category"]; $category = $data["category"];
$directory = ROOT_DIR.IMG_DIR.$category; $directory = IMG_DIR.$category;
if (!is_dir($directory)) return;
foreach (scandir($directory) as $file) {
if ($file === ".") continue;
if ($category === "" && $file === "..") continue;
if (!is_dir($directory)) goto encode; if ($category != "") $file = $category.'/'.$file;
foreach (scandir($directory) as $file) { $is_dir = is_dir(IMG_DIR.$file);
if ($file === ".") continue; if (!$is_dir && !is_valid_file_type($file)) continue;
if ($category === "" && $file === "..") continue;
if ($category != "") $file = $category.'/'.$file; $file_item = [];
$is_dir = is_dir(ROOT_DIR.IMG_DIR.$file); $file_item["is_dir"] = $is_dir;
if (!$is_dir && !is_valid_file_type($file)) continue; $file_item["url"] = ROOT_URL.IMG_URL.$file;
array_push($array, $file_item);
$file_item = []; }
$file_item["is_dir"] = $is_dir; echo json_encode($array);
$file_item["url"] = ROOT_URL.IMG_DIR.$file;
$file_item["thumbnail_url"] = create_thumbnail($file);
array_push($array, $file_item);
}
encode:
echo json_encode($array);
}
function create_thumbnail(string $file): string {
return ROOT_URL.IMG_DIR.$file;
} }
function is_valid_file_type(string $filename): bool { function is_valid_file_type(string $filename): bool {
$file_extensions = [ $file_extensions = [
// Image extensions // Image extensions
'jpg', 'jpg',
'jpeg', 'jpeg',
'png', 'png',
'gif', 'gif',
'tiff', 'tiff',
'bmp', 'bmp',
'webp', 'webp',
'svg', 'svg',
// Video extensions // Video extensions
'mp4', 'mp4',
]; ];
foreach ($file_extensions as $file_extension) { foreach ($file_extensions as $file_extension) {
if (str_ends_with($filename, $file_extension)) return true; if (str_ends_with($filename, $file_extension)) return true;
} }
return false; return false;
} }
?> ?>

View File

@ -1,29 +0,0 @@
<?php
//define("DEBUG", null);
define("LOG_FILE", __DIR__ . "/../php.log");
define("PREFIX_ERROR", "Error: ");
function log_print($string)
{
if(defined("DEBUG") && !defined("NO_ECHO")) {
echo $string . "<br>";
return;
}
$handle = fopen(LOG_FILE, "a");
if (!$handle) {
echo "Unable to open file.<br>";
return;
}
if (!fwrite($handle, $string . "\n")) {
echo "Unable to write to file.<br>";
}
fclose($handle);
}
function log_error($string)
{
log_print(PREFIX_ERROR . $string);
}
?>