1
0

Replaced templates to a generated ones.

This commit is contained in:
Alexander Andreev 2022-06-13 21:07:33 +04:00
parent dc9b66cae4
commit 2cdd27d249
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

View File

@ -51,8 +51,6 @@ type UploadHandlers struct {
} }
func NewUploadHandlers(conf *configuration.Configuration, lErr, lUp, lDown *logging.Logger, uploadDirSize *int64) *UploadHandlers { func NewUploadHandlers(conf *configuration.Configuration, lErr, lUp, lDown *logging.Logger, uploadDirSize *int64) *UploadHandlers {
web.CompileTemplates(lErr)
return &UploadHandlers{ return &UploadHandlers{
conf: conf, conf: conf,
logErr: lErr, logErr: lErr,
@ -74,20 +72,7 @@ func (h *UploadHandlers) Index(w http.ResponseWriter, r *http.Request) {
_, _, availStr := utils.ConvertFileSize(storCapacity - *h.uploadDirSize) _, _, availStr := utils.ConvertFileSize(storCapacity - *h.uploadDirSize)
_, _, fMaxSzStr := utils.ConvertFileSize(fMaxSize) _, _, fMaxSzStr := utils.ConvertFileSize(fMaxSize)
if err := web.Template("index").Execute(w, &IndexData{ web.Index(utils.MainSite(r.Host), storCapacity, *h.uploadDirSize, h.conf.Uploads.Limits.KeepForHours, fMaxSzStr, usedStr, capStr, availStr, w)
MainSite: utils.MainSite(r.Host),
FileMaxSz: fMaxSzStr,
StorageCapacity: storCapacity,
StorageCapStr: capStr,
StorageAvailable: storCapacity - *h.uploadDirSize,
StorageAvailStr: availStr,
StorageUsed: *h.uploadDirSize,
StorageUsedStr: usedStr,
KeepForHours: h.conf.Uploads.Limits.KeepForHours,
}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
h.logErr.Fatalln("failed to execute Index template:", err)
}
} }
func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) { func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
@ -119,12 +104,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
if leftSpace < fHandler.Size { if leftSpace < fHandler.Size {
h.logErr.Println("not enough space left in storage, only", leftSpace>>20, "MiB left") h.logErr.Println("not enough space left in storage, only", leftSpace>>20, "MiB left")
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
if err := web.Template("nospace").Execute(w, &NotFoundData{ web.ErrorNoSpace(utils.MainSite(r.Host), w)
MainSite: utils.MainSite(r.Host),
}); err != nil {
h.logErr.Fatalln("failed to execute NoSpace template:", err)
http.Error(w, "cannot execute a NoSpace template. But error was that there's no space for uploads left", http.StatusInternalServerError)
}
} }
s256 := sha256.New() s256 := sha256.New()
@ -187,14 +167,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
return return
} }
if err := web.Template("uploaded").Execute(w, &UploadedData{ web.Uploaded(utils.MainSite(r.Host), downloadURLParsed.String(), h.conf.Uploads.Limits.KeepForHours, w)
MainSite: utils.MainSite(r.Host),
DownloadURL: downloadURLParsed.String(),
KeepForHours: h.conf.Uploads.Limits.KeepForHours,
}); err != nil {
h.logErr.Fatalln("failed to execute Uploaded template:", err)
http.Error(w, "cannot execute Uploaded template, but here is your download link: "+downloadURLParsed.String(), http.StatusInternalServerError)
}
} }
func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) { func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) {
@ -229,10 +202,5 @@ func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) {
func (h *UploadHandlers) NotFound(w http.ResponseWriter, r *http.Request) { func (h *UploadHandlers) NotFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
if err := web.Template("404").Execute(w, NotFoundData{ web.Error404(utils.MainSite(r.Host), w)
MainSite: utils.MainSite(r.Host),
}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
h.logErr.Fatalln("failed to execute 404 template:", err)
}
} }