Workspace ticket #171 (decided 2026-10-20): the tenant's own documents live next to BreakPilot's deliveries ("Unterlagen") in the same table and bucket, in three areas, with the original and the prepared version the Factory delivers. - migration 0022: documents get area (legal_web / product_public / internal; NULL = Unterlagen), legal_kind (the 8 folders as the workspace and Factory name them), processing_status (none/pending/ready/failed), external_id (the Factory's tenant_document_id, unique per tenant); document_preparations (chunk file and/or cookie-banner capture with digest, format, revision, pin, image digests); store_assets (images per tenant, content-addressed) - GET /v1/tenants/{id}/store; POST …/store/documents (customer upload: legal folder, product of an internal document; legal and product documents wait for the Factory, an internal one only on request); GET …/original, …/prepared, …/consent-banner and …/store/assets/{sha}: every byte is hashed and compared before anything is sent (fail-closed, 502 digest_mismatch), HTML originals sandboxed - POST /v1/admin/tenants/{id}/store/import (named staff only): a .tar.gz in the Factory's run layout; unsafe paths and links refused, every file checked against its digest, keyed on tenant_document_id so it can run again; per-document outcome, never content - Unterlagen lists stay as they were (area NULL only); deleting a store document removes its prepared files; storage gains Get - OpenAPI for the seven routes; tests on memory and Postgres with synthetic data only Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
186 lines
4.6 KiB
Go
186 lines
4.6 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
// storeOrder sorts the tenant's own documents: by area, legal folder (in the
|
|
// folders' order), title, then id.
|
|
func storeOrder(out []Document) {
|
|
area := map[string]int{AreaLegalWeb: 0, AreaProductPublic: 1, AreaInternal: 2}
|
|
kind := map[string]int{}
|
|
for i, k := range LegalKinds {
|
|
kind[k] = i
|
|
}
|
|
sort.Slice(out, func(i, j int) bool {
|
|
a, b := out[i], out[j]
|
|
if area[a.Area] != area[b.Area] {
|
|
return area[a.Area] < area[b.Area]
|
|
}
|
|
if kind[a.LegalKind] != kind[b.LegalKind] {
|
|
return kind[a.LegalKind] < kind[b.LegalKind]
|
|
}
|
|
if a.Title != b.Title {
|
|
return a.Title < b.Title
|
|
}
|
|
return a.ID < b.ID
|
|
})
|
|
}
|
|
|
|
func (m *Memory) ListStoreDocuments(_ context.Context, tenantID string) ([]Document, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
out := []Document{}
|
|
for _, d := range m.documents {
|
|
if d.TenantID == tenantID && d.Area != "" {
|
|
out = append(out, *d.copy())
|
|
}
|
|
}
|
|
storeOrder(out)
|
|
return out, nil
|
|
}
|
|
|
|
func (m *Memory) GetDocumentByExternalID(_ context.Context, tenantID, externalID string) (*Document, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
for _, d := range m.documents {
|
|
if externalID != "" && d.TenantID == tenantID && d.ExternalID == externalID {
|
|
return d.copy(), nil
|
|
}
|
|
}
|
|
return nil, ErrNotFound
|
|
}
|
|
|
|
func (m *Memory) SetDocumentProcessing(_ context.Context, docID, status, errMsg string) (*Document, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
d, ok := m.documents[docID]
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
switch status {
|
|
case ProcessingNone, ProcessingPending, ProcessingReady, ProcessingFailed:
|
|
default:
|
|
return nil, ErrInvalidInput
|
|
}
|
|
if len(errMsg) > 500 {
|
|
errMsg = errMsg[:500]
|
|
}
|
|
d.ProcessingStatus, d.ProcessingError, d.UpdatedAt = status, errMsg, time.Now().UTC()
|
|
return d.copy(), nil
|
|
}
|
|
|
|
func (m *Memory) SaveDocumentPreparation(_ context.Context, p Preparation) (*Preparation, error) {
|
|
if !p.HasChunks() && !p.HasConsent() {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
d, ok := m.documents[p.DocumentID]
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
cur := m.preparations[p.DocumentID]
|
|
next := mergePreparation(cur, p)
|
|
next.DeliveredAt = time.Now().UTC()
|
|
m.preparations[p.DocumentID] = &next
|
|
if next.HasChunks() {
|
|
d.ProcessingStatus, d.ProcessingError = ProcessingReady, ""
|
|
}
|
|
out := next
|
|
out.AssetDigests = append([]string(nil), next.AssetDigests...)
|
|
return &out, nil
|
|
}
|
|
|
|
// mergePreparation lays the delivered parts over what is stored: a part that
|
|
// was not delivered stays as it was.
|
|
func mergePreparation(cur *Preparation, p Preparation) Preparation {
|
|
next := p
|
|
if cur != nil {
|
|
if !p.HasChunks() {
|
|
next.Format, next.Revision, next.Pin = cur.Format, cur.Revision, cur.Pin
|
|
next.ChunkObjectKey, next.ChunkSHA256, next.ChunkBytes, next.ChunkCount = cur.ChunkObjectKey, cur.ChunkSHA256, cur.ChunkBytes, cur.ChunkCount
|
|
next.AssetDigests = cur.AssetDigests
|
|
}
|
|
if !p.HasConsent() {
|
|
next.ConsentObjectKey, next.ConsentSHA256 = cur.ConsentObjectKey, cur.ConsentSHA256
|
|
}
|
|
}
|
|
if next.AssetDigests == nil {
|
|
next.AssetDigests = []string{}
|
|
}
|
|
return next
|
|
}
|
|
|
|
func (m *Memory) GetDocumentPreparation(_ context.Context, docID string) (*Preparation, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
p, ok := m.preparations[docID]
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
out := *p
|
|
out.AssetDigests = append([]string(nil), p.AssetDigests...)
|
|
return &out, nil
|
|
}
|
|
|
|
func (m *Memory) ListDocumentPreparations(_ context.Context, tenantID string) (map[string]Preparation, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
out := map[string]Preparation{}
|
|
for id, p := range m.preparations {
|
|
if d, ok := m.documents[id]; ok && d.TenantID == tenantID {
|
|
cp := *p
|
|
cp.AssetDigests = append([]string(nil), p.AssetDigests...)
|
|
out[id] = cp
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (m *Memory) PutStoreAsset(_ context.Context, a StoreAsset) (*StoreAsset, error) {
|
|
if !hex64(a.SHA256) || a.ObjectKey == "" {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if _, ok := m.tenants[a.TenantID]; !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
key := a.TenantID + "/" + a.SHA256
|
|
if cur, ok := m.assets[key]; ok {
|
|
out := *cur
|
|
return &out, nil
|
|
}
|
|
a.CreatedAt = time.Now().UTC()
|
|
m.assets[key] = &a
|
|
out := a
|
|
return &out, nil
|
|
}
|
|
|
|
func (m *Memory) GetStoreAsset(_ context.Context, tenantID, sha string) (*StoreAsset, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
a, ok := m.assets[tenantID+"/"+sha]
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
out := *a
|
|
return &out, nil
|
|
}
|
|
|
|
// hex64 reports whether s is a lowercase hex SHA-256.
|
|
func hex64(s string) bool {
|
|
if len(s) != 64 {
|
|
return false
|
|
}
|
|
for _, c := range s {
|
|
if (c < '0' || c > '9') && (c < 'a' || c > 'f') {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|