package main import ( "fmt" "time" "gosvc/httpserver" "gosvc/logger" "gosvc/validator" "robotfs/entity" "robotfs/utils" ) type DeleteParams struct { Path string IsDir bool } func (d *DeleteParams) Validate() error { return validator.WithIf( d.Path == "", "Path is empty", ).Validate() } type CopyParams struct { GeneralParams } type RenameParams struct { GeneralParams } 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", ), ) } 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.FileSystem.CreateFile(req.Context(), utils.FullPath(newPath), file, contentType, fileSize); err != nil { logger.Error("create %s: %v", path, err) out := entity.NewError[any]( entity.CodeInternalServer, entity.ErrUploadFile, map[string]interface{}{ "detail": "upload file failed, " + err.Error(), }, ) return resp.OK(out).JSON() } return resp.OK(entity.NewSuccess("")).JSON() } func (s *Service) HandleInfoFile( req *httpserver.Request, resp *httpserver.Response, ) *httpserver.Response { path := req.QueryString("path") newPath := utils.NormalizePath(path) entry, err := s.FileSystem.FindEntry(req.Context(), utils.FullPath(newPath)) if err != nil { logger.Error("info %s: %v", path, err) out := entity.NewError[any]( entity.CodeInternalServer, entity.ErrInfoFile, map[string]interface{}{ "detail": "info file failed, " + err.Error(), }, ) return resp.OK(out).JSON() } return resp.OK(entity.NewSuccess(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.FileSystem.DownloadFile(req.Context(), utils.FullPath(newPath)) if err != nil { logger.Error("download %s: %v", path, err) out := entity.NewError[any]( entity.CodeInternalServer, entity.ErrDownloadFile, map[string]interface{}{ "detail": "download file failed, " + err.Error(), }, ) return resp.OK(out).JSON() } 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.FileSystem.DeleteFile(req.Context(), utils.FullPath(newPath), isDir) if err != nil { logger.Error("delete file %s: %v", path, err) out := entity.NewError[any]( entity.CodeInternalServer, entity.ErrDeleteFile, map[string]interface{}{ "detail": "delete file failed, " + err.Error(), }, ) return resp.OK(out).JSON() } return resp.OK(entity.NewSuccess("")).JSON() } 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.FileSystem.RenameFile(req.Context(), srcPath, dstPath, isDir) if err != nil { logger.Error("move file %s => %s: %v", srcPath, dstPath, err) out := entity.NewError[any]( entity.CodeInternalServer, entity.ErrRenameFile, map[string]interface{}{ "detail": "move file failed, " + err.Error(), }, ) return resp.OK(out).JSON() } return resp.OK(entity.NewSuccess("")).JSON() } 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.FileSystem.CopyFile(req.Context(), srcPath, dstPath, isDir) if err != nil { logger.Error("copy file %s => %s: %v", srcPath, dstPath, err) out := entity.NewError[any]( entity.CodeInternalServer, entity.ErrCopyFile, map[string]interface{}{ "detail": "copy file failed, " + err.Error(), }, ) return resp.OK(out).JSON() } return resp.OK(entity.NewSuccess("")).JSON() }