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") 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 { return resp.NoContent() } func (s *Service) HandleDownloadFile( req *httpserver.Request, resp *httpserver.Response, ) *httpserver.Response { path := req.QueryString("path") newPath := utils.NormalizePath(path) _, _, 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()) } return resp.NoContent() } 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("rename %s to %s: %v", srcPath, dstPath, err) return resp.InternalServerError("rename file failed, " + err.Error()) } return resp.NoContent() } func (s *Service) HandleCopyFile( req *httpserver.Request, resp *httpserver.Response, ) *httpserver.Response { return resp.NoContent() }