repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
3764a26
parent
0cea3b3
author
Eric Bower
date
2024-06-16 14:15:14 +0000 UTC
chore(pgs): better error message for uploads
3 files changed,  +25, -5
M filehandlers/assets/handler.go
+14, -1
 1@@ -378,6 +378,12 @@ func (h *UploadAssetHandler) Write(s ssh.Session, entry *utils.FileEntry) (strin
 2 		wish.Fatalln(s, msg)
 3 		return "", fmt.Errorf(msg)
 4 	}
 5+	logger = logger.With(
 6+		"storage max", storageMax,
 7+		"current storage max", curStorageSize,
 8+		"file max", fileMax,
 9+		"sizeRemaining", sizeRemaining,
10+	)
11 
12 	fsize, err := h.writeAsset(
13 		shared.NewMaxBytesReader(data.Reader, int64(sizeRemaining)),
14@@ -385,7 +391,14 @@ func (h *UploadAssetHandler) Write(s ssh.Session, entry *utils.FileEntry) (strin
15 	)
16 	if err != nil {
17 		logger.Error("could not write asset", "err", err.Error())
18-		return "", err
19+		cerr := fmt.Errorf(
20+			"%s: storage size %.2fmb, storage max %.2fmb, file max %.2fmb",
21+			err,
22+			shared.BytesToMB(int(curStorageSize)),
23+			shared.BytesToMB(int(storageMax)),
24+			shared.BytesToMB(int(fileMax)),
25+		)
26+		return "", cerr
27 	}
28 
29 	deltaFileSize := curFileSize - fsize
M shared/io.go
+6, -3
 1@@ -2,6 +2,7 @@ package shared
 2 
 3 import (
 4 	"errors"
 5+	"fmt"
 6 	"io"
 7 )
 8 
 9@@ -9,17 +10,19 @@ import (
10 var ErrSizeExceeded = errors.New("stream size exceeded")
11 
12 type MaxBytesReader struct {
13-	io.Reader       // reader object
14+	io.Reader // reader object
15+	Limit     int64
16 	N         int64 // max bytes remaining.
17 }
18 
19 func NewMaxBytesReader(r io.Reader, limit int64) *MaxBytesReader {
20-	return &MaxBytesReader{r, limit}
21+	return &MaxBytesReader{r, limit, limit}
22 }
23 
24 func (b *MaxBytesReader) Read(p []byte) (n int, err error) {
25 	if b.N <= 0 {
26-		return 0, ErrSizeExceeded
27+		err := fmt.Errorf("%w: %.2fmb", ErrSizeExceeded, BytesToMB(int(b.Limit)))
28+		return 0, err
29 	}
30 
31 	if int64(len(p)) > b.N {
M shared/util.go
+5, -1
 1@@ -148,8 +148,12 @@ func Shasum(data []byte) string {
 2 	return hex.EncodeToString(bs)
 3 }
 4 
 5+func BytesToMB(size int) float32 {
 6+	return ((float32(size) / 1000) / 1000)
 7+}
 8+
 9 func BytesToGB(size int) float32 {
10-	return (((float32(size) / 1000) / 1000) / 1000)
11+	return BytesToMB(size) / 1000
12 }
13 
14 // https://stackoverflow.com/a/46964105