Forráskód Böngészése

WebClient: fix sorting by size

Fixes #1313

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
Nicola Murino 2 éve
szülő
commit
f938af5a61

+ 1 - 1
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)

+ 7 - 7
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())

+ 5 - 0
templates/webclient/base.html

@@ -258,6 +258,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
         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')
+        }
     </script>
 
     <!-- Page level plugins -->

+ 12 - 1
templates/webclient/files.html

@@ -1191,7 +1191,18 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
                         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) {

+ 12 - 1
templates/webclient/sharefiles.html

@@ -494,7 +494,18 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
                         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": [],