Browse Source

Show fileSize.

HEAD
Yang Luo 3 years ago
parent
commit
995a584b8d
3 changed files with 26 additions and 2 deletions
  1. +1
    -0
      object/store.go
  2. +9
    -2
      object/store_provider.go
  3. +16
    -0
      util/file.go

+ 1
- 0
object/store.go View File

@@ -10,6 +10,7 @@ import (
type File struct {
Key string `xorm:"varchar(100)" json:"key"`
Title string `xorm:"varchar(100)" json:"title"`
FileSize int64 `json:"fileSize"`
ModifiedTime string `xorm:"varchar(100)" json:"modifiedTime"`
IsLeaf bool `json:"isLeaf"`
Children []*File `xorm:"varchar(1000)" json:"children"`


+ 9
- 2
object/store_provider.go View File

@@ -1,6 +1,7 @@
package object

import (
"fmt"
"strings"
"time"

@@ -8,7 +9,7 @@ import (
"github.com/casbin/casbase/util"
)

func (store *Store) createPathIfNotExisted(tokens []string, lastModifiedTime string, isLeaf bool) {
func (store *Store) createPathIfNotExisted(tokens []string, fileSize int64, lastModifiedTime string, isLeaf bool) {
currentFile := store.FileTree
if currentFile == nil {
currentFile = &File{
@@ -51,6 +52,10 @@ func (store *Store) createPathIfNotExisted(tokens []string, lastModifiedTime str
}

if i == len(tokens)-1 {
if newFile.IsLeaf {
newFile.Title = fmt.Sprintf("%s (%s)", token, util.GetFileSizeString(fileSize))
}
newFile.FileSize = fileSize
newFile.ModifiedTime = lastModifiedTime
}

@@ -70,8 +75,10 @@ func (store *Store) Populate() {
isLeaf = false
}

fileSize := object.Size

tokens := strings.Split(strings.Trim(object.Key, "/"), "/")
store.createPathIfNotExisted(tokens, lastModifiedTime, isLeaf)
store.createPathIfNotExisted(tokens, fileSize, lastModifiedTime, isLeaf)

//fmt.Printf("%s, %d, %v\n", object.Key, object.Size, object.LastModified)
}


+ 16
- 0
util/file.go View File

@@ -2,6 +2,7 @@ package util

import (
"bufio"
"fmt"
"os"
"strings"
)
@@ -112,3 +113,18 @@ func LoadVectorFileBySpace(path string) ([]string, [][]float64) {

return nameArray, dataArray
}

//https://yourbasic.org/golang/formatting-byte-size-to-human-readable-format/
func GetFileSizeString(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB",
float64(b)/float64(div), "kMGTPE"[exp])
}

Loading…
Cancel
Save