Started working on the view upload page

This commit is contained in:
Leon Mika 2026-03-02 22:26:40 +11:00
parent 0a9af9cde8
commit 48f39133d7
18 changed files with 327 additions and 45 deletions

View file

@ -5,7 +5,7 @@
package sqlgen
type PendingUpload struct {
ID interface{}
ID int64
SiteID int64
Guid string
UserID int64
@ -50,12 +50,13 @@ type Site struct {
}
type Upload struct {
ID interface{}
ID int64
SiteID int64
Guid string
MimeType string
Filename string
FileSize int64
Slug string
Alt string
CreatedAt int64
}

View file

@ -41,7 +41,7 @@ type InsertPendingUploadParams struct {
UploadStartedAt int64
}
func (q *Queries) InsertPendingUpload(ctx context.Context, arg InsertPendingUploadParams) (interface{}, error) {
func (q *Queries) InsertPendingUpload(ctx context.Context, arg InsertPendingUploadParams) (int64, error) {
row := q.db.QueryRowContext(ctx, insertPendingUpload,
arg.SiteID,
arg.Guid,
@ -51,7 +51,7 @@ func (q *Queries) InsertPendingUpload(ctx context.Context, arg InsertPendingUplo
arg.MimeType,
arg.UploadStartedAt,
)
var id interface{}
var id int64
err := row.Scan(&id)
return id, err
}

View file

@ -13,7 +13,7 @@ const deleteUpload = `-- name: DeleteUpload :exec
DELETE FROM uploads WHERE id = ?
`
func (q *Queries) DeleteUpload(ctx context.Context, id interface{}) error {
func (q *Queries) DeleteUpload(ctx context.Context, id int64) error {
_, err := q.db.ExecContext(ctx, deleteUpload, id)
return err
}
@ -24,9 +24,11 @@ INSERT INTO uploads (
guid,
mime_type,
filename,
created_at,
alt
) VALUES (?, ?, ?, ?, ?, ?)
file_size,
slug,
alt,
created_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
RETURNING id
`
@ -35,8 +37,10 @@ type InsertUploadParams struct {
Guid string
MimeType string
Filename string
CreatedAt int64
FileSize int64
Slug string
Alt string
CreatedAt int64
}
func (q *Queries) InsertUpload(ctx context.Context, arg InsertUploadParams) error {
@ -45,17 +49,19 @@ func (q *Queries) InsertUpload(ctx context.Context, arg InsertUploadParams) erro
arg.Guid,
arg.MimeType,
arg.Filename,
arg.CreatedAt,
arg.FileSize,
arg.Slug,
arg.Alt,
arg.CreatedAt,
)
return err
}
const selectUploadByID = `-- name: SelectUploadByID :one
SELECT id, site_id, guid, mime_type, filename, file_size, alt, created_at FROM uploads WHERE id = ?
SELECT id, site_id, guid, mime_type, filename, file_size, slug, alt, created_at FROM uploads WHERE id = ? LIMIT 1
`
func (q *Queries) SelectUploadByID(ctx context.Context, id interface{}) (Upload, error) {
func (q *Queries) SelectUploadByID(ctx context.Context, id int64) (Upload, error) {
row := q.db.QueryRowContext(ctx, selectUploadByID, id)
var i Upload
err := row.Scan(
@ -65,6 +71,33 @@ func (q *Queries) SelectUploadByID(ctx context.Context, id interface{}) (Upload,
&i.MimeType,
&i.Filename,
&i.FileSize,
&i.Slug,
&i.Alt,
&i.CreatedAt,
)
return i, err
}
const selectUploadBySiteIDAndSlug = `-- name: SelectUploadBySiteIDAndSlug :one
SELECT id, site_id, guid, mime_type, filename, file_size, slug, alt, created_at FROM uploads WHERE site_id = ? AND slug = ? LIMIT 1
`
type SelectUploadBySiteIDAndSlugParams struct {
SiteID int64
Slug string
}
func (q *Queries) SelectUploadBySiteIDAndSlug(ctx context.Context, arg SelectUploadBySiteIDAndSlugParams) (Upload, error) {
row := q.db.QueryRowContext(ctx, selectUploadBySiteIDAndSlug, arg.SiteID, arg.Slug)
var i Upload
err := row.Scan(
&i.ID,
&i.SiteID,
&i.Guid,
&i.MimeType,
&i.Filename,
&i.FileSize,
&i.Slug,
&i.Alt,
&i.CreatedAt,
)
@ -72,7 +105,7 @@ func (q *Queries) SelectUploadByID(ctx context.Context, id interface{}) (Upload,
}
const selectUploadsOfSite = `-- name: SelectUploadsOfSite :many
SELECT id, site_id, guid, mime_type, filename, file_size, alt, created_at FROM uploads WHERE site_id = ? ORDER BY created_at DESC
SELECT id, site_id, guid, mime_type, filename, file_size, slug, alt, created_at FROM uploads WHERE site_id = ? ORDER BY created_at DESC
`
func (q *Queries) SelectUploadsOfSite(ctx context.Context, siteID int64) ([]Upload, error) {
@ -91,6 +124,7 @@ func (q *Queries) SelectUploadsOfSite(ctx context.Context, siteID int64) ([]Uplo
&i.MimeType,
&i.Filename,
&i.FileSize,
&i.Slug,
&i.Alt,
&i.CreatedAt,
); err != nil {
@ -113,7 +147,7 @@ UPDATE uploads SET alt = ? WHERE id = ?
type UpdateUploadParams struct {
Alt string
ID interface{}
ID int64
}
func (q *Queries) UpdateUpload(ctx context.Context, arg UpdateUploadParams) error {

View file

@ -30,6 +30,18 @@ func (db *Provider) SelectUploadsOfSite(ctx context.Context, siteID int64) ([]mo
return uploads, nil
}
func (db *Provider) SelectUploadBySiteIDAndSlug(ctx context.Context, siteID int64, slug string) (models.Upload, error) {
row, err := db.queries.SelectUploadBySiteIDAndSlug(ctx, sqlgen.SelectUploadBySiteIDAndSlugParams{
SiteID: siteID,
Slug: slug,
})
if err != nil {
return models.Upload{}, err
}
return dbUploadToUpload(row), nil
}
func (db *Provider) SaveUpload(ctx context.Context, upload *models.Upload) error {
if upload.ID == 0 {
if err := db.queries.InsertUpload(ctx, sqlgen.InsertUploadParams{
@ -37,8 +49,10 @@ func (db *Provider) SaveUpload(ctx context.Context, upload *models.Upload) error
Guid: upload.GUID,
MimeType: upload.MIMEType,
Filename: upload.Filename,
CreatedAt: upload.CreatedAt.Unix(),
FileSize: upload.FileSize,
Slug: upload.Slug,
Alt: upload.Alt,
CreatedAt: upload.CreatedAt.Unix(),
}); err != nil {
return err
}
@ -82,17 +96,14 @@ func (db *Provider) DeletePendingUpload(ctx context.Context, guid string) error
}
func dbUploadToUpload(row sqlgen.Upload) models.Upload {
var id int64
if idVal, ok := row.ID.(int64); ok {
id = idVal
}
return models.Upload{
ID: id,
ID: row.ID,
SiteID: row.SiteID,
GUID: row.Guid,
MIMEType: row.MimeType,
FileSize: row.FileSize,
Filename: row.Filename,
Slug: row.Slug,
Alt: row.Alt,
CreatedAt: time.Unix(row.CreatedAt, 0).UTC(),
}

View file

@ -0,0 +1,32 @@
package uploadfiles
import (
"emperror.dev/errors"
"github.com/barasher/go-exiftool"
"lmika.dev/lmika/weiro/models"
)
func (p *Provider) StripeEXIFData(site models.Site, up models.Upload) error {
uploadFilename := p.uploadFileName(site, up)
et, err := exiftool.NewExiftool()
if err != nil {
return err
}
defer et.Close()
fileInfos := et.ExtractMetadata(uploadFilename)
if len(fileInfos) == 0 {
return errors.New("no exif data found")
}
fileInfo := fileInfos[0]
fileInfo.ClearAll()
fileOut := []exiftool.FileMetadata{fileInfo}
et.WriteMetadata(fileOut)
if fileOut[0].Err != nil {
return fileOut[0].Err
}
return nil
}

View file

@ -1,7 +1,7 @@
package uploadfiles
import (
"fmt"
"io"
"os"
"path/filepath"
@ -19,17 +19,24 @@ func New(baseDir string) *Provider {
}
func (p *Provider) AdoptFile(site models.Site, up models.Upload, filename string) error {
baseDir := filepath.Join(p.baseDir, site.GUID,
fmt.Sprintf("%04d", up.CreatedAt.Year()),
fmt.Sprintf("%02d", up.CreatedAt.Month()))
fullPath := p.uploadFileName(site, up)
baseDir := filepath.Dir(fullPath)
if err := os.MkdirAll(baseDir, 0755); err != nil {
return err
}
targetFilename := filepath.Join(baseDir, up.GUID)
if err := os.Rename(filename, targetFilename); err != nil {
if err := os.Rename(filename, fullPath); err != nil {
return err
}
return nil
}
func (p *Provider) OpenUpload(site models.Site, up models.Upload) (io.ReadCloser, error) {
fullPath := p.uploadFileName(site, up)
return os.Open(fullPath)
}
func (p *Provider) uploadFileName(site models.Site, up models.Upload) string {
return filepath.Join(p.baseDir, site.GUID, up.Slug)
}