repos / pico

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

commit
0c3442b
parent
83e0992
author
Eric Bower
date
2023-08-13 15:37:46 +0000 UTC
fix(pgs): better error handling for project mgmt
6 files changed,  +35, -10
M filehandlers/assets/asset.go
+5, -0
 1@@ -11,6 +11,11 @@ import (
 2 )
 3 
 4 func (h *UploadAssetHandler) validateAsset(data *FileData) (bool, error) {
 5+	projectName := shared.GetProjectName(data.FileEntry)
 6+	if projectName == "" || projectName == "/" || projectName == "." {
 7+		return false, fmt.Errorf("ERROR: invalid project name, you must copy files to a non-root folder (e.g. pgs.sh:/project-name)")
 8+	}
 9+
10 	fname := filepath.Base(data.Filepath)
11 	if int(data.Size) > h.Cfg.MaxAssetSize {
12 		return false, fmt.Errorf("ERROR: file (%s) has exceeded maximum file size (%d bytes)", fname, h.Cfg.MaxAssetSize)
M filehandlers/assets/handler.go
+1, -1
1@@ -111,7 +111,7 @@ func (h *UploadAssetHandler) List(s ssh.Session, fpath string) ([]os.FileInfo, e
2 		}
3 		fileList = append(fileList, info)
4 	} else {
5-		fileList, err = h.Storage.ListFiles(bucket, fpath)
6+		fileList, err = h.Storage.ListFiles(bucket, fpath, false)
7 		if err != nil {
8 			return fileList, err
9 		}
M pgs/wish.go
+25, -5
 1@@ -36,7 +36,8 @@ func getHelpText(userName, projectName string) string {
 2 	helpStr := "commands: [rm, list, link, unlink]\n\n"
 3 	sshCmdStr := fmt.Sprintf("ssh %s@pgs.sh", userName)
 4 	helpStr += fmt.Sprintf("`%s help`: prints this screen\n", sshCmdStr)
 5-	helpStr += fmt.Sprintf("`%s list`: lists projects\n", sshCmdStr)
 6+	helpStr += fmt.Sprintf("`%s stats`: prints stats for user\n", sshCmdStr)
 7+	helpStr += fmt.Sprintf("`%s ls`: lists projects\n", sshCmdStr)
 8 	helpStr += fmt.Sprintf("`%s %s rm`: deletes `%s`\n", sshCmdStr, projectName, projectName)
 9 	helpStr += fmt.Sprintf("`%s %s link projectB`: symbolic link from `%s` to `projectB`\n", sshCmdStr, projectName, projectName)
10 	helpStr += fmt.Sprintf("`%s %s unlink`: removes symbolic link for `%s`\n", sshCmdStr, projectName, projectName)
11@@ -95,7 +96,7 @@ func WishMiddleware(handler *uploadassets.UploadAssetHandler) wish.Middleware {
12 					str := "stats\n"
13 					str += "=====\n"
14 					str += fmt.Sprintf(
15-						"space:\t\t%.4f/%.2fGB, %.2f%%\n",
16+						"space:\t\t%.4f/%.4fGB, %.4f%%\n",
17 						shared.BytesToGB(int(totalFileSize)),
18 						shared.BytesToGB(cfg.MaxSize),
19 						(float32(totalFileSize)/float32(cfg.MaxSize))*100,
20@@ -118,6 +119,9 @@ func WishMiddleware(handler *uploadassets.UploadAssetHandler) wish.Middleware {
21 
22 					for _, project := range projects {
23 						out := fmt.Sprintf("%s (links to: %s)\n", project.Name, project.ProjectDir)
24+						if project.Name == project.ProjectDir {
25+							out = fmt.Sprintf("%s\n", project.Name)
26+						}
27 						_, _ = session.Write([]byte(out))
28 					}
29 				}
30@@ -160,6 +164,14 @@ func WishMiddleware(handler *uploadassets.UploadAssetHandler) wish.Middleware {
31 				log.Infof("user (%s) running `link` command with (%s) (%s)", user.Name, projectName, linkTo)
32 
33 				projectDir := linkTo
34+				_, err := dbpool.FindProjectByName(user.ID, linkTo)
35+				if err != nil {
36+					e := fmt.Errorf("(%s) project doesn't exist", linkTo)
37+					log.Error(e)
38+					utils.ErrorHandler(session, e)
39+					return
40+				}
41+
42 				project, err := dbpool.FindProjectByName(user.ID, projectName)
43 				if err == nil {
44 					log.Infof("user (%s) already has project (%s), updating ...", user.Name, projectName)
45@@ -191,6 +203,11 @@ func WishMiddleware(handler *uploadassets.UploadAssetHandler) wish.Middleware {
46 						log.Error(err)
47 						utils.ErrorHandler(session, err)
48 					}
49+				} else {
50+					e := fmt.Errorf("(%s) project not found for user (%s)", projectName, user.Name)
51+					log.Error(e)
52+					utils.ErrorHandler(session, e)
53+					return
54 				}
55 
56 				bucketName := shared.GetAssetBucketName(user.ID)
57@@ -201,7 +218,7 @@ func WishMiddleware(handler *uploadassets.UploadAssetHandler) wish.Middleware {
58 					return
59 				}
60 
61-				fileList, err := store.ListFiles(bucket, projectName)
62+				fileList, err := store.ListFiles(bucket, projectName, true)
63 				if err != nil {
64 					log.Error(err)
65 					return
66@@ -217,9 +234,12 @@ func WishMiddleware(handler *uploadassets.UploadAssetHandler) wish.Middleware {
67 					}
68 				}
69 				return
70+			} else {
71+				e := fmt.Errorf("%s not a valid command", args)
72+				log.Error(e)
73+				utils.ErrorHandler(session, e)
74+				return
75 			}
76-
77-			sshHandler(session)
78 		}
79 	}
80 }
M shared/storage/fs.go
+1, -1
1@@ -121,7 +121,7 @@ func (s *StorageFS) DeleteFile(bucket Bucket, fpath string) error {
2 	return nil
3 }
4 
5-func (s *StorageFS) ListFiles(bucket Bucket, dir string) ([]os.FileInfo, error) {
6+func (s *StorageFS) ListFiles(bucket Bucket, dir string, recursive bool) ([]os.FileInfo, error) {
7 	var fileList []os.FileInfo
8 	fpath := filepath.Join(bucket.Path, dir)
9 	err := filepath.WalkDir(fpath, func(s string, d fs.DirEntry, err error) error {
M shared/storage/minio.go
+2, -2
 1@@ -95,11 +95,11 @@ func (s *StorageMinio) GetBucketQuota(bucket Bucket) (uint64, error) {
 2 	return 0, fmt.Errorf("%s bucket not found in account info", bucket.Name)
 3 }
 4 
 5-func (s *StorageMinio) ListFiles(bucket Bucket, dir string) ([]os.FileInfo, error) {
 6+func (s *StorageMinio) ListFiles(bucket Bucket, dir string, recursive bool) ([]os.FileInfo, error) {
 7 	var fileList []os.FileInfo
 8 
 9 	resolved := strings.TrimPrefix(dir, "/")
10-	opts := minio.ListObjectsOptions{Prefix: resolved, Recursive: false}
11+	opts := minio.ListObjectsOptions{Prefix: resolved, Recursive: recursive}
12 	for obj := range s.Client.ListObjects(context.Background(), bucket.Name, opts) {
13 		if obj.Err != nil {
14 			return fileList, obj.Err
M shared/storage/storage.go
+1, -1
1@@ -39,5 +39,5 @@ type ObjectStorage interface {
2 	GetFile(bucket Bucket, fpath string) (ReaderAtCloser, error)
3 	PutFile(bucket Bucket, fpath string, contents ReaderAtCloser) (string, error)
4 	DeleteFile(bucket Bucket, fpath string) error
5-	ListFiles(bucket Bucket, dir string) ([]os.FileInfo, error)
6+	ListFiles(bucket Bucket, dir string, recursive bool) ([]os.FileInfo, error)
7 }