repos / pico

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

commit
a8ab7f3
parent
dc6a7d0
author
Antonio Mika
date
2023-11-09 17:24:57 +0000 UTC
Improve pgs filesystem support (and fix local storage)
10 files changed,  +66, -34
M filehandlers/assets/handler.go
+13, -3
 1@@ -102,13 +102,16 @@ func (h *UploadAssetHandler) Read(s ssh.Session, entry *utils.FileEntry) (os.Fil
 2 	return fileInfo, reader, nil
 3 }
 4 
 5-func (h *UploadAssetHandler) List(s ssh.Session, fpath string) ([]os.FileInfo, error) {
 6+func (h *UploadAssetHandler) List(s ssh.Session, fpath string, isDir bool) ([]os.FileInfo, error) {
 7 	var fileList []os.FileInfo
 8+
 9 	user, err := getUser(s)
10 	if err != nil {
11 		return fileList, err
12 	}
13-	cleanFilename := filepath.Base(fpath)
14+
15+	cleanFilename := fpath
16+
17 	bucketName := shared.GetAssetBucketName(user.ID)
18 	bucket, err := h.Storage.GetBucket(bucketName)
19 	if err != nil {
20@@ -125,12 +128,19 @@ func (h *UploadAssetHandler) List(s ssh.Session, fpath string) ([]os.FileInfo, e
21 			FName:  name,
22 			FIsDir: true,
23 		}
24+
25 		fileList = append(fileList, info)
26 	} else {
27-		fileList, err = h.Storage.ListFiles(bucket, fpath, false)
28+		if cleanFilename != "/" && isDir {
29+			cleanFilename += "/"
30+		}
31+
32+		foundList, err := h.Storage.ListFiles(bucket, cleanFilename, false)
33 		if err != nil {
34 			return fileList, err
35 		}
36+
37+		fileList = append(fileList, foundList...)
38 	}
39 
40 	return fileList, nil
M filehandlers/imgs/handler.go
+1, -1
1@@ -111,7 +111,7 @@ func (h *UploadImgHandler) Read(s ssh.Session, entry *utils.FileEntry) (os.FileI
2 	return fileInfo, reader, nil
3 }
4 
5-func (h *UploadImgHandler) List(s ssh.Session, fpath string) ([]os.FileInfo, error) {
6+func (h *UploadImgHandler) List(s ssh.Session, fpath string, isDir bool) ([]os.FileInfo, error) {
7 	var fileList []os.FileInfo
8 	user, err := getUser(s)
9 	if err != nil {
M filehandlers/post_handler.go
+1, -1
1@@ -89,7 +89,7 @@ func (h *ScpUploadHandler) Read(s ssh.Session, entry *utils.FileEntry) (os.FileI
2 	return fileInfo, reader, nil
3 }
4 
5-func (h *ScpUploadHandler) List(s ssh.Session, fpath string) ([]os.FileInfo, error) {
6+func (h *ScpUploadHandler) List(s ssh.Session, fpath string, isDir bool) ([]os.FileInfo, error) {
7 	var fileList []os.FileInfo
8 	user, err := getUser(s)
9 	if err != nil {
M shared/storage/fs.go
+38, -18
 1@@ -3,8 +3,8 @@ package storage
 2 import (
 3 	"fmt"
 4 	"io"
 5-	"io/fs"
 6 	"os"
 7+	"path"
 8 	"path/filepath"
 9 	"strings"
10 
11@@ -123,26 +123,46 @@ func (s *StorageFS) DeleteFile(bucket Bucket, fpath string) error {
12 
13 func (s *StorageFS) ListFiles(bucket Bucket, dir string, recursive bool) ([]os.FileInfo, error) {
14 	var fileList []os.FileInfo
15-	fpath := filepath.Join(bucket.Path, dir)
16-	err := filepath.WalkDir(fpath, func(s string, d fs.DirEntry, err error) error {
17+
18+	fpath := path.Join(bucket.Path, dir)
19+
20+	info, err := os.Stat(fpath)
21+	if err != nil {
22+		return fileList, err
23+	}
24+
25+	if info.IsDir() && !strings.HasSuffix(dir, "/") {
26+		fileList = append(fileList, &utils.VirtualFile{
27+			FName:    "",
28+			FIsDir:   info.IsDir(),
29+			FSize:    info.Size(),
30+			FModTime: info.ModTime(),
31+		})
32+
33+		return fileList, err
34+	}
35+
36+	files, err := os.ReadDir(fpath)
37+	if err != nil {
38+		fileList = append(fileList, info)
39+		return fileList, nil
40+	}
41+
42+	for _, f := range files {
43+		info, err := f.Info()
44 		if err != nil {
45-			return err
46+			return fileList, err
47 		}
48-		if !d.IsDir() {
49-			fileInfo, err := os.Stat(s)
50-			if err != nil {
51-				return err
52-			}
53-			info := &utils.VirtualFile{
54-				FName:    strings.Replace(s, bucket.Path, "", 1),
55-				FIsDir:   d.IsDir(),
56-				FSize:    fileInfo.Size(),
57-				FModTime: fileInfo.ModTime(),
58-			}
59-			fileList = append(fileList, info)
60+
61+		i := &utils.VirtualFile{
62+			FName:    f.Name(),
63+			FIsDir:   f.IsDir(),
64+			FSize:    info.Size(),
65+			FModTime: info.ModTime(),
66 		}
67-		return nil
68-	})
69+
70+		fileList = append(fileList, i)
71+	}
72 
73 	return fileList, err
74 }
M shared/storage/minio.go
+3, -1
 1@@ -99,6 +99,7 @@ func (s *StorageMinio) ListFiles(bucket Bucket, dir string, recursive bool) ([]o
 2 	var fileList []os.FileInfo
 3 
 4 	resolved := strings.TrimPrefix(dir, "/")
 5+
 6 	opts := minio.ListObjectsOptions{Prefix: resolved, Recursive: recursive}
 7 	for obj := range s.Client.ListObjects(context.Background(), bucket.Name, opts) {
 8 		if obj.Err != nil {
 9@@ -108,8 +109,9 @@ func (s *StorageMinio) ListFiles(bucket Bucket, dir string, recursive bool) ([]o
10 		if obj.Size == 0 {
11 			isDir = true
12 		}
13+
14 		info := &utils.VirtualFile{
15-			FName:    strings.TrimSuffix(obj.Key, "/"),
16+			FName:    strings.TrimSuffix(strings.TrimPrefix(obj.Key, resolved), "/"),
17 			FIsDir:   isDir,
18 			FSize:    obj.Size,
19 			FModTime: obj.LastModified,
M wish/cmd/server/main.go
+1, -1
1@@ -42,7 +42,7 @@ func (h *handler) Read(session ssh.Session, entry *utils.FileEntry) (os.FileInfo
2 	}, data, nil
3 }
4 
5-func (h *handler) List(session ssh.Session, fpath string) ([]os.FileInfo, error) {
6+func (h *handler) List(session ssh.Session, fpath string, isDir bool) ([]os.FileInfo, error) {
7 	return nil, nil
8 }
9 
M wish/list/list.go
+1, -1
1@@ -18,7 +18,7 @@ func Middleware(writeHandler utils.CopyFromClientHandler) wish.Middleware {
2 				return
3 			}
4 
5-			fileList, err := writeHandler.List(session, "/")
6+			fileList, err := writeHandler.List(session, "/", true)
7 			if err != nil {
8 				utils.ErrorHandler(session, err)
9 				return
M wish/send/rsync/rsync.go
+1, -1
1@@ -26,7 +26,7 @@ func (h *handler) Skip(file *rsyncutils.ReceiverFile) bool {
2 }
3 
4 func (h *handler) List(path string) ([]fs.FileInfo, error) {
5-	list, err := h.writeHandler.List(h.session, path)
6+	list, err := h.writeHandler.List(h.session, path, true)
7 	if err != nil {
8 		return nil, err
9 	}
M wish/send/sftp/handler.go
+6, -6
 1@@ -40,7 +40,7 @@ func (f *handler) Filecmd(r *sftp.Request) error {
 2 		_, err := f.writeHandler.Write(f.session, entry)
 3 
 4 		return err
 5-	case "Setstat":
 6+	case "Setstat", "Mkdir":
 7 		return nil
 8 	}
 9 	return errors.New("unsupported")
10@@ -48,15 +48,15 @@ func (f *handler) Filecmd(r *sftp.Request) error {
11 
12 func (f *handler) Filelist(r *sftp.Request) (sftp.ListerAt, error) {
13 	switch r.Method {
14-	case "List":
15-		fallthrough
16-	case "Stat":
17-		listData, err := f.writeHandler.List(f.session, r.Filepath)
18+	case "List", "Stat":
19+		list := r.Method == "List"
20+
21+		listData, err := f.writeHandler.List(f.session, r.Filepath, list)
22 		if err != nil {
23 			return nil, err
24 		}
25 
26-		if r.Filepath != "/" {
27+		if list {
28 			listData = slices.DeleteFunc(listData, func(f os.FileInfo) bool {
29 				return f.Name() == "/"
30 			})
M wish/send/utils/utils.go
+1, -1
1@@ -58,7 +58,7 @@ type CopyFromClientHandler interface {
2 	// Write should write the given file.
3 	Write(ssh.Session, *FileEntry) (string, error)
4 	Read(ssh.Session, *FileEntry) (os.FileInfo, io.ReaderAt, error)
5-	List(ssh.Session, string) ([]os.FileInfo, error)
6+	List(ssh.Session, string, bool) ([]os.FileInfo, error)
7 	Validate(ssh.Session) error
8 }
9