113 lines
2.5 KiB
Go
113 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gosvc/httpserver"
|
|
"gosvc/logger"
|
|
"gosvc/validator"
|
|
"robotfs/utils"
|
|
)
|
|
|
|
type Entry struct {
|
|
Name string `json:"name"`
|
|
IsDir bool `json:"is_dir"`
|
|
}
|
|
|
|
type ListResult struct {
|
|
Entries []Entry `json:"entries"`
|
|
MoreAvailable bool `json:"more_available"`
|
|
IsEmptyFolder bool `json:"is_empty_folder"`
|
|
LastFileName string `json:"last_file_name"`
|
|
}
|
|
|
|
type MkdirParams struct {
|
|
Path string
|
|
}
|
|
|
|
func (m *MkdirParams) Validate() error {
|
|
return validator.WithIf(
|
|
m.Path == "", "Path is empty",
|
|
).Validate()
|
|
}
|
|
|
|
func (s *Service) HandleMkdir(
|
|
req *httpserver.Request,
|
|
resp *httpserver.Response,
|
|
) *httpserver.Response {
|
|
params := req.Binded.(*MkdirParams)
|
|
path := params.Path
|
|
newPath := utils.NormalizePath(path)
|
|
|
|
err := s.FileSystemManager.MakeDirectory(req.Context(), utils.FullPath(newPath))
|
|
if err != nil {
|
|
logger.Error("makeDirectory %s: %v", path, err)
|
|
return resp.InternalServerError("mkdir failed, " + err.Error())
|
|
}
|
|
|
|
return resp.NoContent()
|
|
}
|
|
|
|
func (s *Service) HandleListDirectory(
|
|
req *httpserver.Request,
|
|
resp *httpserver.Response,
|
|
) *httpserver.Response {
|
|
path := req.QueryString("path")
|
|
startFileName := req.QueryString("startFileName")
|
|
inclusive := req.QueryBool("inclusive")
|
|
limit := req.QueryInt("limit")
|
|
newPath := utils.NormalizePath(path)
|
|
|
|
rawEntries, moreAvailable, err := s.FileSystemManager.ListDirectoryEntries(
|
|
context.Background(),
|
|
utils.FullPath(newPath),
|
|
startFileName,
|
|
inclusive,
|
|
limit,
|
|
)
|
|
if err != nil {
|
|
logger.Error("listDirectory %s %s %d: %v", path, startFileName, limit, err)
|
|
return resp.InternalServerError("listDirectory failed, " + err.Error())
|
|
}
|
|
|
|
result := ListResult{
|
|
MoreAvailable: moreAvailable,
|
|
IsEmptyFolder: len(rawEntries) == 0,
|
|
}
|
|
|
|
if len(rawEntries) > 0 {
|
|
entries := make([]Entry, len(rawEntries))
|
|
for i, e := range rawEntries {
|
|
entries[i] = Entry{
|
|
Name: e.FullPath.Name(),
|
|
IsDir: e.IsDir,
|
|
}
|
|
}
|
|
result.Entries = entries
|
|
result.LastFileName = entries[len(entries)-1].Name
|
|
}
|
|
|
|
return resp.OK(result).JSON()
|
|
}
|
|
|
|
func (s *Service) HandleDeleteDirectory(
|
|
req *httpserver.Request,
|
|
resp *httpserver.Response,
|
|
) *httpserver.Response {
|
|
return resp.NoContent()
|
|
}
|
|
|
|
func (s *Service) HandleRenameDirectory(
|
|
req *httpserver.Request,
|
|
resp *httpserver.Response,
|
|
) *httpserver.Response {
|
|
return resp.NoContent()
|
|
}
|
|
|
|
func (s *Service) HandleCopyDirectory(
|
|
req *httpserver.Request,
|
|
resp *httpserver.Response,
|
|
) *httpserver.Response {
|
|
return resp.NoContent()
|
|
}
|