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 | |
| parent | 8583185e4d7229a247846167e9c6096dc7bf32a4 (diff) | |
feat: paste text
Diffstat (limited to 'public')
| -rw-r--r-- | public/index.php | 60 | ||||
| -rw-r--r-- | public/static/style.css | 45 | ||||
| -rw-r--r-- | 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'; </section> <section class="box column"> - <div class="tab"> - <p>File Upload</p> + <div class="tabs"> + <div class="form-upload-tab tab" id="form-tab-file"> + <button onclick="showUploadType('file')" class="transparent"> + <p>File Upload</p> + </button> + </div> + <div class="form-upload-tab tab disabled" id="form-tab-text"> + <button onclick="showUploadType('text')" class="transparent"> + <p>Text</p> + </button> + </div> </div> <div class="content"> <form class="column gap-8" action="/upload.php" method="post" enctype="multipart/form-data" @@ -70,6 +79,10 @@ include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php'; <?php endif; ?> </div> + <div class="column" id="form-text-upload"> + <textarea name="paste" placeholder="Enter your text here..."></textarea> + </div> + <button type="submit">Upload</button> </form> </div> @@ -88,6 +101,7 @@ include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php'; </body> <script> + document.getElementById('form-text-upload').style.display = 'none'; let file = null; const uploadedFiles = document.getElementById('uploaded-files'); @@ -110,12 +124,17 @@ include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php'; <?php if (FILEEXT_ENABLED): ?> const fileURLWrapper = document.querySelector('#form-upload-wrapper>div'); - fileURL.addEventListener('change', () => { + fileURL.addEventListener('keyup', () => { fileUploadWrapper.style.display = fileURL.value.length == 0 ? 'block' : 'none'; formSubmitButton.style.display = fileURL.value.length == 0 ? 'none' : 'block'; }); <?php endif; ?> + const textArea = document.querySelector('#form-text-upload>textarea'); + textArea.addEventListener('keyup', () => { + formSubmitButton.style.display = textArea.value.length == 0 ? 'none' : 'block'; + }); + const formSubmitButton = document.querySelector('#form-upload button[type=submit]'); const formFile = document.getElementById('form-file'); @@ -169,13 +188,30 @@ include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php'; formSubmitButton.style.display = 'none'; + if (textArea.value.length > 0) { + formSubmitButton.style.display = 'block'; + showUploadType('text'); + } + function fileUpload(is_url) { + if (textArea.value.length > 0) { + file = null; + formFile.value = null; + } + const form = new FormData(formUpload); + if (file) { form.set('file', file); } - fileUploadWrapper.innerHTML = is_url ? `<h1>Uploading ${fileURL.value}</h1><p>This might take a while...</p>` : `<h1>Uploading ${file.name}...</h1><p>This might take a while...</p>`; + if (is_url) { + fileUploadWrapper.innerHTML = `<h1>Uploading ${fileURL.value}</h1><p>This might take a while...</p>`; + } else if (file) { + fileUploadWrapper.innerHTML = `<h1>Uploading ${file.name}...</h1><p>This might take a while...</p>`; + } else { + fileUploadWrapper.innerHTML = `<h1>Uploading...</h1>`; + } fileUploadWrapper.style.display = 'block'; <?php if (FILEEXT_ENABLED): ?> fileURLWrapper.style.display = 'none'; @@ -215,6 +251,7 @@ include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php'; uploadedFiles.innerHTML = addUploadedFile(json.data) + uploadedFiles.innerHTML; uploadedFiles.parentElement.style.display = 'flex'; + textArea.value = ''; // saving file let files = getUploadedFiles(); @@ -273,6 +310,21 @@ include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php'; } return JSON.parse(files); } + + function showUploadType(type) { + document.getElementById('form-upload-wrapper').style.display = type == 'text' ? 'none' : 'flex'; + document.getElementById('form-text-upload').style.display = type == 'text' ? 'flex' : 'none'; + + const tabs = document.querySelectorAll('.form-upload-tab'); + + for (const tab of tabs) { + if (tab.getAttribute('id') == `form-tab-${type}`) { + tab.classList.remove('disabled'); + } else { + tab.classList.add('disabled'); + } + } + } </script> </html>
\ 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."); } |
