Skip to content

Commit

Permalink
refactor: AssetNames.PrintObjectName
Browse files Browse the repository at this point in the history
fix: listing objects should use `AssetNames.ObjectName`
  • Loading branch information
neurosnap committed Aug 18, 2024
1 parent c7df7b8 commit 07a552a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
10 changes: 10 additions & 0 deletions asset.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package pobj

import (
"fmt"

"github.com/charmbracelet/ssh"
"github.com/picosh/send/send/utils"
)

type AssetNames interface {
BucketName(sesh ssh.Session) (string, error)
ObjectName(sesh ssh.Session, entry *utils.FileEntry) (string, error)
PrintObjectName(sesh ssh.Session, entry *utils.FileEntry, bucketName string) (string, error)
}

type AssetNamesBasic struct{}
Expand All @@ -21,6 +24,13 @@ func (an *AssetNamesBasic) BucketName(sesh ssh.Session) (string, error) {
func (an *AssetNamesBasic) ObjectName(sesh ssh.Session, entry *utils.FileEntry) (string, error) {
return entry.Filepath, nil
}
func (an *AssetNamesBasic) PrintObjectName(sesh ssh.Session, entry *utils.FileEntry, bucketName string) (string, error) {
objectName, err := an.ObjectName(sesh, entry)
if err != nil {
return "", err
}
return fmt.Sprintf("%s%s", bucketName, objectName), nil
}

type AssetNamesForceBucket struct {
*AssetNamesBasic
Expand Down
20 changes: 12 additions & 8 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,13 @@ func (h *UploadAssetHandler) List(s ssh.Session, fpath string, isDir bool, recur
return fileList, err
}

if cleanFilename == "" || cleanFilename == "." {
name := cleanFilename
fname, err := h.Cfg.AssetNames.ObjectName(s, &utils.FileEntry{Filepath: cleanFilename})
if err != nil {
return fileList, err
}

if fname == "" || fname == "." {
name := fname
if name == "" {
name = "/"
}
Expand All @@ -148,11 +153,12 @@ func (h *UploadAssetHandler) List(s ssh.Session, fpath string, isDir bool, recur

fileList = append(fileList, info)
} else {
if cleanFilename != "/" && isDir {
cleanFilename += "/"
name := fname
if name != "/" && isDir {
name += "/"
}

foundList, err := h.Cfg.Storage.ListObjects(bucket, cleanFilename, recursive)
foundList, err := h.Cfg.Storage.ListObjects(bucket, name, recursive)
if err != nil {
return fileList, err
}
Expand Down Expand Up @@ -216,12 +222,10 @@ func (h *UploadAssetHandler) Write(s ssh.Session, entry *utils.FileEntry) (strin
return "", err
}

fpath, err := h.Cfg.AssetNames.ObjectName(s, entry)
url, err := h.Cfg.AssetNames.PrintObjectName(s, entry, bucket.Name)
if err != nil {
return "", err
}
// TODO: make it the object store URL
url := fmt.Sprintf("%s%s", bucket.Name, fpath)
return url, nil
}

Expand Down

0 comments on commit 07a552a

Please sign in to comment.