2019-07-20 10:26:52 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
"github.com/drakkan/sftpgo/sftpd"
|
|
|
|
"github.com/drakkan/sftpgo/utils"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getQuotaScans(w http.ResponseWriter, r *http.Request) {
|
|
|
|
render.JSON(w, r, sftpd.GetQuotaScans())
|
|
|
|
}
|
|
|
|
|
|
|
|
func startQuotaScan(w http.ResponseWriter, r *http.Request) {
|
|
|
|
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) {
|
|
|
|
sendAPIResponse(w, r, err, "Scan started", http.StatusCreated)
|
|
|
|
go func() {
|
2019-07-27 07:38:09 +00:00
|
|
|
numFiles, size, _, err := utils.ScanDirContents(user.HomeDir)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-09-05 14:21:35 +00:00
|
|
|
logger.Warn(logSender, "", "error scanning user home dir %v: %v", user.HomeDir, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
} else {
|
2019-07-28 20:04:50 +00:00
|
|
|
err := dataprovider.UpdateUserQuota(dataProvider, user, numFiles, size, true)
|
2019-09-05 14:21:35 +00:00
|
|
|
logger.Debug(logSender, "", "user dir scanned, user: %v, dir: %v, error: %v", user.Username, user.HomeDir, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
sftpd.RemoveQuotaScan(user.Username)
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "Another scan is already in progress", http.StatusConflict)
|
|
|
|
}
|
|
|
|
}
|