2019-10-07 16:19:01 +00:00
|
|
|
package httpd
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/go-chi/render"
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
"github.com/drakkan/sftpgo/sftpd"
|
2020-06-07 21:30:18 +00:00
|
|
|
"github.com/drakkan/sftpgo/vfs"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getQuotaScans(w http.ResponseWriter, r *http.Request) {
|
|
|
|
render.JSON(w, r, sftpd.GetQuotaScans())
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func getVFolderQuotaScans(w http.ResponseWriter, r *http.Request) {
|
|
|
|
render.JSON(w, r, sftpd.GetVFoldersQuotaScans())
|
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
func startQuotaScan(w http.ResponseWriter, r *http.Request) {
|
2020-01-31 18:04:00 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2019-07-20 10:26:52 +00:00
|
|
|
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
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
if dataprovider.GetQuotaTracking() == 0 {
|
|
|
|
sendAPIResponse(w, r, nil, "Quota tracking is disabled!", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
if sftpd.AddQuotaScan(user.Username) {
|
2020-04-30 12:23:55 +00:00
|
|
|
go doQuotaScan(user) //nolint:errcheck
|
2019-07-20 10:26:52 +00:00
|
|
|
sendAPIResponse(w, r, err, "Scan started", http.StatusCreated)
|
2019-12-27 22:12:44 +00:00
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "Another scan is already in progress", http.StatusConflict)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func startVFolderQuotaScan(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
var f vfs.BaseVirtualFolder
|
|
|
|
err := render.DecodeJSON(r.Body, &f)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
folder, err := dataprovider.GetFolderByPath(dataProvider, f.MappedPath)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if dataprovider.GetQuotaTracking() == 0 {
|
|
|
|
sendAPIResponse(w, r, nil, "Quota tracking is disabled!", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if sftpd.AddVFolderQuotaScan(folder.MappedPath) {
|
|
|
|
go doFolderQuotaScan(folder) //nolint:errcheck
|
|
|
|
sendAPIResponse(w, r, err, "Scan started", http.StatusCreated)
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "Another scan is already in progress", http.StatusConflict)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
func doQuotaScan(user dataprovider.User) error {
|
2020-04-30 12:23:55 +00:00
|
|
|
defer sftpd.RemoveQuotaScan(user.Username) //nolint:errcheck
|
2020-01-19 06:41:05 +00:00
|
|
|
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
|
|
|
|
}
|
2020-01-19 12:58:55 +00:00
|
|
|
numFiles, size, err := fs.ScanRootDirContents()
|
2020-01-19 06:41:05 +00:00
|
|
|
if err != nil {
|
2020-01-22 18:42:23 +00:00
|
|
|
logger.Warn(logSender, "", "error scanning user home dir %#v: %v", user.Username, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = dataprovider.UpdateUserQuota(dataProvider, user, numFiles, size, true)
|
|
|
|
logger.Debug(logSender, "", "user home dir scanned, user: %#v, error: %v", user.Username, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func doFolderQuotaScan(folder vfs.BaseVirtualFolder) error {
|
|
|
|
defer sftpd.RemoveVFolderQuotaScan(folder.MappedPath) //nolint:errcheck
|
|
|
|
fs := vfs.NewOsFs("", "", nil).(vfs.OsFs)
|
|
|
|
numFiles, size, err := fs.GetDirSize(folder.MappedPath)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "error scanning folder %#v: %v", folder.MappedPath, err)
|
|
|
|
return err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
err = dataprovider.UpdateVirtualFolderQuota(dataProvider, folder, numFiles, size, true)
|
|
|
|
logger.Debug(logSender, "", "virtual folder %#v scanned, error: %v", folder.MappedPath, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
return err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|