Added imagekit optimization support

This commit is contained in:
2025-07-29 17:52:39 +03:30
parent 48b893a403
commit 051408fcdd
6 changed files with 43 additions and 5 deletions

View File

@@ -21,6 +21,9 @@ func main() {
services.InitPostService()
// Not needed but anyways
services.InitImgKitHelper()
ticker := time.NewTicker(10 * time.Minute)
runFetchPosts := func() {

View File

@@ -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

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -16,9 +16,14 @@ func InitApiEndpointHandler() {
ApiEndpointHandlerInstance = &ApiEndpointHandler{
PostService: *services.PostServiceInstance,
}
}
func (ps *ApiEndpointHandler) GetRandomPost(c *gin.Context) {
c.JSON(200,ps.PostService.GetRandomPost())
}
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)
}

View File

@@ -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),
})
}