diff --git a/PKGBUILD b/PKGBUILD index 7471148..fc5793b 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,5 +1,5 @@ pkgname=simplefileshare -pkgver=0.1.4 +pkgver=0.1.3 pkgrel=0 pkgdesc="Simple file share" arch=('x86_64' 'aarch64') diff --git a/handlers.go b/handlers.go index 1b5e353..9043fbc 100644 --- a/handlers.go +++ b/handlers.go @@ -74,37 +74,30 @@ func HandleUpload(w http.ResponseWriter, r *http.Request) { return } - 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() + file, header, err := r.FormFile("file") + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + defer file.Close() - 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 - } + filePath := filepath.Join(config.StoragePath, header.Filename) + if _, err := os.Stat(filePath); !os.IsNotExist(err) { + http.Error(w, "файл с таким именем уже существует", http.StatusBadRequest) + return } - if len(errors) > 0 { - http.Error(w, strings.Join(errors, "\n"), http.StatusBadRequest) + 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 } } @@ -134,7 +127,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) { @@ -164,5 +157,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 e177df8..536ae2f 100644 --- a/main.go +++ b/main.go @@ -28,7 +28,7 @@ func init() { } if config.RemoveFilePeriod > 0 { - go removeOldFilesThread(time.Duration(config.RemoveFilePeriod) * time.Hour) + go removeOldFilesThread(config.StoragePath, time.Duration(config.RemoveFilePeriod)*time.Hour) } http.HandleFunc("/", HandleRoot) diff --git a/templates/index.htm b/templates/index.htm index 06346cf..e1c18ef 100644 --- a/templates/index.htm +++ b/templates/index.htm @@ -1,5 +1,5 @@ - + @@ -12,12 +12,12 @@
- icon + File Storage
@@ -30,47 +30,42 @@ Размер Дата - {{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 697e2d2..d6695c7 100644 --- a/walker.go +++ b/walker.go @@ -8,12 +8,12 @@ import ( log "github.com/sirupsen/logrus" ) -func removeOldFilesThread(olderThan time.Duration) { +func removeOldFilesThread(path string, olderThan time.Duration) { ticker := time.NewTicker(time.Hour) - for range ticker.C { + for _ = range ticker.C { log.Debugln("Removing old files...") - err := removeOldFiles(olderThan) + err := removeOldFiles(path, olderThan) if err != nil { log.Println(err) } @@ -21,7 +21,7 @@ func removeOldFilesThread(olderThan time.Duration) { } } -func removeOldFiles(olderThan time.Duration) error { +func removeOldFiles(path string, olderThan time.Duration) error { return filepath.Walk(config.StoragePath, func(path string, info os.FileInfo, err error) error { if err != nil { return err