Skip to content

Commit

Permalink
feat: 文件管理优化
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 20, 2024
1 parent 480a6a8 commit a2ebc07
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 50 deletions.
5 changes: 5 additions & 0 deletions internal/http/request/file.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package request

type FileList struct {
Path string `json:"path" form:"path" validate:"required"`
Sort string `json:"sort" form:"sort"`
}

type FilePath struct {
Path string `json:"path" form:"path" validate:"required"`
}
Expand Down
37 changes: 30 additions & 7 deletions internal/service/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
stdos "os"
"path/filepath"
"slices"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -132,7 +133,7 @@ func (s *FileService) Upload(w http.ResponseWriter, r *http.Request) {
return
}
if io.Exists(path) {
Error(w, http.StatusForbidden, "目标路径%s已存在", path)
Error(w, http.StatusForbidden, "目标路径 %s 已存在", path)
return
}

Expand Down Expand Up @@ -168,7 +169,8 @@ func (s *FileService) Move(w http.ResponseWriter, r *http.Request) {
}

if io.Exists(req.Target) && !req.Force {
Error(w, http.StatusForbidden, "目标路径%s已存在", req.Target)
Error(w, http.StatusForbidden, "目标路径 %s 已存在", req.Target)
return
}

if err = io.Mv(req.Source, req.Target); err != nil {
Expand All @@ -187,7 +189,8 @@ func (s *FileService) Copy(w http.ResponseWriter, r *http.Request) {
}

if io.Exists(req.Target) && !req.Force {
Error(w, http.StatusForbidden, "目标路径%s已存在", req.Target)
Error(w, http.StatusForbidden, "目标路径 %s 已存在", req.Target)
return
}

if err = io.Cp(req.Source, req.Target); err != nil {
Expand Down Expand Up @@ -343,21 +346,41 @@ func (s *FileService) Search(w http.ResponseWriter, r *http.Request) {
}

func (s *FileService) List(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.FilePath](r)
req, err := Bind[request.FileList](r)
if err != nil {
Error(w, http.StatusInternalServerError, "%v", err)
return
}

fileInfoList, err := io.ReadDir(req.Path)
list, err := io.ReadDir(req.Path)
if err != nil {
Error(w, http.StatusInternalServerError, "%v", err)
return
}

if req.Sort == "asc" {
slices.SortFunc(list, func(a, b stdos.DirEntry) int {
return strings.Compare(strings.ToLower(b.Name()), strings.ToLower(a.Name()))
})
} else if req.Sort == "desc" {
slices.SortFunc(list, func(a, b stdos.DirEntry) int {
return strings.Compare(strings.ToLower(a.Name()), strings.ToLower(b.Name()))
})
} else {
slices.SortFunc(list, func(a, b stdos.DirEntry) int {
if a.IsDir() && !b.IsDir() {
return -1
}
if !a.IsDir() && b.IsDir() {
return 1
}
return strings.Compare(strings.ToLower(a.Name()), strings.ToLower(b.Name()))
})
}

var paths []any
for _, fileInfo := range fileInfoList {
info, _ := fileInfo.Info()
for _, file := range list {
info, _ := file.Info()
stat := info.Sys().(*syscall.Stat_t)

paths = append(paths, map[string]any{
Expand Down
37 changes: 30 additions & 7 deletions internal/service/file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
stdos "os"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -132,7 +133,7 @@ func (s *FileService) Upload(w http.ResponseWriter, r *http.Request) {
return
}
if io.Exists(path) {
Error(w, http.StatusForbidden, "目标路径%s已存在", path)
Error(w, http.StatusForbidden, "目标路径 %s 已存在", path)
return
}

Expand Down Expand Up @@ -168,7 +169,8 @@ func (s *FileService) Move(w http.ResponseWriter, r *http.Request) {
}

if io.Exists(req.Target) && !req.Force {
Error(w, http.StatusForbidden, "目标路径"+req.Target+"已存在")
Error(w, http.StatusForbidden, "目标路径 %s 已存在", req.Target)
return
}

if err = io.Mv(req.Source, req.Target); err != nil {
Expand All @@ -187,7 +189,8 @@ func (s *FileService) Copy(w http.ResponseWriter, r *http.Request) {
}

if io.Exists(req.Target) && !req.Force {
Error(w, http.StatusForbidden, "目标路径"+req.Target+"已存在")
Error(w, http.StatusForbidden, "目标路径 %s 已存在", req.Target)
return
}

if err = io.Cp(req.Source, req.Target); err != nil {
Expand Down Expand Up @@ -330,21 +333,41 @@ func (s *FileService) Search(w http.ResponseWriter, r *http.Request) {
}

func (s *FileService) List(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.FilePath](r)
req, err := Bind[request.FileList](r)
if err != nil {
Error(w, http.StatusInternalServerError, "%v", err)
return
}

fileInfoList, err := io.ReadDir(req.Path)
list, err := io.ReadDir(req.Path)
if err != nil {
Error(w, http.StatusInternalServerError, "%v", err)
return
}

if req.Sort == "asc" {
slices.SortFunc(list, func(a, b stdos.DirEntry) int {
return strings.Compare(strings.ToLower(b.Name()), strings.ToLower(a.Name()))
})
} else if req.Sort == "desc" {
slices.SortFunc(list, func(a, b stdos.DirEntry) int {
return strings.Compare(strings.ToLower(a.Name()), strings.ToLower(b.Name()))
})
} else {
slices.SortFunc(list, func(a, b stdos.DirEntry) int {
if a.IsDir() && !b.IsDir() {
return -1
}
if !a.IsDir() && b.IsDir() {
return 1
}
return strings.Compare(strings.ToLower(a.Name()), strings.ToLower(b.Name()))
})
}

var paths []any
for _, fileInfo := range fileInfoList {
info, _ := fileInfo.Info()
for _, file := range list {
info, _ := file.Info()

paths = append(paths, map[string]any{
"name": info.Name(),
Expand Down
4 changes: 2 additions & 2 deletions web/src/api/panel/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ export default {
search: (keyword: string): Promise<AxiosResponse<any>> =>
request.post('/file/search', { keyword }),
// 获取文件列表
list: (path: string, page: number, limit: number): Promise<AxiosResponse<any>> =>
request.get('/file/list', { params: { path, page, limit } })
list: (path: string, page: number, limit: number, sort: string): Promise<AxiosResponse<any>> =>
request.get('/file/list', { params: { path, page, limit, sort } })
}
Loading

0 comments on commit a2ebc07

Please sign in to comment.