diff --git a/pkg/files/files.go b/pkg/files/files.go index 0ff61cb..fa3025b 100644 --- a/pkg/files/files.go +++ b/pkg/files/files.go @@ -10,9 +10,10 @@ import ( const FileDateFormat = "2006-01-02 15:04:05 MST" type DirStat struct { - Files int64 - FilesSize string - Directories int64 + Files int64 + FilesSize string + FilesSizeUnit string + Directories int64 } type DirEntry struct { @@ -20,6 +21,7 @@ type DirEntry struct { Link string Datetime time.Time Size string + SizeUnit string } // ScanDirectory returns entries of directory which is located by its relative @@ -58,11 +60,13 @@ func ScanDirectory(base, rel string) (entries []DirEntry, stats DirStat, err err }) stats.Directories++ } else { + sz, ui := convertFileSize(entry.Size()) fileEntries = append(fileEntries, DirEntry{ Name: entry.Name(), Link: "/file" + rel + url.PathEscape(entry.Name()), Datetime: entry.ModTime(), - Size: convertFileSize(entry.Size()), + Size: sz, + SizeUnit: ui, }) totalFilesSize += entry.Size() @@ -70,7 +74,7 @@ func ScanDirectory(base, rel string) (entries []DirEntry, stats DirStat, err err } } - stats.FilesSize = convertFileSize(totalFilesSize) + stats.FilesSize, stats.FilesSizeUnit = convertFileSize(totalFilesSize) entries = append(entries, dirEntries...) entries = append(entries, fileEntries...) diff --git a/pkg/files/filesize.go b/pkg/files/filesize.go index d3f7f2c..8462eb1 100644 --- a/pkg/files/filesize.go +++ b/pkg/files/filesize.go @@ -8,9 +8,9 @@ import ( var sizeSuffixes = [...]string{"B", "KiB", "MiB", "GiB", "TiB"} // convertFileSize converts size in bytes down to biggest units it represents. -// Returns a concatenation of a converted size and a unit -func convertFileSize(size int64) string { - var idx int +// Returns a converted size string and a unit idx +func convertFileSize(size int64) (string, string) { + var idx uint8 var fSize float64 = float64(size) for idx = 0; fSize >= 1024; fSize /= 1024 { idx++ @@ -18,5 +18,5 @@ func convertFileSize(size int64) string { fSizeStr := strconv.FormatFloat(fSize, 'f', 3, 64) fSizeStr = strings.TrimRight(fSizeStr, "0") fSizeStr = strings.TrimSuffix(fSizeStr, ".") - return fSizeStr + " " + sizeSuffixes[idx] + return fSizeStr, sizeSuffixes[idx] } diff --git a/web/index.templ b/web/index.templ index 80f7472..65a0e34 100644 --- a/web/index.templ +++ b/web/index.templ @@ -74,7 +74,7 @@ templ Index(currentPath, progVer string, stat *files.DirStat, entries *[]files.D { entry.Name } { utils.ToClientTimezone(entry.Datetime, r).Format(files.FileDateFormat) } - { entry.Size } + { entry.Size + " " + i18n.T(ctx, "size-unit."+entry.SizeUnit) } } diff --git a/web/locales/en/en.yaml b/web/locales/en/en.yaml index ec97aab..c9f5cd6 100644 --- a/web/locales/en/en.yaml +++ b/web/locales/en/en.yaml @@ -17,4 +17,10 @@ en: autoplay: Autoplay footer: author: Alexander ❝Arav❞ Andreev - privacy: Privacy statements \ No newline at end of file + privacy: Privacy statements + size-unit: + B: "B" + KiB: "KiB" + MiB: "MiB" + GiB: "GiB" + TiB: "TiB" \ No newline at end of file diff --git a/web/locales/ru/ru.yaml b/web/locales/ru/ru.yaml index 84df43e..6f97967 100644 --- a/web/locales/ru/ru.yaml +++ b/web/locales/ru/ru.yaml @@ -17,4 +17,10 @@ ru: autoplay: Автовоспроизведение footer: author: Александр «Arav» Андреев - privacy: О приватности \ No newline at end of file + privacy: О приватности + size-unit: + B: "Б" + KiB: "КиБ" + MiB: "МиБ" + GiB: "ГиБ" + TiB: "ТиБ" \ No newline at end of file