diff options
author | FChannel <> | 2022-05-02 12:07:00 -0700 |
---|---|---|
committer | FChannel <> | 2022-06-19 12:53:29 -0700 |
commit | 733f911fadc872933481bcbe087d519ce00372df (patch) | |
tree | 54788da4f730bd11934401c1eca48a3c87f08027 /post/util.go | |
parent | e80fe14f7985f9e85bfb9582926acd7891455786 (diff) |
cleaned up main
Diffstat (limited to 'post/util.go')
-rw-r--r-- | post/util.go | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/post/util.go b/post/util.go index a9d326a..c653276 100644 --- a/post/util.go +++ b/post/util.go @@ -1,6 +1,7 @@ package post import ( + "fmt" "io/ioutil" "mime/multipart" "os" @@ -323,3 +324,61 @@ func ObjectFromForm(ctx *fiber.Ctx, obj activitypub.ObjectBase) (activitypub.Obj return obj, nil } + +func ResizeAttachmentToPreview() error { + return activitypub.GetObjectsWithoutPreviewsCallback(func(id, href, mediatype, name string, size int, published time.Time) error { + re := regexp.MustCompile(`^\w+`) + + _type := re.FindString(mediatype) + + if _type == "image" { + + re = regexp.MustCompile(`.+/`) + + file := re.ReplaceAllString(mediatype, "") + + nHref := util.GetUniqueFilename(file) + + var nPreview activitypub.NestedObjectBase + + re = regexp.MustCompile(`/\w+$`) + actor := re.ReplaceAllString(id, "") + + nPreview.Type = "Preview" + uid, err := util.CreateUniqueID(actor) + if err != nil { + return err + } + + nPreview.Id = fmt.Sprintf("%s/%s", actor, uid) + nPreview.Name = name + nPreview.Href = config.Domain + "" + nHref + nPreview.MediaType = mediatype + nPreview.Size = int64(size) + nPreview.Published = published + nPreview.Updated = published + + re = regexp.MustCompile(`/public/.+`) + + objFile := re.FindString(href) + + if id != "" { + cmd := exec.Command("convert", "."+objFile, "-resize", "250x250>", "-strip", "."+nHref) + + if err := cmd.Run(); err == nil { + fmt.Println(objFile + " -> " + nHref) + if err := activitypub.WritePreviewToDB(nPreview); err != nil { + return err + } + if err := activitypub.UpdateObjectWithPreview(id, nPreview.Id); err != nil { + return err + } + } else { + return err + } + } + } + + return nil + }) +} |