Skip to content

Commit

Permalink
chore(prose): migrate images to pgs
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Jan 18, 2025
1 parent f22cdb1 commit ab80ef0
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 231 deletions.
99 changes: 99 additions & 0 deletions cmd/scripts/prose-imgs-migrate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"bytes"
"io"
"log/slog"
"path/filepath"
"time"

"github.com/picosh/pico/db"
"github.com/picosh/pico/db/postgres"
"github.com/picosh/pico/prose"
"github.com/picosh/pico/shared"
"github.com/picosh/pico/shared/storage"
sst "github.com/picosh/pobj/storage"
sendUtils "github.com/picosh/send/utils"
)

func bail(err error) {
if err != nil {
panic(err)
}
}

func upload(logger *slog.Logger, st storage.StorageServe, bucket sst.Bucket, fpath string, rdr io.Reader) error {
toSite := filepath.Join("prose", fpath)
logger.Info("uploading object", "bucket", bucket.Name, "object", toSite)
buf := &bytes.Buffer{}
size, err := io.Copy(buf, rdr)
if err != nil {
return err
}

_, _, err = st.PutObject(bucket, toSite, buf, &sendUtils.FileEntry{
Mtime: time.Now().Unix(),
Size: size,
})
return err
}

func images(logger *slog.Logger, dbh db.DB, st storage.StorageServe, bucket sst.Bucket, user *db.User) error {
posts, err := dbh.FindPostsForUser(&db.Pager{Num: 2000, Page: 0}, user.ID, "imgs")
if err != nil {
return err
}

if len(posts.Data) == 0 {
logger.Info("user does not have any images, skipping")
return nil
}

imgBucket, err := st.GetBucket(shared.GetImgsBucketName(user.ID))
if err != nil {
logger.Info("user does not have an images dir, skipping")
return nil
}

/* imgs, err := st.ListObjects(imgBucket, "/", false)
if err != nil {
return err
} */

for _, posts := range posts.Data {
rdr, _, err := st.GetObject(imgBucket, posts.Filename)
if err != nil {
logger.Error("get object", "err", err)
return err
}
err = upload(logger, st, bucket, posts.Filename, rdr)
if err != nil {
return err
}
}

return nil
}

func main() {
cfg := prose.NewConfigSite()
logger := cfg.Logger
picoDb := postgres.NewDB(cfg.DbURL, logger)
st, err := storage.NewStorageMinio(logger, cfg.MinioURL, cfg.MinioUser, cfg.MinioPass)
bail(err)

users, err := picoDb.FindUsers()
bail(err)

for _, user := range users {
logger.Info("migrating user images", "user", user.Name)

bucket, err := st.UpsertBucket(shared.GetAssetBucketName(user.ID))
bail(err)
_, _ = picoDb.InsertProject(user.ID, "prose", "prose")
err = images(logger, picoDb, st, bucket, user)
if err != nil {
logger.Error("image uploader", "err", err)
}
}
}
12 changes: 8 additions & 4 deletions filehandlers/imgs/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func NewUploadImgHandler(dbpool db.DB, cfg *shared.ConfigSite, storage storage.S
}
}

func (h *UploadImgHandler) getObjectPath(fpath string) string {
return filepath.Join("prose", fpath)
}

