Add Twitter video support and use sql.Null[T] for nullable columns
- Store Twitter video URLs in embed_video_url column instead of downloading - Play videos directly from Twitter's CDN in <video> element - Fix Twitter API parsing (content_type/url fields) - Strip t.co URLs from tweet text descriptions - Use sql.Null[T] generic type for nullable DB columns - Add Nullable[T] and Ptr[T] helper functions - Add play indicator overlay for video items in grid - Add migration for embed_video_url column
This commit is contained in:
parent
cdcc5b5293
commit
2887d9c430
8 changed files with 226 additions and 120 deletions
|
|
@ -2,6 +2,7 @@ package handlers
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
|
@ -17,6 +18,22 @@ import (
|
|||
"lookbook/internal/video"
|
||||
)
|
||||
|
||||
// strOrNil returns nil if s is empty, otherwise a pointer to s.
|
||||
func strOrNil(s string) *string {
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
return &s
|
||||
}
|
||||
|
||||
// nullStr creates a sql.Null[string] from a string (empty string = invalid).
|
||||
func nullStr(s string) sql.Null[string] {
|
||||
if s == "" {
|
||||
return sql.Null[string]{}
|
||||
}
|
||||
return sql.Null[string]{V: s, Valid: true}
|
||||
}
|
||||
|
||||
type urlMetadata struct {
|
||||
Title string
|
||||
Description string
|
||||
|
|
@ -26,9 +43,9 @@ type urlMetadata struct {
|
|||
Provider string
|
||||
VideoID string
|
||||
EmbedHTML string
|
||||
VideoURL string // Direct video URL (for Twitter)
|
||||
}
|
||||
|
||||
// fetchURLMetadata fetches metadata for a URL, trying embed detection first, then OpenGraph
|
||||
func fetchURLMetadata(ctx context.Context, url string) (*urlMetadata, error) {
|
||||
// Check if it's a YouTube/Vimeo/Twitter embed
|
||||
videoInfo, err := embed.Detect(ctx, url)
|
||||
|
|
@ -41,6 +58,7 @@ func fetchURLMetadata(ctx context.Context, url string) (*urlMetadata, error) {
|
|||
Provider: string(videoInfo.Provider),
|
||||
VideoID: videoInfo.VideoID,
|
||||
EmbedHTML: videoInfo.EmbedHTML,
|
||||
VideoURL: videoInfo.VideoURL,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -151,33 +169,34 @@ func HandleCreateFromLink(rc *RequestContext, w http.ResponseWriter, r *http.Req
|
|||
itemType = "link"
|
||||
}
|
||||
|
||||
// For embeds, fetch thumbnail and video URL
|
||||
var imageURL, videoURL string
|
||||
if req.ImageURL != nil {
|
||||
imageURL = *req.ImageURL
|
||||
}
|
||||
if itemType == "embed" && embedProvider != nil {
|
||||
if videoInfo, err := embed.Detect(ctx, req.URL); err == nil && videoInfo != nil {
|
||||
imageURL = videoInfo.ThumbnailURL
|
||||
videoURL = videoInfo.VideoURL
|
||||
}
|
||||
}
|
||||
|
||||
// Create the item
|
||||
it, err := item.QCreate(ctx, rc.DB, item.CreateParams{
|
||||
Title: req.Title,
|
||||
Description: req.Description,
|
||||
LinkURL: &req.URL,
|
||||
Title: item.Nullable(req.Title),
|
||||
Description: item.Nullable(req.Description),
|
||||
LinkURL: item.Nullable(&req.URL),
|
||||
ItemType: itemType,
|
||||
EmbedProvider: embedProvider,
|
||||
EmbedVideoID: embedVideoID,
|
||||
EmbedHTML: embedHTML,
|
||||
EmbedProvider: item.Nullable(embedProvider),
|
||||
EmbedVideoID: item.Nullable(embedVideoID),
|
||||
EmbedHTML: item.Nullable(embedHTML),
|
||||
EmbedVideoURL: nullStr(videoURL),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Download and store image if available
|
||||
var imageURL string
|
||||
if req.ImageURL != nil {
|
||||
imageURL = *req.ImageURL
|
||||
}
|
||||
|
||||
// For embeds, fetch thumbnail
|
||||
if itemType == "embed" && embedProvider != nil {
|
||||
if videoInfo, err := embed.Detect(ctx, req.URL); err == nil && videoInfo != nil {
|
||||
imageURL = videoInfo.ThumbnailURL
|
||||
}
|
||||
}
|
||||
|
||||
// Download and store thumbnail
|
||||
if imageURL != "" {
|
||||
imgData, contentType, err := opengraph.DownloadImage(ctx, imageURL)
|
||||
if err != nil {
|
||||
|
|
@ -277,17 +296,9 @@ func HandleUpload(rc *RequestContext, w http.ResponseWriter, r *http.Request) er
|
|||
}
|
||||
|
||||
// Create item
|
||||
var titlePtr, descPtr *string
|
||||
if title != "" {
|
||||
titlePtr = &title
|
||||
}
|
||||
if description != "" {
|
||||
descPtr = &description
|
||||
}
|
||||
|
||||
it, err := item.QCreate(ctx, rc.DB, item.CreateParams{
|
||||
Title: titlePtr,
|
||||
Description: descPtr,
|
||||
Title: item.Nullable(strOrNil(title)),
|
||||
Description: item.Nullable(strOrNil(description)),
|
||||
ItemType: itemType,
|
||||
})
|
||||
if err != nil {
|
||||
|
|
@ -359,9 +370,9 @@ func HandleCreateQuote(rc *RequestContext, w http.ResponseWriter, r *http.Reques
|
|||
defer cancel()
|
||||
|
||||
it, err := item.QCreate(ctx, rc.DB, item.CreateParams{
|
||||
Title: req.Source,
|
||||
Description: &req.Text,
|
||||
LinkURL: req.SourceURL,
|
||||
Title: item.Nullable(req.Source),
|
||||
Description: nullStr(req.Text),
|
||||
LinkURL: item.Nullable(req.SourceURL),
|
||||
ItemType: "quote",
|
||||
})
|
||||
if err != nil {
|
||||
|
|
@ -517,32 +528,28 @@ func HandleRefreshMetadata(rc *RequestContext, w http.ResponseWriter, r *http.Re
|
|||
return writeJSON(w, http.StatusNotFound, map[string]string{"error": "not found"})
|
||||
}
|
||||
|
||||
if it.LinkURL == nil || *it.LinkURL == "" {
|
||||
if !it.LinkURL.Valid || it.LinkURL.V == "" {
|
||||
return writeJSON(w, http.StatusBadRequest, map[string]string{"error": "item has no link URL"})
|
||||
}
|
||||
|
||||
meta, err := fetchURLMetadata(ctx, *it.LinkURL)
|
||||
meta, err := fetchURLMetadata(ctx, it.LinkURL.V)
|
||||
if err != nil {
|
||||
return writeJSON(w, http.StatusBadRequest, map[string]string{"error": fmt.Sprintf("failed to fetch: %v", err)})
|
||||
}
|
||||
|
||||
// Update title and description
|
||||
var titlePtr, descPtr *string
|
||||
if meta.Title != "" {
|
||||
titlePtr = &meta.Title
|
||||
}
|
||||
if meta.Description != "" {
|
||||
descPtr = &meta.Description
|
||||
}
|
||||
// Update title, description, and video URL
|
||||
item.QUpdate(ctx, rc.DB, it.ID, item.UpdateParams{
|
||||
Title: titlePtr,
|
||||
Description: descPtr,
|
||||
Title: nullStr(meta.Title),
|
||||
Description: nullStr(meta.Description),
|
||||
LinkURL: it.LinkURL,
|
||||
})
|
||||
if meta.VideoURL != "" {
|
||||
item.QUpdateVideoURL(ctx, rc.DB, it.ID, meta.VideoURL)
|
||||
}
|
||||
|
||||
// Download and replace thumbnail
|
||||
if meta.ImageURL != "" {
|
||||
// Delete existing media for this item
|
||||
// Delete existing thumbnails
|
||||
media.QDeleteByItemID(ctx, rc.DB, it.ID)
|
||||
|
||||
imgData, contentType, err := opengraph.DownloadImage(ctx, meta.ImageURL)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue