diff --git a/index.html.tmpl b/index.html.tmpl index 07e90af..077cdd2 100644 --- a/index.html.tmpl +++ b/index.html.tmpl @@ -93,6 +93,7 @@

Upload with curl

curl --upload-file myfile.txt {{ .Proto }}://{{ .Host }}/myfile.txt

The server will respond with a download URL.

+

Maximum upload size: {{ .MaxUploadSize }} bytes

@@ -105,6 +106,7 @@

Web Upload

📁 Click to select files or drag and drop here

+

Maximum file size:

@@ -120,6 +122,22 @@ const progressContainer = document.getElementById('progressContainer'); const progressBar = document.getElementById('progressBar'); const results = document.getElementById('results'); + const maxUploadSize = parseInt(document.getElementById('maxUploadSizeLabel').dataset.maxBytes, 10) || 0; + + function formatBytes(bytes) { + if (!bytes) return 'unknown'; + const units = ['B', 'KB', 'MB', 'GB', 'TB']; + let value = bytes; + let unitIndex = 0; + while (value >= 1024 && unitIndex < units.length - 1) { + value /= 1024; + unitIndex++; + } + return value.toFixed(unitIndex === 0 ? 0 : 1) + ' ' + units[unitIndex]; + } + + document.getElementById('maxUploadSizeLabel').textContent = formatBytes(maxUploadSize); + document.getElementById('maxUploadSizeText').textContent = formatBytes(maxUploadSize); uploadArea.addEventListener('click', () => fileInput.click()); @@ -146,6 +164,27 @@ function uploadFiles(files) { if (files.length === 0) return; + const oversizedFiles = []; + let totalSize = 0; + for (let file of files) { + totalSize += file.size; + if (maxUploadSize && file.size > maxUploadSize) { + oversizedFiles.push(file.name); + } + } + + if (oversizedFiles.length > 0) { + results.innerHTML = '
Upload failed: the following file(s) exceed the maximum size of ' + + formatBytes(maxUploadSize) + ': ' + oversizedFiles.join(', ') + '
'; + return; + } + + if (maxUploadSize && totalSize > maxUploadSize) { + results.innerHTML = '
Upload failed: total upload size (' + formatBytes(totalSize) + + ') exceeds the maximum of ' + formatBytes(maxUploadSize) + '
'; + return; + } + const formData = new FormData(); for (let file of files) { formData.append('files', file); diff --git a/main.go b/main.go index e568f31..54cd2f7 100644 --- a/main.go +++ b/main.go @@ -430,7 +430,11 @@ func logRequest(r *http.Request, status int, duration time.Duration) { func serveFrontpage(w http.ResponseWriter, r *http.Request) { proto := getProtocol(r) host := getHost(r) - pageData := map[string]string{"Proto": proto, "Host": host} + pageData := map[string]any{ + "Proto": proto, + "Host": host, + "MaxUploadSize": maxUploadSize, + } w.Header().Set("Content-Type", "text/html; charset=utf-8") indexHTML.Execute(w, pageData)