1
0

Now instead of computing dir size on every request use a variable that is updated using inotify.

This commit is contained in:
Alexander Andreev 2022-02-08 00:25:50 +04:00
parent f7390b0763
commit 9a4461ac4e
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F

View File

@ -49,32 +49,29 @@ type UploadHandlers struct {
logErr *logging.Logger logErr *logging.Logger
logUpload *logging.Logger logUpload *logging.Logger
logDownload *logging.Logger logDownload *logging.Logger
uploadDirSize *int64
} }
func NewUploadHandlers(conf *configuration.Configuration, lErr, lUp, lDown *logging.Logger) *UploadHandlers { func NewUploadHandlers(conf *configuration.Configuration, lErr, lUp, lDown *logging.Logger, uploadDirSize *int64) *UploadHandlers {
compiledTemplates = amber.MustCompileDir(conf.WebDir+"/templates", compiledTemplates = amber.MustCompileDir(conf.WebDir+"/templates",
amber.DefaultDirOptions, defaultAmberOptions) amber.DefaultDirOptions, defaultAmberOptions)
return &UploadHandlers{ return &UploadHandlers{
conf: conf, conf: conf,
logErr: lErr, logErr: lErr,
logUpload: lUp, logUpload: lUp,
logDownload: lDown} logDownload: lDown,
uploadDirSize: uploadDirSize}
} }
func (h *UploadHandlers) Index(w http.ResponseWriter, r *http.Request) { func (h *UploadHandlers) Index(w http.ResponseWriter, r *http.Request) {
var storCapacity int64 = h.conf.Uploads.Limits.Storage << 20 var storCapacity int64 = h.conf.Uploads.Limits.Storage << 20
var fMaxSize int64 = h.conf.Uploads.Limits.FileSize << 20 var fMaxSize int64 = h.conf.Uploads.Limits.FileSize << 20
var storSize int64 = 0
storSize, err := utils.DirectorySize(h.conf.Uploads.Directory)
if err != nil {
h.logErr.Printf("cannot compute storage size: %s", err)
}
_, _, capStr := utils.ConvertFileSize(storCapacity) _, _, capStr := utils.ConvertFileSize(storCapacity)
_, _, usedStr := utils.ConvertFileSize(storSize) _, _, usedStr := utils.ConvertFileSize(*h.uploadDirSize)
_, _, availStr := utils.ConvertFileSize(storCapacity - storSize) _, _, availStr := utils.ConvertFileSize(storCapacity - *h.uploadDirSize)
_, _, fMaxSzStr := utils.ConvertFileSize(fMaxSize) _, _, fMaxSzStr := utils.ConvertFileSize(fMaxSize)
if err := compiledTemplates["index"].Execute(w, &IndexData{ if err := compiledTemplates["index"].Execute(w, &IndexData{
@ -82,9 +79,9 @@ func (h *UploadHandlers) Index(w http.ResponseWriter, r *http.Request) {
FileMaxSz: fMaxSzStr, FileMaxSz: fMaxSzStr,
StorageCapacity: storCapacity, StorageCapacity: storCapacity,
StorageCapStr: capStr, StorageCapStr: capStr,
StorageAvailable: storCapacity - storSize, StorageAvailable: storCapacity - *h.uploadDirSize,
StorageAvailStr: availStr, StorageAvailStr: availStr,
StorageUsed: storSize, StorageUsed: *h.uploadDirSize,
StorageUsedStr: usedStr, StorageUsedStr: usedStr,
}); err != nil { }); err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)