2024-01-01 10:31:45 +00:00
|
|
|
// Copyright (C) 2019 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
2021-01-17 21:29:08 +00:00
|
|
|
"context"
|
2022-12-23 12:08:04 +00:00
|
|
|
"fmt"
|
2020-06-07 21:30:18 +00:00
|
|
|
"net/http"
|
2023-02-09 08:33:33 +00:00
|
|
|
"net/url"
|
2020-06-07 21:30:18 +00:00
|
|
|
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/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)
|
2022-12-23 12:08:04 +00:00
|
|
|
|
2022-07-24 06:10:23 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
var folder vfs.BaseVirtualFolder
|
2022-07-24 06:10:23 +00:00
|
|
|
err = render.DecodeJSON(r.Body, &folder)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2022-12-23 12:08:04 +00:00
|
|
|
if err := dataprovider.AddFolder(&folder, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role); 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
|
|
|
}
|
2023-02-09 08:33:33 +00:00
|
|
|
w.Header().Add("Location", fmt.Sprintf("%s/%s", folderPath, url.PathEscape(folder.Name)))
|
2023-04-18 16:11:23 +00:00
|
|
|
renderFolder(w, r, folder.Name, &claims, 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
|
|
|
|
2022-12-23 08:36:20 +00:00
|
|
|
var updatedFolder vfs.BaseVirtualFolder
|
|
|
|
err = render.DecodeJSON(r.Body, &updatedFolder)
|
2021-02-01 18:04:15 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2022-12-23 08:36:20 +00:00
|
|
|
updatedFolder.ID = folder.ID
|
|
|
|
updatedFolder.Name = folder.Name
|
|
|
|
updatedFolder.FsConfig.SetEmptySecretsIfNil()
|
|
|
|
updateEncryptedSecrets(&updatedFolder.FsConfig, folder.FsConfig.S3Config.AccessSecret, folder.FsConfig.AzBlobConfig.AccountKey,
|
|
|
|
folder.FsConfig.AzBlobConfig.SASURL, folder.FsConfig.GCSConfig.Credentials, folder.FsConfig.CryptConfig.Passphrase,
|
|
|
|
folder.FsConfig.SFTPConfig.Password, folder.FsConfig.SFTPConfig.PrivateKey, folder.FsConfig.SFTPConfig.KeyPassphrase,
|
|
|
|
folder.FsConfig.HTTPConfig.Password, folder.FsConfig.HTTPConfig.APIKey)
|
|
|
|
|
|
|
|
err = dataprovider.UpdateFolder(&updatedFolder, folder.Users, folder.Groups, claims.Username,
|
|
|
|
util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role)
|
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
|
|
|
}
|
|
|
|
|
2023-04-18 16:11:23 +00:00
|
|
|
func renderFolder(w http.ResponseWriter, r *http.Request, name string, claims *jwtTokenClaims, status int) {
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, err := dataprovider.GetFolderByName(name)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
2020-06-07 21:30:18 +00:00
|
|
|
return
|
|
|
|
}
|
2023-04-18 16:11:23 +00:00
|
|
|
if hideConfidentialData(claims, r) {
|
|
|
|
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)
|
2023-04-18 16:11:23 +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")
|
2023-04-18 16:11:23 +00:00
|
|
|
renderFolder(w, r, name, &claims, http.StatusOK)
|
2021-02-01 18:04:15 +00:00
|
|
|
}
|
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")
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.DeleteFolder(name, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role)
|
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
|
|
|
}
|