Files
pages/internal/store/document_store.go
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

426 lines
7.5 KiB
Go

package store
import (
"bytes"
"crypto/sha256"
"database/sql"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
type memoryDocumentReader struct {
*bytes.Reader
}
func (memoryDocumentReader) Close() error { return nil }
var (
ErrInvalidDocumentHash = errors.New("invalid Document SHA-256")
ErrDocumentHashMismatch = errors.New(
"Document payload does not match SHA-256",
)
)
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) {
path, err := s.documentPath(hash)
if err != nil {
return nil, err
}
if path != "" {
return os.Open(path)
}
var body string
if err := s.DB.QueryRow(`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{bytes.NewReader([]byte(body))}, nil
}
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 string,
body string,
) error {
if s.DocumentRoot == "" {
return nil
}
actualHash :=
revisionHash(
body,
)
if !strings.EqualFold(expectedHash, actualHash) {
return fmt.Errorf(
"%w: expected %s, got %s",
ErrDocumentHashMismatch,
expectedHash,
actualHash,
)
}
path, err :=
s.documentPath(
actualHash,
)
if err != nil {
return err
}
if existing, err := os.ReadFile(path); err == nil {
if revisionHash(string(existing)) != actualHash {
return fmt.Errorf(
"%w: corrupt existing file %s",
ErrDocumentHashMismatch,
path,
)
}
return nil
} else if !errors.Is(err, os.ErrNotExist) {
return err
}
dir :=
filepath.Dir(
path,
)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
temp, err :=
os.CreateTemp(
dir,
".document-*",
)
if err != nil {
return err
}
tempPath :=
temp.Name()
defer os.Remove(tempPath)
if err := temp.Chmod(0644); err != nil {
temp.Close()
return err
}
if _, err := temp.WriteString(body); err != nil {
temp.Close()
return err
}
if err := temp.Sync(); err != nil {
temp.Close()
return err
}
if err := temp.Close(); err != nil {
return err
}
if err := os.Rename(tempPath, path); err != nil {
return err
}
return nil
}
func (s *Store) readDocument(
hash string,
) (string, error) {
if s.DocumentRoot == "" {
return "", os.ErrNotExist
}
path, err :=
s.documentPath(
hash,
)
if err != nil {
return "", err
}
payload, err :=
os.ReadFile(
path,
)
if err != nil {
return "", err
}
body :=
string(
payload,
)
if revisionHash(body) != strings.ToLower(hash) {
return "", fmt.Errorf(
"%w: corrupt file %s",
ErrDocumentHashMismatch,
path,
)
}
return body, nil
}
func (s *Store) EnsureDocumentStore() error {
if s.DocumentRoot == "" {
return nil
}
if err := os.MkdirAll(
filepath.Join(
s.DocumentRoot,
"sha256",
),
0755,
); err != nil {
return err
}
rows, err := s.DB.Query(`
SELECT
document_hash,
COALESCE(body, '')
FROM revisions
WHERE document_hash IS NOT NULL
AND document_hash != ''
`)
if err != nil {
return err
}
for rows.Next() {
var hash string
var body string
if err := rows.Scan(&hash, &body); err != nil {
rows.Close()
return err
}
if err := s.persistDocument(hash, body); err != nil {
rows.Close()
return err
}
}
if err := rows.Err(); err != nil {
rows.Close()
return err
}
return rows.Close()
}
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
}