improve dnd; prevent large files early on upload
This commit is contained in:
@@ -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"]
|
||||||
@@ -20,6 +20,7 @@
|
|||||||
!web/**
|
!web/**
|
||||||
!.prettierrc
|
!.prettierrc
|
||||||
!.env.example
|
!.env.example
|
||||||
|
!.air.toml
|
||||||
|
|
||||||
# !Makefile
|
# !Makefile
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,9 @@ type HomePageData struct {
|
|||||||
Qt string
|
Qt string
|
||||||
PastePlaceholder string
|
PastePlaceholder string
|
||||||
FileMaxSize string
|
FileMaxSize string
|
||||||
|
FileMaxSizeBytes int64
|
||||||
PasteMaxSize string
|
PasteMaxSize string
|
||||||
|
PasteMaxSizeBytes int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pages *Pages) Render(w http.ResponseWriter, name string, data any) {
|
func (pages *Pages) Render(w http.ResponseWriter, name string, data any) {
|
||||||
@@ -110,7 +112,9 @@ func HomeHandler(pages *Pages, cfg config.Config) http.HandlerFunc {
|
|||||||
data := HomePageData{
|
data := HomePageData{
|
||||||
Expiries: cfg.Expiries,
|
Expiries: cfg.Expiries,
|
||||||
FileMaxSize: humanize.Bytes(uint64(cfg.MaxFileBytes)),
|
FileMaxSize: humanize.Bytes(uint64(cfg.MaxFileBytes)),
|
||||||
|
FileMaxSizeBytes: cfg.MaxFileBytes,
|
||||||
PasteMaxSize: humanize.Bytes(uint64(cfg.MaxPasteBytes)),
|
PasteMaxSize: humanize.Bytes(uint64(cfg.MaxPasteBytes)),
|
||||||
|
PasteMaxSizeBytes: cfg.MaxPasteBytes,
|
||||||
Qt: GetRandomQt(pages.Qts),
|
Qt: GetRandomQt(pages.Qts),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,8 +53,14 @@ func UploadHandler(db *gorm.DB, cfg config.Config, p *pages.Pages) http.HandlerF
|
|||||||
return
|
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
|
// 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
|
var err error
|
||||||
mediaType := "text/plain"
|
mediaType := "text/plain"
|
||||||
|
|||||||
@@ -200,7 +200,7 @@ option {
|
|||||||
|
|
||||||
.btn:disabled {
|
.btn:disabled {
|
||||||
background-color: var(--surface);
|
background-color: var(--surface);
|
||||||
color: var(--text-muted);
|
color: color-mix(in oklab, var(--text-muted), var(--bg));
|
||||||
cursor: not-allowed;
|
cursor: not-allowed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
const fileInput = document.getElementById("upload-file");
|
const fileInput = document.getElementById("upload-file");
|
||||||
|
let dragCounter = 0;
|
||||||
|
|
||||||
function isFile(event) {
|
function isFile(event) {
|
||||||
return event.dataTransfer.types.includes("Files");
|
return event.dataTransfer.types.includes("Files");
|
||||||
}
|
}
|
||||||
|
|
||||||
function resetDrag() {
|
function resetDrag() {
|
||||||
|
dragCounter = 0;
|
||||||
document.body.classList.remove("dragging");
|
document.body.classList.remove("dragging");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -12,6 +14,7 @@ document.addEventListener("dragenter", (event) => {
|
|||||||
if (!isFile(event)) return;
|
if (!isFile(event)) return;
|
||||||
|
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
dragCounter++;
|
||||||
document.body.classList.add("dragging");
|
document.body.classList.add("dragging");
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -22,7 +25,12 @@ document.addEventListener("dragover", (event) => {
|
|||||||
event.dataTransfer.dropEffect = "copy";
|
event.dataTransfer.dropEffect = "copy";
|
||||||
});
|
});
|
||||||
|
|
||||||
document.addEventListener("dragleave", () => {
|
document.addEventListener("dragleave", (event) => {
|
||||||
|
if (!isFile(event)) return;
|
||||||
|
dragCounter--;
|
||||||
|
if (dragCounter <= 0) {
|
||||||
|
resetDrag();
|
||||||
|
}
|
||||||
resetDrag();
|
resetDrag();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
})
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
<title>minibin</title>
|
<title>minibin</title>
|
||||||
<link rel="stylesheet" href="/css/main.css" />
|
<link rel="stylesheet" href="/css/main.css" />
|
||||||
<script defer src="/js/dnd.js"></script>
|
<script defer src="/js/dnd.js"></script>
|
||||||
|
<script defer src="/js/upload.js"></script>
|
||||||
{{if .Qt}}
|
{{if .Qt}}
|
||||||
<style>
|
<style>
|
||||||
:root {
|
:root {
|
||||||
@@ -28,7 +29,7 @@
|
|||||||
<input type="radio" name="upload-type" id="type-paste" />
|
<input type="radio" name="upload-type" id="type-paste" />
|
||||||
<label for="type-paste">paste</label>
|
<label for="type-paste">paste</label>
|
||||||
<div class="file-panel">
|
<div class="file-panel">
|
||||||
<form action="/upload?return=html" method="post" enctype="multipart/form-data">
|
<form id="upload-file-form" action="/upload?return=html" method="post" enctype="multipart/form-data">
|
||||||
<label class="muted" for="file-expiry">expiry</label>
|
<label class="muted" for="file-expiry">expiry</label>
|
||||||
<select id="file-expiry" name="expiry">
|
<select id="file-expiry" name="expiry">
|
||||||
{{range .Expiries}}
|
{{range .Expiries}}
|
||||||
@@ -40,9 +41,10 @@
|
|||||||
<option value="long">16 characters</option>
|
<option value="long">16 characters</option>
|
||||||
<option value="short">10 characters</option>
|
<option value="short">10 characters</option>
|
||||||
</select>
|
</select>
|
||||||
<input id="upload-file" type="file" required name="file" />
|
<input id="upload-file" data-max-size="{{.FileMaxSizeBytes}}" type="file" required name="file" />
|
||||||
<div class="submit-container">
|
<div class="submit-container">
|
||||||
<button type="submit" class="btn">upload file</button>
|
<button id="upload-file-button" type="submit" class="btn">upload file</button>
|
||||||
|
<span id="upload-file-message"></span>
|
||||||
<span class="muted">max {{.FileMaxSize}}</span>
|
<span class="muted">max {{.FileMaxSize}}</span>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
Reference in New Issue
Block a user