|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +// VaultStats holds statistics about the vault |
| 9 | +type VaultStats struct { |
| 10 | + TotalFiles int |
| 11 | + TotalFolders int |
| 12 | + TotalSize int64 |
| 13 | +} |
| 14 | + |
| 15 | +// GetVaultStats calculates statistics about the vault |
| 16 | +func GetVaultStats(session *Session) VaultStats { |
| 17 | + stats := VaultStats{} |
| 18 | + |
| 19 | + // Recursively count files and folders |
| 20 | + var countEntries func(Entry) |
| 21 | + countEntries = func(e Entry) { |
| 22 | + if e.Type == "file" { |
| 23 | + stats.TotalFiles++ |
| 24 | + } else { |
| 25 | + stats.TotalFolders++ |
| 26 | + for _, subEntry := range e.Contents { |
| 27 | + countEntries(subEntry) |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // Count all entries in the index |
| 33 | + for _, entry := range session.Index { |
| 34 | + countEntries(entry) |
| 35 | + } |
| 36 | + |
| 37 | + return stats |
| 38 | +} |
| 39 | + |
| 40 | +// GetFileInfo retrieves detailed information about a specific file |
| 41 | +func GetFileInfo(vaultPath string, session *Session) (map[string]interface{}, error) { |
| 42 | + entry, err := session.Index.FindEntry(vaultPath) |
| 43 | + if err != nil { |
| 44 | + return nil, fmt.Errorf("could not find file in vault: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + if entry.Type == "folder" { |
| 48 | + return nil, fmt.Errorf("'%s' is a directory, not a file", vaultPath) |
| 49 | + } |
| 50 | + |
| 51 | + // Fetch file from remote to get size |
| 52 | + PrintProgressStep(1, 2, "Fetching file metadata...") |
| 53 | + encryptedData, err := FetchRaw(session.Username, entry.RealName) |
| 54 | + if err != nil { |
| 55 | + return nil, fmt.Errorf("failed to fetch file from remote: %w", err) |
| 56 | + } |
| 57 | + PrintCompletionLine("File metadata retrieved") |
| 58 | + |
| 59 | + info := map[string]interface{}{ |
| 60 | + "name": strings.Split(vaultPath, "/")[len(strings.Split(vaultPath, "/"))-1], |
| 61 | + "vaultPath": vaultPath, |
| 62 | + "storageID": entry.RealName, |
| 63 | + "encryptedSize": len(encryptedData), |
| 64 | + "fileKey": entry.FileKey, |
| 65 | + } |
| 66 | + |
| 67 | + return info, nil |
| 68 | +} |
| 69 | + |
| 70 | +// PrintVaultInfo prints formatted vault information |
| 71 | +func PrintVaultInfo(session *Session) { |
| 72 | + stats := GetVaultStats(session) |
| 73 | + |
| 74 | + fmt.Println("\n╔════════════════════════════════════════╗") |
| 75 | + fmt.Println("║ VAULT INFORMATION ║") |
| 76 | + fmt.Println("╚════════════════════════════════════════╝") |
| 77 | + fmt.Printf("Username: %s\n", session.Username) |
| 78 | + fmt.Printf("Total Files: %d\n", stats.TotalFiles) |
| 79 | + fmt.Printf("Total Folders: %d\n", stats.TotalFolders) |
| 80 | + fmt.Println("\n╔════════════════════════════════════════╗") |
| 81 | + fmt.Println("║ VAULT SETTINGS ║") |
| 82 | + fmt.Println("╚════════════════════════════════════════╝") |
| 83 | + fmt.Printf("Commit Author: %s <%s>\n", session.Settings.CommitAuthorName, session.Settings.CommitAuthorEmail) |
| 84 | + fmt.Printf("Commit Message: %s\n", session.Settings.CommitMessage) |
| 85 | + fmt.Printf("File Hash Length: %d characters\n", session.Settings.FileHashLength) |
| 86 | + fmt.Printf("Share Hash Length: %d characters\n", session.Settings.ShareHashLength) |
| 87 | + fmt.Println() |
| 88 | +} |
| 89 | + |
| 90 | +// PrintFileInfo prints formatted file information |
| 91 | +func PrintFileInfo(fileInfo map[string]interface{}) { |
| 92 | + fmt.Println("\n╔════════════════════════════════════════╗") |
| 93 | + fmt.Println("║ FILE INFORMATION ║") |
| 94 | + fmt.Println("╚════════════════════════════════════════╝") |
| 95 | + fmt.Printf("File Name: %s\n", fileInfo["name"]) |
| 96 | + fmt.Printf("Vault Path: %s\n", fileInfo["vaultPath"]) |
| 97 | + fmt.Printf("Storage ID (Hash): %s\n", fileInfo["storageID"]) |
| 98 | + fmt.Printf("Encrypted Size: %d bytes\n", fileInfo["encryptedSize"]) |
| 99 | + fmt.Printf("File Key (encrypted): %s\n", fileInfo["fileKey"]) |
| 100 | + fmt.Println() |
| 101 | +} |
0 commit comments