diff options
| author | ilotterytea <iltsu@alright.party> | 2025-05-31 22:56:55 +0400 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-05-31 22:56:55 +0400 |
| commit | f3a8aa77b1c9a6918ec3630e990b6e3bf1bb7b67 (patch) | |
| tree | 405ff2490fa2eaa9546de33a8dc059a6cc3be984 /public/upload.php | |
| parent | 8583185e4d7229a247846167e9c6096dc7bf32a4 (diff) | |
feat: paste text
Diffstat (limited to 'public/upload.php')
| -rw-r--r-- | public/upload.php | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/public/upload.php b/public/upload.php index 0a4f871..9eecf30 100644 --- a/public/upload.php +++ b/public/upload.php @@ -13,10 +13,18 @@ if (!is_dir(FILE_DIRECTORY) && !mkdir(FILE_DIRECTORY, 0777, true)) { } try { - $url = $_POST['url'] ?? null; - $file = $_FILES['file'] ?? null; + $url = isset($_POST['url']) ? $_POST['url'] ?: null : null; + $file = isset($_FILES['file']) ? $_FILES['file'] ?: null : null; + if (empty($file['tmp_name'])) { + $file = null; + } + $paste = isset($_POST['paste']) ? $_POST['paste'] ?: null : null; $file_data = null; + if (!(isset($file) ^ isset($url) ^ isset($paste)) || (isset($file) && isset($url) && isset($paste))) { + throw new RuntimeException('You can upload only one type of content: file, URL or text'); + } + if (FILEEXT_ENABLED && isset($url) && !empty($url)) { $output = []; exec('yt-dlp -f "worst" --get-filename -o "%(filesize_approx)s %(ext)s %(duration)s" ' . escapeshellarg($url) . '', $output); @@ -41,6 +49,12 @@ try { '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']) || @@ -82,7 +96,7 @@ try { $file_id = generate_random_char_sequence(FILE_ID_CHARACTERS, FILE_ID_LENGTH); $file_data['id'] = $file_id; - if (isset($url) && !empty($url)) { + if (isset($url)) { $result = 0; $output = []; @@ -98,7 +112,9 @@ 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 (!move_uploaded_file($file['tmp_name'], FILE_DIRECTORY . sprintf('/%s.%s', $file_id, $file_ext))) { + } else if (isset($paste) && !file_put_contents(FILE_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']))) { throw new RuntimeException("Failed to save the file. Try again later."); } |
