diff --git a/cmd/dwelling-upload/main.go b/cmd/dwelling-upload/main.go index 482d61b..11bd601 100644 --- a/cmd/dwelling-upload/main.go +++ b/cmd/dwelling-upload/main.go @@ -24,6 +24,7 @@ var version string func main() { flag.Parse() + log.SetFlags(log.Llongfile) if *showVersion { fmt.Println("dwelling-upload Ver. ", version, "\nCopyright (c) 2022,2023 Alexander \"Arav\" Andreev ") @@ -54,12 +55,6 @@ func main() { } } - logErr, err := logging.New(config.Log.Error) - if err != nil { - log.Fatalln("error logger:", err) - } - defer logErr.Close() - logFile, err := logging.New(config.Log.File) if err != nil { log.Fatalln("file logger:", err) @@ -68,23 +63,23 @@ func main() { watcha, err := watcher.NewInotifyWatcher() if err != nil { - logErr.Fatalln(err) + log.Fatalln(err) } defer watcha.Close() if err := watcha.AddWatch(config.Uploads.Directory, watcher.CrDelMask); err != nil { - logErr.Fatalln(err) + log.Fatalln(err) } uploadDirNotify := make(chan uint32) uploadDirSize, err := utils.DirectorySize(config.Uploads.Directory) if err != nil { - logErr.Fatalf("failed to get initial size of %s: %s", config.Uploads.Directory, err) + log.Fatalf("failed to get initial size of %s: %s", config.Uploads.Directory, err) } watcha.WatchForMask(uploadDirNotify, watcher.CrDelMask) - hand := http.NewUploadHandlers(config, logErr, logFile, &uploadDirSize) + hand := http.NewUploadHandlers(config, logFile, &uploadDirSize) srv := http.NewHttpServer() srv.SetNotFoundHandler(http.NotFound) @@ -100,7 +95,7 @@ func main() { srv.DELETE("/:hash", hand.Delete) if err := srv.Start(network, *listenAddress); err != nil { - logErr.Fatalln("failed to start a server:", err) + log.Fatalln("failed to start a server:", err) } doneSignal := make(chan os.Signal, 1) @@ -112,12 +107,11 @@ func main() { for { select { case <-logReopenSignal: - logErr.Reopen(config.Log.Error) logFile.Reopen(config.Log.File) case <-uploadDirNotify: uploadDirSize, err = utils.DirectorySize(config.Uploads.Directory) if err != nil { - logErr.Println("failed to get uploads directory size:", err) + log.Println("failed to get uploads directory size:", err) } } } @@ -126,6 +120,6 @@ func main() { <-doneSignal if err := srv.Stop(); err != nil { - logErr.Fatalln("failed to properly shutdown a server:", err) + log.Fatalln("failed to properly shutdown a server:", err) } } diff --git a/internal/http/handlers.go b/internal/http/handlers.go index 52392af..d1a036b 100644 --- a/internal/http/handlers.go +++ b/internal/http/handlers.go @@ -10,6 +10,7 @@ import ( "encoding/hex" "fmt" "io" + "log" "net/http" "net/url" "os" @@ -20,16 +21,14 @@ import ( type UploadHandlers struct { conf *configuration.Configuration - logErr *logging.Logger logFile *logging.Logger uploadDirSize *int64 } -func NewUploadHandlers(conf *configuration.Configuration, lErr, lFile *logging.Logger, uploadDirSize *int64) *UploadHandlers { +func NewUploadHandlers(conf *configuration.Configuration, lFile *logging.Logger, uploadDirSize *int64) *UploadHandlers { return &UploadHandlers{ conf: conf, - logErr: lErr, logFile: lFile, uploadDirSize: uploadDirSize} } @@ -57,16 +56,16 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) { r.Body = http.MaxBytesReader(w, r.Body, fMaxSizeBytes) if err := r.ParseMultipartForm(fMaxSizeBytes); err != nil { - h.logErr.Println("failed to parse form:", err) - http.Error(w, err.Error(), http.StatusExpectationFailed) + log.Println("failed to parse upload form:", err) + http.Error(w, "Failed to parse upload form.", http.StatusExpectationFailed) return } f, fHandler, err := r.FormFile("file") if err != nil { - h.logErr.Println("failed to open incoming file:", err) + log.Println("failed to open incoming file:", err) w.WriteHeader(http.StatusInternalServerError) - http.Error(w, "cannot read incoming file", http.StatusInternalServerError) + http.Error(w, "Error reading an incoming file.", http.StatusInternalServerError) return } defer func() { @@ -77,15 +76,15 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) { var leftSpace int64 = storCapacity - *h.uploadDirSize if leftSpace < fHandler.Size { - h.logErr.Println("not enough space left in storage, only", leftSpace>>20, "MiB left") + log.Println("not enough space left in storage, only", leftSpace>>20, "MiB left") w.WriteHeader(http.StatusInternalServerError) web.ErrorNoSpace(utils.MainSite(r.Host), w) } s256 := sha256.New() if _, err := io.Copy(s256, f); err != nil { - h.logErr.Println("failed to compute SHA-256 hash:", err) - http.Error(w, "cannot compute hash for a file", http.StatusInternalServerError) + log.Println("failed to compute a SHA-256 hash:", err) + http.Error(w, "A hash for the file cannot be computed.", http.StatusInternalServerError) return } @@ -101,8 +100,8 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) { if os.IsNotExist(err) { fDst, err := os.Create(fPath) if err != nil { - h.logErr.Println("failed to open file for writing", err) - http.Error(w, "cannot create your file", http.StatusInternalServerError) + log.Println("failed to open file for writing", err) + http.Error(w, "File cannot be written.", http.StatusInternalServerError) return } defer fDst.Close() @@ -117,8 +116,8 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) { _, err = io.Copy(fDst, f) if err != nil { - h.logErr.Println("failed to copy uploaded file to destination:", err) - http.Error(w, "cannot copy file's content", http.StatusInternalServerError) + log.Println("failed to copy uploaded file to destination:", err) + http.Error(w, "Failed to copy uploaded file to the storage.", http.StatusInternalServerError) return } @@ -164,8 +163,8 @@ func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) { fd, err := os.Open(path) if err != nil { - h.logErr.Println("failed to open file to read:", err) - w.WriteHeader(http.StatusInternalServerError) + log.Println("failed to open file to read:", err) + http.Error(w, "Failed to open file to read.", http.StatusInternalServerError) return } defer fd.Close() @@ -196,8 +195,8 @@ func (h *UploadHandlers) Delete(w http.ResponseWriter, r *http.Request) { err = os.Remove(path) if err != nil { - w.WriteHeader(http.StatusInternalServerError) - fmt.Fprint(w, err) + log.Println("failed to remove a file:", err) + http.Error(w, "Failed to remove a file.", http.StatusInternalServerError) return }