Started a repository of the uploads
This commit is contained in:
parent
6b697e008f
commit
0a9af9cde8
11 changed files with 101 additions and 15 deletions
|
|
@ -55,6 +55,7 @@ type Upload struct {
|
|||
Guid string
|
||||
MimeType string
|
||||
Filename string
|
||||
FileSize int64
|
||||
Alt string
|
||||
CreatedAt int64
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,15 @@ import (
|
|||
"context"
|
||||
)
|
||||
|
||||
const deletePendingUpload = `-- name: DeletePendingUpload :exec
|
||||
DELETE FROM pending_uploads WHERE guid = ?
|
||||
`
|
||||
|
||||
func (q *Queries) DeletePendingUpload(ctx context.Context, guid string) error {
|
||||
_, err := q.db.ExecContext(ctx, deletePendingUpload, guid)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertPendingUpload = `-- name: InsertPendingUpload :one
|
||||
INSERT INTO pending_uploads (
|
||||
site_id,
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ func (q *Queries) InsertUpload(ctx context.Context, arg InsertUploadParams) erro
|
|||
}
|
||||
|
||||
const selectUploadByID = `-- name: SelectUploadByID :one
|
||||
SELECT id, site_id, guid, mime_type, filename, alt, created_at FROM uploads WHERE id = ?
|
||||
SELECT id, site_id, guid, mime_type, filename, file_size, alt, created_at FROM uploads WHERE id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) SelectUploadByID(ctx context.Context, id interface{}) (Upload, error) {
|
||||
|
|
@ -64,6 +64,7 @@ func (q *Queries) SelectUploadByID(ctx context.Context, id interface{}) (Upload,
|
|||
&i.Guid,
|
||||
&i.MimeType,
|
||||
&i.Filename,
|
||||
&i.FileSize,
|
||||
&i.Alt,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
|
|
@ -71,7 +72,7 @@ func (q *Queries) SelectUploadByID(ctx context.Context, id interface{}) (Upload,
|
|||
}
|
||||
|
||||
const selectUploadsOfSite = `-- name: SelectUploadsOfSite :many
|
||||
SELECT id, site_id, guid, mime_type, filename, alt, created_at FROM uploads WHERE site_id = ? ORDER BY created_at DESC
|
||||
SELECT id, site_id, guid, mime_type, filename, file_size, alt, created_at FROM uploads WHERE site_id = ? ORDER BY created_at DESC
|
||||
`
|
||||
|
||||
func (q *Queries) SelectUploadsOfSite(ctx context.Context, siteID int64) ([]Upload, error) {
|
||||
|
|
@ -89,6 +90,7 @@ func (q *Queries) SelectUploadsOfSite(ctx context.Context, siteID int64) ([]Uplo
|
|||
&i.Guid,
|
||||
&i.MimeType,
|
||||
&i.Filename,
|
||||
&i.FileSize,
|
||||
&i.Alt,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ func (db *Provider) SaveUpload(ctx context.Context, upload *models.Upload) error
|
|||
Guid: upload.GUID,
|
||||
MimeType: upload.MIMEType,
|
||||
Filename: upload.Filename,
|
||||
CreatedAt: upload.CreatedAt,
|
||||
CreatedAt: upload.CreatedAt.Unix(),
|
||||
Alt: upload.Alt,
|
||||
}); err != nil {
|
||||
return err
|
||||
|
|
@ -77,6 +77,10 @@ func (db *Provider) SavePendingUpload(ctx context.Context, pending *models.Pendi
|
|||
return err
|
||||
}
|
||||
|
||||
func (db *Provider) DeletePendingUpload(ctx context.Context, guid string) error {
|
||||
return db.queries.DeletePendingUpload(ctx, guid)
|
||||
}
|
||||
|
||||
func dbUploadToUpload(row sqlgen.Upload) models.Upload {
|
||||
var id int64
|
||||
if idVal, ok := row.ID.(int64); ok {
|
||||
|
|
@ -90,7 +94,7 @@ func dbUploadToUpload(row sqlgen.Upload) models.Upload {
|
|||
MIMEType: row.MimeType,
|
||||
Filename: row.Filename,
|
||||
Alt: row.Alt,
|
||||
CreatedAt: row.CreatedAt,
|
||||
CreatedAt: time.Unix(row.CreatedAt, 0).UTC(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
35
providers/uploadfiles/provider.go
Normal file
35
providers/uploadfiles/provider.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package uploadfiles
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
)
|
||||
|
||||
type Provider struct {
|
||||
baseDir string
|
||||
}
|
||||
|
||||
func New(baseDir string) *Provider {
|
||||
return &Provider{
|
||||
baseDir: baseDir,
|
||||
}
|
||||
}
|
||||
|
||||
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()))
|
||||
|
||||
if err := os.MkdirAll(baseDir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
targetFilename := filepath.Join(baseDir, up.GUID)
|
||||
if err := os.Rename(filename, targetFilename); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue