robotfs/api_file.go
2025-05-19 01:21:29 +08:00

102 lines
2.0 KiB
Go

package main
import (
"gosvc/httpserver"
"gosvc/logger"
"gosvc/validator"
"robotfs/utils"
)
type DeleteParams struct {
Path string
IsDir bool
}
func (d *DeleteParams) Validate() error {
return validator.WithIf(
d.Path == "", "Path is empty",
).Validate()
}
type GeneralParams struct {
SrcPath string
DstPath string
IsDir bool
}
func (g *GeneralParams) Validate() error {
return validator.ChainValidate(
validator.WithIf(
g.SrcPath == "", "SrcPath is empty",
),
validator.WithIf(
g.DstPath == "", "DstPath is empty",
),
)
}
type CopyParams struct {
GeneralParams
}
type RenameParams struct {
GeneralParams
}
func (s *Service) HandleUploadFile(
req *httpserver.Request,
resp *httpserver.Response,
) *httpserver.Response {
path := req.QueryString("path")
newPath := utils.NormalizePath(path)
file, fileHeader, err := req.FormFile("file")
if err != nil {
return resp.InternalServerError("get file failed, " + err.Error())
}
defer file.Close()
contentType := fileHeader.Header.Get("Content-Type")
if err := s.Engine.FileSystemManager.CreateFile(req.Context(), utils.FullPath(newPath), file, contentType); err != nil {
logger.Error("create %s: %v", path, err)
return resp.InternalServerError("create file failed, " + err.Error())
}
return resp.NoContent()
}
func (s *Service) HandleInfoFile(
req *httpserver.Request,
resp *httpserver.Response,
) *httpserver.Response {
return resp.NoContent()
}
func (s *Service) HandleDownloadFile(
req *httpserver.Request,
resp *httpserver.Response,
) *httpserver.Response {
return resp.NoContent()
}
func (s *Service) HandleDeleteFile(
req *httpserver.Request,
resp *httpserver.Response,
) *httpserver.Response {
return resp.NoContent()
}
func (s *Service) HandleRenameFile(
req *httpserver.Request,
resp *httpserver.Response,
) *httpserver.Response {
return resp.NoContent()
}
func (s *Service) HandleCopyFile(
req *httpserver.Request,
resp *httpserver.Response,
) *httpserver.Response {
return resp.NoContent()
}