diff options
author | FChannel <=> | 2021-01-17 20:45:07 -0800 |
---|---|---|
committer | FChannel <=> | 2021-01-17 20:45:07 -0800 |
commit | 9dc91965b1886e813aa29f5f7fb6512e2af29858 (patch) | |
tree | 14f80f35b7e6cfba1471e213ce9fd62dc97961e6 /main.go | |
parent | 89f88072c9ffd6a6e321ee7b5455665d82dbd6d7 (diff) |
support for preview images
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 56 |
1 files changed, 56 insertions, 0 deletions
@@ -551,6 +551,47 @@ func ProcessActivity(db *sql.DB, activity Activity) { } } +func CreatePreviewObject(obj ObjectBase) *NestedObjectBase { + + re := regexp.MustCompile(`/.+$`) + + mimetype := re.ReplaceAllString(obj.MediaType, "") + + var nPreview NestedObjectBase + + if mimetype != "image" { + return &nPreview + } + + re = regexp.MustCompile(`.+/`) + + file := re.ReplaceAllString(obj.MediaType, "") + + href := GetUniqueFilename(file) + + nPreview.Type = "Preview" + nPreview.Name = obj.Name + nPreview.Href = Domain + "" + href + nPreview.MediaType = obj.MediaType + nPreview.Size = obj.Size + nPreview.Published = obj.Published + + re = regexp.MustCompile(`/public/.+`) + + objFile := re.FindString(obj.Href) + + cmd := exec.Command("convert", "." + objFile ,"-resize", "250x250", "." + href) + + err := cmd.Run() + + if CheckError(err, "error with resize attachment preview") != nil { + var preview NestedObjectBase + return &preview + } + + return &nPreview +} + func CreateAttachmentObject(file multipart.File, header *multipart.FileHeader) ([]ObjectBase, *os.File) { contentType, _ := GetFileContentType(file) filename := header.Filename @@ -1021,3 +1062,18 @@ func IsInStringArray(array []string, value string) bool { } +func GetUniqueFilename(_type string) string { + id := RandomID(8) + file := "/public/" + id + "." + _type + + for true { + if _, err := os.Stat("." + file); err == nil { + id = RandomID(8) + file = "/public/" + id + "." + _type + }else{ + return "/public/" + id + "." + _type + } + } + + return "" +} |