robotfs/api_file.go
2025-05-28 16:41:55 +08:00

164 lines
4.1 KiB
Go

package main
import (
"fmt"
"time"
"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")
fileSize := fileHeader.Size
if err := s.Engine.FileSystemManager.CreateFile(req.Context(), utils.FullPath(newPath), file, contentType, fileSize); 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 {
path := req.QueryString("path")
newPath := utils.NormalizePath(path)
entry, err := s.FileSystemManager.FindEntry(req.Context(), utils.FullPath(newPath))
if err != nil {
logger.Error("info %s: %v", path, err)
return resp.InternalServerError("info file failed, " + err.Error())
}
return resp.OK(entry).JSON()
}
func (s *Service) HandleDownloadFile(
req *httpserver.Request,
resp *httpserver.Response,
) *httpserver.Response {
path := req.QueryString("path")
preview := req.QueryBool("preview")
newPath := utils.NormalizePath(path)
downloader, entry, err := s.FileSystemManager.DownloadFile(req.Context(), utils.FullPath(newPath))
if err != nil {
logger.Error("download %s: %v", path, err)
return resp.InternalServerError("download file failed, " + err.Error())
}
defer downloader.Close()
if !preview {
resp.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", entry.FullPath.Name()))
}
return resp.ServeContent(entry.FullPath.Name(), time.Unix(entry.LastModificationTime, 0), downloader)
}
func (s *Service) HandleDeleteFile(
req *httpserver.Request,
resp *httpserver.Response,
) *httpserver.Response {
params := req.Binded.(*DeleteParams)
path := params.Path
isDir := params.IsDir
newPath := utils.NormalizePath(path)
err := s.FileSystemManager.DeleteFile(req.Context(), utils.FullPath(newPath), isDir)
if err != nil {
logger.Error("delete file %s: %v", path, err)
return resp.InternalServerError("delete file failed, " + err.Error())
}
return resp.NoContent()
}
func (s *Service) HandleRenameFile(
req *httpserver.Request,
resp *httpserver.Response,
) *httpserver.Response {
params := req.Binded.(*RenameParams)
isDir := params.IsDir
srcPath := utils.FullPath(utils.NormalizePath(params.SrcPath))
dstPath := utils.FullPath(utils.NormalizePath(params.DstPath))
err := s.FileSystemManager.RenameFile(req.Context(), srcPath, dstPath, isDir)
if err != nil {
logger.Error("move file %s => %s: %v", srcPath, dstPath, err)
return resp.InternalServerError("move file failed, " + err.Error())
}
return resp.NoContent()
}
func (s *Service) HandleCopyFile(
req *httpserver.Request,
resp *httpserver.Response,
) *httpserver.Response {
params := req.Binded.(*CopyParams)
isDir := params.IsDir
srcPath := utils.FullPath(utils.NormalizePath(params.SrcPath))
dstPath := utils.FullPath(utils.NormalizePath(params.DstPath))
err := s.FileSystemManager.CopyFile(req.Context(), srcPath, dstPath, isDir)
if err != nil {
logger.Error("copy file %s => %s: %v", srcPath, dstPath, err)
return resp.InternalServerError("copy file failed, " + err.Error())
}
return resp.NoContent()
}