func (h *UploadImgHandler) Read(s ssh.Session, entry *sendutils.FileEntry) (os.FileInfo, sendutils.ReaderAtCloser, error) {
user, err := h.DBPool.FindUser(s.Permissions().Extensions["user_id"])
if err != nil {
Expand All @@ -71,12 +75,12 @@ func (h *UploadImgHandler) Read(s ssh.Session, entry *sendutils.FileEntry) (os.F
FModTime: *post.UpdatedAt,
}

bucket, err := h.Storage.GetBucket(user.ID)
bucket, err := h.Storage.GetBucket(shared.GetAssetBucketName(user.ID))
if err != nil {
return nil, nil, err
}

contents, _, err := h.Storage.GetObject(bucket, post.Filename)
contents, _, err := h.Storage.GetObject(bucket, h.getObjectPath(post.Filename))
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -218,13 +222,13 @@ func (h *UploadImgHandler) Delete(s ssh.Session, entry *sendutils.FileEntry) err
return fmt.Errorf("error for %s: %v", filename, err)
}

bucket, err := h.Storage.UpsertBucket(user.ID)
bucket, err := h.Storage.UpsertBucket(shared.GetAssetBucketName(user.ID))
if err != nil {
return err
}

logger.Info("deleting image")
err = h.Storage.DeleteObject(bucket, filename)
err = h.Storage.DeleteObject(bucket, h.getObjectPath(filename))
if err != nil {
return err
}
Expand Down
26 changes: 2 additions & 24 deletions filehandlers/imgs/img.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (h *UploadImgHandler) metaImg(data *PostMetaData) error {
return nil
}

bucket, err := h.Storage.UpsertBucket(data.User.ID)
bucket, err := h.Storage.UpsertBucket(shared.GetAssetBucketName(data.User.ID))
if err != nil {
return err
}
Expand All @@ -58,7 +58,7 @@ func (h *UploadImgHandler) metaImg(data *PostMetaData) error {

fname, _, err := h.Storage.PutObject(
bucket,
data.Filename,
h.getObjectPath(data.Filename),
sendutils.NopReaderAtCloser(reader),
&sendutils.FileEntry{},
)
Expand Down Expand Up @@ -128,18 +128,6 @@ func (h *UploadImgHandler) writeImg(s ssh.Session, data *PostMetaData) error {
logger.Error("post could not create", "err", err.Error())
return fmt.Errorf("error for %s: %v", data.Filename, err)
}

if len(data.Tags) > 0 {
logger.Info(
"found post tags, replacing with old tags",
"tags", strings.Join(data.Tags, ","),
)
err = h.DBPool.ReplaceTagsForPost(data.Tags, data.Post.ID)
if err != nil {
logger.Error("post could not replace tags", "err", err.Error())
return fmt.Errorf("error for %s: %v", data.Filename, err)
}
}
} else {
if data.Shasum == data.Cur.Shasum && modTime.Equal(*data.Cur.UpdatedAt) {
logger.Info("image found, but image is identical, skipping")
Expand Down Expand Up @@ -167,16 +155,6 @@ func (h *UploadImgHandler) writeImg(s ssh.Session, data *PostMetaData) error {
logger.Error("post could not update", "err", err.Error())
return fmt.Errorf("error for %s: %v", data.Filename, err)
}

logger.Info(
"found post tags, replacing with old tags",
"tags", strings.Join(data.Tags, ","),
)
err = h.DBPool.ReplaceTagsForPost(data.Tags, data.Cur.ID)
if err != nil {
logger.Error("post could not replace tags", "err", err.Error())
return fmt.Errorf("error for %s: %v", data.Filename, err)
}
}

return nil
Expand Down
168 changes: 0 additions & 168 deletions imgs/api.go

This file was deleted.

1 change: 0 additions & 1 deletion imgs/html/rss.page.tmpl

This file was deleted.

Empty file removed imgs/public/.gitkeep
Empty file.
2 changes: 0 additions & 2 deletions pgs/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ var imgRegex = regexp.MustCompile("(.+.(?:jpg|jpeg|png|gif|webp|svg))(/.+)")
func (web *WebRouter) AssetRequest(w http.ResponseWriter, r *http.Request) {
fname := r.PathValue("fname")
if imgRegex.MatchString(fname) {
fmt.Println("HIT")
web.ImageRequest(w, r)
return
}
Expand All @@ -415,7 +414,6 @@ func (web *WebRouter) ImageRequest(w http.ResponseWriter, r *http.Request) {
if len(matches) >= 3 {
imgOpts = matches[2]
}
fmt.Println("ZZZ", fname, imgOpts)

opts, err := storage.UriToImgProcessOpts(imgOpts)
if err != nil {
Expand Down
Loading

0 comments on commit ab80ef0

Please sign in to comment.