diff options
| author | ilotterytea <iltsu@alright.party> | 2025-03-24 14:07:21 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-03-24 14:07:21 +0500 |
| commit | 52fea7284ff009a6afddbc6e64bdf49c24f2a038 (patch) | |
| tree | af334cf32499e121937db12aae83b6d23f72e521 /public/upload.php | |
| parent | 83db2ca649ee7d4068b070a032800037caf2e116 (diff) | |
feat: download external files
Diffstat (limited to 'public/upload.php')
| -rw-r--r-- | public/upload.php | 113 |
1 files changed, 79 insertions, 34 deletions
diff --git a/public/upload.php b/public/upload.php index 5c4e3cb..2fd7c67 100644 --- a/public/upload.php +++ b/public/upload.php @@ -7,57 +7,102 @@ if ($_SERVER['REQUEST_METHOD'] != 'POST') { exit; } -if (!isset($_FILES['file'])) { - json_response(null, "No 'file' specified!", 400); - exit(); -} - if (!is_dir(FILE_DIRECTORY) && !mkdir(FILE_DIRECTORY, 0777, true)) { json_response(null, 'Failed to create a directory for user files', 500); exit(); } try { - $file = $_FILES['file']; + $url = $_POST['url'] ?? null; + $file = $_FILES['file'] ?? null; + $file_data = null; - if ( - !isset($file['error']) || - is_array($file['error']) - ) { - throw new RuntimeException('Invalid parameters.'); - } + if (FILEEXT_ENABLED && isset($url) && !empty($url)) { + $output = []; + exec('yt-dlp --get-filename -o "%(filesize_approx)s %(ext)s %(duration)s" ' . escapeshellarg($url) . '', $output); + if (empty($output)) { + throw new RuntimeException('Bad URL'); + } + + $output = explode(' ', $output[0]); + + // TODO: some videos don't have duration + $duration = intval($output[2]); + if ($duration > FILEEXT_MAX_DURATION) { + throw new RuntimeException(sprintf("File must be under %d minutes", FILEEXT_MAX_DURATION / 60)); + } - // checking file size - switch ($file['error']) { - case UPLOAD_ERR_OK: - break; - case UPLOAD_ERR_NO_FILE: - throw new RuntimeException('No file sent.'); - case UPLOAD_ERR_INI_SIZE: - case UPLOAD_ERR_FORM_SIZE: - throw new RuntimeException('Exceeded filesize limit.'); - default: - throw new RuntimeException('Unknown errors.'); + if (!array_key_exists($output[1], FILE_ACCEPTED_MIME_TYPES)) { + throw new RuntimeException("Unsupported extension: {$output[1]}"); + } + + $file_data = [ + 'size' => intval($output[0]), + 'mime' => FILE_ACCEPTED_MIME_TYPES[$output[1]], + 'extension' => $output[1] + ]; + } else if (isset($file)) { + if ( + !isset($file['error']) || + is_array($file['error']) + ) { + throw new RuntimeException('Invalid parameters.'); + } + + // checking file error + switch ($file['error']) { + case UPLOAD_ERR_OK: + break; + case UPLOAD_ERR_NO_FILE: + throw new RuntimeException('No file sent.'); + case UPLOAD_ERR_INI_SIZE: + case UPLOAD_ERR_FORM_SIZE: + throw new RuntimeException('Exceeded filesize limit.'); + default: + throw new RuntimeException('Unknown errors.'); + } + + // checking file mimetype + $finfo = new finfo(FILEINFO_MIME_TYPE); + if (false === $file_ext = array_search($finfo->file($file['tmp_name']), FILE_ACCEPTED_MIME_TYPES, true)) { + throw new RuntimeException("Invalid file format."); + } + + $file_data = [ + 'size' => $file['size'], + 'mime' => FILE_ACCEPTED_MIME_TYPES[$file_ext], + 'extension' => $file_ext + ]; } - // checking file mimetype - $finfo = new finfo(FILEINFO_MIME_TYPE); - if (false === $file_ext = array_search($finfo->file($file['tmp_name']), FILE_ACCEPTED_MIME_TYPES, true)) { - throw new RuntimeException("Invalid file format."); + if (!$file_data) { + throw new RuntimeException('No URL or file specified'); } $file_id = generate_random_char_sequence(FILE_ID_CHARACTERS, FILE_ID_LENGTH); + $file_data['id'] = $file_id; + + if (isset($url)) { + $result = 0; + $output = []; + + exec(sprintf( + 'yt-dlp -o "%s/%s.%s" %s 2>&1', + FILE_DIRECTORY, + $file_id, + $file_data['extension'], + escapeshellarg($url) + ), $output, $result); - if (!move_uploaded_file($file['tmp_name'], FILE_DIRECTORY . sprintf('/%s.%s', $file_id, $file_ext))) { + if ($result != 0) { + 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 (!move_uploaded_file($file['tmp_name'], FILE_DIRECTORY . sprintf('/%s.%s', $file_id, $file_ext))) { throw new RuntimeException("Failed to save the file. Try again later."); } - json_response([ - 'id' => $file_id, - 'extension' => $file_ext, - 'mime' => FILE_ACCEPTED_MIME_TYPES[$file_ext], - 'size' => $file['size'] - ], null, 201); + json_response($file_data, null, 201); } catch (RuntimeException $e) { json_response(null, $e->getMessage(), 400); }
\ No newline at end of file |
