From 08fea42e13bd16b7f50ece10eebcbebe8c9509fb Mon Sep 17 00:00:00 2001 From: FChannel <=> Date: Tue, 19 Jan 2021 22:17:57 -0800 Subject: function to make preview image of attachment image --- Database.go | 2 -- main.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/Database.go b/Database.go index 3b4dfc4..d0f6892 100644 --- a/Database.go +++ b/Database.go @@ -420,7 +420,6 @@ func GetObjectRepliesDB(db *sql.DB, parent ObjectBase) (*CollectionBase, int, in err = rows.Scan(&post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.AttributedTo, &attachID, &previewID, &actor.Id) - CheckError(err, "error with replies db scan") post.Actor = &actor @@ -448,7 +447,6 @@ func GetObjectRepliesDB(db *sql.DB, parent ObjectBase) (*CollectionBase, int, in var postc int var imgc int for _, e := range remoteCollection.OrderedItems { - nColl.OrderedItems = append(nColl.OrderedItems, e) postc = postc + 1 if len(e.Attachment) > 0 { diff --git a/main.go b/main.go index e7af54b..8c7ff94 100644 --- a/main.go +++ b/main.go @@ -1127,4 +1127,78 @@ func DeleteObjectAndRepliesRequest(db *sql.DB, id string) { MakeActivityRequest(activity) } +func ResizeAttachmentToPreview(db *sql.DB) { + query := `select id, href, mediatype, name, size, published from activitystream where id in (select attachment from activitystream where attachment!='' and preview='')` + rows, err := db.Query(query) + + CheckError(err, "error getting attachments") + + + defer rows.Close() + for rows.Next() { + + var id string + var href string + var mediatype string + var name string + var size int + var published string + + rows.Scan(&id, &href, &mediatype, &name, &size, &published) + + re := regexp.MustCompile(`^\w+`) + + _type := re.FindString(mediatype) + + if _type == "image" { + + re = regexp.MustCompile(`.+/`) + + file := re.ReplaceAllString(mediatype, "") + + nHref := GetUniqueFilename(file) + + var nPreview NestedObjectBase + + re = regexp.MustCompile(`/\w+$`) + actor := re.ReplaceAllString(id, "") + + nPreview.Type = "Preview" + nPreview.Id = fmt.Sprintf("%s/%s", actor, CreateUniqueID(db, actor)) + nPreview.Name = name + nPreview.Href = 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>", "." + nHref) + + err := cmd.Run() + + CheckError(err, "error with resize attachment preview") + + if err == nil { + fmt.Println(objFile + " -> " + nHref) + WritePreviewToDB(db, nPreview) + UpdateObjectWithPreview(db, id, nPreview.Id) + } + } + } + } +} + +func UpdateObjectWithPreview(db *sql.DB, id string, preview string) { + query := `update activitystream set preview=$1 where attachment=$2` + + _, err := db.Exec(query, preview, id) + + CheckError(err, "could not update activity stream with preview") + +} -- cgit v1.2.3