package store import ( "bytes" "context" "crypto/sha256" "database/sql" "encoding/hex" "errors" "io" "os" "path/filepath" "strings" "pageswiki/internal/geduldh" ) type memoryDocumentReader struct { reader *bytes.Reader ctx context.Context } func (memoryDocumentReader) Close() error { return nil } func (r memoryDocumentReader) Read(p []byte) (int, error) { if err := r.ctx.Err(); err != nil { return 0, err } return r.reader.Read(p) } func (r memoryDocumentReader) ReadAt(p []byte, offset int64) (int, error) { if err := r.ctx.Err(); err != nil { return 0, err } return r.reader.ReadAt(p, offset) } func (r memoryDocumentReader) Seek(offset int64, whence int) (int64, error) { if err := r.ctx.Err(); err != nil { return 0, err } return r.reader.Seek(offset, whence) } var ( ErrInvalidDocumentHash = geduldh.ErrInvalidHash ErrDocumentHashMismatch = geduldh.ErrHashMismatch ) func MigrateLegacyDocumentRoot( dataDir string, ) (string, error) { documentRoot := filepath.Join( dataDir, "documents", ) legacyRoot := filepath.Join( dataDir, ".documents", ) _, documentErr := os.Stat( documentRoot, ) _, legacyErr := os.Stat( legacyRoot, ) documentExists := documentErr == nil legacyExists := legacyErr == nil if documentErr != nil && !errors.Is(documentErr, os.ErrNotExist) { return "", documentErr } if legacyErr != nil && !errors.Is(legacyErr, os.ErrNotExist) { return "", legacyErr } if documentExists && legacyExists { return "", errors.New( "both documents and legacy .documents directories exist", ) } if legacyExists { if err := os.Rename( legacyRoot, documentRoot, ); err != nil { return "", err } } return documentRoot, nil } func validDocumentHash(hash string) bool { if len(hash) != sha256.Size*2 { return false } decoded, err := hex.DecodeString( hash, ) return err == nil && len(decoded) == sha256.Size } func (s *Store) documentPath( hash string, ) (string, error) { hash = strings.ToLower( strings.TrimSpace( hash, ), ) if !validDocumentHash(hash) { return "", ErrInvalidDocumentHash } if s.DocumentRoot == "" { return "", nil } return filepath.Join( s.DocumentRoot, "sha256", hash[:2], hash, ), nil } func (s *Store) DocumentPath(hash string) (string, error) { return s.documentPath(hash) } // OpenDocument opens an immutable content-addressed Document. Callers must // perform authorization through a referencing object before invoking it. func (s *Store) OpenDocument(hash string) (io.ReadSeekCloser, error) { return s.OpenDocumentContext(context.Background(), hash) } // OpenDocumentContext supports bounded ReaderAt access without hydrating a // revision body. A request/job context must be used by new storage consumers. func (s *Store) OpenDocumentContext(ctx context.Context, hash string) (geduldh.Reader, error) { if err := ctx.Err(); err != nil { return nil, err } if _, err := geduldh.Key(hash); err != nil { return nil, err } if s.DocumentRoot != "" { backend, err := s.documentStorage() if err != nil { return nil, err } return backend.Open(ctx, hash) } var body string if err := s.DB.QueryRowContext(ctx, `SELECT COALESCE(body,'') FROM revisions WHERE document_hash=? ORDER BY rowid DESC LIMIT 1`, strings.ToLower(strings.TrimSpace(hash))).Scan(&body); err != nil { return nil, err } return memoryDocumentReader{reader: bytes.NewReader([]byte(body)), ctx: ctx}, nil } // documentStorage is the payload-only backend boundary. DocumentRoot still // anchors legacy calendars and instance paths; it must not become an S3 URL. // Remote selection is intentionally not exposed until canonical state, CLI, // deployment profiles and backend contract tests have been migrated. func (s *Store) documentStorage() (geduldh.Store, error) { return geduldh.NewLocal(s.DocumentRoot) } type DocumentReference struct { Revision *Revision Path string } // DocumentReferencesInDomain returns bounded semantic references for a // Document. The HTTP layer still evaluates read permission for each Path. func (s *Store) DocumentReferencesInDomain(domainUUID, hash string) ([]DocumentReference, error) { hash = strings.ToLower(strings.TrimSpace(hash)) if !validDocumentHash(hash) { return nil, sql.ErrNoRows } rows, err := s.DB.Query(` SELECT r.uuid,r.object_uuid,COALESCE(r.document_hash,''), COALESCE(NULLIF(r.lang,''),'und'), COALESCE(NULLIF(r.content_type,''),'application/octet-stream'), COALESCE(r.filename,''),'',COALESCE(r.message,''), COALESCE(r.created_at,0), COALESCE(r.created_at_ms,COALESCE(r.created_at,0)*1000),p.path FROM revisions r JOIN paths p ON p.object_uuid=r.object_uuid AND p.domain_uuid=? WHERE r.document_hash=? OR EXISTS ( SELECT 1 FROM revision_document_refs x WHERE x.revision_uuid=r.uuid AND x.document_hash=? ) ORDER BY COALESCE(r.created_at_ms,r.created_at*1000) DESC, COALESCE(p.is_primary,0) DESC,p.path,r.rowid DESC LIMIT 256 `, domainUUID, hash, hash) if err != nil { return nil, err } defer rows.Close() var references []DocumentReference for rows.Next() { revision := &Revision{} var path string if err := rows.Scan(&revision.UUID, &revision.ObjectUUID, &revision.DocumentHash, &revision.Language, &revision.ContentType, &revision.Filename, &revision.Body, &revision.Message, &revision.CreatedAt, &revision.CreatedAtMS, &path); err != nil { return nil, err } if revision.DocumentHash != hash { // The containing Revision supplies authorization. The referenced // Document has independent payload metadata and is detected at serve time. revision.DocumentHash = hash revision.ContentType = "application/octet-stream" revision.Filename = hash } references = append(references, DocumentReference{Revision: revision, Path: path}) } if err := rows.Err(); err != nil { return nil, err } if len(references) == 0 { return nil, sql.ErrNoRows } return references, nil } func (s *Store) persistDocument(expectedHash, body string) error { if s.ReadOnly { return ErrReadOnly } if s.DocumentRoot == "" { return nil } backend, err := s.documentStorage() if err != nil { return err } return backend.PutIfAbsent(context.Background(), expectedHash, int64(len(body)), strings.NewReader(body)) } func (s *Store) readDocument(hash string) (string, error) { if s.DocumentRoot == "" { return "", os.ErrNotExist } backend, err := s.documentStorage() if err != nil { return "", err } reader, err := backend.Open(context.Background(), hash) if err != nil { return "", err } defer reader.Close() payload, err := io.ReadAll(reader) if err != nil { return "", err } if revisionHash(string(payload)) != strings.ToLower(strings.TrimSpace(hash)) { return "", ErrDocumentHashMismatch } return string(payload), nil } func (s *Store) EnsureDocumentStore() error { return s.EnsureDocumentStoreContext(context.Background()) } func (s *Store) EnsureDocumentStoreContext(ctx context.Context) error { for { progress, err := s.BackfillDocuments(ctx, 128) if err != nil { return err } if progress.Completed { return nil } } } func (s *Store) hydrateRevision( revision *Revision, ) (*Revision, error) { if revision == nil || s.DocumentRoot == "" { return revision, nil } body, err := s.readDocument( revision.DocumentHash, ) if errors.Is(err, os.ErrNotExist) { if err := s.persistDocument( revision.DocumentHash, revision.Body, ); err != nil { return nil, err } body = revision.Body } else if err != nil { return nil, err } revision.Body = body return revision, nil }