1
0

Localization of size units.

This commit is contained in:
Alexander Andreev 2024-12-30 15:45:23 +04:00
parent 28488a75c2
commit 3bb26f397c
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34
5 changed files with 28 additions and 12 deletions

View File

@ -10,9 +10,10 @@ import (
const FileDateFormat = "2006-01-02 15:04:05 MST" const FileDateFormat = "2006-01-02 15:04:05 MST"
type DirStat struct { type DirStat struct {
Files int64 Files int64
FilesSize string FilesSize string
Directories int64 FilesSizeUnit string
Directories int64
} }
type DirEntry struct { type DirEntry struct {
@ -20,6 +21,7 @@ type DirEntry struct {
Link string Link string
Datetime time.Time Datetime time.Time
Size string Size string
SizeUnit string
} }
// ScanDirectory returns entries of directory which is located by its relative // 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++ stats.Directories++
} else { } else {
sz, ui := convertFileSize(entry.Size())
fileEntries = append(fileEntries, DirEntry{ fileEntries = append(fileEntries, DirEntry{
Name: entry.Name(), Name: entry.Name(),
Link: "/file" + rel + url.PathEscape(entry.Name()), Link: "/file" + rel + url.PathEscape(entry.Name()),
Datetime: entry.ModTime(), Datetime: entry.ModTime(),
Size: convertFileSize(entry.Size()), Size: sz,
SizeUnit: ui,
}) })
totalFilesSize += entry.Size() 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, dirEntries...)
entries = append(entries, fileEntries...) entries = append(entries, fileEntries...)

View File

@ -8,9 +8,9 @@ import (
var sizeSuffixes = [...]string{"B", "KiB", "MiB", "GiB", "TiB"} var sizeSuffixes = [...]string{"B", "KiB", "MiB", "GiB", "TiB"}
// convertFileSize converts size in bytes down to biggest units it represents. // convertFileSize converts size in bytes down to biggest units it represents.
// Returns a concatenation of a converted size and a unit // Returns a converted size string and a unit idx
func convertFileSize(size int64) string { func convertFileSize(size int64) (string, string) {
var idx int var idx uint8
var fSize float64 = float64(size) var fSize float64 = float64(size)
for idx = 0; fSize >= 1024; fSize /= 1024 { for idx = 0; fSize >= 1024; fSize /= 1024 {
idx++ idx++
@ -18,5 +18,5 @@ func convertFileSize(size int64) string {
fSizeStr := strconv.FormatFloat(fSize, 'f', 3, 64) fSizeStr := strconv.FormatFloat(fSize, 'f', 3, 64)
fSizeStr = strings.TrimRight(fSizeStr, "0") fSizeStr = strings.TrimRight(fSizeStr, "0")
fSizeStr = strings.TrimSuffix(fSizeStr, ".") fSizeStr = strings.TrimSuffix(fSizeStr, ".")
return fSizeStr + " " + sizeSuffixes[idx] return fSizeStr, sizeSuffixes[idx]
} }

View File

@ -74,7 +74,7 @@ templ Index(currentPath, progVer string, stat *files.DirStat, entries *[]files.D
<tr tabindex={ strconv.FormatInt(int64(i)+1, 10) }> <tr tabindex={ strconv.FormatInt(int64(i)+1, 10) }>
<td><a href={ templ.SafeURL(entry.Link) }>{ entry.Name }</a></td> <td><a href={ templ.SafeURL(entry.Link) }>{ entry.Name }</a></td>
<td>{ utils.ToClientTimezone(entry.Datetime, r).Format(files.FileDateFormat) }</td> <td>{ utils.ToClientTimezone(entry.Datetime, r).Format(files.FileDateFormat) }</td>
<td>{ entry.Size }</td> <td>{ entry.Size + " " + i18n.T(ctx, "size-unit."+entry.SizeUnit) }</td>
</tr> </tr>
} }
</tbody> </tbody>

View File

@ -17,4 +17,10 @@ en:
autoplay: Autoplay autoplay: Autoplay
footer: footer:
author: Alexander ❝Arav❞ Andreev author: Alexander ❝Arav❞ Andreev
privacy: Privacy statements privacy: Privacy statements
size-unit:
B: "B"
KiB: "KiB"
MiB: "MiB"
GiB: "GiB"
TiB: "TiB"

View File

@ -17,4 +17,10 @@ ru:
autoplay: Автовоспроизведение autoplay: Автовоспроизведение
footer: footer:
author: Александр «Arav» Андреев author: Александр «Arav» Андреев
privacy: О приватности privacy: О приватности
size-unit:
B: "Б"
KiB: "КиБ"
MiB: "МиБ"
GiB: "ГиБ"
TiB: "ТиБ"