From 9b9920b5ed234caef554b8717bb4fd2b9e99c942 Mon Sep 17 00:00:00 2001 From: Joren Date: Wed, 2 Apr 2025 21:04:03 +0200 Subject: [PATCH] storage --- main.go | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 7646c21..119cc45 100644 --- a/main.go +++ b/main.go @@ -4,10 +4,10 @@ import ( "bufio" "flag" "fmt" + "io" "log" "os" "os/exec" - "io" "path/filepath" "regexp" "strconv" @@ -16,10 +16,10 @@ import ( ) type FileQuality struct { - Path string - Bitrate int - Bitdepth int - Size int64 + Path string + Bitrate int + Bitdepth int + Size int64 } var ( @@ -149,6 +149,19 @@ func findDuplicateFiles(rootDir string) (map[string][]FileQuality, error) { return fileGroups, nil } +func bytesToHumanReadable(bytes int64) string { + const unit = 1024 + if bytes < unit { + return fmt.Sprintf("%d B", bytes) + } + div, exp := int64(unit), 0 + for n := bytes / unit; n >= unit; n /= unit { + div *= unit + exp++ + } + return fmt.Sprintf("%.1f %ciB", float64(bytes)/float64(div), "KMGTPE"[exp]) +} + func processFileGroup(baseFile string, files []FileQuality) { writeLog(fmt.Sprintf("Processing group for: %s", baseFile)) @@ -212,11 +225,11 @@ func processFileGroup(baseFile string, files []FileQuality) { continue } } - writeLog(fmt.Sprintf(" * %s: %s (Recovering %d bytes)", action, file.Path, file.Size)) + writeLog(fmt.Sprintf(" * %s: %s (Recovering %s)", action, file.Path, bytesToHumanReadable(file.Size))) } } - writeLog(fmt.Sprintf(" Total recoverable space: %d bytes", totalSize-bestFile.Size)) + writeLog(fmt.Sprintf(" Total recoverable space: %s", bytesToHumanReadable(totalSize-bestFile.Size))) } func main() { @@ -271,11 +284,19 @@ func main() { writeLog("") } + var totalRecoverableSpace int64 + for baseFile, group := range fileGroups { processFileGroup(baseFile, group) + for _, file := range group { + if file.Path != baseFile { + totalRecoverableSpace += file.Size + } + } writeLog("") } + writeLog(fmt.Sprintf("Total recoverable space: %s", bytesToHumanReadable(totalRecoverableSpace))) writeLog(fmt.Sprintf("Cleanup completed at %s", getCurrentTime())) writeLog("===============================") }