From 3b974ed6a33e5529dc2568b907ce8d9f3ef9f560 Mon Sep 17 00:00:00 2001 From: xenofem Date: Tue, 3 May 2022 19:21:42 -0400 Subject: [PATCH] check upload limits beforehand --- src/upload.rs | 6 +++--- static/css/states.css | 5 +++++ static/index.html | 3 +++ static/js/upload.js | 39 +++++++++++++++++++++++++++------------ 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/upload.rs b/src/upload.rs index b06e012..02cb518 100644 --- a/src/upload.rs +++ b/src/upload.rs @@ -139,7 +139,7 @@ struct UploadManifest { enum ServerMessage { Ready { code: String }, TooBig { max_size: u64 }, - TooLong { max_days: u16 }, + TooLong { max_lifetime: u16 }, IncorrectPassword, Error { details: String }, } @@ -150,8 +150,8 @@ impl From<&Error> for ServerMessage { Error::TooBig(max_size) => ServerMessage::TooBig { max_size: *max_size, }, - Error::TooLong(max_days) => ServerMessage::TooLong { - max_days: *max_days, + Error::TooLong(max_lifetime) => ServerMessage::TooLong { + max_lifetime: *max_lifetime, }, Error::IncorrectPassword => ServerMessage::IncorrectPassword, _ => ServerMessage::Error { diff --git a/static/css/states.css b/static/css/states.css index 748c4a3..165854f 100644 --- a/static/css/states.css +++ b/static/css/states.css @@ -2,6 +2,7 @@ * List of classes the body can have: * * landing: haven't entered upload password yet + * uploads_closed: uploading is currently unavailable * no_files: no files are selected * selecting: upload hasn't started yet * uploading: upload is in progress @@ -15,7 +16,11 @@ body.landing .section_heading { display: revert; } #download { display: none; } body.landing #download { display: revert; } +#uploads_closed_notice { display: none; } +body.uploads_closed #uploads_closed_notice { display: revert; } + body.noscript #upload { display: none; } +body.uploads_closed #upload { display: none; } #message { display: none; } body.completed #message { diff --git a/static/index.html b/static/index.html index ff438e6..58ecd49 100644 --- a/static/index.html +++ b/static/index.html @@ -29,6 +29,9 @@ +
+

Uploading is currently closed.

+

Upload

diff --git a/static/js/upload.js b/static/js/upload.js index 6cca00c..c61c1b9 100644 --- a/static/js/upload.js +++ b/static/js/upload.js @@ -23,7 +23,7 @@ let progress; let progressBar; document.addEventListener('DOMContentLoaded', () => { - document.body.className = "landing"; + document.body.className = 'landing'; messageBox = document.getElementById('message'); fileInput = document.getElementById('file_input'); @@ -34,6 +34,17 @@ document.addEventListener('DOMContentLoaded', () => { progress = document.getElementById('progress'); progressBar = document.getElementById('progress_bar'); + fetch('/upload/limits.json') + .then((res) => res.json()) + .then((limits) => { + if (limits.open === false) { + document.body.className = 'uploads_closed landing'; + return; + } + maxSize = limits.max_size; + updateMaxLifetime(limits.max_lifetime); + }); + const uploadPasswordInput = document.getElementById('upload_password'); const uploadPasswordForm = document.getElementById('upload_password_form'); uploadPasswordForm.addEventListener('submit', (e) => { @@ -193,17 +204,8 @@ function handleMessage(msg) { maxSize = reply.max_size; updateFiles(); } else if (reply.type === 'too_long') { - let options = Array.from(lifetimeInput.options); - options.reverse(); - for (const option of options) { - if (option.value > reply.max_days) { - option.disabled = true; - } else { - option.selected = true; - break; - } - } - displayError(`The maximum retention time for uploads is ${reply.max_days} days`); + updateMaxLifetime(reply.max_lifetime); + displayError(`The maximum retention time for uploads is ${reply.max_lifetime} days`); } else if (reply.type === 'incorrect_password') { messageBox.textContent = ('Incorrect password'); document.body.className = 'error landing'; @@ -221,6 +223,19 @@ function handleMessage(msg) { } } +function updateMaxLifetime(lifetime) { + let options = Array.from(lifetimeInput.options); + options.reverse(); + for (const option of options) { + if (option.value > lifetime) { + option.disabled = true; + } else { + option.selected = true; + break; + } + } +} + function sendData() { if (fileIndex >= files.length) { finishSending();