From 123f44d1c8fd1d5877d654bc47ea78b249ddadee Mon Sep 17 00:00:00 2001 From: Suacrbah <5744580+Suacrbah@user.noreply.gitee.com> Date: Sun, 31 Dec 2023 18:34:43 +0800 Subject: [PATCH] feat: add lazy loading folder size --- files/file.go | 31 ++++++++++++++++++++++++ frontend/src/api/files.js | 15 ++++++++++++ frontend/src/components/prompts/Info.vue | 31 +++++++++++++++++++++++- http/http.go | 1 + http/resource.go | 17 +++++++++++++ 5 files changed, 94 insertions(+), 1 deletion(-) diff --git a/files/file.go b/files/file.go index f181f185..bd7f6988 100644 --- a/files/file.go +++ b/files/file.go @@ -95,6 +95,37 @@ 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) { + var size int64 + err := filepath.WalkDir(path, func(_ string, d os.DirEntry, err error) error { + if err != nil { + return err + } + if !d.IsDir() { + info, err := d.Info() + if err != nil { + return err + } + size += info.Size() + } + return err + }) + return size, err +} + func stat(opts FileOptions) (*FileInfo, error) { var file *FileInfo diff --git a/frontend/src/api/files.js b/frontend/src/api/files.js index 1c2ea80a..46549696 100644 --- a/frontend/src/api/files.js +++ b/frontend/src/api/files.js @@ -42,6 +42,16 @@ 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"); } @@ -163,6 +173,11 @@ export async function checksum(url, algo) { return (await data.json()).checksums[algo]; } +export async function foldersize(url) { + const data = await resourceSizeAction(`${url}`, "GET"); + return (await data.json()).size; +} + export function getDownloadURL(file, inline) { const params = { ...(inline && { inline: "true" }), diff --git a/frontend/src/components/prompts/Info.vue b/frontend/src/components/prompts/Info.vue index 03bbfb58..871a0c1a 100644 --- a/frontend/src/components/prompts/Info.vue +++ b/frontend/src/components/prompts/Info.vue @@ -13,11 +13,20 @@ {{ $t("prompts.displayName") }} {{ name }}
-+
{{ $t("prompts.size") }}: {{ humanSize }}
+
+ Size: {{
+ $t("prompts.show")
+ }}
+