- commit
- adf0961
- parent
- 80bbdc0
- author
- Eric Bower
- date
- 2023-08-16 18:13:44 +0000 UTC
fix: only delete assets from exact project match refactor: abstract removing assets
1 files changed,
+40,
-38
+40,
-38
1@@ -9,6 +9,7 @@ import (
2 "github.com/picosh/pico/db"
3 uploadassets "github.com/picosh/pico/filehandlers/assets"
4 "github.com/picosh/pico/shared"
5+ "github.com/picosh/pico/shared/storage"
6 "github.com/picosh/pico/wish/cms/util"
7 "github.com/picosh/pico/wish/send/utils"
8 )
9@@ -32,6 +33,34 @@ func getUser(s ssh.Session, dbpool db.DB) (*db.User, error) {
10 return user, nil
11 }
12
13+type ProjectDetails struct {
14+ session ssh.Session
15+ store storage.ObjectStorage
16+}
17+
18+func (pd *ProjectDetails) rmProjectAssets(userID string, projectName string) error {
19+ bucketName := shared.GetAssetBucketName(userID)
20+ bucket, err := pd.store.GetBucket(bucketName)
21+ if err != nil {
22+ return err
23+ }
24+
25+ fileList, err := pd.store.ListFiles(bucket, projectName+"/", true)
26+ if err != nil {
27+ return err
28+ }
29+
30+ for _, file := range fileList {
31+ err = pd.store.DeleteFile(bucket, file.Name())
32+ if err == nil {
33+ _, _ = pd.session.Write([]byte(fmt.Sprintf("deleted (%s)\n", file.Name())))
34+ } else {
35+ return err
36+ }
37+ }
38+ return nil
39+}
40+
41 func getHelpText(userName, projectName string) string {
42 helpStr := "commands: [help, stats, ls, rm, clean, link, links, unlink]\n\n"
43 sshCmdStr := fmt.Sprintf("ssh %s@pgs.sh", userName)
44@@ -275,14 +304,6 @@ func WishMiddleware(handler *uploadassets.UploadAssetHandler) wish.Middleware {
45 utils.ErrorHandler(session, err)
46 }
47
48- bucketName := shared.GetAssetBucketName(user.ID)
49- bucket, err := store.GetBucket(bucketName)
50- if err != nil {
51- log.Error(err)
52- utils.ErrorHandler(session, err)
53- return
54- }
55-
56 rmProjects := []*db.Project{}
57 for _, project := range projects {
58 links, err := dbpool.FindProjectLinks(user.ID, project.Name)
59@@ -299,20 +320,14 @@ func WishMiddleware(handler *uploadassets.UploadAssetHandler) wish.Middleware {
60 }
61
62 for _, project := range rmProjects {
63- fileList, err := store.ListFiles(bucket, project.Name, true)
64+ pd := ProjectDetails{
65+ session: session,
66+ store: store,
67+ }
68+ err = pd.rmProjectAssets(user.ID, project.Name)
69 if err != nil {
70 log.Error(err)
71- return
72- }
73-
74- for _, file := range fileList {
75- err = store.DeleteFile(bucket, file.Name())
76- if err == nil {
77- _, _ = session.Write([]byte(fmt.Sprintf("deleted (%s)\n", file.Name())))
78- } else {
79- log.Error(err)
80- utils.ErrorHandler(session, err)
81- }
82+ utils.ErrorHandler(session, err)
83 }
84
85 err = dbpool.RemoveProject(project.ID)
86@@ -351,29 +366,16 @@ func WishMiddleware(handler *uploadassets.UploadAssetHandler) wish.Middleware {
87 return
88 }
89
90- bucketName := shared.GetAssetBucketName(user.ID)
91- bucket, err := store.GetBucket(bucketName)
92- if err != nil {
93- log.Error(err)
94- utils.ErrorHandler(session, err)
95- return
96+ pd := ProjectDetails{
97+ session: session,
98+ store: store,
99 }
100-
101- fileList, err := store.ListFiles(bucket, projectName, true)
102+ err = pd.rmProjectAssets(user.ID, project.Name)
103 if err != nil {
104 log.Error(err)
105- return
106+ utils.ErrorHandler(session, err)
107 }
108
109- for _, file := range fileList {
110- err = store.DeleteFile(bucket, file.Name())
111- if err == nil {
112- _, _ = session.Write([]byte(fmt.Sprintf("deleted (%s)\n", file.Name())))
113- } else {
114- log.Error(err)
115- utils.ErrorHandler(session, err)
116- }
117- }
118 return
119 } else {
120 e := fmt.Errorf("%s not a valid command", args)