From 2d159874f1471c4cb1f3c22999964fb9dff9cd21 Mon Sep 17 00:00:00 2001 From: hladu357 Date: Thu, 13 Jun 2024 16:22:59 +0200 Subject: [PATCH] upload page added --- html/user.html | 259 ++++++++++++++++++++++++++++++++++++++------ html/user_bad.html | 0 html/user_good.html | 192 ++++++++++++++++++++++++++++++++ upload.go | 32 +++++- 4 files changed, 450 insertions(+), 33 deletions(-) create mode 100644 html/user_bad.html create mode 100644 html/user_good.html diff --git a/html/user.html b/html/user.html index 929ea2b..2fa45c3 100644 --- a/html/user.html +++ b/html/user.html @@ -1,45 +1,240 @@ - - - Chunked File Upload + + +File Upload with Drag and Drop + - - +
+
+

Drag & Drop files here

+
+ + + - + } + + function getFileSize(size) { + if (size === 0) return '0 Bytes'; + const k = 1024; + const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; + const i = Math.floor(Math.log(size) / Math.log(k)); + return parseFloat((size / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; + } + + uploadButton.addEventListener('click', function() { + if (filesToUpload.length > 0) { + const totalSize = filesToUpload.reduce((acc, file) => acc + file.size, 0); + if (totalSize > sizeLimit) { + alert('Total file size exceeds the limit! Please remove some files.'); + } else { + // Here you can send filesToUpload to the server or perform other actions + console.log('Uploading files:', filesToUpload); + alert('Files uploaded successfully!'); + // Clear the filesToUpload array and update the file list display + filesToUpload = []; + updateFileList(); + updateProgressBar(); + } + } else { + alert('Please select files to upload.'); + } + }); + + - \ No newline at end of file + diff --git a/html/user_bad.html b/html/user_bad.html new file mode 100644 index 0000000..e69de29 diff --git a/html/user_good.html b/html/user_good.html new file mode 100644 index 0000000..4672183 --- /dev/null +++ b/html/user_good.html @@ -0,0 +1,192 @@ + + + + + +File Upload with Drag and Drop + + + +
+
+

Drag & Drop files here

+
+ + + + +
+
+ + + + + diff --git a/upload.go b/upload.go index 6f5f30b..17c2586 100644 --- a/upload.go +++ b/upload.go @@ -59,6 +59,13 @@ func (d *UploadDatabase) commit(size int64, finalName string, lifefile time.Dura return id } +func (d *UploadDatabase) exists(id uuid.UUID) bool { + d.mtx.Lock() + defer d.mtx.Unlock() + _, found := d.data[id] + return found +} + func (d *UploadDatabase) activate(id uuid.UUID) *UploadContext { d.mtx.Lock() defer d.mtx.Unlock() @@ -69,7 +76,12 @@ func (d *UploadDatabase) activate(id uuid.UUID) *UploadContext { } func (d *UploadDatabase) deactivate(id uuid.UUID) { +} +func (d *UploadDatabase) getSize(id uuid.UUID) int64 { + d.mtx.Lock() + defer d.mtx.Unlock() + return d.data[id].maxSize } func initialize() { @@ -127,7 +139,7 @@ func generateHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, "Commit failed", http.StatusBadRequest) return } - uploadLink = fmt.Sprintf("https://localhost:8080/upload?key=%s", id) + uploadLink = fmt.Sprintf("http://localhost:8080/upload?key=%s", id) response := map[string]string{"uploadLink": uploadLink} w.Header().Set("Content-Type", "application/json") @@ -138,7 +150,25 @@ func generateHandler(w http.ResponseWriter, r *http.Request) { } func uploadHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + w.WriteHeader(http.StatusMethodNotAllowed) + } + id, err := uuid.Parse(r.URL.Query().Get("key")) + if err != nil || !db.exists(id) { + http.ServeFile(w, r, "./html/user_bad.html") + return + } + + if !r.URL.Query().Has("limit") { + q := r.URL.Query() + q.Set("limit", strconv.FormatInt(db.getSize(id), 10)) + r.URL.RawQuery = q.Encode() + http.Redirect(w, r, r.URL.String(), http.StatusSeeOther) + return + } + + http.ServeFile(w, r, "./html/user.html") } func fileHandler(w http.ResponseWriter, r *http.Request) {