From c41ff56ffa3f86af02be0772cee90d798c733020 Mon Sep 17 00:00:00 2001 From: nxshock Date: Wed, 10 Jul 2024 16:16:51 +0500 Subject: [PATCH] Implement multiple file upload --- PKGBUILD | 2 +- handlers.go | 53 ++++++++++++++++++++++------------------ main.go | 2 +- templates/index.htm | 59 +++++++++++++++++++++++---------------------- 4 files changed, 62 insertions(+), 54 deletions(-) diff --git a/PKGBUILD b/PKGBUILD index fc5793b..7471148 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,5 +1,5 @@ pkgname=simplefileshare -pkgver=0.1.3 +pkgver=0.1.4 pkgrel=0 pkgdesc="Simple file share" arch=('x86_64' 'aarch64') diff --git a/handlers.go b/handlers.go index 9043fbc..1b5e353 100644 --- a/handlers.go +++ b/handlers.go @@ -74,30 +74,37 @@ func HandleUpload(w http.ResponseWriter, r *http.Request) { return } - file, header, err := r.FormFile("file") - if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - return - } - defer file.Close() + var errors []string + for _, header := range r.MultipartForm.File["file"] { + file, err := header.Open() + if err != nil { + errors = append(errors, err.Error()) + continue + } + defer file.Close() - filePath := filepath.Join(config.StoragePath, header.Filename) - if _, err := os.Stat(filePath); !os.IsNotExist(err) { - http.Error(w, "файл с таким именем уже существует", http.StatusBadRequest) - return + filePath := filepath.Join(config.StoragePath, header.Filename) + if _, err := os.Stat(filePath); !os.IsNotExist(err) { + errors = append(errors, fmt.Sprintf("файл с именем %s уже существует", header.Filename)) + continue + } + + f, err := os.Create(filePath) + if err != nil { + errors = append(errors, err.Error()) + continue + } + defer f.Close() + + _, err = io.Copy(f, file) + if err != nil { + errors = append(errors, err.Error()) + continue + } } - f, err := os.Create(filePath) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - defer f.Close() - - _, err = io.Copy(f, file) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return + if len(errors) > 0 { + http.Error(w, strings.Join(errors, "\n"), http.StatusBadRequest) } } @@ -127,7 +134,7 @@ func HandleDownload(w http.ResponseWriter, r *http.Request) { w.Header().Set("Accept-Ranges", "none") w.Header().Set("Content-Length", strconv.Itoa(int(fileStat.Size()))) - io.CopyBuffer(w, f, make([]byte, 4096)) + _, _ = io.CopyBuffer(w, f, make([]byte, 4096)) } func HandleStream(w http.ResponseWriter, r *http.Request) { @@ -157,5 +164,5 @@ func HandleIcon(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "image/svg+xml") w.Header().Set("Cache-Control", "public, max-age=31557600") - io.Copy(w, f) + _, _ = io.Copy(w, f) } diff --git a/main.go b/main.go index 536ae2f..e177df8 100644 --- a/main.go +++ b/main.go @@ -28,7 +28,7 @@ func init() { } if config.RemoveFilePeriod > 0 { - go removeOldFilesThread(config.StoragePath, time.Duration(config.RemoveFilePeriod)*time.Hour) + go removeOldFilesThread(time.Duration(config.RemoveFilePeriod) * time.Hour) } http.HandleFunc("/", HandleRoot) diff --git a/templates/index.htm b/templates/index.htm index 2fd2292..06346cf 100644 --- a/templates/index.htm +++ b/templates/index.htm @@ -7,33 +7,6 @@ File Storage - - @@ -43,8 +16,8 @@ File Storage
@@ -70,6 +43,34 @@
Файлы хранятся как минимум {{.StorageDuration}} ч.
+ \ No newline at end of file