diff --git a/.air.toml b/.air.toml new file mode 100644 index 0000000..af6930a --- /dev/null +++ b/.air.toml @@ -0,0 +1,9 @@ +#:schema https://json.schemastore.org/any.json +#https://github.com/air-verse/air/blob/master/air_example.toml +root = "." +tmp_dir = "tmp" +env_files = [".env"] +[build] +cmd = "go build -o ./tmp/main.exe ./cmd/minibin" +entrypoint = ["./tmp/main.exe"] +exclude_dir = ["tmp", "uploads"] diff --git a/.gitignore b/.gitignore index 8d9031a..237445e 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ !web/** !.prettierrc !.env.example +!.air.toml # !Makefile diff --git a/internal/pages/pages.go b/internal/pages/pages.go index 440e76f..e152abe 100644 --- a/internal/pages/pages.go +++ b/internal/pages/pages.go @@ -22,11 +22,13 @@ type Pages struct { } type HomePageData struct { - Expiries []config.Expiry - Qt string - PastePlaceholder string - FileMaxSize string - PasteMaxSize string + Expiries []config.Expiry + Qt string + PastePlaceholder string + FileMaxSize string + FileMaxSizeBytes int64 + PasteMaxSize string + PasteMaxSizeBytes int64 } func (pages *Pages) Render(w http.ResponseWriter, name string, data any) { @@ -108,10 +110,12 @@ func LoadPages() (*Pages, error) { func HomeHandler(pages *Pages, cfg config.Config) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { data := HomePageData{ - Expiries: cfg.Expiries, - FileMaxSize: humanize.Bytes(uint64(cfg.MaxFileBytes)), - PasteMaxSize: humanize.Bytes(uint64(cfg.MaxPasteBytes)), - Qt: GetRandomQt(pages.Qts), + Expiries: cfg.Expiries, + FileMaxSize: humanize.Bytes(uint64(cfg.MaxFileBytes)), + FileMaxSizeBytes: cfg.MaxFileBytes, + PasteMaxSize: humanize.Bytes(uint64(cfg.MaxPasteBytes)), + PasteMaxSizeBytes: cfg.MaxPasteBytes, + Qt: GetRandomQt(pages.Qts), } if len(pages.Placeholders) > 0 { diff --git a/internal/uploads/handlers.go b/internal/uploads/handlers.go index 5f89a62..e2ebb1a 100644 --- a/internal/uploads/handlers.go +++ b/internal/uploads/handlers.go @@ -53,8 +53,14 @@ func UploadHandler(db *gorm.DB, cfg config.Config, p *pages.Pages) http.HandlerF return } + maxUploadSize := max(cfg.MaxFileBytes, cfg.MaxPasteBytes) + if r.ContentLength > maxUploadSize { + p.HTTPError(w, r, "upload too large", http.StatusRequestEntityTooLarge) + return + } + // use the largest of the sizes, and then do another size check per type down the line - r.Body = http.MaxBytesReader(w, r.Body, max(cfg.MaxFileBytes, cfg.MaxPasteBytes)) + r.Body = http.MaxBytesReader(w, r.Body, maxUploadSize) var err error mediaType := "text/plain" diff --git a/web/static/css/main.css b/web/static/css/main.css index 93b77c9..c9d8839 100644 --- a/web/static/css/main.css +++ b/web/static/css/main.css @@ -200,7 +200,7 @@ option { .btn:disabled { background-color: var(--surface); - color: var(--text-muted); + color: color-mix(in oklab, var(--text-muted), var(--bg)); cursor: not-allowed; } diff --git a/web/static/js/dnd.js b/web/static/js/dnd.js index 80a5fa1..aba25b5 100644 --- a/web/static/js/dnd.js +++ b/web/static/js/dnd.js @@ -1,10 +1,12 @@ const fileInput = document.getElementById("upload-file"); +let dragCounter = 0; function isFile(event) { return event.dataTransfer.types.includes("Files"); } function resetDrag() { + dragCounter = 0; document.body.classList.remove("dragging"); } @@ -12,6 +14,7 @@ document.addEventListener("dragenter", (event) => { if (!isFile(event)) return; event.preventDefault(); + dragCounter++; document.body.classList.add("dragging"); }); @@ -22,7 +25,12 @@ document.addEventListener("dragover", (event) => { event.dataTransfer.dropEffect = "copy"; }); -document.addEventListener("dragleave", () => { +document.addEventListener("dragleave", (event) => { + if (!isFile(event)) return; + dragCounter--; + if (dragCounter <= 0) { + resetDrag(); + } resetDrag(); }); diff --git a/web/static/js/upload.js b/web/static/js/upload.js new file mode 100644 index 0000000..50884f5 --- /dev/null +++ b/web/static/js/upload.js @@ -0,0 +1,19 @@ +const uploadFileInput = document.getElementById("upload-file"), + uploadFileMessage = document.getElementById("upload-file-message"), + uploadFileButton = document.getElementById("upload-file-button"), + uploadFileForm = document.getElementById("upload-file-form"); +const maxFileSize = uploadFileInput.dataset.maxSize; +uploadFileInput.addEventListener("change", () => { + let file = uploadFileInput.files[0] + if (file && file.size > maxFileSize) { + uploadFileInput.value = ""; + uploadFileMessage.textContent = "file size too large" + } + else { + uploadFileMessage.textContent = "" + } +}); +uploadFileForm.addEventListener("submit", () => { + uploadFileButton.classList.add("loading") + uploadFileButton.disabled = true; +}) \ No newline at end of file diff --git a/web/templates/home.html b/web/templates/home.html index 0d76ea6..4d19e80 100644 --- a/web/templates/home.html +++ b/web/templates/home.html @@ -6,6 +6,7 @@ minibin + {{if .Qt}}