summaryrefslogtreecommitdiff
path: root/public/upload.php
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-06-01 00:01:49 +0400
committerilotterytea <iltsu@alright.party>2025-06-01 00:01:49 +0400
commit0ac128d6a2df3a84f49a4d86cfbf501bbbb52a43 (patch)
tree6e9727270a8adfc7c1a0ab1fee2870264875840f /public/upload.php
parent4da453ba7cd09849d7c3c1229188e1ee3b3866be (diff)
feat: file thumbnails
Diffstat (limited to 'public/upload.php')
-rw-r--r--public/upload.php39
1 files changed, 35 insertions, 4 deletions
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 @@
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/../config.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/utils.php';
+include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/thumbnails.php';
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
json_response(null, 'Method not allowed', 405);
exit;
}
-if (!is_dir(FILE_DIRECTORY) && !mkdir(FILE_DIRECTORY, 0777, true)) {
+if (!is_dir(FILE_UPLOAD_DIRECTORY) && !mkdir(FILE_UPLOAD_DIRECTORY, 0777, true)) {
json_response(null, 'Failed to create a directory for user files', 500);
exit();
}
@@ -102,7 +103,7 @@ try {
exec(sprintf(
'yt-dlp -f "worst" -o "%s/%s.%s" %s 2>&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 {