Commit c2b9386
Antonio Mika
·
2026-10-10 23:13:14 -0400 EDT
parent cd3a2a1
feat(uploads): only show storage usage once a user reaches their quota
5 files changed,
+35,
-18
+3,
-1
| ... | ... | @@ -6,6 +6,7 @@ import ( | |
| 6 | 6 | "os" | |
| 7 | 7 | "os/exec" | |
| 8 | 8 | "path/filepath" | |
| 9 | + | "slices" | |
| 9 | 10 | "strings" | |
| 10 | 11 | "testing" | |
| 11 | 12 | "time" |
| ... | ... | @@ -658,7 +659,8 @@ func TestSingleFileIntoProject(t *testing.T) { | |
| 658 | 659 | t.Fatalf("exit %d\n%s", code, out) | |
| 659 | 660 | } | |
| 660 | 661 | env.assertStored(map[string]string{"post.md": "hello"}) | |
| 661 | - | if !strings.Contains(out, "https://testusr-site.pgs.sh/post.md ") { | |
| 662 | + | lines := strings.Split(out, "\n") | |
| 663 | + | if !slices.ContainsFunc(lines, func(l string) bool { return strings.TrimSpace(l) == "https://testusr-site.pgs.sh/post.md" }) { | |
| 662 | 664 | t.Errorf("wrong url\n%s", out) | |
| 663 | 665 | } | |
| 664 | 666 | }) |
+1,
-8
| ... | ... | @@ -404,14 +404,7 @@ func (h *UploadAssetHandler) write(s *pssh.SSHServerConnSession, entry *sendutil | |
| 404 | 404 | strings.Replace(data.Filepath, "/"+projectName+"/", "", 1), | |
| 405 | 405 | ) | |
| 406 | 406 | ||
| 407 | - | maxSize := int(featureFlag.Data.StorageMax) | |
| 408 | - | str := fmt.Sprintf( | |
| 409 | - | "%s (space: %.2f/%.2fGB, %.2f%%)", | |
| 410 | - | url, | |
| 411 | - | shared.BytesToGB(int(nextStorageSize)), | |
| 412 | - | shared.BytesToGB(maxSize), | |
| 413 | - | (float32(nextStorageSize)/float32(maxSize))*100, | |
| 414 | - | ) | |
| 407 | + | str := url + shared.StorageUsage(int(nextStorageSize), int(featureFlag.Data.StorageMax)) | |
| 415 | 408 | ||
| 416 | 409 | surrogate := getSurrogateKey(user.Name, projectName) | |
| 417 | 410 | h.Cfg.CacheClearingQueue <- surrogate |
+1,
-9
| ... | ... | @@ -206,15 +206,7 @@ func (h *UploadImgHandler) Write(s *pssh.SSHServerConnSession, entry *sendutils. | |
| 206 | 206 | user.Name, | |
| 207 | 207 | metadata.Filename, | |
| 208 | 208 | ) | |
| 209 | - | maxSize := int(featureFlag.Data.StorageMax) | |
| 210 | - | str := fmt.Sprintf( | |
| 211 | - | "%s (space: %.2f/%.2fGB, %.2f%%)", | |
| 212 | - | url, | |
| 213 | - | shared.BytesToGB(metadata.TotalFileSize+fileSize), | |
| 214 | - | shared.BytesToGB(maxSize), | |
| 215 | - | (float32(totalFileSize)/float32(maxSize))*100, | |
| 216 | - | ) | |
| 217 | - | return str, nil | |
| 209 | + | return url + shared.StorageUsage(metadata.TotalFileSize+fileSize, int(featureFlag.Data.StorageMax)), nil | |
| 218 | 210 | } | |
| 219 | 211 | ||
| 220 | 212 | func (h *UploadImgHandler) Delete(s *pssh.SSHServerConnSession, entry *sendutils.FileEntry) error { |
| ... | ... | @@ -144,6 +144,16 @@ func BytesToGB(size int) float32 { | |
| 144 | 144 | return BytesToMB(size) / 1000 | |
| 145 | 145 | } | |
| 146 | 146 | ||
| 147 | + | // StorageUsage is the note an upload prints about storage. It is empty | |
| 148 | + | // until a user reaches their quota. | |
| 149 | + | func StorageUsage(used, max int) string { | |
| 150 | + | if max <= 0 || used < max { | |
| 151 | + | return "" | |
| 152 | + | } | |
| 153 | + | pct := float32(used) / float32(max) * 100 | |
| 154 | + | return fmt.Sprintf(" (space: %.2f/%.2fGB, %.2f%%)", BytesToGB(used), BytesToGB(max), pct) | |
| 155 | + | } | |
| 156 | + | ||
| 147 | 157 | // https://stackoverflow.com/a/46964105 | |
| 148 | 158 | func StartOfMonth() time.Time { | |
| 149 | 159 | now := time.Now() |
| ... | ... | @@ -0,0 +1,20 @@ | |
| 1 | + | package shared | |
| 2 | + | ||
| 3 | + | import "testing" | |
| 4 | + | ||
| 5 | + | func TestStorageUsage(t *testing.T) { | |
| 6 | + | tests := []struct { | |
| 7 | + | used, max int | |
| 8 | + | want string | |
| 9 | + | }{ | |
| 10 | + | {used: 0, max: 1000 * MB, want: ""}, | |
| 11 | + | {used: 999 * MB, max: 1000 * MB, want: ""}, | |
| 12 | + | {used: 1000 * MB, max: 1000 * MB, want: " (space: 1.00/1.00GB, 100.00%)"}, | |
| 13 | + | {used: 10, max: 0, want: ""}, | |
| 14 | + | } | |
| 15 | + | for _, tt := range tests { | |
| 16 | + | if got := StorageUsage(tt.used, tt.max); got != tt.want { | |
| 17 | + | t.Errorf("StorageUsage(%d, %d) = %q, want %q", tt.used, tt.max, got, tt.want) | |
| 18 | + | } | |
| 19 | + | } | |
| 20 | + | } |