repos / pico

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

commit
ccf8adf
parent
fa23407
author
Antonio Mika
date
2022-08-26 16:50:50 +0000 UTC
Added storage to scp handler
5 files changed,  +63, -16
M cmd/lists/ssh/main.go
+15, -1
 1@@ -10,6 +10,7 @@ import (
 2 
 3 	"git.sr.ht/~erock/pico/db/postgres"
 4 	"git.sr.ht/~erock/pico/filehandlers"
 5+	"git.sr.ht/~erock/pico/imgs/storage"
 6 	"git.sr.ht/~erock/pico/lists"
 7 	"git.sr.ht/~erock/pico/shared"
 8 	"git.sr.ht/~erock/pico/wish/cms"
 9@@ -69,7 +70,20 @@ func main() {
10 		Cfg: cfg,
11 		Db:  dbh,
12 	}
13-	handler := filehandlers.NewScpPostHandler(dbh, cfg, hooks)
14+
15+	var st storage.ObjectStorage
16+	var err error
17+	if cfg.MinioURL == "" {
18+		st, err = storage.NewStorageFS(cfg.StorageDir)
19+	} else {
20+		st, err = storage.NewStorageMinio(cfg.MinioURL, cfg.MinioUser, cfg.MinioPass)
21+	}
22+
23+	if err != nil {
24+		logger.Fatal(err)
25+	}
26+
27+	handler := filehandlers.NewScpPostHandler(dbh, cfg, hooks, st)
28 
29 	sshServer := &SSHServer{}
30 	s, err := wish.NewServer(
M cmd/pastes/ssh/main.go
+15, -1
 1@@ -10,6 +10,7 @@ import (
 2 
 3 	"git.sr.ht/~erock/pico/db/postgres"
 4 	"git.sr.ht/~erock/pico/filehandlers"
 5+	"git.sr.ht/~erock/pico/imgs/storage"
 6 	"git.sr.ht/~erock/pico/pastes"
 7 	"git.sr.ht/~erock/pico/shared"
 8 	"git.sr.ht/~erock/pico/wish/cms"
 9@@ -68,7 +69,20 @@ func main() {
10 		Cfg: cfg,
11 		Db:  dbh,
12 	}
13-	handler := filehandlers.NewScpPostHandler(dbh, cfg, hooks)
14+
15+	var st storage.ObjectStorage
16+	var err error
17+	if cfg.MinioURL == "" {
18+		st, err = storage.NewStorageFS(cfg.StorageDir)
19+	} else {
20+		st, err = storage.NewStorageMinio(cfg.MinioURL, cfg.MinioUser, cfg.MinioPass)
21+	}
22+
23+	if err != nil {
24+		logger.Fatal(err)
25+	}
26+
27+	handler := filehandlers.NewScpPostHandler(dbh, cfg, hooks, st)
28 
29 	sshServer := &SSHServer{}
30 	s, err := wish.NewServer(
M cmd/prose/ssh/main.go
+15, -1
 1@@ -10,6 +10,7 @@ import (
 2 
 3 	"git.sr.ht/~erock/pico/db/postgres"
 4 	"git.sr.ht/~erock/pico/filehandlers"
 5+	"git.sr.ht/~erock/pico/imgs/storage"
 6 	"git.sr.ht/~erock/pico/prose"
 7 	"git.sr.ht/~erock/pico/shared"
 8 	"git.sr.ht/~erock/pico/wish/cms"
 9@@ -68,7 +69,20 @@ func main() {
10 		Cfg: cfg,
11 		Db:  dbh,
12 	}
13-	handler := filehandlers.NewScpPostHandler(dbh, cfg, hooks)
14+
15+	var st storage.ObjectStorage
16+	var err error
17+	if cfg.MinioURL == "" {
18+		st, err = storage.NewStorageFS(cfg.StorageDir)
19+	} else {
20+		st, err = storage.NewStorageMinio(cfg.MinioURL, cfg.MinioUser, cfg.MinioPass)
21+	}
22+
23+	if err != nil {
24+		logger.Fatal(err)
25+	}
26+
27+	handler := filehandlers.NewScpPostHandler(dbh, cfg, hooks, st)
28 
29 	sshServer := &SSHServer{}
30 	s, err := wish.NewServer(
M filehandlers/post_handler.go
+16, -12
 1@@ -12,6 +12,7 @@ import (
 2 
 3 	"git.sr.ht/~erock/pico/db"
 4 	"git.sr.ht/~erock/pico/imgs"
 5+	"git.sr.ht/~erock/pico/imgs/storage"
 6 	"git.sr.ht/~erock/pico/shared"
 7 	"git.sr.ht/~erock/pico/wish/cms/util"
 8 	"git.sr.ht/~erock/pico/wish/send/utils"
 9@@ -32,17 +33,21 @@ type ScpFileHooks interface {
10 }
11 
12 type ScpUploadHandler struct {
13-	User   *db.User
14-	DBPool db.DB
15-	Cfg    *shared.ConfigSite
16-	Hooks  ScpFileHooks
17+	User      *db.User
18+	DBPool    db.DB
19+	Cfg       *shared.ConfigSite
20+	Hooks     ScpFileHooks
21+	ImgClient *imgs.ImgsAPI
22 }
23 
24-func NewScpPostHandler(dbpool db.DB, cfg *shared.ConfigSite, hooks ScpFileHooks) *ScpUploadHandler {
25+func NewScpPostHandler(dbpool db.DB, cfg *shared.ConfigSite, hooks ScpFileHooks, st storage.ObjectStorage) *ScpUploadHandler {
26+	client := imgs.NewImgsAPI(dbpool, st)
27+
28 	return &ScpUploadHandler{
29-		DBPool: dbpool,
30-		Cfg:    cfg,
31-		Hooks:  hooks,
32+		DBPool:    dbpool,
33+		Cfg:       cfg,
34+		Hooks:     hooks,
35+		ImgClient: client,
36 	}
37 }
38 
39@@ -135,14 +140,13 @@ func (h *ScpUploadHandler) Write(s ssh.Session, entry *utils.FileEntry) (string,
40 	userID := h.User.ID
41 	filename := entry.Name
42 
43-	client := imgs.NewImgsAPI(h.DBPool)
44-	if shared.IsExtAllowed(filename, client.Cfg.AllowedExt) {
45-		if !client.HasAccess(userID) {
46+	if shared.IsExtAllowed(filename, h.ImgClient.Cfg.AllowedExt) {
47+		if !h.ImgClient.HasAccess(userID) {
48 			msg := "user (%s) does not have access to imgs.sh, cannot upload file (%s)"
49 			return "", fmt.Errorf(msg, h.User.Name, filename)
50 		}
51 
52-		return client.Upload(s, entry)
53+		return h.ImgClient.Upload(s, entry)
54 	}
55 
56 	var origText []byte
M imgs/client.go
+2, -1
 1@@ -20,11 +20,12 @@ type ImgsAPI struct {
 2 	St  storage.ObjectStorage
 3 }
 4 
 5-func NewImgsAPI(dbpool db.DB) *ImgsAPI {
 6+func NewImgsAPI(dbpool db.DB, st storage.ObjectStorage) *ImgsAPI {
 7 	cfg := NewConfigSite()
 8 	return &ImgsAPI{
 9 		Cfg: cfg,
10 		Db:  dbpool,
11+		St:  st,
12 	}
13 }
14