diff --git a/cmd/CatsOfMastodonBotGo/main.go b/cmd/CatsOfMastodonBotGo/main.go index 996f2af..ee37dd9 100644 --- a/cmd/CatsOfMastodonBotGo/main.go +++ b/cmd/CatsOfMastodonBotGo/main.go @@ -21,6 +21,9 @@ func main() { services.InitPostService() + // Not needed but anyways + services.InitImgKitHelper() + ticker := time.NewTicker(10 * time.Minute) runFetchPosts := func() { diff --git a/internal/config/config.go b/internal/config/config.go index 03bf16b..789e125 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -22,6 +22,8 @@ type config struct { DBUser string DBPassword string DBName string + + ImageKitId string } var Config *config @@ -65,7 +67,6 @@ func Load() *config { audience = "CatsOfMastodonBotGo" } - dbEngine := os.Getenv("CAOM_DB_ENGINE") dbHost := os.Getenv("CAOM_DB_HOST") dbPort := os.Getenv("CAOM_DB_PORT") @@ -82,6 +83,11 @@ func Load() *config { dbPassword = "" dbName = "caom.db" } + + imageKitId := os.Getenv("CAOM_IMAGEKIT_ID") + if imageKitId == "" { + slog.Info("No imagekit id provided, not using imagekit") + } // Inititlize AppContext var appContext = &config{ AdminPassword: adminPassword, @@ -98,6 +104,8 @@ func Load() *config { DBUser: dbUser, DBPassword: dbPassword, DBName: dbName, + + ImageKitId: imageKitId, } return appContext diff --git a/internal/services/imgKitHelper.go b/internal/services/imgKitHelper.go new file mode 100644 index 0000000..3c5801b --- /dev/null +++ b/internal/services/imgKitHelper.go @@ -0,0 +1,20 @@ +package services + +import "CatsOfMastodonBotGo/internal/config" + +type ImgKitHelper struct { +} + +var ImgKitHelperInstance *ImgKitHelper + +func InitImgKitHelper() { + ImgKitHelperInstance = &ImgKitHelper{} +} + +func GetPreviewUrl(url string) string { + return "https://ik.imagekit.io/" + config.Config.ImageKitId + "/tr:w-500,h-500,c-at_max,f-webp,q-60/" + url +} + +func GetRemoteUrl(url string) string { + return "https://ik.imagekit.io/" + config.Config.ImageKitId + "/tr:q-70,dpr-auto,f-webp/" + url +} diff --git a/internal/web/handlers/adminDash.go b/internal/web/handlers/adminDash.go index 396c199..d4b9517 100644 --- a/internal/web/handlers/adminDash.go +++ b/internal/web/handlers/adminDash.go @@ -53,6 +53,8 @@ func (ps *AdminDashboardHandler) RejectMedia(c *gin.Context) { func (ps *AdminDashboardHandler) GetMedia(c *gin.Context) { media := ps.PostService.GetMedia() + media.PreviewUrl = services.GetPreviewUrl(media.PreviewUrl) + media.RemoteUrl = services.GetPreviewUrl(media.RemoteUrl) c.JSON(http.StatusOK, media) } diff --git a/internal/web/handlers/apiEndpoint.go b/internal/web/handlers/apiEndpoint.go index 8eb760b..5b5c74c 100644 --- a/internal/web/handlers/apiEndpoint.go +++ b/internal/web/handlers/apiEndpoint.go @@ -16,9 +16,14 @@ func InitApiEndpointHandler() { ApiEndpointHandlerInstance = &ApiEndpointHandler{ PostService: *services.PostServiceInstance, } - + } func (ps *ApiEndpointHandler) GetRandomPost(c *gin.Context) { - c.JSON(200,ps.PostService.GetRandomPost()) -} \ No newline at end of file + post := ps.PostService.GetRandomPost() + for _, attachment := range post.Attachments { + attachment.RemoteUrl = services.GetRemoteUrl(attachment.RemoteUrl) + attachment.PreviewUrl = services.GetPreviewUrl(attachment.RemoteUrl) + } + c.JSON(200, post) +} diff --git a/internal/web/handlers/embedCard.go b/internal/web/handlers/embedCard.go index 01951ca..d414772 100644 --- a/internal/web/handlers/embedCard.go +++ b/internal/web/handlers/embedCard.go @@ -22,6 +22,6 @@ func (ps *EmbedCardHandler) GetEmbedCard(c *gin.Context) { post := ps.PostService.GetRandomPost() c.HTML(200, "home/embed.html", gin.H{ "postUrl": post.Url, - "imageUrl": post.Attachments[0].RemoteUrl, + "imageUrl": services.GetRemoteUrl(post.Attachments[0].RemoteUrl), }) }