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
88 lines
1.9 KiB
Go
88 lines
1.9 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
func (m *Memory) CreateDocument(_ context.Context, d Document) (*Document, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if d.ID == "" || d.ObjectKey == "" || d.Title == "" {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
if _, ok := m.tenants[d.TenantID]; !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
for _, x := range m.documents {
|
|
if x.ID == d.ID || x.ObjectKey == d.ObjectKey {
|
|
return nil, ErrConflict
|
|
}
|
|
}
|
|
now := time.Now().UTC()
|
|
if d.Status == "" {
|
|
d.Status = DocumentDraft
|
|
}
|
|
d.CreatedAt, d.UpdatedAt = now, now
|
|
row := d
|
|
m.documents[d.ID] = &row
|
|
out := row
|
|
return &out, nil
|
|
}
|
|
|
|
func (m *Memory) GetDocument(_ context.Context, id string) (*Document, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
d, ok := m.documents[id]
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
out := *d
|
|
return &out, nil
|
|
}
|
|
|
|
func (m *Memory) ListDocuments(_ context.Context, tenantID string, publishedOnly bool) ([]Document, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
out := []Document{}
|
|
for _, d := range m.documents {
|
|
if d.TenantID != tenantID || (publishedOnly && d.Status != DocumentPublished) {
|
|
continue
|
|
}
|
|
out = append(out, *d)
|
|
}
|
|
sort.Slice(out, func(i, j int) bool {
|
|
if out[i].CreatedAt.Equal(out[j].CreatedAt) {
|
|
return out[i].ID > out[j].ID
|
|
}
|
|
return out[i].CreatedAt.After(out[j].CreatedAt)
|
|
})
|
|
return out, nil
|
|
}
|
|
|
|
func (m *Memory) PublishDocument(_ context.Context, id, by string, at time.Time) (*Document, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
d, ok := m.documents[id]
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
if d.Status != DocumentPublished {
|
|
at = at.UTC()
|
|
d.Status, d.PublishedBy, d.PublishedAt, d.UpdatedAt = DocumentPublished, by, &at, at
|
|
}
|
|
out := *d
|
|
return &out, nil
|
|
}
|
|
|
|
func (m *Memory) DeleteDocument(_ context.Context, id string) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if _, ok := m.documents[id]; !ok {
|
|
return ErrNotFound
|
|
}
|
|
delete(m.documents, id)
|
|
return nil
|
|
}
|