package handlers import ( "net/http" "strings" "ai-media-hub/backend/models" "ai-media-hub/backend/services" "github.com/gin-gonic/gin" ) func (a *App) searchGiphy(c *gin.Context) { if a.GiphyService == nil || !a.GiphyService.Config.Enabled { c.JSON(http.StatusServiceUnavailable, gin.H{"error": "giphy search is disabled"}) return } var req struct { Query string `json:"query"` MaxResults int `json:"maxResults"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } req.Query = strings.TrimSpace(req.Query) if req.Query == "" { c.JSON(http.StatusBadRequest, gin.H{"error": "query is required"}) return } a.Hub.Broadcast("progress", gin.H{"type": "giphy-search", "status": "expanding query for GIPHY", "progress": 20}) resp, err := a.GiphyService.SearchImages(req.Query, req.MaxResults) if err != nil { a.debug("giphy:search_error", gin.H{"query": req.Query, "error": err.Error()}) a.Hub.Broadcast("progress", gin.H{"type": "giphy-search", "status": "giphy search failed", "progress": 100, "message": err.Error()}) c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()}) return } a.debug("giphy:search_complete", gin.H{ "query": req.Query, "expandedQueries": resp.ExpandedQueries, "total": resp.Total, "warning": resp.Warning, }) a.Hub.Broadcast("progress", gin.H{"type": "giphy-search", "status": "giphy search complete", "progress": 100, "count": resp.Total}) c.JSON(http.StatusOK, resp) } func (a *App) downloadGiphy(c *gin.Context) { if a.GiphyService == nil || !a.GiphyService.Config.Enabled { c.JSON(http.StatusServiceUnavailable, gin.H{"ok": false, "error": "GIPHY_DISABLED", "message": "GIPHY download is disabled"}) return } var req struct { ProviderID string `json:"providerId"` Title string `json:"title"` DownloadURL string `json:"downloadUrl"` OriginalQuery string `json:"originalQuery"` SelectedExpansionQuery string `json:"selectedExpansionQuery"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"ok": false, "error": "INVALID_REQUEST", "message": err.Error()}) return } resp, err := a.GiphyService.DownloadMedia(services.GiphyDownloadRequest{ ProviderID: req.ProviderID, Title: req.Title, DownloadURL: req.DownloadURL, OriginalQuery: req.OriginalQuery, SelectedExpansionQuery: req.SelectedExpansionQuery, }) if err != nil { a.debug("giphy:download_error", gin.H{ "providerId": req.ProviderID, "title": req.Title, "error": err.Error(), }) status := http.StatusBadGateway if resp.Error == "INVALID_REQUEST" || resp.Error == "INVALID_DOWNLOAD_URL" { status = http.StatusBadRequest } c.JSON(status, resp) return } if a.DB != nil { if recordID, dbErr := models.InsertDownload(a.DB, req.DownloadURL, "GIPHY", resp.SavedPath, "completed"); dbErr == nil { _ = models.MarkDownloadCompleted(a.DB, recordID, "completed") } } a.Hub.Broadcast("progress", gin.H{"type": "giphy-download", "status": "giphy download complete", "progress": 100, "fileName": resp.FileName}) c.JSON(http.StatusOK, resp) }