Staff upload a file for a tenant as a draft (streamed to an S3 bucket, hashed on the way, never buffered whole), publish it, or delete it; the tenant lists its published documents and gets a five-minute presigned download link. Uploads, publications and downloads are audited, a download under the person the portal names. Migration 0008 adds the documents table; internal/storage wraps minio-go (Hetzner Object Storage, the bucket is created in its region when missing). Without DOCUMENTS_S3_* the routes answer 503 and the rest of the registry is unaffected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
112 lines
3.2 KiB
Go
112 lines
3.2 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
const documentSelect = `
|
|
SELECT id::text, tenant_id::text, title, description, filename, content_type, size_bytes, sha256,
|
|
object_key, status, uploaded_by, COALESCE(published_by, ''), published_at, created_at, updated_at
|
|
FROM documents `
|
|
|
|
func scanDocument(row pgx.Row) (*Document, error) {
|
|
var d Document
|
|
if err := row.Scan(&d.ID, &d.TenantID, &d.Title, &d.Description, &d.Filename, &d.ContentType, &d.SizeBytes, &d.SHA256,
|
|
&d.ObjectKey, &d.Status, &d.UploadedBy, &d.PublishedBy, &d.PublishedAt, &d.CreatedAt, &d.UpdatedAt); err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &d, nil
|
|
}
|
|
|
|
func (p *Postgres) CreateDocument(ctx context.Context, d Document) (*Document, error) {
|
|
if d.ID == "" || d.ObjectKey == "" || d.Title == "" {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
if d.Status == "" {
|
|
d.Status = DocumentDraft
|
|
}
|
|
_, err := p.pool.Exec(ctx, `
|
|
INSERT INTO documents (id, tenant_id, title, description, filename, content_type, size_bytes, sha256, object_key, status, uploaded_by)
|
|
VALUES ($1::uuid, $2::uuid, $3, $4, $5, $6, $7, $8, $9, $10, $11)`,
|
|
d.ID, d.TenantID, d.Title, d.Description, d.Filename, d.ContentType, d.SizeBytes, d.SHA256, d.ObjectKey, d.Status, d.UploadedBy)
|
|
switch {
|
|
case err == nil:
|
|
return p.GetDocument(ctx, d.ID)
|
|
case isFKViolation(err), isInvalidUUID(err):
|
|
return nil, ErrNotFound
|
|
case isUniqueViolation(err):
|
|
return nil, ErrConflict
|
|
case isCheckViolation(err):
|
|
return nil, ErrInvalidInput
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (p *Postgres) GetDocument(ctx context.Context, id string) (*Document, error) {
|
|
d, err := scanDocument(p.pool.QueryRow(ctx, documentSelect+`WHERE id = $1::uuid`, id))
|
|
if isInvalidUUID(err) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return d, err
|
|
}
|
|
|
|
func (p *Postgres) ListDocuments(ctx context.Context, tenantID string, publishedOnly bool) ([]Document, error) {
|
|
q := documentSelect + `WHERE tenant_id = $1::uuid`
|
|
if publishedOnly {
|
|
q += ` AND status = 'published'`
|
|
}
|
|
rows, err := p.pool.Query(ctx, q+` ORDER BY created_at DESC, id DESC`, tenantID)
|
|
if isInvalidUUID(err) {
|
|
return []Document{}, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []Document{}
|
|
for rows.Next() {
|
|
d, err := scanDocument(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, *d)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (p *Postgres) PublishDocument(ctx context.Context, id, by string, at time.Time) (*Document, error) {
|
|
// An already published document is returned unchanged.
|
|
_, err := p.pool.Exec(ctx, `
|
|
UPDATE documents SET status = 'published', published_by = $2, published_at = $3, updated_at = $3
|
|
WHERE id = $1::uuid AND status <> 'published'`, id, by, at.UTC())
|
|
if isInvalidUUID(err) {
|
|
return nil, ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return p.GetDocument(ctx, id)
|
|
}
|
|
|
|
func (p *Postgres) DeleteDocument(ctx context.Context, id string) error {
|
|
tag, err := p.pool.Exec(ctx, `DELETE FROM documents WHERE id = $1::uuid`, id)
|
|
if isInvalidUUID(err) {
|
|
return ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|