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 (
66 "os"
77 "os/exec"
88 "path/filepath"
9+ "slices"
910 "strings"
1011 "testing"
1112 "time"
......@@ -658,7 +659,8 @@ func TestSingleFileIntoProject(t *testing.T) {
658659 t.Fatalf("exit %d\n%s", code, out)
659660 }
660661 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" }) {
662664 t.Errorf("wrong url\n%s", out)
663665 }
664666 })
+1, -8
......@@ -404,14 +404,7 @@ func (h *UploadAssetHandler) write(s *pssh.SSHServerConnSession, entry *sendutil
404404 strings.Replace(data.Filepath, "/"+projectName+"/", "", 1),
405405 )
406406
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))
415408
416409 surrogate := getSurrogateKey(user.Name, projectName)
417410 h.Cfg.CacheClearingQueue <- surrogate
+1, -9
......@@ -206,15 +206,7 @@ func (h *UploadImgHandler) Write(s *pssh.SSHServerConnSession, entry *sendutils.
206206 user.Name,
207207 metadata.Filename,
208208 )
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
218210 }
219211
220212 func (h *UploadImgHandler) Delete(s *pssh.SSHServerConnSession, entry *sendutils.FileEntry) error {
+10, -0
......@@ -144,6 +144,16 @@ func BytesToGB(size int) float32 {
144144 return BytesToMB(size) / 1000
145145 }
146146
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+
147157 // https://stackoverflow.com/a/46964105
148158 func StartOfMonth() time.Time {
149159 now := time.Now()
+20, -0
......@@ -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+}