From f3a8aa77b1c9a6918ec3630e990b6e3bf1bb7b67 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sat, 31 May 2025 22:56:55 +0400 Subject: feat: paste text --- public/index.php | 60 +++++++++++++++++++++++++++++++++++++++++++++---- public/static/style.css | 45 ++++++++++++++++++++++++++++++++++--- public/upload.php | 24 ++++++++++++++++---- 3 files changed, 118 insertions(+), 11 deletions(-) diff --git a/public/index.php b/public/index.php index cea582f..5e2f77a 100644 --- a/public/index.php +++ b/public/index.php @@ -37,8 +37,17 @@ include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php';
-
-

File Upload

+
+
+ +
+
+ +
+
+ +
+
@@ -88,6 +101,7 @@ include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php'; \ No newline at end of file diff --git a/public/static/style.css b/public/static/style.css index 78aa25c..15550e4 100644 --- a/public/static/style.css +++ b/public/static/style.css @@ -13,6 +13,21 @@ margin: 0; } +button.transparent { + margin: 0; + padding: 0; + border: 0; + font-size: 100%; + font: inherit; + color: inherit; + vertical-align: baseline; + background: none; +} + +button:hover { + cursor: pointer; +} + body { background: linear-gradient(0deg, var(--background-2), var(--background)); color: var(--foreground); @@ -66,21 +81,45 @@ button[type=submit]:hover { cursor: pointer; } +#form-text-upload>textarea { + resize: vertical; + height: 256px; +} + /** BOX */ .box { background: var(--box-content-background); border: 2px solid var(--box-border); } -.box:has(.content)>.tab { +.box>.tabs { + display: flex; + flex-direction: row; + align-items: center; + gap: 8px; +} + +.tabs>.tab {} + +.box:has(.tabs), +.box:has(.tab) { + background: none; + border: none; +} + +.box:has(.content)>.tab, +.tabs>.tab:not(.disabled) { background: var(--box-tab-background); - border-bottom: 2px solid var(--box-border); + border: 2px solid var(--box-border); + border-bottom: none; padding: 8px; font-weight: bold; } -.box:has(.tab)>.content { +.box:has(.tab)>.content, +.box:has(.tabs)>.content { background: var(--box-content-background); + border: 2px solid var(--box-border); padding: 8px; } 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."); } -- cgit v1.2.3