From 0ac128d6a2df3a84f49a4d86cfbf501bbbb52a43 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sun, 1 Jun 2025 00:01:49 +0400 Subject: feat: file thumbnails --- lib/partials.php | 2 +- lib/thumbnails.php | 41 +++++++++++++++++++++++++++++++++++++++++ public/index.php | 4 +++- public/upload.php | 39 +++++++++++++++++++++++++++++++++++---- 4 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 lib/thumbnails.php diff --git a/lib/partials.php b/lib/partials.php index 13ea156..8a7824e 100644 --- a/lib/partials.php +++ b/lib/partials.php @@ -20,7 +20,7 @@ function html_big_navbar() function html_footer() { - $files = glob(FILE_DIRECTORY . "/*.*"); + $files = glob(FILE_UPLOAD_DIRECTORY . "/*.*"); $file_size = 0; foreach ($files as $file) { diff --git a/lib/thumbnails.php b/lib/thumbnails.php new file mode 100644 index 0000000..5afc775 --- /dev/null +++ b/lib/thumbnails.php @@ -0,0 +1,41 @@ +&1"; + $magick_command = "magick $folder_path/frames_*.png -loop 0 -delay 60 -resize {$width}x{$height} $output_path 2>&1"; + + exec($ffmpeg_command, $ffmpeg_output, $ffmpeg_result_code); + exec($magick_command, $magick_output, $magick_result_code); + + array_map('unlink', array_filter((array) glob("$folder_path/*.*"))); + rmdir($folder_path); + + return $ffmpeg_result_code === 0 && $magick_result_code === 0 ? 0 : -1; +} \ No newline at end of file diff --git a/public/index.php b/public/index.php index 5e2f77a..b85dadf 100644 --- a/public/index.php +++ b/public/index.php @@ -263,11 +263,13 @@ include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php'; function addUploadedFile(file) { return `
+
- ${file.id}.${file.extension} +

No thumbnail.

+

${file.id}.${file.extension}

${file.mime}

diff --git a/public/upload.php b/public/upload.php index 3ef0a0c..ae550c9 100644 --- a/public/upload.php +++ b/public/upload.php @@ -1,13 +1,14 @@ &1', - FILE_DIRECTORY, + FILE_UPLOAD_DIRECTORY, $file_id, $file_data['extension'], escapeshellarg($url) @@ -112,12 +113,42 @@ try { error_log(sprintf("Failed to download a file (URL: %s): %s", $url, implode('\n', $output))); throw new RuntimeException('Failed to download a file! Try again later.'); } - } else if (isset($paste) && !file_put_contents(FILE_DIRECTORY . sprintf('/%s.%s', $file_id, $file_data['extension']), $paste)) { + } else if (isset($paste) && !file_put_contents(FILE_UPLOAD_DIRECTORY . sprintf('/%s.%s', $file_id, $file_data['extension']), $paste)) { throw new RuntimeException('Failed to paste a text! Try again later.'); - } else if (isset($file) && !move_uploaded_file($file['tmp_name'], FILE_DIRECTORY . sprintf('/%s.%s', $file_id, $file_data['extension']))) { + } else if (isset($file) && !move_uploaded_file($file['tmp_name'], FILE_UPLOAD_DIRECTORY . sprintf('/%s.%s', $file_id, $file_data['extension']))) { throw new RuntimeException("Failed to save the file. Try again later."); } + if (FILE_THUMBNAILS && !is_dir(FILE_THUMBNAIL_DIRECTORY) && !mkdir(FILE_THUMBNAIL_DIRECTORY, 0777, true)) { + throw new RuntimeException('Failed to create a directory for thumbnails'); + } + + if ( + FILE_THUMBNAILS && ( + ( + str_starts_with($file_data['mime'], 'image/') && + $thumbnail_error = generate_image_thumbnail( + FILE_UPLOAD_DIRECTORY . "/{$file_id}.{$file_data['extension']}", + FILE_THUMBNAIL_DIRECTORY . "/{$file_id}.webp", + FILE_THUMBNAIL_SIZE[0], + FILE_THUMBNAIL_SIZE[1] + ) + ) || + ( + str_starts_with($file_data['mime'], 'video/') && + $thumbnail_error = generate_video_thumbnail( + FILE_UPLOAD_DIRECTORY . "/{$file_id}.{$file_data['extension']}", + FILE_THUMBNAIL_DIRECTORY . "/{$file_id}", + FILE_THUMBNAIL_DIRECTORY . "/{$file_id}.webp", + FILE_THUMBNAIL_SIZE[0], + FILE_THUMBNAIL_SIZE[1] + ) + ) + ) + ) { + throw new RuntimeException("Failed to create a thumbnail (Error code {$thumbnail_error})"); + } + if ($_SERVER['HTTP_ACCEPT'] == 'application/json') { json_response($file_data, null, 201); } else { -- cgit v1.2.3