FILEEXT_MAX_DURATION) { throw new RuntimeException(sprintf("File must be under %d minutes", FILEEXT_MAX_DURATION / 60)); } 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($paste)) { $file_data = [ 'size' => strlen($paste), 'mime' => 'text/plain', 'extension' => 'txt' ]; } 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 ]; } if (!$file_data) { throw new RuntimeException('No URL or file specified'); } $file_id_length = FILE_ID_LENGTH; $file_id_gen_attempts = 0; do { $file_id = FILE_ID_PREFIX . generate_random_char_sequence(FILE_ID_CHARACTERS, $file_id_length); if ($file_id_gen_attempts > 20) { $file_id_length++; $file_id_gen_attempts = 0; } $file_id_gen_attempts++; } while (is_file(FILE_UPLOAD_DIRECTORY . "/{$file_id}.{$file_data['extension']}")); $file_data['id'] = $file_id; if (isset($url)) { $result = 0; $output = []; exec(sprintf( 'yt-dlp -f "worst" -o "%s/%s.%s" %s 2>&1', FILE_UPLOAD_DIRECTORY, $file_id, $file_data['extension'], escapeshellarg($url) ), $output, $result); 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 (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_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})"); } $file_data['urls'] = [ 'download_url' => INSTANCE_URL . "/{$file_data['id']}.{$file_data['extension']}" ]; if ($_SERVER['HTTP_ACCEPT'] == 'application/json') { json_response($file_data, null, 201); } else { header("Location: /{$file_data['id']}.{$file_data['extension']}"); } } catch (RuntimeException $e) { if ($_SERVER['HTTP_ACCEPT'] == 'application/json') { json_response(null, $e->getMessage(), 400); } else { http_response_code(400); echo $e->getMessage(); } }