From 733f911fadc872933481bcbe087d519ce00372df Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Mon, 2 May 2022 12:07:00 -0700 Subject: cleaned up main --- post/util.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'post') 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 + }) +} -- cgit v1.2.3