repos / pico

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

commit
388b092
parent
96d1147
author
Eric Bower
date
2023-09-01 14:23:26 +0000 UTC
fix(pgs): allow users to run commands when bucket quota exceeded
2 files changed,  +25, -11
M filehandlers/assets/asset.go
+9, -0
 1@@ -11,6 +11,15 @@ import (
 2 )
 3 
 4 func (h *UploadAssetHandler) validateAsset(data *FileData) (bool, error) {
 5+	if data.BucketQuota >= uint64(h.Cfg.MaxSize) {
 6+		return false, fmt.Errorf(
 7+			"ERROR: user (%s) has exceeded (%d bytes) max (%d bytes)",
 8+			data.User.Name,
 9+			data.BucketQuota,
10+			h.Cfg.MaxSize,
11+		)
12+	}
13+
14 	projectName := shared.GetProjectName(data.FileEntry)
15 	if projectName == "" || projectName == "/" || projectName == "." {
16 		return false, fmt.Errorf("ERROR: invalid project name, you must copy files to a non-root folder (e.g. pgs.sh:/project-name)")
M filehandlers/assets/handler.go
+16, -11
 1@@ -19,6 +19,7 @@ import (
 2 
 3 type ctxUserKey struct{}
 4 type ctxBucketKey struct{}
 5+type ctxBucketQuotaKey struct{}
 6 type ctxProjectKey struct{}
 7 
 8 func getAssetURL(c *shared.ConfigSite, username, projectName, fpath string) string {
 9@@ -49,6 +50,10 @@ func getBucket(s ssh.Session) (storage.Bucket, error) {
10 	return bucket, nil
11 }
12 
13+func getBucketQuota(s ssh.Session) uint64 {
14+	return s.Context().Value(ctxBucketQuotaKey{}).(uint64)
15+}
16+
17 func getUser(s ssh.Session) (*db.User, error) {
18 	user := s.Context().Value(ctxUserKey{}).(*db.User)
19 	if user == nil {
20@@ -59,9 +64,10 @@ func getUser(s ssh.Session) (*db.User, error) {
21 
22 type FileData struct {
23 	*utils.FileEntry
24-	Text   []byte
25-	User   *db.User
26-	Bucket storage.Bucket
27+	Text        []byte
28+	User        *db.User
29+	Bucket      storage.Bucket
30+	BucketQuota uint64
31 }
32 
33 type UploadAssetHandler struct {
34@@ -170,11 +176,8 @@ func (h *UploadAssetHandler) Validate(s ssh.Session) error {
35 	if err != nil {
36 		return err
37 	}
38-
39+	s.Context().SetValue(ctxBucketQuotaKey{}, totalFileSize)
40 	h.Cfg.Logger.Infof("(%s) bucket size is current (%d bytes)", user.Name, totalFileSize)
41-	if totalFileSize >= uint64(h.Cfg.MaxSize) {
42-		return fmt.Errorf("ERROR: user (%s) has exceeded (%d bytes) max (%d bytes)", user.Name, totalFileSize, h.Cfg.MaxSize)
43-	}
44 
45 	s.Context().SetValue(ctxUserKey{}, user)
46 	h.Cfg.Logger.Infof("(%s) attempting to upload files to (%s)", user.Name, h.Cfg.Space)
47@@ -226,11 +229,13 @@ func (h *UploadAssetHandler) Write(s ssh.Session, entry *utils.FileEntry) (strin
48 		s.Context().SetValue(ctxProjectKey{}, project)
49 	}
50 
51+	bucketQuota := getBucketQuota(s)
52 	data := &FileData{
53-		FileEntry: entry,
54-		User:      user,
55-		Text:      origText,
56-		Bucket:    bucket,
57+		FileEntry:   entry,
58+		User:        user,
59+		Text:        origText,
60+		Bucket:      bucket,
61+		BucketQuota: bucketQuota,
62 	}
63 	err = h.writeAsset(data)
64 	if err != nil {