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
logUpload *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",
amber.DefaultDirOptions, defaultAmberOptions)
return &UploadHandlers{
conf: conf,
logErr: lErr,
logUpload: lUp,
logDownload: lDown}
conf: conf,
logErr: lErr,
logUpload: lUp,
logDownload: lDown,
uploadDirSize: uploadDirSize}
}
func (h *UploadHandlers) Index(w http.ResponseWriter, r *http.Request) {
var storCapacity int64 = h.conf.Uploads.Limits.Storage << 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)
_, _, usedStr := utils.ConvertFileSize(storSize)
_, _, availStr := utils.ConvertFileSize(storCapacity - storSize)
_, _, usedStr := utils.ConvertFileSize(*h.uploadDirSize)
_, _, availStr := utils.ConvertFileSize(storCapacity - *h.uploadDirSize)
_, _, fMaxSzStr := utils.ConvertFileSize(fMaxSize)
if err := compiledTemplates["index"].Execute(w, &IndexData{
@ -82,9 +79,9 @@ func (h *UploadHandlers) Index(w http.ResponseWriter, r *http.Request) {
FileMaxSz: fMaxSzStr,
StorageCapacity: storCapacity,
StorageCapStr: capStr,
StorageAvailable: storCapacity - storSize,
StorageAvailable: storCapacity - *h.uploadDirSize,
StorageAvailStr: availStr,
StorageUsed: storSize,
StorageUsed: *h.uploadDirSize,
StorageUsedStr: usedStr,
}); err != nil {
w.WriteHeader(http.StatusInternalServerError)