From f938af5a61e7c16100dbae4546fcd5135907a1c9 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Sun, 4 Jun 2023 21:45:31 +0200 Subject: [PATCH] WebClient: fix sorting by size Fixes #1313 Signed-off-by: Nicola Murino --- internal/httpd/httpd_test.go | 2 +- internal/httpd/webclient.go | 14 +++++++------- templates/webclient/base.html | 5 +++++ templates/webclient/files.html | 13 ++++++++++++- templates/webclient/sharefiles.html | 13 ++++++++++++- 5 files changed, 37 insertions(+), 10 deletions(-) diff --git a/internal/httpd/httpd_test.go b/internal/httpd/httpd_test.go index 04fa93e3..df73afc7 100644 --- a/internal/httpd/httpd_test.go +++ b/internal/httpd/httpd_test.go @@ -15242,7 +15242,7 @@ func TestWebGetFiles(t *testing.T) { setJWTCookieForReq(req, webToken) rr = executeRequest(req) checkResponseCode(t, http.StatusOK, rr) - var dirContents []map[string]string + var dirContents []map[string]any err = json.Unmarshal(rr.Body.Bytes(), &dirContents) assert.NoError(t, err) assert.Len(t, dirContents, 1) diff --git a/internal/httpd/webclient.go b/internal/httpd/webclient.go index 6ad7d905..82d6db7c 100644 --- a/internal/httpd/webclient.go +++ b/internal/httpd/webclient.go @@ -759,18 +759,18 @@ func (s *httpdServer) handleShareGetDirContents(w http.ResponseWriter, r *http.R sendAPIResponse(w, r, err, "Unable to get directory contents", getMappedStatusCode(err)) return } - results := make([]map[string]string, 0, len(contents)) + results := make([]map[string]any, 0, len(contents)) for _, info := range contents { if !info.Mode().IsDir() && !info.Mode().IsRegular() { continue } - res := make(map[string]string) + res := make(map[string]any) if info.IsDir() { res["type"] = "1" res["size"] = "" } else { res["type"] = "2" - res["size"] = util.ByteCountIEC(info.Size()) + res["size"] = info.Size() } res["meta"] = fmt.Sprintf("%v_%v", res["type"], info.Name()) res["name"] = info.Name() @@ -882,9 +882,9 @@ func (s *httpdServer) handleClientGetDirContents(w http.ResponseWriter, r *http. return } - results := make([]map[string]string, 0, len(contents)) + results := make([]map[string]any, 0, len(contents)) for _, info := range contents { - res := make(map[string]string) + res := make(map[string]any) res["url"] = getFileObjectURL(name, info.Name(), webClientFilesPath) if info.IsDir() { res["type"] = "1" @@ -894,9 +894,9 @@ func (s *httpdServer) handleClientGetDirContents(w http.ResponseWriter, r *http. if info.Mode()&os.ModeSymlink != 0 { res["size"] = "" } else { - res["size"] = util.ByteCountIEC(info.Size()) + res["size"] = info.Size() if info.Size() < httpdMaxEditFileSize { - res["edit_url"] = strings.Replace(res["url"], webClientFilesPath, webClientEditFilePath, 1) + res["edit_url"] = strings.Replace(res["url"].(string), webClientFilesPath, webClientEditFilePath, 1) } if len(s.binding.WebClientIntegrations) > 0 { extension := path.Ext(info.Name()) diff --git a/templates/webclient/base.html b/templates/webclient/base.html index 70bb1d7c..b68f1e79 100644 --- a/templates/webclient/base.html +++ b/templates/webclient/base.html @@ -258,6 +258,11 @@ along with this program. If not, see . function UnicodeDecodeB64(str) { return decodeURIComponent(atob(str)); } + + function fileSizeIEC(a,b,c,d,e){ + return (b=Math,c=b.log,d=1024,e=c(a)/c(d)|0,a/b.pow(d,e)).toFixed(1) + +' '+(e?'KMGTPEZY'[--e]+'iB':'Bytes') + } diff --git a/templates/webclient/files.html b/templates/webclient/files.html index f3fa7397..99c4c601 100644 --- a/templates/webclient/files.html +++ b/templates/webclient/files.html @@ -1191,7 +1191,18 @@ along with this program. If not, see . return data; } }, - { "data": "size" }, + { + "data": "size", + "render": function (data, type, row) { + if (type === 'display') { + if (data){ + return fileSizeIEC(data); + } + return ""; + } + return data; + } + }, { "data": "last_modified" }, { "data": "edit_url", "render": function (data, type, row) { diff --git a/templates/webclient/sharefiles.html b/templates/webclient/sharefiles.html index 00f51c6d..9daec775 100644 --- a/templates/webclient/sharefiles.html +++ b/templates/webclient/sharefiles.html @@ -494,7 +494,18 @@ along with this program. If not, see . return data; } }, - { "data": "size" }, + { + "data": "size", + "render": function (data, type, row) { + if (type === 'display') { + if (data){ + return fileSizeIEC(data); + } + return ""; + } + return data; + } + }, { "data": "last_modified" } ], "buttons": [],