1
0

Logging using a handmade one.

This commit is contained in:
Alexander Andreev 2022-02-07 19:42:09 +04:00
parent f5ce98c021
commit cafb305b76
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F

View File

@ -3,6 +3,7 @@ package handlers
import (
"crypto/sha256"
"dwelling-upload/internal/configuration"
"dwelling-upload/pkg/logging"
"dwelling-upload/pkg/server"
"dwelling-upload/pkg/utils"
"encoding/base64"
@ -11,7 +12,6 @@ import (
"html/template"
"io"
"io/fs"
"log"
"net/http"
"os"
"path"
@ -44,13 +44,20 @@ type UploadedData struct {
type UploadHandlers struct {
conf *configuration.Configuration
logErr *logging.Logger
logUpload *logging.Logger
logDownload *logging.Logger
}
func NewUploadHandlers(conf *configuration.Configuration) *UploadHandlers {
func NewUploadHandlers(conf *configuration.Configuration, lErr, lUp, lDown *logging.Logger) *UploadHandlers {
compiledTemplates = amber.MustCompileDir(conf.WebDir+"/templates",
amber.DefaultDirOptions, defaultAmberOptions)
return &UploadHandlers{
conf: conf}
conf: conf,
logErr: lErr,
logUpload: lUp,
logDownload: lDown}
}
func (h *UploadHandlers) Index(w http.ResponseWriter, r *http.Request) {
@ -84,7 +91,7 @@ func (h *UploadHandlers) Index(w http.ResponseWriter, r *http.Request) {
StorageUsedStr: usedStr,
}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Fatalln("failed to execute Index template:", err)
h.logErr.Fatalln("failed to execute Index template:", err)
}
}
@ -94,14 +101,14 @@ 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 {
log.Println("failed to parse form:", err)
h.logErr.Println("failed to parse form:", err)
http.Error(w, "request too big", http.StatusExpectationFailed)
return
}
f, fHandler, err := r.FormFile("file")
if err != nil {
log.Println("failed to open incoming file:", err)
h.logErr.Println("failed to open incoming file:", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
@ -112,7 +119,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
s256 := sha256.New()
if _, err := io.Copy(s256, f); err != nil {
log.Println("failed to compute SHA-256 hash:", err)
h.logErr.Println("failed to compute SHA-256 hash:", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
@ -129,14 +136,14 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
if os.IsNotExist(err) {
fDst, err := os.Create(fPath)
if err != nil {
log.Println("failed to open file for writing", err)
h.logErr.Println("failed to open file for writing", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
n, err := io.Copy(fDst, f)
if err != nil {
log.Println("failed to copy uploaded file to destination:", err)
h.logErr.Println("failed to copy uploaded file to destination:", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
@ -144,7 +151,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
fDst.Sync()
fDst.Close()
log.Printf("| %s | %s | %s | SHA256 %s | %s | %d", r.RemoteAddr, utils.NetworkType(r.Host),
h.logUpload.Printf("| %s | %s | %s | SHA256 %s | %s | %d", r.RemoteAddr, utils.NetworkType(r.Host),
fHandler.Filename, fSha256, fSaltedHash, fHandler.Size)
w.WriteHeader(http.StatusCreated)
@ -160,7 +167,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
KeepForHours: h.conf.Uploads.Limits.KeepForHours,
}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Fatalln("failed to execute Index template:", err)
h.logErr.Fatalln("failed to execute Index template:", err)
}
}
@ -181,11 +188,13 @@ func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) {
fd, err := os.Open(path)
if err != nil {
log.Println("failed to open file to read:", err)
h.logErr.Println("failed to open file to read:", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
defer fd.Close()
h.logDownload.Printf("| %s | %s | %s | %s", r.RemoteAddr, utils.NetworkType(r.Host), name, saltedHash)
http.ServeContent(w, r, path, stat.ModTime(), fd)
}