mirror of
https://github.com/drakkan/sftpgo.git
synced 2024-11-22 07:30:25 +00:00
f02e24437a
test cases migration to testify is now complete. Linters are enabled for test cases too
53 lines
1.6 KiB
Go
53 lines
1.6 KiB
Go
package httpd
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
"github.com/drakkan/sftpgo/logger"
|
|
"github.com/drakkan/sftpgo/sftpd"
|
|
)
|
|
|
|
func getQuotaScans(w http.ResponseWriter, r *http.Request) {
|
|
render.JSON(w, r, sftpd.GetQuotaScans())
|
|
}
|
|
|
|
func startQuotaScan(w http.ResponseWriter, r *http.Request) {
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
var u dataprovider.User
|
|
err := render.DecodeJSON(r.Body, &u)
|
|
if err != nil {
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
return
|
|
}
|
|
user, err := dataprovider.UserExists(dataProvider, u.Username)
|
|
if err != nil {
|
|
sendAPIResponse(w, r, err, "", http.StatusNotFound)
|
|
return
|
|
}
|
|
if sftpd.AddQuotaScan(user.Username) {
|
|
go doQuotaScan(user) //nolint:errcheck
|
|
sendAPIResponse(w, r, err, "Scan started", http.StatusCreated)
|
|
} else {
|
|
sendAPIResponse(w, r, err, "Another scan is already in progress", http.StatusConflict)
|
|
}
|
|
}
|
|
|
|
func doQuotaScan(user dataprovider.User) error {
|
|
defer sftpd.RemoveQuotaScan(user.Username) //nolint:errcheck
|
|
fs, err := user.GetFilesystem("")
|
|
if err != nil {
|
|
logger.Warn(logSender, "", "unable scan quota for user %#v error creating filesystem: %v", user.Username, err)
|
|
return err
|
|
}
|
|
numFiles, size, err := fs.ScanRootDirContents()
|
|
if err != nil {
|
|
logger.Warn(logSender, "", "error scanning user home dir %#v: %v", user.Username, err)
|
|
} else {
|
|
err = dataprovider.UpdateUserQuota(dataProvider, user, numFiles, size, true)
|
|
logger.Debug(logSender, "", "user home dir scanned, user: %#v, error: %v", user.Username, err)
|
|
}
|
|
return err
|
|
}
|