1
0

Handle situation when there's not enough space left.

This commit is contained in:
Alexander Andreev 2022-02-09 01:48:19 +04:00
parent efd83144c4
commit c6aed4b67f
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F
2 changed files with 45 additions and 1 deletions

View File

@ -105,6 +105,7 @@ func (h *UploadHandlers) Index(w http.ResponseWriter, r *http.Request) {
func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
var fMaxSizeBytes int64 = h.conf.Uploads.Limits.FileSize << 20
var storCapacity int64 = h.conf.Uploads.Limits.Storage << 20
r.Body = http.MaxBytesReader(w, r.Body, fMaxSizeBytes)
@ -125,6 +126,19 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
f.Close()
}()
var leftSpace int64 = storCapacity - *h.uploadDirSize
if leftSpace < fHandler.Size {
h.logErr.Println("not enough space left in storage, only", leftSpace>>20, "MiB left")
w.WriteHeader(http.StatusInternalServerError)
if err := compiledTemplates["uploaded"].Execute(w, &NotFoundData{
MainSite: utils.MainSite(r.Host),
}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
h.logErr.Fatalln("failed to execute NoSpace template:", err)
}
}
s256 := sha256.New()
if _, err := io.Copy(s256, f); err != nil {
h.logErr.Println("failed to compute SHA-256 hash:", err)
@ -192,7 +206,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
KeepForHours: h.conf.Uploads.Limits.KeepForHours,
}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
h.logErr.Fatalln("failed to execute Index template:", err)
h.logErr.Fatalln("failed to execute Uploaded template:", err)
}
}
@ -276,4 +290,16 @@ func compileTemplates(lErr *logging.Logger) {
}
compiledTemplates["404"] = notfoundTpl
nospaceStr, err := jade.ParseFileFromFileSystem("nospace.jade", templatesFS)
if err != nil {
lErr.Fatalln(err)
}
nospaceTpl, err := template.New("nospace").Parse(nospaceStr)
if err != nil {
lErr.Fatalln(err)
}
compiledTemplates["nospace"] = nospaceTpl
}

View File

@ -0,0 +1,18 @@
extends base.jade
block head
style(type="text/css").
#error {
font-size: 3.5rem;
line-height: 5rem;
text-align: center;
margin: 6rem 0; }
#error h1 { font-size: 6rem; }
block header
h1 :(
block body
section#error
h1 Not enough space left