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 e1c18ef..06346cf 100644 --- a/templates/index.htm +++ b/templates/index.htm @@ -1,5 +1,5 @@ - + @@ -12,12 +12,12 @@
- + icon File Storage
@@ -30,42 +30,47 @@ Размер Дата -{{range .Files}} + {{range .Files}} {{.Name}} просмотр -
{{.Size}}
+ +
{{.Size}}
+ {{.Date}} -{{end}} + {{end}} +
+ - - + \ No newline at end of file diff --git a/walker.go b/walker.go index d6695c7..697e2d2 100644 --- a/walker.go +++ b/walker.go @@ -8,12 +8,12 @@ import ( log "github.com/sirupsen/logrus" ) -func removeOldFilesThread(path string, olderThan time.Duration) { +func removeOldFilesThread(olderThan time.Duration) { ticker := time.NewTicker(time.Hour) - for _ = range ticker.C { + for range ticker.C { log.Debugln("Removing old files...") - err := removeOldFiles(path, olderThan) + err := removeOldFiles(olderThan) if err != nil { log.Println(err) } @@ -21,7 +21,7 @@ func removeOldFilesThread(path string, olderThan time.Duration) { } } -func removeOldFiles(path string, olderThan time.Duration) error { +func removeOldFiles(olderThan time.Duration) error { return filepath.Walk(config.StoragePath, func(path string, info os.FileInfo, err error) error { if err != nil { return err