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"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/vfs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getFolders(w http.ResponseWriter, r *http.Request) {
|
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
|
|
|
|
|
|
|
folders, err := dataprovider.GetFolders(limit, offset, order)
|
2020-06-07 21:30:18 +00:00
|
|
|
if err == nil {
|
|
|
|
render.JSON(w, r, folders)
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
var err error
|
|
|
|
|
|
|
|
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-02-01 18:04:15 +00:00
|
|
|
folderID := folder.ID
|
|
|
|
err = render.DecodeJSON(r.Body, &folder)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
folder.ID = folderID
|
|
|
|
folder.Name = name
|
|
|
|
err = dataprovider.UpdateFolder(&folder)
|
|
|
|
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-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) {
|
|
|
|
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) {
|
|
|
|
name := getURLParam(r, "name")
|
|
|
|
err := dataprovider.DeleteFolder(name)
|
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
|
|
|
}
|