Pinterest-style visual bookmarking app with: - URL metadata extraction (OG/Twitter meta, oEmbed fallback) - Image caching in Postgres with 480px thumbnails - Multi-tag filtering with Ctrl/Cmd for OR mode - Fuzzy tag suggestions and inline tag editing - Browser console auth() with first-use password setup - Brutalist UI with Commit Mono font and Pico CSS - Light/dark mode via browser preference
119 lines
2.9 KiB
Go
119 lines
2.9 KiB
Go
package image
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"time"
|
|
)
|
|
|
|
type Row struct {
|
|
ID int64
|
|
ItemID int64
|
|
OriginalURL string
|
|
ContentType string
|
|
Bytes []byte
|
|
Width int
|
|
Height int
|
|
IsThumb bool
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
type Ref struct {
|
|
ID int64
|
|
ItemID int64
|
|
IsThumb bool
|
|
}
|
|
|
|
func QListByItem(ctx context.Context, db *sql.DB, itemID int64) ([]Row, error) {
|
|
rows, err := db.QueryContext(ctx, `
|
|
SELECT id, item_id, original_url, content_type, bytes, width, height, is_thumb, created_at
|
|
FROM image
|
|
WHERE item_id = $1
|
|
ORDER BY is_thumb DESC, id ASC
|
|
`, itemID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var items []Row
|
|
for rows.Next() {
|
|
var row Row
|
|
if err := rows.Scan(&row.ID, &row.ItemID, &row.OriginalURL, &row.ContentType, &row.Bytes, &row.Width, &row.Height, &row.IsThumb, &row.CreatedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, row)
|
|
}
|
|
return items, rows.Err()
|
|
}
|
|
|
|
func QListPrimaryRefsByItems(ctx context.Context, db *sql.DB, itemIDs []int64) (map[int64]Ref, error) {
|
|
if len(itemIDs) == 0 {
|
|
return map[int64]Ref{}, nil
|
|
}
|
|
|
|
rows, err := db.QueryContext(ctx, `
|
|
SELECT id, item_id, is_thumb
|
|
FROM image
|
|
WHERE item_id = ANY($1)
|
|
ORDER BY item_id ASC, is_thumb DESC, id ASC
|
|
`, itemIDs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
results := make(map[int64]Ref)
|
|
for rows.Next() {
|
|
var row Ref
|
|
if err := rows.Scan(&row.ID, &row.ItemID, &row.IsThumb); err != nil {
|
|
return nil, err
|
|
}
|
|
if _, exists := results[row.ItemID]; !exists {
|
|
results[row.ItemID] = row
|
|
}
|
|
}
|
|
return results, rows.Err()
|
|
}
|
|
|
|
func QFindByID(ctx context.Context, db *sql.DB, id int64) (*Row, error) {
|
|
var row Row
|
|
err := db.QueryRowContext(ctx, `
|
|
SELECT id, item_id, original_url, content_type, bytes, width, height, is_thumb, created_at
|
|
FROM image
|
|
WHERE id = $1
|
|
LIMIT 1
|
|
`, id).Scan(&row.ID, &row.ItemID, &row.OriginalURL, &row.ContentType, &row.Bytes, &row.Width, &row.Height, &row.IsThumb, &row.CreatedAt)
|
|
if err == sql.ErrNoRows {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &row, nil
|
|
}
|
|
|
|
func QCreate(ctx context.Context, db *sql.DB, itemID int64, originalURL, contentType string, bytes []byte, width, height int, isThumb bool) (Row, error) {
|
|
var row Row
|
|
err := db.QueryRowContext(ctx, `
|
|
INSERT INTO image (item_id, original_url, content_type, bytes, width, height, is_thumb)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
|
RETURNING id, item_id, original_url, content_type, bytes, width, height, is_thumb, created_at
|
|
`, itemID, originalURL, contentType, bytes, width, height, isThumb).Scan(
|
|
&row.ID,
|
|
&row.ItemID,
|
|
&row.OriginalURL,
|
|
&row.ContentType,
|
|
&row.Bytes,
|
|
&row.Width,
|
|
&row.Height,
|
|
&row.IsThumb,
|
|
&row.CreatedAt,
|
|
)
|
|
return row, err
|
|
}
|
|
|
|
func QDeleteByItem(ctx context.Context, db *sql.DB, itemID int64) error {
|
|
_, err := db.ExecContext(ctx, `DELETE FROM image WHERE item_id = $1`, itemID)
|
|
return err
|
|
}
|