2020-06-07 21:30:18 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
2021-01-17 21:29:08 +00:00
|
|
|
"context"
|
2020-06-07 21:30:18 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/dataprovider"
|
2021-10-10 11:08:05 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/vfs"
|
2020-06-07 21:30:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getFolders(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-02-01 18:04:15 +00:00
|
|
|
limit, offset, order, err := getSearchFilters(w, r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
folders, err := dataprovider.GetFolders(limit, offset, order, false)
|
|
|
|
if err != nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
|
2022-04-25 13:49:11 +00:00
|
|
|
return
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
render.JSON(w, r, folders)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func addFolder(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
var folder vfs.BaseVirtualFolder
|
|
|
|
err := render.DecodeJSON(r.Body, &folder)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-01-05 08:50:22 +00:00
|
|
|
err = dataprovider.AddFolder(&folder)
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
2020-06-07 21:30:18 +00:00
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
2021-01-17 21:29:08 +00:00
|
|
|
return
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
renderFolder(w, r, folder.Name, http.StatusCreated)
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func updateFolder(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-10-10 11:08:05 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
|
|
|
|
name := getURLParam(r, "name")
|
|
|
|
folder, err := dataprovider.GetFolderByName(name)
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
users := folder.Users
|
2022-04-25 13:49:11 +00:00
|
|
|
groups := folder.Groups
|
2021-02-01 18:04:15 +00:00
|
|
|
folderID := folder.ID
|
2022-01-31 17:01:37 +00:00
|
|
|
name = folder.Name
|
2021-03-21 18:15:47 +00:00
|
|
|
currentS3AccessSecret := folder.FsConfig.S3Config.AccessSecret
|
|
|
|
currentAzAccountKey := folder.FsConfig.AzBlobConfig.AccountKey
|
2021-06-11 20:27:36 +00:00
|
|
|
currentAzSASUrl := folder.FsConfig.AzBlobConfig.SASURL
|
2021-03-21 18:15:47 +00:00
|
|
|
currentGCSCredentials := folder.FsConfig.GCSConfig.Credentials
|
|
|
|
currentCryptoPassphrase := folder.FsConfig.CryptConfig.Passphrase
|
|
|
|
currentSFTPPassword := folder.FsConfig.SFTPConfig.Password
|
|
|
|
currentSFTPKey := folder.FsConfig.SFTPConfig.PrivateKey
|
|
|
|
|
|
|
|
folder.FsConfig.S3Config = vfs.S3FsConfig{}
|
|
|
|
folder.FsConfig.AzBlobConfig = vfs.AzBlobFsConfig{}
|
|
|
|
folder.FsConfig.GCSConfig = vfs.GCSFsConfig{}
|
|
|
|
folder.FsConfig.CryptConfig = vfs.CryptFsConfig{}
|
|
|
|
folder.FsConfig.SFTPConfig = vfs.SFTPFsConfig{}
|
2021-02-01 18:04:15 +00:00
|
|
|
err = render.DecodeJSON(r.Body, &folder)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
folder.ID = folderID
|
|
|
|
folder.Name = name
|
2021-03-21 18:15:47 +00:00
|
|
|
folder.FsConfig.SetEmptySecretsIfNil()
|
2021-06-11 20:27:36 +00:00
|
|
|
updateEncryptedSecrets(&folder.FsConfig, currentS3AccessSecret, currentAzAccountKey, currentAzSASUrl, currentGCSCredentials,
|
2021-03-21 18:15:47 +00:00
|
|
|
currentCryptoPassphrase, currentSFTPPassword, currentSFTPKey)
|
2022-04-25 13:49:11 +00:00
|
|
|
err = dataprovider.UpdateFolder(&folder, users, groups, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-02-01 18:04:15 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendAPIResponse(w, r, nil, "Folder updated", http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func renderFolder(w http.ResponseWriter, r *http.Request, name string, status int) {
|
|
|
|
folder, err := dataprovider.GetFolderByName(name)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
2020-06-07 21:30:18 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-22 18:03:25 +00:00
|
|
|
folder.PrepareForRendering()
|
2021-02-01 18:04:15 +00:00
|
|
|
if status != http.StatusOK {
|
|
|
|
ctx := context.WithValue(r.Context(), render.StatusCtxKey, status)
|
|
|
|
render.JSON(w, r.WithContext(ctx), folder)
|
|
|
|
} else {
|
|
|
|
render.JSON(w, r, folder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFolderByName(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-02-01 18:04:15 +00:00
|
|
|
name := getURLParam(r, "name")
|
|
|
|
renderFolder(w, r, name, http.StatusOK)
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func deleteFolder(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-10-10 11:08:05 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
name := getURLParam(r, "name")
|
2021-10-10 11:08:05 +00:00
|
|
|
err = dataprovider.DeleteFolder(name, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2020-06-20 10:38:04 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
2020-06-07 21:30:18 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
sendAPIResponse(w, r, err, "Folder deleted", http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|