From a1f79e11978fde9676fd67150b77fd305e1e019b Mon Sep 17 00:00:00 2001 From: Suacrbah <5744580+Suacrbah@user.noreply.gitee.com> Date: Sun, 7 Jan 2024 22:51:31 +0800 Subject: [PATCH] fix: fix formatting and add switch for showing folder size --- files/file.go | 24 ++++++++++-------------- frontend/src/api/files.js | 15 +++------------ frontend/src/components/prompts/Info.vue | 14 ++++++++++---- http/resource.go | 3 ++- 4 files changed, 25 insertions(+), 31 deletions(-) diff --git a/files/file.go b/files/file.go index bd7f6988..c0460ac8 100644 --- a/files/file.go +++ b/files/file.go @@ -55,6 +55,7 @@ type FileOptions struct { Modify bool Expand bool ReadHeader bool + FolderSize bool Token string Checker rules.Checker Content bool @@ -78,6 +79,14 @@ func NewFileInfo(opts FileOptions) (*FileInfo, error) { return nil, err } + if file.IsDir && opts.FolderSize { + size, err := getFolderSize(file.RealPath()) + if err != nil { + return nil, err + } + file.Size = size + } + if opts.Expand { if file.IsDir { if err := file.readListing(opts.Checker, opts.ReadHeader); err != nil { //nolint:govet @@ -95,20 +104,7 @@ func NewFileInfo(opts FileOptions) (*FileInfo, error) { return file, err } -func NewFolderInfo(opts FileOptions) (*FileInfo, error) { - file, err := NewFileInfo(opts) - if err != nil { - return nil, err - } - size, err := GetFolderSize(file.RealPath()) - if err != nil { - return nil, err - } - file.Size = size - return file, nil -} - -func GetFolderSize(path string) (int64, error) { +func getFolderSize(path string) (int64, error) { var size int64 err := filepath.WalkDir(path, func(_ string, d os.DirEntry, err error) error { if err != nil { diff --git a/frontend/src/api/files.js b/frontend/src/api/files.js index 46549696..72e0b31d 100644 --- a/frontend/src/api/files.js +++ b/frontend/src/api/files.js @@ -42,16 +42,6 @@ async function resourceAction(url, method, content) { return res; } -async function resourceSizeAction(url, method, content) { - url = removePrefix(url); - let opts = { method }; - if (content) { - opts.body = content; - } - const res = await fetchURL(`/api/resources/size${url}`, opts); - return res; -} - export async function remove(url) { return resourceAction(url, "DELETE"); } @@ -173,8 +163,9 @@ export async function checksum(url, algo) { return (await data.json()).checksums[algo]; } -export async function foldersize(url) { - const data = await resourceSizeAction(`${url}`, "GET"); +export async function folderSize(url) { + url = removePrefix(url); + const data = await fetchURL(`/api/resources/size${url}`, { method: "GET" }); return (await data.json()).size; } diff --git a/frontend/src/components/prompts/Info.vue b/frontend/src/components/prompts/Info.vue index 871a0c1a..968446e4 100644 --- a/frontend/src/components/prompts/Info.vue +++ b/frontend/src/components/prompts/Info.vue @@ -18,10 +18,10 @@ {{ humanSize }}

-

+

Size: {{ + >{{ $t("prompts.show") }} @@ -103,6 +103,12 @@ import { files as api } from "@/api"; export default { name: "info", + props: { + showFolderSize: { + type: Boolean, + default: true, + }, + }, computed: { ...mapState(["req", "selected"]), ...mapGetters(["selectedCount", "isListing"]), @@ -175,7 +181,7 @@ export default { this.$showError(e); } }, - foldersize: async function (event) { + folderSize: async function (event) { event.preventDefault(); try { @@ -186,7 +192,7 @@ export default { if (!item.isDir) { totalSize += item.size; } else { - totalSize += await api.foldersize(item.url) + totalSize += await api.folderSize(item.url) } } diff --git a/http/resource.go b/http/resource.go index 8107e262..91683725 100644 --- a/http/resource.go +++ b/http/resource.go @@ -20,11 +20,12 @@ import ( ) var resourceGetSizeHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) { - folder, err := files.NewFolderInfo(files.FileOptions{ + folder, err := files.NewFileInfo(files.FileOptions{ Fs: d.user.Fs, Path: r.URL.Path, Modify: d.user.Perm.Modify, Expand: true, + FolderSize: true, ReadHeader: d.server.TypeDetectionByHeader, Checker: d, Content: true,