always use logger

This commit is contained in:
Andrea Spacca 2021-07-07 21:03:54 +02:00
parent 811679f8fb
commit d9198e78db
3 changed files with 54 additions and 56 deletions

View file

@ -30,7 +30,6 @@ import (
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"time" "time"
@ -58,7 +57,7 @@ func (s *Server) scanHandler(w http.ResponseWriter, r *http.Request) {
abort := make(chan bool) abort := make(chan bool)
response, err := c.ScanStream(reader, abort) response, err := c.ScanStream(reader, abort)
if err != nil { if err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }

View file

@ -40,7 +40,6 @@ import (
html_template "html/template" html_template "html/template"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"mime" "mime"
"net/http" "net/http"
"net/url" "net/url"
@ -125,7 +124,7 @@ func (s *Server) previewHandler(w http.ResponseWriter, r *http.Request) {
metadata, err := s.CheckMetadata(token, filename, false) metadata, err := s.CheckMetadata(token, filename, false)
if err != nil { if err != nil {
log.Printf("Error metadata: %s", err.Error()) s.logger.Printf("Error metadata: %s", err.Error())
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return return
} }
@ -272,7 +271,7 @@ func sanitize(fileName string) string {
func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) { func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) {
if err := r.ParseMultipartForm(_24K); nil != err { if err := r.ParseMultipartForm(_24K); nil != err {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, "Error occurred copying to output stream", 500) http.Error(w, "Error occurred copying to output stream", 500)
return return
} }
@ -290,7 +289,7 @@ func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) {
var err error var err error
if f, err = fheader.Open(); err != nil { if f, err = fheader.Open(); err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
@ -299,7 +298,7 @@ func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) {
n, err := io.CopyN(&b, f, _24K+1) n, err := io.CopyN(&b, f, _24K+1)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
@ -310,14 +309,14 @@ func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) {
if n > _24K { if n > _24K {
file, err = ioutil.TempFile(s.tempPath, "transfer-") file, err = ioutil.TempFile(s.tempPath, "transfer-")
if err != nil { if err != nil {
log.Fatal(err) s.logger.Fatal(err)
} }
n, err = io.Copy(file, io.MultiReader(&b, f)) n, err = io.Copy(file, io.MultiReader(&b, f))
if err != nil { if err != nil {
cleanTmpFile(file) s.cleanTmpFile(file)
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
@ -330,7 +329,7 @@ func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) {
contentLength := n contentLength := n
if s.maxUploadSize > 0 && contentLength > s.maxUploadSize { if s.maxUploadSize > 0 && contentLength > s.maxUploadSize {
log.Print("Entity too large") s.logger.Print("Entity too large")
http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge) http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
return return
} }
@ -339,23 +338,23 @@ func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) {
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
if err := json.NewEncoder(buffer).Encode(metadata); err != nil { if err := json.NewEncoder(buffer).Encode(metadata); err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, errors.New("Could not encode metadata").Error(), 500) http.Error(w, errors.New("Could not encode metadata").Error(), 500)
cleanTmpFile(file) s.cleanTmpFile(file)
return return
} else if err := s.storage.Put(token, fmt.Sprintf("%s.metadata", filename), buffer, "text/json", uint64(buffer.Len())); err != nil { } else if err := s.storage.Put(token, fmt.Sprintf("%s.metadata", filename), buffer, "text/json", uint64(buffer.Len())); err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, errors.New("Could not save metadata").Error(), 500) http.Error(w, errors.New("Could not save metadata").Error(), 500)
cleanTmpFile(file) s.cleanTmpFile(file)
return return
} }
log.Printf("Uploading %s %s %d %s", token, filename, contentLength, contentType) s.logger.Printf("Uploading %s %s %d %s", token, filename, contentLength, contentType)
if err = s.storage.Put(token, filename, reader, contentType, uint64(contentLength)); err != nil { if err = s.storage.Put(token, filename, reader, contentType, uint64(contentLength)); err != nil {
log.Printf("Backend storage error: %s", err.Error()) s.logger.Printf("Backend storage error: %s", err.Error())
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
@ -365,21 +364,21 @@ func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) {
relativeURL, _ := url.Parse(path.Join(s.proxyPath, token, filename)) relativeURL, _ := url.Parse(path.Join(s.proxyPath, token, filename))
fmt.Fprintln(w, getURL(r, s.proxyPort).ResolveReference(relativeURL).String()) fmt.Fprintln(w, getURL(r, s.proxyPort).ResolveReference(relativeURL).String())
cleanTmpFile(file) s.cleanTmpFile(file)
} }
} }
} }
func cleanTmpFile(f *os.File) { func (s *Server) cleanTmpFile(f *os.File) {
if f != nil { if f != nil {
err := f.Close() err := f.Close()
if err != nil { if err != nil {
log.Printf("Error closing tmpfile: %s (%s)", err, f.Name()) s.logger.Printf("Error closing tmpfile: %s (%s)", err, f.Name())
} }
err = os.Remove(f.Name()) err = os.Remove(f.Name())
if err != nil { if err != nil {
log.Printf("Error removing tmpfile: %s (%s)", err, f.Name()) s.logger.Printf("Error removing tmpfile: %s (%s)", err, f.Name())
} }
} }
} }
@ -447,7 +446,7 @@ func (s *Server) putHandler(w http.ResponseWriter, r *http.Request) {
n, err := io.CopyN(&b, f, _24K+1) n, err := io.CopyN(&b, f, _24K+1)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
log.Printf("Error putting new file: %s", err.Error()) s.logger.Printf("Error putting new file: %s", err.Error())
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
@ -457,16 +456,16 @@ func (s *Server) putHandler(w http.ResponseWriter, r *http.Request) {
if n > _24K { if n > _24K {
file, err = ioutil.TempFile(s.tempPath, "transfer-") file, err = ioutil.TempFile(s.tempPath, "transfer-")
if err != nil { if err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
defer cleanTmpFile(file) defer s.cleanTmpFile(file)
n, err = io.Copy(file, io.MultiReader(&b, f)) n, err = io.Copy(file, io.MultiReader(&b, f))
if err != nil { if err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
@ -480,13 +479,13 @@ func (s *Server) putHandler(w http.ResponseWriter, r *http.Request) {
} }
if s.maxUploadSize > 0 && contentLength > s.maxUploadSize { if s.maxUploadSize > 0 && contentLength > s.maxUploadSize {
log.Print("Entity too large") s.logger.Print("Entity too large")
http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge) http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
return return
} }
if contentLength == 0 { if contentLength == 0 {
log.Print("Empty content-length") s.logger.Print("Empty content-length")
http.Error(w, errors.New("Could not upload empty file").Error(), 400) http.Error(w, errors.New("Could not upload empty file").Error(), 400)
return return
} }
@ -499,21 +498,21 @@ func (s *Server) putHandler(w http.ResponseWriter, r *http.Request) {
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
if err := json.NewEncoder(buffer).Encode(metadata); err != nil { if err := json.NewEncoder(buffer).Encode(metadata); err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, errors.New("Could not encode metadata").Error(), 500) http.Error(w, errors.New("Could not encode metadata").Error(), 500)
return return
} else if err := s.storage.Put(token, fmt.Sprintf("%s.metadata", filename), buffer, "text/json", uint64(buffer.Len())); err != nil { } else if err := s.storage.Put(token, fmt.Sprintf("%s.metadata", filename), buffer, "text/json", uint64(buffer.Len())); err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, errors.New("Could not save metadata").Error(), 500) http.Error(w, errors.New("Could not save metadata").Error(), 500)
return return
} }
log.Printf("Uploading %s %s %d %s", token, filename, contentLength, contentType) s.logger.Printf("Uploading %s %s %d %s", token, filename, contentLength, contentType)
var err error var err error
if err = s.storage.Put(token, filename, reader, contentType, uint64(contentLength)); err != nil { if err = s.storage.Put(token, filename, reader, contentType, uint64(contentLength)); err != nil {
log.Printf("Error putting new file: %s", err.Error()) s.logger.Printf("Error putting new file: %s", err.Error())
http.Error(w, errors.New("Could not save file").Error(), 500) http.Error(w, errors.New("Could not save file").Error(), 500)
return return
} }
@ -723,7 +722,7 @@ func (s *Server) purgeHandler() {
select { select {
case <-ticker.C: case <-ticker.C:
err := s.storage.Purge(s.purgeDays) err := s.storage.Purge(s.purgeDays)
log.Printf("error cleaning up expired files: %v", err) s.logger.Printf("error cleaning up expired files: %v", err)
} }
} }
}() }()
@ -737,7 +736,7 @@ func (s *Server) deleteHandler(w http.ResponseWriter, r *http.Request) {
deletionToken := vars["deletionToken"] deletionToken := vars["deletionToken"]
if err := s.CheckDeletionToken(deletionToken, token, filename); err != nil { if err := s.CheckDeletionToken(deletionToken, token, filename); err != nil {
log.Printf("Error metadata: %s", err.Error()) s.logger.Printf("Error metadata: %s", err.Error())
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return return
} }
@ -747,7 +746,7 @@ func (s *Server) deleteHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return return
} else if err != nil { } else if err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, "Could not delete file.", 500) http.Error(w, "Could not delete file.", 500)
return return
} }
@ -773,7 +772,7 @@ func (s *Server) zipHandler(w http.ResponseWriter, r *http.Request) {
filename := sanitize(strings.Split(key, "/")[1]) filename := sanitize(strings.Split(key, "/")[1])
if _, err := s.CheckMetadata(token, filename, true); err != nil { if _, err := s.CheckMetadata(token, filename, true); err != nil {
log.Printf("Error metadata: %s", err.Error()) s.logger.Printf("Error metadata: %s", err.Error())
continue continue
} }
@ -784,7 +783,7 @@ func (s *Server) zipHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "File not found", 404) http.Error(w, "File not found", 404)
return return
} else { } else {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, "Could not retrieve file.", 500) http.Error(w, "Could not retrieve file.", 500)
return return
} }
@ -802,20 +801,20 @@ func (s *Server) zipHandler(w http.ResponseWriter, r *http.Request) {
fw, err := zw.CreateHeader(header) fw, err := zw.CreateHeader(header)
if err != nil { if err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, "Internal server error.", 500) http.Error(w, "Internal server error.", 500)
return return
} }
if _, err = io.Copy(fw, reader); err != nil { if _, err = io.Copy(fw, reader); err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, "Internal server error.", 500) http.Error(w, "Internal server error.", 500)
return return
} }
} }
if err := zw.Close(); err != nil { if err := zw.Close(); err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, "Internal server error.", 500) http.Error(w, "Internal server error.", 500)
return return
} }
@ -845,7 +844,7 @@ func (s *Server) tarGzHandler(w http.ResponseWriter, r *http.Request) {
filename := sanitize(strings.Split(key, "/")[1]) filename := sanitize(strings.Split(key, "/")[1])
if _, err := s.CheckMetadata(token, filename, true); err != nil { if _, err := s.CheckMetadata(token, filename, true); err != nil {
log.Printf("Error metadata: %s", err.Error()) s.logger.Printf("Error metadata: %s", err.Error())
continue continue
} }
@ -855,7 +854,7 @@ func (s *Server) tarGzHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "File not found", 404) http.Error(w, "File not found", 404)
return return
} else { } else {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, "Could not retrieve file.", 500) http.Error(w, "Could not retrieve file.", 500)
return return
} }
@ -870,13 +869,13 @@ func (s *Server) tarGzHandler(w http.ResponseWriter, r *http.Request) {
err = zw.WriteHeader(header) err = zw.WriteHeader(header)
if err != nil { if err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, "Internal server error.", 500) http.Error(w, "Internal server error.", 500)
return return
} }
if _, err = io.Copy(zw, reader); err != nil { if _, err = io.Copy(zw, reader); err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, "Internal server error.", 500) http.Error(w, "Internal server error.", 500)
return return
} }
@ -904,7 +903,7 @@ func (s *Server) tarHandler(w http.ResponseWriter, r *http.Request) {
filename := strings.Split(key, "/")[1] filename := strings.Split(key, "/")[1]
if _, err := s.CheckMetadata(token, filename, true); err != nil { if _, err := s.CheckMetadata(token, filename, true); err != nil {
log.Printf("Error metadata: %s", err.Error()) s.logger.Printf("Error metadata: %s", err.Error())
continue continue
} }
@ -914,7 +913,7 @@ func (s *Server) tarHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "File not found", 404) http.Error(w, "File not found", 404)
return return
} else { } else {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, "Could not retrieve file.", 500) http.Error(w, "Could not retrieve file.", 500)
return return
} }
@ -929,13 +928,13 @@ func (s *Server) tarHandler(w http.ResponseWriter, r *http.Request) {
err = zw.WriteHeader(header) err = zw.WriteHeader(header)
if err != nil { if err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, "Internal server error.", 500) http.Error(w, "Internal server error.", 500)
return return
} }
if _, err = io.Copy(zw, reader); err != nil { if _, err = io.Copy(zw, reader); err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, "Internal server error.", 500) http.Error(w, "Internal server error.", 500)
return return
} }
@ -951,7 +950,7 @@ func (s *Server) headHandler(w http.ResponseWriter, r *http.Request) {
metadata, err := s.CheckMetadata(token, filename, false) metadata, err := s.CheckMetadata(token, filename, false)
if err != nil { if err != nil {
log.Printf("Error metadata: %s", err.Error()) s.logger.Printf("Error metadata: %s", err.Error())
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return return
} }
@ -962,7 +961,7 @@ func (s *Server) headHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return return
} else if err != nil { } else if err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, "Could not retrieve file.", 500) http.Error(w, "Could not retrieve file.", 500)
return return
} }
@ -986,7 +985,7 @@ func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) {
metadata, err := s.CheckMetadata(token, filename, true) metadata, err := s.CheckMetadata(token, filename, true)
if err != nil { if err != nil {
log.Printf("Error metadata: %s", err.Error()) s.logger.Printf("Error metadata: %s", err.Error())
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return return
} }
@ -997,7 +996,7 @@ func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return return
} else if err != nil { } else if err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, "Could not retrieve file.", 500) http.Error(w, "Could not retrieve file.", 500)
return return
} }
@ -1027,7 +1026,7 @@ func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) {
if w.Header().Get("Range") == "" { if w.Header().Get("Range") == "" {
if _, err = io.Copy(w, reader); err != nil { if _, err = io.Copy(w, reader); err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, "Error occurred copying to output stream", 500) http.Error(w, "Error occurred copying to output stream", 500)
return return
} }
@ -1037,12 +1036,12 @@ func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) {
file, err := ioutil.TempFile(s.tempPath, "range-") file, err := ioutil.TempFile(s.tempPath, "range-")
if err != nil { if err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, "Error occurred copying to output stream", 500) http.Error(w, "Error occurred copying to output stream", 500)
return return
} }
defer cleanTmpFile(file) defer s.cleanTmpFile(file)
tee := io.TeeReader(reader, file) tee := io.TeeReader(reader, file)
for { for {
@ -1053,7 +1052,7 @@ func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) {
} }
if err != nil { if err != nil {
log.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, "Error occurred copying to output stream", 500) http.Error(w, "Error occurred copying to output stream", 500)
return return
} }

View file

@ -161,7 +161,7 @@ func LogFile(logger *log.Logger, s string) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
f, err := os.OpenFile(s, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) f, err := os.OpenFile(s, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil { if err != nil {
log.Fatalf("error opening file: %v", err) logger.Fatalf("error opening file: %v", err)
} }
logger.SetOutput(f) logger.SetOutput(f)