Skip to content

Commit

Permalink
refactor: use pobj (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap authored Feb 18, 2024
1 parent 1e436da commit 15dc5a8
Show file tree
Hide file tree
Showing 29 changed files with 85 additions and 511 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.21
go-version: 1.22
- name: Checkout repo
uses: actions/checkout@v3
- name: Lint the codebase
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
fetch-depth: 0
- uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: 1.22
- name: install pgit
run: |
go install github.com/picosh/pgit@latest
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM --platform=$BUILDPLATFORM golang:1.21 as builder-deps
FROM --platform=$BUILDPLATFORM golang:1.22 as builder-deps
LABEL maintainer="Pico Maintainers <[email protected]>"

WORKDIR /app
Expand Down
4 changes: 2 additions & 2 deletions cmd/scripts/clean-object-store/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {
picoCfg.MinioPass = os.Getenv("MINIO_ROOT_PASSWORD")
picoDb := postgres.NewDB(picoCfg.DbURL, picoCfg.Logger)

var st storage.ObjectStorage
var st storage.StorageServe
var err error
st, err = storage.NewStorageMinio(picoCfg.MinioURL, picoCfg.MinioUser, picoCfg.MinioPass)
bail(err)
Expand All @@ -66,7 +66,7 @@ func main() {

bucket, err := st.GetBucket(bucketName)
bail(err)
bucketProjects, err := st.ListFiles(bucket, "/", false)
bucketProjects, err := st.ListObjects(bucket, "/", false)
bail(err)

userID := strings.Replace(bucketName, "static-", "", 1)
Expand Down
2 changes: 1 addition & 1 deletion feeds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func StartApiServer() {
defer db.Close()
logger := cfg.Logger

var st storage.ObjectStorage
var st storage.StorageServe
var err error
if cfg.MinioURL == "" {
st, err = storage.NewStorageFS(cfg.StorageDir)
Expand Down
2 changes: 1 addition & 1 deletion feeds/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func StartSshServer() {
Db: dbh,
}

var st storage.ObjectStorage
var st storage.StorageServe
var err error
if cfg.MinioURL == "" {
st, err = storage.NewStorageFS(cfg.StorageDir)
Expand Down
24 changes: 13 additions & 11 deletions filehandlers/assets/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/picosh/pico/shared"
"github.com/picosh/pico/shared/storage"
"github.com/picosh/pico/wish/cms/util"
"github.com/picosh/pobj"
sst "github.com/picosh/pobj/storage"
"github.com/picosh/send/send/utils"
)

Expand All @@ -33,8 +35,8 @@ func getProject(s ssh.Session) *db.Project {
return project
}

func getBucket(s ssh.Session) (storage.Bucket, error) {
bucket := s.Context().Value(ctxBucketKey{}).(storage.Bucket)
func getBucket(s ssh.Session) (sst.Bucket, error) {
bucket := s.Context().Value(ctxBucketKey{}).(sst.Bucket)
if bucket.Name == "" {
return bucket, fmt.Errorf("bucket not set on `ssh.Context()` for connection")
}
Expand All @@ -61,7 +63,7 @@ type FileData struct {
*utils.FileEntry
Text []byte
User *db.User
Bucket storage.Bucket
Bucket sst.Bucket
StorageSize uint64
FeatureFlag *db.FeatureFlag
DeltaFileSize int64
Expand All @@ -70,10 +72,10 @@ type FileData struct {
type UploadAssetHandler struct {
DBPool db.DB
Cfg *shared.ConfigSite
Storage storage.ObjectStorage
Storage storage.StorageServe
}

func NewUploadAssetHandler(dbpool db.DB, cfg *shared.ConfigSite, storage storage.ObjectStorage) *UploadAssetHandler {
func NewUploadAssetHandler(dbpool db.DB, cfg *shared.ConfigSite, storage storage.StorageServe) *UploadAssetHandler {
return &UploadAssetHandler{
DBPool: dbpool,
Cfg: cfg,
Expand Down Expand Up @@ -104,15 +106,15 @@ func (h *UploadAssetHandler) Read(s ssh.Session, entry *utils.FileEntry) (os.Fil
}

fname := shared.GetAssetFileName(entry)
contents, size, modTime, err := h.Storage.GetFile(bucket, fname)
contents, size, modTime, err := h.Storage.GetObject(bucket, fname)
if err != nil {
return nil, nil, err
}

fileInfo.FSize = size
fileInfo.FModTime = modTime

reader := shared.NewAllReaderAt(contents)
reader := pobj.NewAllReaderAt(contents)

return fileInfo, reader, nil
}
Expand Down Expand Up @@ -150,7 +152,7 @@ func (h *UploadAssetHandler) List(s ssh.Session, fpath string, isDir bool, recur
cleanFilename += "/"
}

foundList, err := h.Storage.ListFiles(bucket, cleanFilename, recursive)
foundList, err := h.Storage.ListObjects(bucket, cleanFilename, recursive)
if err != nil {
return fileList, err
}
Expand Down Expand Up @@ -271,7 +273,7 @@ func (h *UploadAssetHandler) Write(s ssh.Session, entry *utils.FileEntry) (strin
// calculate the filsize difference between the same file already
// stored and the updated file being uploaded
assetFilename := shared.GetAssetFileName(entry)
curFileSize, _ := h.Storage.GetFileSize(bucket, assetFilename)
curFileSize, _ := h.Storage.GetObjectSize(bucket, assetFilename)
deltaFileSize := curFileSize - entry.Size

data := &FileData{
Expand Down Expand Up @@ -373,7 +375,7 @@ func (h *UploadAssetHandler) writeAsset(data *FileData) error {
assetFilename := shared.GetAssetFileName(data.FileEntry)

if data.Size == 0 {
err = h.Storage.DeleteFile(data.Bucket, assetFilename)
err = h.Storage.DeleteObject(data.Bucket, assetFilename)
if err != nil {
return err
}
Expand All @@ -390,7 +392,7 @@ func (h *UploadAssetHandler) writeAsset(data *FileData) error {
assetFilename,
)

_, err := h.Storage.PutFile(
_, err := h.Storage.PutObject(
data.Bucket,
assetFilename,
utils.NopReaderAtCloser(reader),
Expand Down
9 changes: 5 additions & 4 deletions filehandlers/imgs/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/picosh/pico/filehandlers/util"
"github.com/picosh/pico/shared"
"github.com/picosh/pico/shared/storage"
"github.com/picosh/pobj"
"github.com/picosh/send/send/utils"
)

Expand All @@ -35,10 +36,10 @@ type PostMetaData struct {
type UploadImgHandler struct {
DBPool db.DB
Cfg *shared.ConfigSite
Storage storage.ObjectStorage
Storage storage.StorageServe
}

func NewUploadImgHandler(dbpool db.DB, cfg *shared.ConfigSite, storage storage.ObjectStorage) *UploadImgHandler {
func NewUploadImgHandler(dbpool db.DB, cfg *shared.ConfigSite, storage storage.StorageServe) *UploadImgHandler {
return &UploadImgHandler{
DBPool: dbpool,
Cfg: cfg,
Expand Down Expand Up @@ -92,12 +93,12 @@ func (h *UploadImgHandler) Read(s ssh.Session, entry *utils.FileEntry) (os.FileI
return nil, nil, err
}

contents, _, _, err := h.Storage.GetFile(bucket, post.Filename)
contents, _, _, err := h.Storage.GetObject(bucket, post.Filename)
if err != nil {
return nil, nil, err
}

reader := shared.NewAllReaderAt(contents)
reader := pobj.NewAllReaderAt(contents)

return fileInfo, reader, nil
}
Expand Down
4 changes: 2 additions & 2 deletions filehandlers/imgs/img.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (h *UploadImgHandler) metaImg(data *PostMetaData) error {

reader := bytes.NewReader([]byte(data.Text))

fname, err := h.Storage.PutFile(
fname, err := h.Storage.PutObject(
bucket,
data.Filename,
utils.NopReaderAtCloser(reader),
Expand Down Expand Up @@ -107,7 +107,7 @@ func (h *UploadImgHandler) writeImg(s ssh.Session, data *PostMetaData) error {
if err != nil {
return err
}
err = h.Storage.DeleteFile(bucket, data.Filename)
err = h.Storage.DeleteObject(bucket, data.Filename)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion filehandlers/post_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type ScpUploadHandler struct {
Hooks ScpFileHooks
}

func NewScpPostHandler(dbpool db.DB, cfg *shared.ConfigSite, hooks ScpFileHooks, st storage.ObjectStorage) *ScpUploadHandler {
func NewScpPostHandler(dbpool db.DB, cfg *shared.ConfigSite, hooks ScpFileHooks, st storage.StorageServe) *ScpUploadHandler {
return &ScpUploadHandler{
DBPool: dbpool,
Cfg: cfg,
Expand Down
8 changes: 4 additions & 4 deletions filehandlers/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ type ctxUserKey struct{}
type ctxFeatureFlagKey struct{}

func GetUser(s ssh.Session) (*db.User, error) {
user := s.Context().Value(ctxUserKey{}).(*db.User)
if user == nil {
user, ok := s.Context().Value(ctxUserKey{}).(*db.User)
if !ok {
return user, fmt.Errorf("user not set on `ssh.Context()` for connection")
}
return user, nil
Expand All @@ -23,8 +23,8 @@ func SetUser(s ssh.Session, user *db.User) {
}

func GetFeatureFlag(s ssh.Session) (*db.FeatureFlag, error) {
ff := s.Context().Value(ctxFeatureFlagKey{}).(*db.FeatureFlag)
if ff.Name == "" {
ff, ok := s.Context().Value(ctxFeatureFlagKey{}).(*db.FeatureFlag)
if !ok || ff.Name == "" {
return ff, fmt.Errorf("feature flag not set on `ssh.Context()` for connection")
}
return ff, nil
Expand Down
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/picosh/pico

go 1.21.4
go 1.22.0

require (
github.com/alecthomas/chroma v0.10.0
Expand All @@ -15,13 +15,12 @@ require (
github.com/gorilla/feeds v1.1.2
github.com/lib/pq v1.10.9
github.com/microcosm-cc/bluemonday v1.0.26
github.com/minio/madmin-go/v3 v3.0.29
github.com/minio/minio-go/v7 v7.0.63
github.com/mmcdole/gofeed v1.2.1
github.com/muesli/reflow v0.3.0
github.com/neurosnap/go-exif-remove v0.0.0-20221010134343-50d1e3c35577
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/picosh/send v0.0.0-20240217010313-c282075fbdf8
github.com/picosh/pobj v0.0.0-20240217191723-5a89dec577a1
github.com/picosh/send v0.0.0-20240217194807-77b972121e63
github.com/sendgrid/sendgrid-go v3.13.0+incompatible
github.com/yuin/goldmark v1.6.0
github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594
Expand Down Expand Up @@ -75,7 +74,9 @@ require (
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/minio/madmin-go/v3 v3.0.29 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/minio/minio-go/v7 v7.0.63 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mmcdole/goxpp v1.1.0 // indirect
github.com/mmcloughlin/md4 v0.1.2 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,10 @@ github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaR
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw=
github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0=
github.com/picosh/send v0.0.0-20240217010313-c282075fbdf8 h1:yYsjCSE+SRMDVj7efPu4I048jevI42ZvaD0GQiRiRBI=
github.com/picosh/send v0.0.0-20240217010313-c282075fbdf8/go.mod h1:1JCq0NVOdTDenQ0/Kd8e4rP80lu06UHJJ+6dQxhcpew=
github.com/picosh/pobj v0.0.0-20240217191723-5a89dec577a1 h1:6aJBn2UsA0pWvwjV6C6/klcMzFUu6PpOfkI5DzHr6xo=
github.com/picosh/pobj v0.0.0-20240217191723-5a89dec577a1/go.mod h1:ILtZ0GOqkozrrGCvyJqCSUIwal2XQqzSfbKCNdS+HyU=
github.com/picosh/send v0.0.0-20240217194807-77b972121e63 h1:VSSbAejFzj2KBThfVnMcNXQwzHmwjPUridgi29LxihU=
github.com/picosh/send v0.0.0-20240217194807-77b972121e63/go.mod h1:1JCq0NVOdTDenQ0/Kd8e4rP80lu06UHJJ+6dQxhcpew=
github.com/pkg/sftp v1.13.6 h1:JFZT4XbOU7l77xGSpOdW+pwIMqP044IyjXX6FGyEKFo=
github.com/pkg/sftp v1.13.6/go.mod h1:tz1ryNURKu77RL+GuCzmoJYxQczL3wLNNpPWagdg4Qk=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
2 changes: 1 addition & 1 deletion imgs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func StartApiServer() {
db := postgres.NewDB(cfg.DbURL, cfg.Logger)
defer db.Close()

var st storage.ObjectStorage
var st storage.StorageServe
var err error
if cfg.MinioURL == "" {
st, err = storage.NewStorageFS(cfg.StorageDir)
Expand Down
2 changes: 1 addition & 1 deletion pastes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func StartApiServer() {
defer db.Close()
logger := cfg.Logger

var st storage.ObjectStorage
var st storage.StorageServe
var err error
if cfg.MinioURL == "" {
st, err = storage.NewStorageFS(cfg.StorageDir)
Expand Down
2 changes: 1 addition & 1 deletion pastes/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func StartSshServer() {
Db: dbh,
}

var st storage.ObjectStorage
var st storage.StorageServe
var err error
if cfg.MinioURL == "" {
st, err = storage.NewStorageFS(cfg.StorageDir)
Expand Down
15 changes: 8 additions & 7 deletions pgs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/picosh/pico/db/postgres"
"github.com/picosh/pico/shared"
"github.com/picosh/pico/shared/storage"
sst "github.com/picosh/pobj/storage"
"github.com/picosh/send/send/utils"
)

Expand All @@ -30,11 +31,11 @@ type AssetHandler struct {
ProjectDir string
Cfg *shared.ConfigSite
Dbpool db.DB
Storage storage.ObjectStorage
Storage storage.StorageServe
Logger *slog.Logger
Cache *gocache.Cache
UserID string
Bucket storage.Bucket
Bucket sst.Bucket
ImgProcessOpts *storage.ImgProcessOpts
}

Expand Down Expand Up @@ -224,7 +225,7 @@ func calcPossibleRoutes(projectName, fp string, userRedirects []*RedirectRule) [

func (h *AssetHandler) handle(w http.ResponseWriter) {
var redirects []*RedirectRule
redirectFp, _, _, err := h.Storage.GetFile(h.Bucket, filepath.Join(h.ProjectDir, "_redirects"))
redirectFp, _, _, err := h.Storage.GetObject(h.Bucket, filepath.Join(h.ProjectDir, "_redirects"))
if err == nil {
defer redirectFp.Close()
buf := new(strings.Builder)
Expand Down Expand Up @@ -253,13 +254,13 @@ func (h *AssetHandler) handle(w http.ResponseWriter) {
var c io.ReadCloser
var err error
if strings.HasPrefix(mimeType, "image/") {
c, contentType, err = h.Storage.ServeFile(
c, contentType, err = h.Storage.ServeObject(
h.Bucket,
fp.Filepath,
h.ImgProcessOpts,
)
} else {
c, _, _, err = h.Storage.GetFile(h.Bucket, fp.Filepath)
c, _, _, err = h.Storage.GetObject(h.Bucket, fp.Filepath)
}
if err == nil {
contents = c
Expand Down Expand Up @@ -337,7 +338,7 @@ func ServeAsset(fname string, opts *storage.ImgProcessOpts, fromImgs bool, w htt
// TODO: this could probably be cleaned up more
// imgs wont have a project directory
projectDir := ""
var bucket storage.Bucket
var bucket sst.Bucket
// imgs has a different bucket directory
if fromImgs {
bucket, err = st.GetBucket(shared.GetImgsBucketName(user.ID))
Expand Down Expand Up @@ -400,7 +401,7 @@ func StartApiServer() {
db := postgres.NewDB(cfg.DbURL, cfg.Logger)
defer db.Close()

var st storage.ObjectStorage
var st storage.StorageServe
var err error
if cfg.MinioURL == "" {
st, err = storage.NewStorageFS(cfg.StorageDir)
Expand Down
Loading

0 comments on commit 15dc5a8

Please sign in to comment.