Compare commits
6
Commits
a34220bf2d
..
v1.2.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4316da578f | ||
|
|
1e38e6e4ee | ||
|
|
37dc36f1f2 | ||
|
|
2d984fd665 | ||
|
|
ac8bd9acb4 | ||
|
|
95b4cf79cf |
@@ -2,7 +2,7 @@
|
||||
|
||||
## Project Status
|
||||
|
||||
`backend/` (Go) et `mobile-kotlin/` (Kotlin/Android) ont du scaffolding en place. Le client mobile est **local-first** : il reflète le SAF dans une base Room (v16, migrations, DAO) et pousse les **métadonnées** de ses mutations locales via un **outbox transactionnel** (`pending_operations` + worker WorkManager → `POST /sync/ops`). Upload physique ciblée (pipeline OCR : `POST /files/upload` + `resourceId`), streaming lecture des fichiers distants (`GET /files/:id/content`, jamais persisté), OCR serveur consommé côté client, recherche locale via Room (+ recherche serveur exposée mais non consommée).
|
||||
`backend/` (Go) et `mobile-kotlin/` (Kotlin/Android) ont du scaffolding en place. Le client mobile est **local-first** : il reflète le SAF dans une base Room (v22, migrations, DAO) et pousse les **métadonnées** de ses mutations locales via un **outbox transactionnel** (`pending_operations` + worker WorkManager → `POST /sync/ops`). Upload physique ciblée (pipeline OCR : `POST /files/upload` + `resourceId`), streaming lecture des fichiers distants (`GET /files/:id/content`, jamais persisté + vignettes `GET /files/:id/preview` en cache), OCR serveur consommé côté client, recherche locale via Room FTS (+ facettes, + recherche serveur exposée), export chiffré (`POST /export`).
|
||||
|
||||
## Architecture
|
||||
|
||||
@@ -38,7 +38,7 @@ Tests mobiles : JVM unit tests dans `app/src/test` (JUnit + Robolectric + Room i
|
||||
- `config/` — env (`godotenv`, optionnel) + defaults: `PORT`, `DATABASE_URL`, `UPLOAD_DIR`, `MAX_FILE_SIZE_MB`, `OCR_LANG`, `AUTH_SECRET` (secret des tokens paseto), `ADMIN_USERNAME`/`ADMIN_PASSWORD` (bootstrap du premier admin)
|
||||
- `models/` — domain entities (users, devices, documents/resources, clients)
|
||||
- `service/` — business logic (permissions, upload, create folder, move, **sync outbox + snapshot**, bootstrap admin)
|
||||
- `handlers/` — HTTP handlers (health, devices register, **auth/login**, **users resolve + change password**, files CRUD/upload/search, folders, **sync/ops + sync/permissions, ocr/jobs, files/:id/content, files/:id/shares, files/:id/links, files/:id/placements, groups CRUD + membres** — réels) ; middleware `RequireAuth`
|
||||
- `handlers/` — HTTP handlers (health, devices register/list/revoke, **auth/login**, **users resolve + change password (révoque les tokens) + usage**, files CRUD/upload/search (+facettes) , folders, **sync/ops + sync/permissions, ocr/jobs, files/:id/content, files/:id/preview, files/:id/shares, files/:id/links, files/:id/placements, files/:id/versions, files/:id/facts, files/:id/relations, trash, duplicates, export, groups CRUD + membres** — réels) ; middleware `RequireAuth` (+ révocation `tv`, devices si `EnforceDeviceExists`)
|
||||
- `repository/` — Postgres persistence (`repository.Resources` : insert/list/get/soft-delete scoping `owner_id`, search, move, rename, root-name unique index, `ListOwned` pour le snapshot, `OpenContent` (bytes streaming + lazy healing `has_content`), `ListShares`/`ListLinks` (owner-only), `SetHasContent` ; `repository.Devices` : Upsert, Exists, MarkUser ; `repository.Operations` : trace outbox idempotente `(device_id, operation_id)` ; `repository.OcrJobs` : jobs queued→processing→done/failed ; `repository.Users` : GetByUsernameNormalized, ResolveExact, UpdatePassword ; `repository.Shares` : Upsert/UpsertGroup/Revoke, CreateLink/RevokeLink, ListActiveForResource, ListActiveLinksForResource, snapshot `ListEffectivePermissions` (grants user + groupe) ; `repository.Groups` : Create/ListForUser/AddMember/RemoveMember/Delete (V2-6, un niveau, sans imbrication) ; `repository.Placements` : EnsureLocation/Report/ListForResource (V2 multi-device) ; IDs sont TEXT 32-hex, `NewID()` = `crypto/rand` 16 octets hex (jamais UUID conversion, cf. `docs/api-v1.md`)
|
||||
- `db/` — package migrations (`golang-migrate/v4`, embarquées via `embed` dans `db/migrations/*.sql`, 000001→000016) : `db.MigrateDatabase(url)` au boot du serveur ; test harness `db/migrations_test.go` (up → assertions schéma → down, `TEST_DATABASE_URL`, skip si PG indisponible) ; `dbtest/` — helper cross-package pour les tests repo/handlers (crée la DB test si absente, reset schema, migrate ; skip si PG down)
|
||||
- `ocr/` — OCR engine behind an interface (Tesseract system call, `OCR_LANG` défaut `fra+eng`)
|
||||
@@ -52,16 +52,16 @@ Tests mobiles : JVM unit tests dans `app/src/test` (JUnit + Robolectric + Room i
|
||||
|
||||
- Entry point: `app/src/main/java/com/kazier/mobile/KazierApplication.kt` (Hilt + `HiltWorkerFactory`) + `MainActivity.kt` (Hilt) ; navigation Compose dans `ui/navigation/` (`NavGraph.kt`, `KazierApp.kt`)
|
||||
- Data layer — `data/`:
|
||||
- `data/local/` — Room SQLite (DB `dot.db`, **version 18**, `Migrations.kt` : tables `folders`, `files` (+ `category`, `processed`, `content`, `ocr_text`, `ocr_attempts`, `ocr_queued_at`, `bytes_uploaded`, `bytes_upload_attempts`, `has_content_server`, `wanted_offline`), `user_preferences`, `pending_operations` (+ `device_id`, `parent_resource_id`), `scan_sessions`, `scan_pages`, `resource_permissions`) : entités + DAO, tri (`FileOrdering`), schéma exporté (`app/schemas/`, m19) ; flag `processed` sur `files` (mode review « traiter », local au device, **gate de sync** : un fichier pas encore traité n'est jamais poussé) ; outbox transactionnel `pending_operations`
|
||||
- `data/local/` — Room SQLite (DB `dot.db`, **version 22**, `Migrations.kt` : tables `folders`, `files` (+ `category`, `processed`, `content`, `ocr_text`, `ocr_attempts`, `ocr_queued_at`, `bytes_uploaded`, `bytes_upload_attempts`, `has_content_server`, `wanted_offline`, `trashed_at`), `user_preferences`, `pending_operations` (+ `device_id`, `parent_resource_id`), `scan_sessions`, `scan_pages`, `resource_permissions`, `resource_facts`, `files_fts` (FTS4 + triggers `FtsTriggers`), `resource_links`, `collections`) : entités + DAO, tri (`FileOrdering`), schéma exporté (`app/schemas/`, m22) ; flag `processed` sur `files` (mode review « traiter », local au device, **gate de sync** : un fichier pas encore traité n'est jamais poussé) ; outbox transactionnel `pending_operations`
|
||||
- `data/remote/` — Retrofit/Moshi : `ApiService.kt` + `dto/Dtos.kt` = **contrat HTTP** (`{ data, meta }`, erreurs `{ error: { code, message } }`) ; `ApiClient.kt` normalise les réponses ; interceptors OkHttp (`AuthInterceptor`, `ServerUrlInterceptor`) ; `SafMultipart.kt` (multipart SAF paresseux partagé upload OCR + V2)
|
||||
- `data/repository/` — `FolderRepository`, `FileRepository`, `AuthRepository`, `OutboxRepository` (enqueue `create_resource`/`move_resource`/`delete_resource` + ops partage `share`/`update_share`/`revoke_share`/`create_link`/`revoke_link` + `report_bytes` V2 (`{ hasBytes }`, nouvel `operation_id` par état), écrivains dans la même transaction Room que la mutation parente ; pour un fichier SAF, la mutation parente du `create_resource` est le **garder** de la review (`markProcessed`), et `hasCreateOperation` en garde l'unicité), `ShareRepository` (hydratation + delta + `canAccess` depuis `GET /sync/permissions`), `OcrRepository` (upload ciblé + jobs + `syncFromServer`), `RemoteContentRepository` (streaming `GET /files/:id/content`, jamais persisté ; **upload V2** des octets après metadata synced, `bytes_uploaded` ; **download V2** vers SAF + rattachement, `wanted_offline`)
|
||||
- `data/repository/` — `FolderRepository`, `FileRepository`, `AuthRepository`, `OutboxRepository` (enqueue `create_resource` (+`scanSessionId` S5)/`move_resource`/`update_metadata` (+`baseUpdatedAt` S6)/`trash_resource`/`restore_resource`/`purge_resource` (V3, `delete_resource` legacy mappé serveur) + ops partage `share`/`update_share`/`revoke_share`/`create_link` (expiration 7j défaut)/`revoke_link` + `report_bytes` V2 (`{ hasBytes }`, nouvel `operation_id` par état) + `correct_facts` S3 (full-replace jeu `user`), écrivains dans la même transaction Room que la mutation parente ; pour un fichier SAF, la mutation parente du `create_resource` est le **garder** de la review (`markProcessed`), et `hasCreateOperation` en garde l'unicité), `ShareRepository` (hydratation + delta + `canAccess` depuis `GET /sync/permissions`), `OcrRepository` (upload ciblé + jobs + `syncFromServer`), `FactsRepository` (sync remplace-en-bloc + corrections optimistes), `LinksRepository` (graphe + verdicts REST), `TrashRepository` (restore/purge/convergence), `CollectionsRepository` (local), `DevicesRepository` (appareils + usage), `RemoteContentRepository` (streaming `GET /files/:id/content`, jamais persisté ; **upload V2** des octets après metadata synced, `bytes_uploaded` ; **download V2** vers SAF + rattachement, `wanted_offline`)
|
||||
- `features/` — logique descendue côté client :
|
||||
- `sync/DeviceSync.kt` + `SafScanner.kt` — sync device↔SAF : **two-pass walk** (listing hors transaction puis upserts Room dans une seule transaction), réconciliation `exists = 0` (jamais de suppression), single-flight via `Mutex` ; les nouveaux **dossiers** sont journalisés dans l'outbox à la découverte, les **fichiers** uniquement au « garder » de la review (gate `processed`)
|
||||
- `saf/` — `SafUris`, `SafFolderCreator`, `FileMover` (relocalisation SAF `DocumentsContract.moveDocument`, repli métadonnée seule ; le déplacement est poussé en `move_resource`), `SafFileDeleter` (suppression SAF + `delete_resource` atomique), `FileDeleter.releaseLocalCopies` (purge sûre V2-3 : gate `isReleasable`, détachement cloud-only, zéro outbox)
|
||||
- `sync/OutboxSyncWorker.kt` — worker WorkManager (`enqueueUniqueWork` KEEP, single-flight, `NetworkType.CONNECTED`, backoff 30s) qui draine `pending_operations` vers `POST /sync/ops` (batch 20, dead-letter immédiat `failed` sur erreur 4xx non-idempotente — `attempts` diagnostic, purge synced > 7 jours, **sauf `report_bytes` : dead-letter + suite du batch, jamais bloquant**) ; **schedulé** au login/restauration de session (`AuthViewModel`) et après chaque `syncAll()` (`SyncViewModel`) ; enchaîne `OcrAutoWorker` + `BytesUploadWorker` quand un fichier est promu (`DownloadWorker` sur demande hors-ligne) ; `RetentionWorker` (passe quotidienne, purge sûre des copies anciennes selon `RetentionStore`, sans réseau)
|
||||
- `sync/SyncViewModel.kt` — état du sync exposé à l'UI (`SyncStatus` : marche en cours, ops en attente, dernières opérations) via le badge header `ui/components/SyncStatusBadge.kt` + `SyncOperationsDialog.kt` (« liste des sync »)
|
||||
- `auth/` — session (login user + token paseto + **re-login silencieux** sur 401 via identifiants chiffrés persistés, single-flight, événements `Reauthenticated`/`SessionExpired`) : `SessionManager`, `SecureTokenStore`, `TokenProvider`, `TokenStore` (+ `Credentials`)
|
||||
- `ui/` — écrans Compose : `folderlist`, `folderdetail`, `document` (contenu PDF/image/texte local + streaming distant image/texte/PDF multi-pages via `RemoteDocumentViewer`, autres types non supportés en streaming), `search` (**recherche locale** via Room, sans endpoint serveur), `settings` (URL serveur + thème + stockage/retention), `pdfbuilder` (multi-select → génération PDF), `auth` (login, mode local, avis d'expiration), `share` (`ShareBottomSheet` : grants + liens + ouverture de lien + copies V2 + groupes V2-6 ; `OpenLinkScreen` + deep-link `kazier://share/<token>`), `components`, `theme`
|
||||
- `ui/` — écrans Compose : `folderlist`, `folderdetail`, `document` (contenu PDF/image/texte local + streaming distant image/texte/PDF multi-pages via `RemoteDocumentViewer`, autres types non supportés en streaming + fiche faits/liens + partage ACTION_SEND), `search` (**recherche locale** via Room FTS + facettes + collections locales), `settings` (URL serveur + thème + stockage/retention + export chiffré + appareils + corbeille + doublons), `trash`, `duplicates`, `devices`, `facts` (bottom-sheet), `pdfbuilder` (multi-select → génération PDF), `auth` (login, mode local, avis d'expiration), `share` (`ShareBottomSheet` : grants + liens + ouverture de lien + copies V2 + groupes V2-6 ; `OpenLinkScreen` + deep-link `kazier://share/<token>`), `components`, `theme`
|
||||
- `domain/` — stores de préférences: `DeviceIdentity`, `ActiveUserStore`, `LocalModeStore`, `ServerConfigStore`, `ThemePreferenceStore`, `RetentionStore` (`all` | `last_months:N`), `GenerateId` (identifiants 32-hex)
|
||||
- `di/` — modules Hilt (`AppModule`, `DatabaseModule`, `NetworkModule`)
|
||||
- Base URL serveur **configurée à l'exécution** dans les Réglages (`ServerConfigStore`, persistée en `user_preferences`), défaut `http://10.0.2.2:8080/api/v1` (pas de variable d'env)
|
||||
|
||||
@@ -5,6 +5,27 @@ Le format suit [Keep a Changelog](https://keepachangelog.com/fr/1.1.0/) et le ve
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [1.2.0] - 2026-09-26
|
||||
|
||||
V3 « Le classeur qui comprend » : filet anti-perte, extraction générique,
|
||||
recherche plein-texte + facettes, graphe documentaire, confiance & partage
|
||||
sobre, preview cloud, export chiffré. Contrat consolidé (`docs/api-v1.md`),
|
||||
vision et plan (`V3.md`).
|
||||
|
||||
### Ajouté
|
||||
- Corbeille V3 (S1 backend) : `DELETE /files/:id` = trash par défaut (`?mode=purge` = définitif), `POST /files/:id/restore`, `GET /trash` (paginé), trash/restore récursifs (subtree + chaîne d'ancêtres), purge auto serveur après 30j (`TrashRetention`, passe au boot + horaire).
|
||||
- Versions V3 (S1 backend) : table `resource_versions` (N=3, élagage auto), historique figé à chaque overwrite (`Upload` ciblé), `GET /files/:id/versions` + `POST /files/:id/versions/:n/restore` (l'actuel est à son tour versionné), upload ciblé vers trashé → `NOT_FOUND`.
|
||||
- Outbox : ops `trash_resource` / `restore_resource` / `purge_resource` (owner-only, `delete_resource` legacy mappé vers trash, même idempotence) ; purge d'octets best-effort côté serveur.
|
||||
- Migrations `000017` (index corbeille) + `000018` (versions), contrat `docs/api-v1.md` §3/§6.1 étendu, test de contrat `router_test.go`.
|
||||
- Extract générique V3 (S2 backend) : package `extract` (règles FR stdlib — dates/échéances/périodes/montants/émetteurs/références masquées/`doc_type` suggéré, confiances, corpus testé), table `resource_facts` (migration `000019`, `origin` auto/user), hook worker OCR (`OnText` + backfill boot borné, best-effort, anti-panic), `GET /files/:id/facts` (viewer+, `[]` si vide, 404 si trashé/étranger).
|
||||
- Corrections V3 (S3) : `PATCH /files/:id/facts` + op `correct_facts` (full-replace du jeu `user`, editor+, validation Go → 400 jamais 500), Room v20 (`resource_facts` + `FactsDao`, migration 19→20), fiche « Ce qu'on a compris » sur le viewer (chips, confiance grisée + suggéré, édition par type, reset auto), `FactsRepository` (sync remplace-en-bloc, correction optimiste + outbox atomique), `GET /files/:id/facts` consommé côté client.
|
||||
- Recherche V3 (S4) : colonne `search_tsv` (migration `000020`, `french`, index GIN) + `SearchDocumentsVisible` (nom LIKE + TS + faits, rang nom > TS > fait sûr > fait douteux, facettes émetteur/type/montant/période), `GET /files/search` étendu (400 si facette malformée), Room v21 (FTS4 `files_fts` + triggers partagés fresh-install/migration, entité déclarée pour KSP), `searchDocuments` offline (même rang, accents préservés) + UI filtres (émetteur, types chips, montants, période ISO, compteur, effacer).
|
||||
- Relations V3 (S5) : table `resource_links` + `scan_session_id` (migration `000021`), réconciliation déterministe (same_scan via outbox, doublons via SHA-256 posé à l'upload + backfill boot, same_issuer/référence via faits effectifs, verdicts préservés, élagage auto), `GET /files/:id/relations` + verdict (owner-only), `GET /duplicates` (groupes + gain récupérable), Room v22 (links + collections), « Voir aussi » dans la fiche (verdicts en ligne), écran Doublons (garder le plus récent), collections locales (sauver/rejouer/supprimer depuis la recherche), `scanSessionId` dans les creates de scan.
|
||||
- Confiance V3 (S6) : `token_version` (migration `000022`, claim `tv`, 401 si révoqué, bump au changement de mot de passe — fin de la limite V1), quota (colonne héritée de `000001`, `000023` idempotente, 413 `QUOTA_EXCEEDED` sur créations, `GET /users/me/usage`), appareils (`GET /devices`, `DELETE /devices/:id`, `EnforceDeviceExists` en prod, écran Appareils + quota), renames concurrents transparents (`baseUpdatedAt` + `conflicts[]` non-fataux, gracieuseté 2 s, pipeline mobile persistance + dialogue), liens à expiration 7j par défaut, import « classeur » unifié (scan/note/dossier), onboarding cercle Famille.
|
||||
- Corbeille V3 (S1 mobile, Room v19) : colonne `trashed_at` (files + folders, migration 18→19), tombstone `exists = 0` (requêtes `exists = 1` inchangées), `FileDeleter`/`FolderDeleter`/`SafFileDeleter` → ops `trash_resource` (plus de `delete_resource` émis), garde walk anti-résurrection + reconcile, `TrashRepository` (restore/purge/convergence `GET /trash`/expiration locale 31j), écran Corbeille (Réglages → restore/purge avec confirmation), expiration tombstones dans `RetentionWorker`, dialogue de suppression rewordé (corbeille 30j, plus « irréversible »).
|
||||
- Preview cloud V3 (S7) : table `resource_previews` (migration `000024`), `GET /files/:id/preview` (JPEG 512, images pur Go, PDF page 1 via `pdftoppm` + poppler au Dockerfile, cache régénéré sur changement, 404 `NO_PREVIEW` sinon), vignettes distantes côté mobile (une requête/session puis cache WebP versionné, garde `tried`, icône sinon).
|
||||
- Export chiffré V3 (S7 C3) : `POST /export { password 8+ }` → zip AES-256 streamé (fichiers vifs possédés + `manifest.json` avec faits, tout chiffré, metadata-only sans octets), mobile (mot de passe jamais persisté, téléchargement cache, partage système via FileProvider), bouton Partager (ACTION_SEND local) sur le viewer — impression papier reportée (V4, pas de dep print).
|
||||
|
||||
## [1.1.0] - 2026-09-25
|
||||
|
||||
### Ajouté
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
# Kazier V3 — « Le classeur qui comprend »
|
||||
|
||||
Statut : **livrée en 1.2.0** (2026-09-26). V1 (1.0.0, socle local-first + partage) et V2 (1.1.0, multi-device : upload/download, placements, rétention, groupes) sont livrées. Ce document reste le contrat produit + plan d'exécution de la V3 (toutes slices S1–S7 livrées).
|
||||
|
||||
## 1. Positionnement (acté)
|
||||
|
||||
- **Tout fichier est un document potentiel, y compris une photo.** Une image (scan, photo de papier, photo tout court) suit le même pipeline : OCR quand il y a du texte, faits génériques, FTS, relations. En revanche **aucune analyse biométrique ou sémantique d'image** : pas de visages, pas de lieux, pas d'événements, pas de clustering de personnes. Si une photo contient du texte (affiche, facture photographiée), on l'exploite ; sinon elle reste un fichier comme un autre (nom, dossier, favori, partage).
|
||||
- **Tous les documents comptent pareil.** Pas de top-5 types prioritaires. Extraction **générique d'abord** (dates, montants, émetteurs, références), typage (`facture`, `contrat`…) en **bonus humble** : suggestion corrigeable, jamais gate, jamais pénalisant.
|
||||
- **Humble par design.** L'extraction se trompe (~20% au début). Donc : chaque fait porte une `confidence`, l'UI affiche le douteux en « suggéré », la recherche **déclasse mais n'exclut jamais** sur un fait douteux, et tout est corrigeable en un tap.
|
||||
- **Échéance = facette, pas alerte.** Trop bruitée pour notifier (faux « expiré » dangereux). Filtre + tri uniquement. Alertes = V4 éventuelle, sur preuve de fiabilité via feedback.
|
||||
- **100% self-hosté / local-first.** Aucun appel IA externe. Règles FR + petits modèles CPU côté serveur, cache Room offline, workers async jamais bloquants (même pattern que `Ocr.Run`).
|
||||
- **Pas de LLM génératif en V3.** On extrait des faits vérifiables, on ne rédige rien. Moins d'hallucinations, moins de GPU, plus de confiance famille.
|
||||
|
||||
## 2. Les 5 piliers (cœur, scope 6–8 semaines)
|
||||
|
||||
| # | Pilier | Promesse | Réutilise |
|
||||
|---|---|---|---|
|
||||
| P1 | Filet anti-perte | Corbeille 30j + versions N=3 | `deleted_at` soft-delete, `UPLOAD_DIR/<user>/<resource>.<ext>`, overwrite ciblée V2 |
|
||||
| P2 | Extract générique | Faits universels + type suggéré, corrigés depuis la fiche document | Pipeline `upload ciblé → Tesseract → resources.ocr_text → GET /files/:id/ocr`, gate `processed` |
|
||||
| P3 | Recherche | FTS nom+ocr+faits, facettes, offline | `FileDao.searchWithFilters` (LIKE+accents), `GET /files/search` serveur |
|
||||
| P4 | Relations doc | `same_scan` (sûr) + doublons/versions/même-émetteur (invalidables), collections virtuelles | `scan_sessions`+`scan_pages`, `content_hash` (V2.md), versions P1 |
|
||||
| P5 | Partage sobre | Cercles (groupes V2-6), liens à expiration par défaut, révocation tokens, quota/3-2-1 visible | Snapshot permissions + TTL 24h, `share`/`create_link`, `PATCH /users/me/password` |
|
||||
|
||||
## 2bis. Plan A — cohérence obligatoire (inclus dans la V3, pas du scope en plus)
|
||||
|
||||
Sans ces 4 items, les piliers grincent. Ils sont petits et intégrés aux slices S1–S6 :
|
||||
|
||||
- **A1 Appareils connectés** : exposer `storage_locations` (existe depuis V2, migration `000015`) en lecture `GET /devices` + `DELETE /devices/:id` (révocation = bump `token_version` ciblé ou suppression ligne ; le device révoqué retombe en 401 → re-login refusé `INVALID_DEVICE_ID`). Écran Réglages « Appareils » : voir / révoquer un téléphone perdu. Grosse valeur famille, ~1 endpoint + 1 écran.
|
||||
- **A2 Quota enforcement** : `/usage` (P5) n'est que de l'affichage. Ajouter `users.storage_quota_bytes` (défaut 10 Go, cf. V2.md §3.1) + garde-fou à l'upload (`413 QUOTA_EXCEEDED`) + à l'overwrite versionnée. Migration `000023_quota`. Sans ça, un foyer remplit le disque.
|
||||
- **A3 Corbeille cohérente des deux côtés** : règle purge serveur (cron au boot + passe quotidienne : `trashed_at < NOW()-30j` → purge dure) **+** miroir mobile (`trashed_at` Room, filtre global `exists=1 AND trashed_at IS NULL`, écran Corbeille). Le walk SAF doit ignorer les trashés (garde `hasPendingTrash`, même pattern que FEEDBACK M5) sinon ils ressuscitent. Fait partie de S1, pas une option.
|
||||
- **A4 Conflits rename transparents** : `update_metadata` concurrent (2 devices renomment) = last-writer-wins silencieux aujourd'hui. Minimum V3 : comparer `updated_at` (perdant = op arrivée sur `updated_at` plus récent que son `base_updated_at` → appliquée quand même mais flag `conflict` remonté dans la réponse `POST /sync/ops` + badge UI « renommé ailleurs » via snapshot). Pas de CRDT, juste de la transparence. Payload `update_metadata` étendu : `{ name?, baseUpdatedAt? }`.
|
||||
|
||||
## 2ter. Plan B — quick wins produit (inclus : B1, B2, B4 ; B3/B5 en stretch)
|
||||
|
||||
- **B1 Import « classeur » unifié** : un seul point d'entrée « Ajouter un papier » (scan / photo / PDF / fichier) + file d'attente d'import visible (compteur `unprocessed`, déjà en Room). Aujourd'hui scan + upload + PDF builder sont 3 chemins séparés — les unifier en UX, zéro backend nouveau. Inclus (S6).
|
||||
- **B2 Doublons visibles + action** : `duplicate_of` (P4, hash) exposé en écran « Doublons (240 Mo récupérables) » + action « garder un seul » (= trash les autres, même op `trash_resource`). Les familles adorent. Inclus (S5).
|
||||
- **B3 Impression / export PDF** : ouvrir le PDF builder existant aux docs déjà stockés (pas qu'au multi-select) + `ACTION_SEND` / impression système. Stretch S7 (pur mobile, pas de backend).
|
||||
- **B4 Accessibilité + i18n** : labels TalkBack sur review/chips/facettes, contrastes, chaînes FR/EN des nouveaux écrans dès la slice (pas de rattrapage). Inclus, transverse à chaque slice.
|
||||
- **B5 Onboarding foyer** : écran « Crée ton cercle Famille » au premier login + import guidé des 3 premiers papiers (deep-link vers B1 + création groupe V2-6 pré-rempli). Sans ça, les cercles P5 restent vides. Stretch S6 si le temps manque (1 écran + 1 CTA, pas de backend).
|
||||
|
||||
## 2quater. Plan C — dette V2.md : solder ou enterrer (décisions actées)
|
||||
|
||||
- **C1 `retention_policies` serveur (V2.md §3.6) : ENTERRÉ.** La rétention V2 mobile (`RetentionStore` + `RetentionWorker`) fait le job ; une politique serveur ajouterait un 2e cerveau en conflit avec le walk local-first. Documenter dans `docs/api-v1.md` : « la rétention est une affaire locale ».
|
||||
- **C2 `sync_queue` serveur (V2.md §3.7) : ENTERRÉ.** L'outbox client (`pending_operations` + batch 20 + dead-letter) + workers WM couvrent le besoin. Ne pas rouvrir.
|
||||
- **C3 Backup 3-2-1 réel : EXPORT, pas second serveur.** `role='backup'` reste un modèle, pas une infra. Minimum V3 : bouton « Exporter tout » (zip + manifest `resource_id, name, hash, facts`) depuis Réglages/Stockage — testé, restaurable, chiffré par le mot de passe (zip AES). Stretch S7, à arbitrer contre les embeddings : **l'export gagne** (vrai 3-2-1 perçu, zéro GPU).
|
||||
|
||||
## 3. Backend — plan fichier par fichier
|
||||
|
||||
### 3.1 Migrations (`backend/db/migrations/`, une par palier, up+down + `migrations_test.go`)
|
||||
|
||||
- `000017_trash` : rien à créer — ajouter index partiel `idx_resources_deleted_at ON resources(deleted_at) WHERE deleted_at IS NOT NULL` + colonne `trashed_at TIMESTAMPTZ` (NULL = vivant ; purge = `trashed_at < NOW()-30j`). Le `DELETE` actuel devient trash (soft), pas de breaking change API.
|
||||
- `000018_versions` : table `resource_versions (id 32-hex PK, resource_id FK CASCADE, version_no INT, size_bytes BIGINT, content_hash TEXT, stored_path TEXT, created_at)` + `UNIQUE(resource_id, version_no)`. À chaque overwrite d'octets (upload ciblé), la copie précédente est poussée en version (garder N=3, purger la plus vieille).
|
||||
- `000019_facts` : table `resource_facts (id 32-hex PK, resource_id FK CASCADE, fact_type TEXT CHECK IN ('date','due_date','period','amount','issuer','reference','doc_type'), value_text TEXT, value_num NUMERIC NULL, value_date DATE NULL, confidence REAL CHECK 0..1, origin TEXT CHECK IN ('auto','user'), created_at, updated_at)` + index `(resource_id)`, `(fact_type, value_text)`. `doc_type` n'est qu'un fait comme un autre (suggestion, pas statut).
|
||||
- `000020_search_fts` (livré S4) : colonne `search_tsv TSVECTOR` générée (`name + ocr_text`, `french`) + index GIN ; faits joints à la requête (rang, jamais d'exclusion).
|
||||
- `000021_links` (S5, livré) : table `resource_links (id 32-hex PK, from_id/to_id canoniques, link_type IN (same_scan, duplicate_of, same_issuer, same_reference) — pas de `version_of`, CHECK from != to, UNIQUE trio)` + `resources.scan_session_id` (session opaque du create outbox). `version_of` non matérialisé (versions DANS la ressource).
|
||||
- `000022_token_version` (S6) : `ALTER TABLE users ADD COLUMN token_version INT NOT NULL DEFAULT 0` — `PATCH /users/me/password` bump ; claim paseto `tv`, middleware `RequireAuth` rejette `tv != users.token_version` (lève la limite V1 « tokens non révoqués »).
|
||||
- `000023_quota` (A2, S6) : `ALTER TABLE users ADD COLUMN storage_quota_bytes BIGINT NOT NULL DEFAULT 10737418240` (10 Go, V2.md §3.1) + vue/fonction `user_used_bytes(user_id)` (somme `size_bytes`hors trash). Check à l'upload et à l'overwrite versionnée → `413 QUOTA_EXCEEDED` (nouveau code §7).
|
||||
|
||||
> Ordre imposé par golang-migrate (montée stricte, pas de trous) : FTS a pris `000020` en S4, décalant links→`000021` et token→`000022`.
|
||||
|
||||
### 3.2 Extract — nouveau worker (miroir `service/ocr.go`, `cmd/server/main.go`)
|
||||
|
||||
- Nouveau `backend/extract/rules.go` : pur package, **testable sans PG** — entrée `text string`, sortie `[]Fact{Type, ValueText/Num/Date, Confidence}`. Règles FR : dates (`12/03/2026`, `mars 2026`, `échéance ..`), montants (`1 234,56 €`, `EUR`), raisons sociales (ligne en capitales + SIRET/SIREN voisin), références (`n°`, `facture n°`, `contrat`, IBAN masqué → on ne stocke que les 4 derniers chiffres).
|
||||
- Nouveau `backend/service/extract.go` : struct `Extract{Repository}` + `Run(ctx)` — même squelette que `Ocr.Run` (poll file, `FOR UPDATE SKIP LOCKED`, `QueuePollInterval`, `ResetStale` au boot). Déclencheur : après chaque OCR `done` avec texte non vide (appel direct `Extract.Enqueue/Process` depuis `Ocr.process`, pas de nouvelle file PG — une colonne `extract_status` sur `ocr_jobs` ou simple appel synchrone borné suffit en V3).
|
||||
- `backend/repository/facts.go` : `UpsertAuto(resourceID, facts)` (remplace les `origin='auto'`, ne touche jamais aux `origin='user'`), `Correct(resourceID, facts)` (écrit `origin='user'`, confidence 1.0), `ListForResource`, `SearchFacets(...)`.
|
||||
- `backend/repository/links.go` : `EnsureAuto` (idempotent, upsert par `(from,to,type)`), `SetUserVerdict` (confirme/invalide → `created_by='user'`, confidence 0/1), `ListForResource`. Règles auto V3 : `same_scan` (déterministe depuis `scan_sessions` — en fait côté mobile, voir §4, serveur : `duplicate_of` via `content_hash` égal, `version_of` via `resource_versions`, `same_issuer` via facts `issuer` normalisé identique + confidence = min des deux facts).
|
||||
- `backend/service/resources.go` : trash/restore/purge (`Trash` = set `trashed_at`, `Restore` = NULL, `Purge` = hard delete + versions + faits + liens + fichier physique) ; overwrite d'octets → pousser l'ancienne copie en `resource_versions` avant écriture.
|
||||
- `backend/service/sync.go` : nouvelles ops outbox **`trash_resource` / `restore_resource` / `purge_resource` / `correct_facts`** (même enveloppe §6.1, `operation_id` 32-hex, idempotence `(device_id, operation_id)`, arrêt au premier échec non-idempotent). `delete_resource` legacy → mappé vers `trash_resource` (pas de break client existant). `correct_facts` porte `[{fact_type, value, confidence}]`, applique `Correct` (writer = user, écrase l'auto).
|
||||
- `backend/handlers/` : `GET /trash` (liste `trashed`, paginé, même enveloppe `{data,meta}`), `POST /files/:id/restore`, `DELETE /files/:id?mode=purge` (défaut trash ; `purge` = définitif), `GET /files/:id/versions` + `POST /files/:id/versions/:n/restore`, `GET /files/:id/facts` + `PATCH /files/:id/facts` (correction directe, même sémantique que `correct_facts`), `GET /files/:id/relations` + `POST /files/:id/relations/verdict` (livrés S5 — `/links` étant pris par les liens de partage), `GET /duplicates` (livré S5 B2), `GET /users/me/usage` (quota + copies 3-2-1 : `used_bytes`, `quota_bytes`, `copies`, `offsite_ok`), `GET /devices` + `DELETE /devices/:id` (A1, lecture `storage_locations` + révocation), `GET /export` (C3, zip + manifest, streamé). Tous scopés `viewer+` lecture / `owner` mutation sauf `correct_facts` (`editor+`, comme `update_metadata`).
|
||||
- `backend/handlers/router.go` + `cmd/server/router_test.go` : enregistrer les routes (le test de contrat miroir du client doit rester vert).
|
||||
- `docs/api-v1.md` §6.1/§7 : documenter les 4 ops + codes (`TRASHED` vs `NOT_FOUND` — une ressource trashée reste `NOT_FOUND` en lecture normale, visible seulement via `/trash`).
|
||||
|
||||
### 3.3 Recherche serveur
|
||||
|
||||
- `repository/resources.go` `SearchFilesVisible` : brancher `search_tsv @@ plainto_tsquery('french', q)` en OR avec le `LIKE` existant (compat), rank `ts_rank` + bonus si match facts (`issuer`, `reference`). Jamais d'exclusion sur fait basse-confiance : `confidence < 0.5` → pondération /2, pas filtre.
|
||||
- Facettes : `GET /files/search?q=&issuer=&from=&to=&min_amount=&max_amount=&doc_type=` — filtres SQL sur `resource_facts` (jointure, `origin` indifférent, `doc_type` = suggestion acceptée comme filtre mais affichée « suggéré » si `origin='auto'`).
|
||||
|
||||
## 4. Mobile — plan fichier par fichier
|
||||
|
||||
### 4.1 Room (v19 → v20 faits, v21 FTS — voir S3/S4, `app/schemas/` exporté, tests `MigrationXToYTest`)
|
||||
|
||||
- v19 (S1, livré) : `trashed_at` sur files + folders.
|
||||
- v20 (S3, livré) : `resource_facts (id, resource_id, fact_type, value_text, value_num, value_date, confidence, origin, updated_at)` + `FactsDao` (remplacement en bloc au sync, corrections `user` poussées en outbox).
|
||||
- v22 (S5, livré) : `resource_links` (miroir `GET /relations`, remplacement en bloc, verdicts REST directs) + `LinksDao` + `collections` (requêtes sauvegardées locales, jamais syncées) + `CollectionDao`. `is_favorite` reporté (non bloquant, S6+ si besoin).
|
||||
- v21 (S4) : FTS4 `files_fts (name, ocr_text, facts_text)` + triggers sync + requête `searchDocuments(query, facets)` remplaçant `searchWithFilters` pour les docs (garder l'ancien pour dossiers). `facts_text` = concat des `value_text` — reconstruit à chaque sync facts.
|
||||
- `OutboxRepository.kt` : `enqueueTrash/restore/purge`, `enqueueCorrectFacts` (payload `[{fact_type, value}]`, `GenerateId` 32-hex, même transaction Room que la mutation parente — contrat outbox atomique inchangé). `delete_resource` existant → renommé en trash côté appelants (`FileDeleter`, `FolderDeleter`).
|
||||
|
||||
### 4.2 Review = moment de vérité (zéro écran en plus)
|
||||
|
||||
- `OcrRepository.syncFromServer` : après `ocr_text`, rapatrier `GET /files/:id/facts` → Room (même pattern `syncFromServer`, poll existant réutilisé).
|
||||
- Fiche document (livré S3 — pas le review : le deck ne contient que des fichiers jamais OCR-és, sans faits) : fiche « Ce qu'on a compris » (`FactsBottomSheet`, bouton viewer si `processed`) — chips `Émetteur: EDF • 84,20 € • Échéance: 12/04 (suggéré)`, confiance < 0.5 grisée, user gagne par type à l'affichage, édition par type (texte/nombre/date ISO) → `enqueueCorrectFacts` dans la **même transaction** que l'écriture Room optimiste, reset « rétablir auto » (jeu vide poussé).
|
||||
- Fiche document : section « Voir aussi » (`LinksDao` → `same_scan` d'abord avec badge « sûr », puis autres avec bouton invalider) + section « Faits » (grisé si `confidence < 0.5`).
|
||||
|
||||
### 4.3 Relations `same_scan` (déterministe, local)
|
||||
|
||||
- `ScanExport.kt` : à l'export multi-pages, écrire les liens `same_scan` entre les `resource_id` de la session (même transaction que les `create_resource`). Confiance 1.0, `created_by='user'` (acte volontaire). Poussés via `correct_facts`-like ? Non — nouvelle op `link_pages` ou piggyback sur `create_resource` payload (préféré : champ `scanSessionId` dans le payload `create_resource`, le serveur en déduit `same_scan` — **zéro nouvelle op**).
|
||||
- Collections virtuelles : table locale `collections + collection_members` (requêtes sauvées sur facts, ex. « Impôts 2026 » = `doc_type suggéré:impôt + period:2026`) — pur local en V3, pas de sync (un device, pas de conflit). Sync serveur = V4.
|
||||
|
||||
### 4.4 Recherche + facettes UI
|
||||
|
||||
- Barre unique : texte + chips facettes (`émetteur`, `période`, `montant`, `type suggéré`). Requête Room FTS offline ; si réseau, `GET /files/search` enrichit (même ranking affiché, source badgée « serveur »).
|
||||
- OutboxSyncWorker : après drain, enchaîner `FactsSync` (comme `OcrAutoWorker` aujourd'hui) — pas de nouveau worker WM, simple étape dans le worker existant.
|
||||
|
||||
### 4.5 Partage sobre + confiance
|
||||
|
||||
- Sheet existant : expiration lien **7j par défaut** (aujourd'hui `null`), badge « suggéré » sur `doc_type`, section copies déjà là (placements V2) + ligne quota/3-2-1 (`GET /users/me/usage` → Réglages/Stockage existant).
|
||||
- `SessionManager` : `tv` claim (voir §3.1 `token_version`) — changement de password → autres devices 401 → re-login silencieux existant (aucun code nouveau, juste le claim).
|
||||
|
||||
## 5. Ordre d'exécution (7–9 semaines, chaque ligne = shippable)
|
||||
|
||||
1. **S1 — Filet + A3** (livré : BE `000017/000018` + trash/restore/purge + cron purge 30j + mobile v19 + `FileDeleter` → trash + gardes walk + écran Corbeille) — done quand : supprimer → restaure depuis un 2e device, le walk ne ressuscite pas.
|
||||
2. **S2 — Extract générique** (livré : `extract/rules.go` + `service/extract.go` + `000019` + `UpsertAuto` + `GET /files/:id/facts`) — done quand : un scan EDF propose émetteur/montant/échéance.
|
||||
3. **S3 — Fiche + correction** (livré : `PATCH /files/:id/facts` + op `correct_facts` editor+ + Room v20 facts + fiche viewer + `FactsRepository` + B4 a11y fiche) — done quand : corriger en un tap converge serveur ↔ 2e device.
|
||||
4. **S4 — Recherche** (livré : `000020` FTS + facettes serveur + Room FTS v21 + UI filtres + B4 a11y) — done quand : « facture edf mars » trouve `scan_042.pdf` offline ET online.
|
||||
5. **S5 — Relations + B2** (livré : `000021` links + doublons SHA-256 + same_scan outbox + `GET /relations` + verdict + `GET /duplicates` + Room v22 + « Voir aussi » + Doublons + collections locales) — done quand : fiche CNI → verso + doublon listés, invalidables ; doublons chiffrés en Mo récupérables.
|
||||
6. **S6 — Partage sobre + A1/A2/A4 + B1 (+B5 si temps)** (livré : expiration liens 7j défaut + `000022` token_version + `000023` quota idempotente + `413 QUOTA_EXCEEDED` + `GET /devices` + `DELETE /devices/:id` + `EnforceDeviceExists` prod + écran Appareils + quota/usage + `update_metadata{baseUpdatedAt}` + `conflicts[]` + pipeline mobile + import « classeur » unifié + onboarding cercle Famille) — done quand : lien RIB expiré → `NOT_FOUND`, password changé → autres devices re-logués, device perdu révoqué, quota plein → 413 propre, rename concurrent → badge « renommé ailleurs ».
|
||||
7. **S7 — Preview + export** (livré : `000024` previews + `GET /files/:id/preview` JPEG 512 — images pur Go, PDF via `pdftoppm` + poppler, `NO_PREVIEW` sinon — + vignettes distantes mobile + `POST /export` zip AES + manifest + partage ACTION_SEND + release 1.2.0). Impression papier reportée V4 (pas de dep print).
|
||||
|
||||
## 6. Tests (mêmes harnais, pas de nouveau framework)
|
||||
|
||||
- BE : `dbtest` + `go test ./...` — `extract/rules_test.go` (corpus FR : factures, bulletins, courriers, cas limites montants/dates), tests repo `facts/links` (upsert auto n'écrase pas user, recherche ne filtre pas sur low-confidence), tests handlers (trash/restore/purge, versions, facts, links, usage, devices, export, quota 413, conflit rename), `router_test.go` vert.
|
||||
- Mobile : `:app:testDebugUnitTest` (Robolectric + Room in-memory) — `Migration18To19/19To20Test`, `FactsSyncTest` (round-trip `correct_facts`), `SearchFtsTest` (accents, `%`/`_` échappés, facettes), `SameScanTest` (export multi-pages → liens), `TrashWalkTest` (walk n ressuscite pas les trashés, miroir FEEDBACK M5), `DuplicatesTest`, `QuotaBadgeTest`.
|
||||
|
||||
## 7. Non-goals V3 (actés, basculables V4)
|
||||
|
||||
Alertes d'échéance, résumés/chat LLM, embeddings sémantiques (déscopés au profit de C3 export — tables `facts`/`links` déjà prêtes à les accueillir en V4), analyse d'image biométrique/sémantique (visages, lieux, événements, clustering personnes — **hors vision par principe**, les photos restent des documents image), OCR on-device, transcodage vidéo, E2EE dédiée (les facts/liens héritent des perms `viewer+` comme `ocr_text` — pas de nouveau modèle de droits), sync des collections, client desktop/web, fédération, `retention_policies` serveur (C1 enterré : rétention = affaire locale), `sync_queue` serveur (C2 enterré : l'outbox client fait le job).
|
||||
+4
-2
@@ -17,14 +17,16 @@ RUN CGO_ENABLED=0 GOOS=linux go build \
|
||||
-o /out/kazier-server \
|
||||
./cmd/server/main.go
|
||||
|
||||
# --- runtime: mince, Tesseract (fra+eng) embarqué pour l'OCR ---
|
||||
# --- runtime: mince, Tesseract (fra+eng) embarqué pour l'OCR + poppler
|
||||
# (pdftoppm) pour les vignettes PDF page 1 (V3 S7, best-effort) ---
|
||||
FROM alpine:3.20
|
||||
RUN apk add --no-cache \
|
||||
ca-certificates \
|
||||
tzdata \
|
||||
tesseract-ocr \
|
||||
tesseract-ocr-data-eng \
|
||||
tesseract-ocr-data-fra
|
||||
tesseract-ocr-data-fra \
|
||||
poppler-utils
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
|
||||
@@ -61,6 +61,9 @@ func main() {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
handlers.Auth = authManager
|
||||
// V3 S6 A1 : les tokens d'un device révoqué (ligne `devices` supprimée)
|
||||
// sont rejetés en 401 — révocation réelle, pas décorative.
|
||||
handlers.EnforceDeviceExists = true
|
||||
|
||||
repo := repository.NewRepository(conn)
|
||||
|
||||
@@ -87,6 +90,28 @@ func main() {
|
||||
)
|
||||
handlers.Ocr = service.NewOcr(repo, cfg.UploadDir, cfg.OcrLang, ocr.NewTesseract())
|
||||
|
||||
// V3 S2 : faits génériques — chaque texte OCR extrait alimente
|
||||
// `resource_facts` (origin='auto'). Enrichissement best-effort : un
|
||||
// échec d'extract ne fait jamais échouer le job OCR.
|
||||
extractSvc := service.NewExtract(repo)
|
||||
handlers.Ocr.OnText = func(userID, resourceID, text string) {
|
||||
if err := extractSvc.ProcessText(userID, resourceID, text); err != nil {
|
||||
log.Printf("extract: %s: %v", resourceID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// V3 S5 : backfill des hashes (uploads d'avant la V3) — doublons.
|
||||
go func() {
|
||||
n, err := handlers.Store.BackfillHashes(200)
|
||||
if err != nil {
|
||||
log.Printf("content backfill hashes: %v", err)
|
||||
return
|
||||
}
|
||||
if n > 0 {
|
||||
log.Printf("content backfill hashes: %d fichiers", n)
|
||||
}
|
||||
}()
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
@@ -102,6 +127,47 @@ func main() {
|
||||
}()
|
||||
log.Printf("ocr worker démarré (%d workers)", cfg.OcrWorkers)
|
||||
|
||||
// V3 S2 : backfill extract (une passe bornée au boot — les ressources
|
||||
// avec ocr_text mais sans faits auto, ex. OCR d'avant la V3).
|
||||
go func() {
|
||||
n, err := extractSvc.Backfill(200)
|
||||
if err != nil {
|
||||
log.Printf("extract backfill: %v", err)
|
||||
return
|
||||
}
|
||||
if n > 0 {
|
||||
log.Printf("extract backfill: %d ressources enrichies", n)
|
||||
}
|
||||
}()
|
||||
|
||||
// V3 corbeille : passe de purge (trash > TrashRetention) au boot puis
|
||||
// toutes les heures. Best-effort : une passe ratée sera rejouée à la
|
||||
// suivante, jamais de crash serveur sur une erreur de purge.
|
||||
go func() {
|
||||
purge := func() {
|
||||
n, err := handlers.Store.PurgeExpiredTrash(service.TrashRetention)
|
||||
if err != nil {
|
||||
log.Printf("purge corbeille: %v", err)
|
||||
return
|
||||
}
|
||||
if n > 0 {
|
||||
log.Printf("purge corbeille: %d ressources détruites", n)
|
||||
}
|
||||
}
|
||||
purge()
|
||||
ticker := time.NewTicker(time.Hour)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
purge()
|
||||
}
|
||||
}
|
||||
}()
|
||||
log.Printf("purge corbeille démarrée (rétention %s)", service.TrashRetention)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", cfg.Port),
|
||||
Handler: newRouter(),
|
||||
|
||||
@@ -17,15 +17,30 @@ var expectedRoutes = []string{
|
||||
"GET /api/v1/files/folders",
|
||||
"GET /api/v1/files/:id",
|
||||
"GET /api/v1/files/:id/ocr",
|
||||
"GET /api/v1/files/:id/facts",
|
||||
"PATCH /api/v1/files/:id/facts",
|
||||
"GET /api/v1/files/:id/content",
|
||||
"GET /api/v1/files/:id/preview",
|
||||
"GET /api/v1/files/:id/shares",
|
||||
"GET /api/v1/files/:id/links",
|
||||
"GET /api/v1/files/:id/placements",
|
||||
"GET /api/v1/files/:id/versions",
|
||||
"GET /api/v1/files/:id/relations",
|
||||
"POST /api/v1/files/:id/relations/verdict",
|
||||
"GET /api/v1/duplicates",
|
||||
"POST /api/v1/export",
|
||||
"POST /api/v1/files/:id/restore",
|
||||
"POST /api/v1/files/:id/versions/:n/restore",
|
||||
"GET /api/v1/trash",
|
||||
"DELETE /api/v1/files/:id",
|
||||
"POST /api/v1/files/upload",
|
||||
|
||||
"GET /api/v1/users/resolve",
|
||||
"PATCH /api/v1/users/me/password",
|
||||
"GET /api/v1/users/me/usage",
|
||||
|
||||
"GET /api/v1/devices",
|
||||
"DELETE /api/v1/devices/:id",
|
||||
|
||||
"POST /api/v1/groups",
|
||||
"GET /api/v1/groups",
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
DROP INDEX IF EXISTS idx_resources_trashed_active;
|
||||
@@ -0,0 +1,7 @@
|
||||
-- V3 corbeille : `deleted_at` est le timestamp de trash (restauration =
|
||||
-- `deleted_at = NULL`, purge = hard delete après 30j via PurgeExpired).
|
||||
-- Toutes les lectures filtrent déjà `deleted_at IS NULL` : les trashés sont
|
||||
-- invisibles partout sauf via les requêtes dédiées (ListTrashed). Cet index
|
||||
-- partiel accélère la corbeille (`GET /trash`) et la passe de purge.
|
||||
CREATE INDEX IF NOT EXISTS idx_resources_trashed_active
|
||||
ON resources(user_id, deleted_at) WHERE deleted_at IS NOT NULL;
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS resource_versions;
|
||||
@@ -0,0 +1,19 @@
|
||||
-- V3 versions : historique léger des octets écrasés (N=3, cf. V3.md P1).
|
||||
-- À chaque overwrite (upload ciblé), l'ancienne copie physique est déplacée
|
||||
-- sous `stored_path` (relatif à UPLOAD_DIR) et sa métadonnée figée ici.
|
||||
-- Restaurer = recopier vers l'emplacement live (l'actuel est à son tour
|
||||
-- versionné). `ON DELETE CASCADE` : purger la ressource purge ses versions.
|
||||
CREATE TABLE resource_versions (
|
||||
id TEXT PRIMARY KEY CHECK (id ~ '^[0-9a-f]{32}$'),
|
||||
resource_id TEXT NOT NULL REFERENCES resources(resource_id) ON DELETE CASCADE,
|
||||
version_no INT NOT NULL CHECK (version_no >= 1),
|
||||
size_bytes BIGINT NOT NULL DEFAULT 0,
|
||||
mime_type VARCHAR(255),
|
||||
extension VARCHAR(64),
|
||||
content_hash VARCHAR(128),
|
||||
stored_path TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
UNIQUE (resource_id, version_no)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_versions_resource ON resource_versions(resource_id);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS resource_facts;
|
||||
@@ -0,0 +1,23 @@
|
||||
-- V3 extract générique (S2) : faits universels extraits du texte OCR
|
||||
-- (dates, montants, émetteurs, références, type suggéré) — cf. V3.md §2.
|
||||
-- `doc_type` n'est qu'un fait comme un autre (suggestion, jamais gate) ;
|
||||
-- `origin` distingue l'auto (remplaçable à chaque passe) du `user` (S3,
|
||||
-- correction en un tap — l'auto ne l'écrase jamais). Cascade : purger la
|
||||
-- ressource purge ses faits.
|
||||
CREATE TABLE resource_facts (
|
||||
id TEXT PRIMARY KEY CHECK (id ~ '^[0-9a-f]{32}$'),
|
||||
resource_id TEXT NOT NULL REFERENCES resources(resource_id) ON DELETE CASCADE,
|
||||
fact_type TEXT NOT NULL CHECK (fact_type IN (
|
||||
'date', 'due_date', 'period', 'amount', 'issuer', 'reference', 'doc_type'
|
||||
)),
|
||||
value_text TEXT,
|
||||
value_num NUMERIC,
|
||||
value_date DATE,
|
||||
confidence REAL NOT NULL CHECK (confidence >= 0 AND confidence <= 1),
|
||||
origin TEXT NOT NULL DEFAULT 'auto' CHECK (origin IN ('auto', 'user')),
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_facts_resource ON resource_facts(resource_id);
|
||||
CREATE INDEX idx_facts_type_value ON resource_facts(fact_type, value_text);
|
||||
@@ -0,0 +1,2 @@
|
||||
DROP INDEX IF EXISTS idx_resources_search_tsv;
|
||||
ALTER TABLE resources DROP COLUMN IF EXISTS search_tsv;
|
||||
@@ -0,0 +1,13 @@
|
||||
-- V3 recherche (S4) : plein-texte français sur (nom + OCR).
|
||||
-- Colonne générée stockée (`to_tsvector(regconfig, text)` est IMMUTABLE,
|
||||
-- donc autorisée en GENERATED) : pas de trigger, backfill automatique des
|
||||
-- lignes existantes. Index GIN. Les faits (`resource_facts`) sont joints à
|
||||
-- la requête (jamais dupliqués) : ils pondèrent le rang sans jamais exclure
|
||||
-- (humble par design — une confiance < 0.5 déclasse, cf. SearchDocuments).
|
||||
ALTER TABLE resources
|
||||
ADD COLUMN search_tsv TSVECTOR
|
||||
GENERATED ALWAYS AS (
|
||||
to_tsvector('french', COALESCE(name, '') || ' ' || COALESCE(ocr_text, ''))
|
||||
) STORED;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_resources_search_tsv ON resources USING GIN (search_tsv);
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE resources DROP COLUMN IF EXISTS scan_session_id;
|
||||
DROP TABLE IF EXISTS resource_links;
|
||||
@@ -0,0 +1,32 @@
|
||||
-- V3 relations documentaires (S5) : graphe auto + verdicts utilisateur.
|
||||
-- `resource_links` : arêtes orientées (from_id → to_id) typées, avec
|
||||
-- confiance et origine. `duplicate_of` (même content_hash), `same_scan`
|
||||
-- (même session de scan, déterministe), `same_issuer` / `same_reference`
|
||||
-- (faits normalisés égaux). `version_of` non matérialisé : les versions
|
||||
-- vivent DANS la ressource (resource_versions, 000018), pas entre ressources.
|
||||
-- `created_by` : 'auto' (dérivé serveur, écrasable) vs 'user' (verdict
|
||||
-- explicite — la réconciliation ne touche jamais aux verdicts). Les
|
||||
-- invalidations (confidence 0) sont conservées (pas de re-création auto).
|
||||
-- `resources.scan_session_id` : session de scan opaque fournie par le client
|
||||
-- dans le payload `create_resource` (zéro nouvelle op) — le serveur en
|
||||
-- déduit les arêtes `same_scan`.
|
||||
CREATE TABLE resource_links (
|
||||
id TEXT PRIMARY KEY CHECK (id ~ '^[0-9a-f]{32}$'),
|
||||
from_id TEXT NOT NULL REFERENCES resources(resource_id) ON DELETE CASCADE,
|
||||
to_id TEXT NOT NULL REFERENCES resources(resource_id) ON DELETE CASCADE,
|
||||
link_type TEXT NOT NULL CHECK (link_type IN (
|
||||
'same_scan', 'duplicate_of', 'same_issuer', 'same_reference'
|
||||
)),
|
||||
confidence REAL NOT NULL CHECK (confidence >= 0 AND confidence <= 1),
|
||||
created_by TEXT NOT NULL DEFAULT 'auto' CHECK (created_by IN ('auto', 'user')),
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
CHECK (from_id != to_id),
|
||||
UNIQUE (from_id, to_id, link_type)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_links_from ON resource_links(from_id);
|
||||
CREATE INDEX idx_links_to ON resource_links(to_id);
|
||||
|
||||
ALTER TABLE resources ADD COLUMN scan_session_id TEXT;
|
||||
CREATE INDEX IF NOT EXISTS idx_resources_scan_session ON resources(user_id, scan_session_id)
|
||||
WHERE scan_session_id IS NOT NULL;
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE users DROP COLUMN IF EXISTS token_version;
|
||||
@@ -0,0 +1,6 @@
|
||||
-- V3 S6 : révocation des tokens au changement de mot de passe.
|
||||
-- `token_version` est bumpé par PATCH /users/me/password ; le middleware
|
||||
-- rejette les tokens dont le claim `tv` diffère (401). Défaut 0 : les tokens
|
||||
-- pré-V3 (sans claim `tv`, lus comme 0) restent valides jusqu'au premier
|
||||
-- changement de mot de passe — montée en version sans coupure.
|
||||
ALTER TABLE users ADD COLUMN token_version INT NOT NULL DEFAULT 0;
|
||||
@@ -0,0 +1,2 @@
|
||||
-- No-op : la colonne prédate cette migration (000001), on ne la supprime pas.
|
||||
SELECT 1;
|
||||
@@ -0,0 +1,6 @@
|
||||
-- V3 S6 (A2) : quota de stockage par compte (10 Go par défaut, V2.md §3.1).
|
||||
-- Garde-fou à l'upload (413 QUOTA_EXCEEDED) + affichage `GET /users/me/usage`.
|
||||
-- L'usage est calculé (somme `size_bytes` des fichiers vifs), jamais stocké.
|
||||
-- Note : la colonne existe depuis 000001 (modèle V2) — IF NOT EXISTS pour
|
||||
-- les bases créées avant/après, sans double ajout.
|
||||
ALTER TABLE users ADD COLUMN IF NOT EXISTS storage_quota_bytes BIGINT NOT NULL DEFAULT 10737418240;
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS resource_previews;
|
||||
@@ -0,0 +1,13 @@
|
||||
-- V3 S7 : vignettes serveur pour les fichiers cloud-only (la galerie reste
|
||||
-- < 1 s sans rapatrier les octets). `resource_previews` mémorise la
|
||||
-- génération servie (taille + updated_at sources) pour ne régénérer que sur
|
||||
-- changement ; le fichier vit sous UPLOAD_DIR/.previews/<resource_id>.jpg
|
||||
-- (jamais versionné, reconstruit à la demande). Images : pur Go (stdlib +
|
||||
-- x/image). PDF : `pdftoppm` (poppler, cf. Dockerfile) page 1 ; sans l'outil
|
||||
-- ou autre type → pas de preview (404 NO_PREVIEW, l'UI montre l'icône).
|
||||
CREATE TABLE resource_previews (
|
||||
resource_id TEXT PRIMARY KEY REFERENCES resources(resource_id) ON DELETE CASCADE,
|
||||
src_size_bytes BIGINT NOT NULL,
|
||||
src_updated_at TIMESTAMPTZ NOT NULL,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
@@ -369,6 +369,155 @@ func TestMigrationsUpDown(t *testing.T) {
|
||||
// --- 000016 : groupes + cible groupe sur shares -------------------------
|
||||
assertTables(t, conn, "users", "devices", "resources", "operations", "ocr_jobs", "shares", "share_links", "storage_locations", "resource_placements", "groups", "group_members", "schema_migrations")
|
||||
|
||||
// --- 000017 : corbeille (index partiel deleted_at) ----------------------
|
||||
var trashedIdx int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM pg_index i
|
||||
JOIN pg_class t ON t.oid = i.indrelid
|
||||
WHERE t.relname = 'resources' AND i.indisvalid
|
||||
AND pg_get_indexdef(i.indexrelid) LIKE '%idx_resources_trashed_active%'`).Scan(&trashedIdx)
|
||||
if err != nil {
|
||||
t.Fatalf("000017 index: %v", err)
|
||||
}
|
||||
if trashedIdx == 0 {
|
||||
t.Error("index idx_resources_trashed_active manquant après 000017")
|
||||
}
|
||||
|
||||
// --- 000018 : versions --------------------------------------------------
|
||||
assertTables(t, conn, "users", "devices", "resources", "operations", "ocr_jobs", "shares", "share_links", "storage_locations", "resource_placements", "groups", "group_members", "resource_versions", "schema_migrations")
|
||||
|
||||
assertHexCheck(t, conn, "resource_versions", "id")
|
||||
|
||||
var versionUniq int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM pg_index i
|
||||
JOIN pg_class t ON t.oid = i.indrelid
|
||||
WHERE t.relname = 'resource_versions' AND i.indisunique
|
||||
AND ARRAY(SELECT a.attname FROM unnest(i.indkey) WITH ORDINALITY k(attnum, ord)
|
||||
JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum
|
||||
ORDER BY k.ord)::text[] = ARRAY['resource_id','version_no']`).Scan(&versionUniq)
|
||||
if err != nil {
|
||||
t.Fatalf("versions unique: %v", err)
|
||||
}
|
||||
if versionUniq == 0 {
|
||||
t.Error("contrainte UNIQUE(resource_id, version_no) manquante sur resource_versions après 000018")
|
||||
}
|
||||
|
||||
// --- 000019 : faits génériques -----------------------------------------
|
||||
assertTables(t, conn, "users", "devices", "resources", "operations", "ocr_jobs", "shares", "share_links", "storage_locations", "resource_placements", "groups", "group_members", "resource_versions", "resource_facts", "schema_migrations")
|
||||
|
||||
assertHexCheck(t, conn, "resource_facts", "id")
|
||||
|
||||
var factTypeCheck int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM pg_constraint c
|
||||
JOIN pg_class t ON t.oid = c.conrelid
|
||||
WHERE t.relname = 'resource_facts'
|
||||
AND pg_get_constraintdef(c.oid) LIKE '%doc_type%'
|
||||
AND pg_get_constraintdef(c.oid) LIKE '%due_date%'`).Scan(&factTypeCheck)
|
||||
if err != nil {
|
||||
t.Fatalf("check resource_facts.fact_type: %v", err)
|
||||
}
|
||||
if factTypeCheck == 0 {
|
||||
t.Error("CHECK fact_type manquant sur resource_facts après 000019")
|
||||
}
|
||||
|
||||
// --- 000020 : plein-texte ----------------------------------------------
|
||||
var tsvCol int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'resources' AND column_name = 'search_tsv'`).Scan(&tsvCol)
|
||||
if err != nil {
|
||||
t.Fatalf("search_tsv column: %v", err)
|
||||
}
|
||||
if tsvCol == 0 {
|
||||
t.Error("colonne resources.search_tsv manquante après 000020")
|
||||
}
|
||||
|
||||
var tsvIdx int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM pg_index i
|
||||
JOIN pg_class t ON t.oid = i.indrelid
|
||||
WHERE t.relname = 'resources' AND i.indisvalid
|
||||
AND pg_get_indexdef(i.indexrelid) LIKE '%idx_resources_search_tsv%'`).Scan(&tsvIdx)
|
||||
if err != nil {
|
||||
t.Fatalf("000020 index: %v", err)
|
||||
}
|
||||
if tsvIdx == 0 {
|
||||
t.Error("index idx_resources_search_tsv manquant après 000020")
|
||||
}
|
||||
|
||||
// --- 000021 : graphe documentaire ----------------------------------------
|
||||
assertTables(t, conn, "users", "devices", "resources", "operations", "ocr_jobs", "shares", "share_links", "storage_locations", "resource_placements", "groups", "group_members", "resource_versions", "resource_facts", "resource_links", "schema_migrations")
|
||||
|
||||
// resource_links.id : CHECK 32-hex (requête ciblée — le helper
|
||||
// générique matcherait `from_id <> to_id` qui contient aussi "id").
|
||||
var linksIDCheck string
|
||||
err = conn.QueryRow(`
|
||||
SELECT pg_get_constraintdef(c.oid)
|
||||
FROM pg_constraint c
|
||||
JOIN pg_class t ON t.oid = c.conrelid
|
||||
WHERE t.relname = 'resource_links' AND c.conname = 'resource_links_id_check'`).Scan(&linksIDCheck)
|
||||
if err != nil {
|
||||
t.Fatalf("contrainte CHECK(resource_links.id) manquante: %v", err)
|
||||
}
|
||||
if !strings.Contains(linksIDCheck, "^[0-9a-f]{32}$") {
|
||||
t.Errorf("CHECK resource_links.id attendu 32-hex, obtenu : %s", linksIDCheck)
|
||||
}
|
||||
|
||||
var linksUniq int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM pg_index i
|
||||
JOIN pg_class t ON t.oid = i.indrelid
|
||||
WHERE t.relname = 'resource_links' AND i.indisunique
|
||||
AND ARRAY(SELECT a.attname FROM unnest(i.indkey) WITH ORDINALITY k(attnum, ord)
|
||||
JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum
|
||||
ORDER BY k.ord)::text[] = ARRAY['from_id','to_id','link_type']`).Scan(&linksUniq)
|
||||
if err != nil {
|
||||
t.Fatalf("links unique: %v", err)
|
||||
}
|
||||
if linksUniq == 0 {
|
||||
t.Error("contrainte UNIQUE(from_id, to_id, link_type) manquante sur resource_links après 000021")
|
||||
}
|
||||
|
||||
var scanSessionCol int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'resources' AND column_name = 'scan_session_id'`).Scan(&scanSessionCol)
|
||||
if err != nil {
|
||||
t.Fatalf("scan_session_id column: %v", err)
|
||||
}
|
||||
if scanSessionCol == 0 {
|
||||
t.Error("colonne resources.scan_session_id manquante après 000021")
|
||||
}
|
||||
|
||||
// --- 000022 : révocation tokens ------------------------------------------
|
||||
var tvCol int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'users' AND column_name = 'token_version'`).Scan(&tvCol)
|
||||
if err != nil {
|
||||
t.Fatalf("token_version column: %v", err)
|
||||
}
|
||||
if tvCol == 0 {
|
||||
t.Error("colonne users.token_version manquante après 000022")
|
||||
}
|
||||
|
||||
// --- 000023 : quota (colonne héritée de 000001, IF NOT EXISTS) ------------
|
||||
var quotaCol int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'users' AND column_name = 'storage_quota_bytes'`).Scan("aCol)
|
||||
if err != nil {
|
||||
t.Fatalf("storage_quota_bytes column: %v", err)
|
||||
}
|
||||
if quotaCol == 0 {
|
||||
t.Error("colonne users.storage_quota_bytes manquante après 000023")
|
||||
}
|
||||
|
||||
// --- 000024 : vignettes serveur ------------------------------------------
|
||||
assertTables(t, conn, "users", "devices", "resources", "operations", "ocr_jobs", "shares", "share_links", "storage_locations", "resource_placements", "groups", "group_members", "resource_versions", "resource_facts", "resource_links", "resource_previews", "schema_migrations")
|
||||
|
||||
if err := MigrateDownDatabase(url); err != nil {
|
||||
t.Fatalf("migrate down: %v", err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,408 @@
|
||||
// Package extract derives generic, verifiable facts from OCR text (V3 S2).
|
||||
//
|
||||
// Rules-based French heuristics, zero ML : every fact carries a confidence,
|
||||
// low-confidence facts are suggested (greyed in UI) and user-correctable (S3),
|
||||
// and search demotes but never excludes on them. No LLM, no network, pure
|
||||
// stdlib — it runs inside the OCR worker right after Tesseract.
|
||||
package extract
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// Fact types — mirror of the resource_facts CHECK (migration 000019).
|
||||
type FactType string
|
||||
|
||||
const (
|
||||
FactDate FactType = "date"
|
||||
FactDueDate FactType = "due_date"
|
||||
FactPeriod FactType = "period"
|
||||
FactAmount FactType = "amount"
|
||||
FactIssuer FactType = "issuer"
|
||||
FactReference FactType = "reference"
|
||||
FactDocType FactType = "doc_type"
|
||||
)
|
||||
|
||||
// Fact is one extracted statement. ValueText holds issuer names, reference
|
||||
// codes ("facture:FA-2026-0031"), doc types and periods ("2026-03") ;
|
||||
// ValueNum holds amounts (HasNum) ; ValueDate holds ISO dates ("2026-03-12").
|
||||
type Fact struct {
|
||||
Type FactType
|
||||
ValueText string
|
||||
ValueNum float64
|
||||
HasNum bool
|
||||
ValueDate string
|
||||
Confidence float64
|
||||
}
|
||||
|
||||
// MaxFacts caps the output per resource (OCR garbage must stay bounded).
|
||||
const MaxFacts = 50
|
||||
|
||||
// FromText extracts generic facts from raw OCR text. Deterministic output
|
||||
// order (document order within each type, types in fixed order), duplicates
|
||||
// removed.
|
||||
func FromText(text string) []Fact {
|
||||
e := &extractor{seen: map[string]bool{}}
|
||||
e.extract(collectLines(text))
|
||||
out := e.facts
|
||||
if len(out) > MaxFacts {
|
||||
out = out[:MaxFacts]
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
type extractor struct {
|
||||
facts []Fact
|
||||
seen map[string]bool
|
||||
}
|
||||
|
||||
func (e *extractor) add(f Fact) {
|
||||
if f.Confidence < 0 {
|
||||
f.Confidence = 0
|
||||
}
|
||||
if f.Confidence > 1 {
|
||||
f.Confidence = 1
|
||||
}
|
||||
key := string(f.Type) + "\x00" + f.ValueText + "\x00" + f.ValueDate + "\x00" + fmt.Sprintf("%.2f", f.ValueNum)
|
||||
if e.seen[key] {
|
||||
return
|
||||
}
|
||||
e.seen[key] = true
|
||||
e.facts = append(e.facts, f)
|
||||
}
|
||||
|
||||
type line struct {
|
||||
raw string
|
||||
folded string
|
||||
}
|
||||
|
||||
func collectLines(text string) []line {
|
||||
raw := strings.Split(text, "\n")
|
||||
out := make([]line, 0, len(raw))
|
||||
for _, r := range raw {
|
||||
r = strings.TrimSpace(strings.ReplaceAll(r, "\t", " "))
|
||||
if r == "" {
|
||||
continue
|
||||
}
|
||||
out = append(out, line{raw: r, folded: fold(r)})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (e *extractor) extract(lines []line) {
|
||||
for _, l := range lines {
|
||||
e.extractAmounts(l)
|
||||
e.extractDates(l)
|
||||
e.extractReferences(l)
|
||||
}
|
||||
e.extractIssuers(lines)
|
||||
e.extractDocType(lines)
|
||||
}
|
||||
|
||||
// --- amounts ---------------------------------------------------------------
|
||||
|
||||
// Note : pas de `\b` après `€` — c'est un symbole (non-mot), donc il n'y a
|
||||
// jamais de frontière avec un espace ou une fin de ligne. `eur` garde son
|
||||
// `\b` (évite "europe"), `euros?` n'en a pas besoin.
|
||||
var amountPattern = regexp.MustCompile(`(\d[\d\s\x{a0}\x{202f}]*[,\.]\d{2}|\d[\d\s\x{a0}\x{202f}]*)\s*(€|euros?|eur\b)`)
|
||||
|
||||
func (e *extractor) extractAmounts(l line) {
|
||||
// Percentages are not amounts.
|
||||
if strings.Contains(l.raw, "%") && !strings.ContainsAny(l.raw, "€") {
|
||||
return
|
||||
}
|
||||
for _, m := range amountPattern.FindAllStringSubmatch(l.raw, -1) {
|
||||
num, ok := parseFrenchNumber(m[1])
|
||||
if !ok || num <= 0 || num >= 1e13 {
|
||||
continue
|
||||
}
|
||||
conf := 0.7
|
||||
if amountKeywordPattern.MatchString(l.folded) {
|
||||
conf = 0.9
|
||||
}
|
||||
e.add(Fact{Type: FactAmount, ValueNum: num, HasNum: true, ValueText: formatAmount(num), Confidence: conf})
|
||||
}
|
||||
}
|
||||
|
||||
var amountKeywordPattern = regexp.MustCompile(`\b(total|montant|a payer|net|ttc|solde|du)\b`)
|
||||
|
||||
func formatAmount(num float64) string {
|
||||
return strconv.FormatFloat(num, 'f', 2, 64) + " €"
|
||||
}
|
||||
|
||||
// parseFrenchNumber reads "1 234,56" / "1234.56" / "84 €"-style magnitudes.
|
||||
// Comma = decimal separator (French), dots/spaces (incl. NBSP U+00A0 and
|
||||
// narrow NBSP U+202F, common in "1 234 €") = thousands.
|
||||
func parseFrenchNumber(s string) (float64, bool) {
|
||||
s = strings.ReplaceAll(s, "\u00a0", "")
|
||||
s = strings.ReplaceAll(s, "\u202f", "")
|
||||
s = strings.ReplaceAll(s, " ", "")
|
||||
if strings.Contains(s, ",") {
|
||||
s = strings.ReplaceAll(s, ".", "")
|
||||
s = strings.ReplaceAll(s, ",", ".")
|
||||
}
|
||||
num, err := strconv.ParseFloat(s, 64)
|
||||
return num, err == nil
|
||||
}
|
||||
|
||||
// --- dates -----------------------------------------------------------------
|
||||
|
||||
var numericDatePattern = regexp.MustCompile(`\b(\d{1,2})\s*[/\-.]\s*(\d{1,2})\s*[/\-.]\s*(\d{2}|\d{4})\b`)
|
||||
|
||||
var writtenDatePattern = regexp.MustCompile(`\b(\d{1,2})\s+(janv(?:ier)?|fev(?:rier)?|mars|avr(?:il)?|mai|juin|juil(?:let)?|aout|sept?(?:embre)?|oct(?:obre)?|nov(?:embre)?|dec(?:embre)?)\s+(\d{4})\b`)
|
||||
|
||||
var periodPattern = regexp.MustCompile(`\b(janv(?:ier)?|fev(?:rier)?|mars|avr(?:il)?|mai|juin|juil(?:let)?|aout|sept?(?:embre)?|oct(?:obre)?|nov(?:embre)?|dec(?:embre)?)\s+(\d{4})\b`)
|
||||
|
||||
var monthNumbers = map[string]int{
|
||||
"janv": 1, "janvier": 1, "fev": 2, "fevrier": 2, "mars": 3,
|
||||
"avr": 4, "avril": 4, "mai": 5, "juin": 6, "juil": 7,
|
||||
"juillet": 7, "aout": 8, "sept": 9, "septembre": 9, "oct": 10,
|
||||
"octobre": 10, "nov": 11, "novembre": 11, "dec": 12, "decembre": 12,
|
||||
}
|
||||
|
||||
var dueKeywordPattern = regexp.MustCompile(`\b(echeance|a payer avant|date limite|avant le|regler avant|paiement avant)\b`)
|
||||
|
||||
func fullYear(yy int) int {
|
||||
if yy < 100 {
|
||||
if yy >= 70 {
|
||||
return 1900 + yy
|
||||
}
|
||||
return 2000 + yy
|
||||
}
|
||||
return yy
|
||||
}
|
||||
|
||||
func isoDate(y, m, d int) (string, bool) {
|
||||
if m < 1 || m > 12 || d < 1 || d > 31 || y < 1900 || y > 2100 {
|
||||
return "", false
|
||||
}
|
||||
return fmt.Sprintf("%04d-%02d-%02d", y, m, d), true
|
||||
}
|
||||
|
||||
func (e *extractor) extractDates(l line) {
|
||||
isDue := dueKeywordPattern.MatchString(l.folded)
|
||||
for _, m := range numericDatePattern.FindAllStringSubmatch(l.raw, -1) {
|
||||
d, _ := strconv.Atoi(m[1])
|
||||
mo, _ := strconv.Atoi(m[2])
|
||||
y, _ := strconv.Atoi(m[3])
|
||||
iso, ok := isoDate(fullYear(y), mo, d)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
typ := FactDate
|
||||
conf := 0.6
|
||||
if isDue {
|
||||
typ, conf = FactDueDate, 0.8
|
||||
}
|
||||
e.add(Fact{Type: typ, ValueDate: iso, ValueText: iso, Confidence: conf})
|
||||
}
|
||||
for _, m := range writtenDatePattern.FindAllStringSubmatch(l.folded, -1) {
|
||||
d, _ := strconv.Atoi(m[1])
|
||||
mo := monthNumbers[m[2]]
|
||||
y, _ := strconv.Atoi(m[3])
|
||||
iso, ok := isoDate(y, mo, d)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
typ := FactDate
|
||||
conf := 0.7
|
||||
if isDue {
|
||||
typ, conf = FactDueDate, 0.85
|
||||
}
|
||||
e.add(Fact{Type: typ, ValueDate: iso, ValueText: iso, Confidence: conf})
|
||||
}
|
||||
for _, m := range periodPattern.FindAllStringSubmatch(l.folded, -1) {
|
||||
mo := monthNumbers[m[1]]
|
||||
y, _ := strconv.Atoi(m[2])
|
||||
if mo == 0 || y < 1900 || y > 2100 {
|
||||
continue
|
||||
}
|
||||
e.add(Fact{Type: FactPeriod, ValueText: fmt.Sprintf("%04d-%02d", y, mo), Confidence: 0.7})
|
||||
}
|
||||
}
|
||||
|
||||
// --- references ------------------------------------------------------------
|
||||
|
||||
// referencePattern catches "facture n° FA-2026-0031", "contrat : ABC123",
|
||||
// "client 42" — the code must contain a digit (avoids "FACTURE DU MOIS").
|
||||
var referencePattern = regexp.MustCompile(`(?i)\b(facture|contrat|client|compte|dossier|bulletin|commande|devis|attestation|police)\s*(n°|no|num(?:ero)?|number|ref(?:erence)?)?\s*[:\-#]?\s*([A-Z0-9][A-Z0-9\-/\.]{1,29})`)
|
||||
|
||||
var bareRefPattern = regexp.MustCompile(`\bn°\s*([A-Z0-9][A-Z0-9\-/]{2,20})`)
|
||||
|
||||
var siretPattern = regexp.MustCompile(`(?i)\b(siret|siren|rcs)\b[^\d]{0,10}(\d{3}\s?\d{3}\s?\d{3}(?:\s?\d{5})?)`)
|
||||
|
||||
var ibanPattern = regexp.MustCompile(`\bFR\s?\d{2}(?:\s?[0-9A-Z]{4}){5}\s?[0-9A-Z]{0,3}\b`)
|
||||
|
||||
func hasDigit(s string) bool {
|
||||
for _, r := range s {
|
||||
if r >= '0' && r <= '9' {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (e *extractor) extractReferences(l line) {
|
||||
for _, m := range referencePattern.FindAllStringSubmatch(l.raw, -1) {
|
||||
code := strings.Trim(m[3], "-/.")
|
||||
if !hasDigit(code) || len(code) < 3 {
|
||||
continue
|
||||
}
|
||||
kind := strings.ToLower(m[1])
|
||||
e.add(Fact{Type: FactReference, ValueText: kind + ":" + code, Confidence: 0.75})
|
||||
}
|
||||
for _, m := range bareRefPattern.FindAllStringSubmatch(l.raw, -1) {
|
||||
code := m[1]
|
||||
if !hasDigit(code) {
|
||||
continue
|
||||
}
|
||||
e.add(Fact{Type: FactReference, ValueText: "ref:" + code, Confidence: 0.6})
|
||||
}
|
||||
if m := siretPattern.FindStringSubmatch(l.raw); m != nil {
|
||||
digits := strings.Join(strings.Fields(m[2]), " ")
|
||||
e.add(Fact{Type: FactReference, ValueText: strings.ToLower(m[1]) + ":" + digits, Confidence: 0.9})
|
||||
}
|
||||
if m := ibanPattern.FindString(l.raw); m != "" {
|
||||
compact := strings.ReplaceAll(m, " ", "")
|
||||
if len(compact) == 27 {
|
||||
// Store masked (privacy) : only the last 4 chars.
|
||||
e.add(Fact{Type: FactReference, ValueText: "rib:••••" + compact[len(compact)-4:], Confidence: 0.9})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- issuers ---------------------------------------------------------------
|
||||
|
||||
var companySuffixPattern = regexp.MustCompile(`(?i)\b(sarl|sas|sa|eurl|sasu|sci|eirl|snc|sasau)\b`)
|
||||
|
||||
func isCapsLine(s string) bool {
|
||||
letters := 0
|
||||
for _, r := range s {
|
||||
if unicode.IsLetter(r) {
|
||||
letters++
|
||||
if unicode.IsLower(r) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return letters >= 3
|
||||
}
|
||||
|
||||
func (e *extractor) extractIssuers(lines []line) {
|
||||
found := 0
|
||||
checked := 0
|
||||
for _, l := range lines {
|
||||
if checked >= 8 || found >= 2 {
|
||||
break
|
||||
}
|
||||
raw := strings.TrimSpace(l.raw)
|
||||
if len([]rune(raw)) < 3 || len([]rune(raw)) > 60 {
|
||||
continue
|
||||
}
|
||||
if strings.Contains(raw, "@") || strings.HasPrefix(strings.ToLower(raw), "http") {
|
||||
continue
|
||||
}
|
||||
checked++
|
||||
if !isCapsLine(raw) {
|
||||
continue
|
||||
}
|
||||
// Skip lines that are really dates/amounts/references in disguise.
|
||||
if numericDatePattern.MatchString(raw) || amountPattern.MatchString(raw) || bareRefPattern.MatchString(raw) {
|
||||
continue
|
||||
}
|
||||
conf := 0.45
|
||||
if found == 0 {
|
||||
conf = 0.55
|
||||
}
|
||||
if companySuffixPattern.MatchString(raw) {
|
||||
conf = 0.75
|
||||
}
|
||||
e.add(Fact{Type: FactIssuer, ValueText: raw, Confidence: conf})
|
||||
found++
|
||||
}
|
||||
}
|
||||
|
||||
// --- doc_type suggestion ---------------------------------------------------
|
||||
|
||||
// docTypeKeywords maps a suggested type to weighted French keywords (folded).
|
||||
// A type is emitted when its score reaches docTypeThreshold — the type is a
|
||||
// humble suggestion (S3-correctable), never a gate.
|
||||
var docTypeKeywords = []struct {
|
||||
typ string
|
||||
keywords map[string]int
|
||||
}{
|
||||
{"facture", map[string]int{"facture": 2, "tva": 1, "montant ttc": 2, "total ttc": 2, "a payer": 1, "devis": 1}},
|
||||
{"bulletin", map[string]int{"bulletin": 2, "salaire": 2, "paie": 1, "net social": 2, "brut": 1}},
|
||||
{"impot", map[string]int{"impot": 2, "revenu fiscal": 2, "taxe fonciere": 2, "taxe d habitation": 1, "avis d impot": 2}},
|
||||
{"contrat", map[string]int{"contrat": 2, "bail": 2, "convention": 1, "article ": 1, "assurance": 1}},
|
||||
{"ordonnance", map[string]int{"ordonnance": 2, "posologie": 2, "docteur": 1, "traitement": 1}},
|
||||
{"attestation", map[string]int{"attestation": 2, "certifie": 1, "certificat": 1, "atteste": 1}},
|
||||
{"rib", map[string]int{"rib": 2, "iban": 2, "bic": 1}},
|
||||
{"identite", map[string]int{"carte d identite": 2, "passeport": 2, "permis de conduire": 2, "nationalite": 1}},
|
||||
}
|
||||
|
||||
const docTypeThreshold = 2
|
||||
|
||||
func (e *extractor) extractDocType(lines []line) {
|
||||
joined := "\n"
|
||||
for _, l := range lines {
|
||||
joined += l.folded + "\n"
|
||||
}
|
||||
best, bestScore := "", 0
|
||||
for _, dt := range docTypeKeywords {
|
||||
score := 0
|
||||
for kw, w := range dt.keywords {
|
||||
if strings.Contains(joined, kw) {
|
||||
score += w
|
||||
}
|
||||
}
|
||||
if score > bestScore {
|
||||
best, bestScore = dt.typ, score
|
||||
}
|
||||
}
|
||||
if bestScore >= docTypeThreshold {
|
||||
conf := 0.5 + 0.1*float64(bestScore)
|
||||
if conf > 0.85 {
|
||||
conf = 0.85
|
||||
}
|
||||
e.add(Fact{Type: FactDocType, ValueText: best, Confidence: conf})
|
||||
}
|
||||
}
|
||||
|
||||
// --- folding ---------------------------------------------------------------
|
||||
|
||||
// fold lowercases French text and strips accents (œ→oe, æ→ae, ç→c) for
|
||||
// keyword matching. stdlib-only (no x/text dependency churn).
|
||||
func fold(s string) string {
|
||||
var b strings.Builder
|
||||
b.Grow(len(s))
|
||||
for _, r := range strings.ToLower(s) {
|
||||
switch r {
|
||||
case 'à', 'â', 'ä':
|
||||
b.WriteRune('a')
|
||||
case 'é', 'è', 'ê', 'ë':
|
||||
b.WriteRune('e')
|
||||
case 'î', 'ï':
|
||||
b.WriteRune('i')
|
||||
case 'ô', 'ö':
|
||||
b.WriteRune('o')
|
||||
case 'ù', 'û', 'ü':
|
||||
b.WriteRune('u')
|
||||
case 'ÿ':
|
||||
b.WriteRune('y')
|
||||
case 'ç':
|
||||
b.WriteRune('c')
|
||||
case 'œ':
|
||||
b.WriteString("oe")
|
||||
case 'æ':
|
||||
b.WriteString("ae")
|
||||
default:
|
||||
b.WriteRune(r)
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package extract
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func factsByType(facts []Fact) map[FactType][]Fact {
|
||||
out := map[FactType][]Fact{}
|
||||
for _, f := range facts {
|
||||
out[f.Type] = append(out[f.Type], f)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func hasText(facts []Fact, want string) bool {
|
||||
for _, f := range facts {
|
||||
if f.ValueText == want {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
const edfBill = `EDF - SERVICE CLIENTS
|
||||
TSA 12345
|
||||
Facture n° FA-2026-0031 du 12/03/2026
|
||||
Période : mars 2026
|
||||
Montant total TTC : 1 234,56 €
|
||||
À payer avant le 12/04/2026
|
||||
Référence client : 123456789
|
||||
`
|
||||
|
||||
func TestFactureEDF(t *testing.T) {
|
||||
by := factsByType(FromText(edfBill))
|
||||
|
||||
if !hasText(by[FactReference], "facture:FA-2026-0031") {
|
||||
t.Errorf("référence facture manquante: %+v", by[FactReference])
|
||||
}
|
||||
if !hasText(by[FactDate], "2026-03-12") {
|
||||
t.Errorf("date émission manquante: %+v", by[FactDate])
|
||||
}
|
||||
if !hasText(by[FactDueDate], "2026-04-12") {
|
||||
t.Errorf("échéance manquante: %+v", by[FactDueDate])
|
||||
}
|
||||
if !hasText(by[FactPeriod], "2026-03") {
|
||||
t.Errorf("période manquante: %+v", by[FactPeriod])
|
||||
}
|
||||
found := false
|
||||
for _, f := range by[FactAmount] {
|
||||
if f.HasNum && f.ValueNum == 1234.56 && f.Confidence >= 0.9 {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("montant 1234.56 (conf 0.9) manquant: %+v", by[FactAmount])
|
||||
}
|
||||
if !hasText(by[FactIssuer], "EDF - SERVICE CLIENTS") {
|
||||
t.Errorf("émetteur manquant: %+v", by[FactIssuer])
|
||||
}
|
||||
if !hasText(by[FactDocType], "facture") {
|
||||
t.Errorf("doc_type facture manquant: %+v", by[FactDocType])
|
||||
}
|
||||
}
|
||||
|
||||
const bulletin = `BULLETIN DE SALAIRE
|
||||
DUPONT Jean
|
||||
Période : janvier 2026
|
||||
Salaire brut : 2 845,00 €
|
||||
Net social : 2 210,35 €
|
||||
SIRET 123 456 789 00012
|
||||
`
|
||||
|
||||
func TestBulletinSalaire(t *testing.T) {
|
||||
by := factsByType(FromText(bulletin))
|
||||
if !hasText(by[FactDocType], "bulletin") {
|
||||
t.Errorf("doc_type bulletin manquant: %+v", by[FactDocType])
|
||||
}
|
||||
if !hasText(by[FactPeriod], "2026-01") {
|
||||
t.Errorf("période manquante: %+v", by[FactPeriod])
|
||||
}
|
||||
if !hasText(by[FactReference], "siret:123 456 789 00012") {
|
||||
t.Errorf("siret manquant: %+v", by[FactReference])
|
||||
}
|
||||
amounts := map[float64]bool{}
|
||||
for _, f := range by[FactAmount] {
|
||||
if f.HasNum {
|
||||
amounts[f.ValueNum] = true
|
||||
}
|
||||
}
|
||||
if !amounts[2845.00] || !amounts[2210.35] {
|
||||
t.Errorf("montants bulletin manquants: %+v", by[FactAmount])
|
||||
}
|
||||
}
|
||||
|
||||
const ordonnance = `Dr MARTIN Sophie
|
||||
Ordonnance du 3 mars 2026
|
||||
Doliprane 1000mg — 1 matin et soir pendant 7 jours
|
||||
`
|
||||
|
||||
func TestOrdonnance(t *testing.T) {
|
||||
by := factsByType(FromText(ordonnance))
|
||||
if !hasText(by[FactDocType], "ordonnance") {
|
||||
t.Errorf("doc_type ordonnance manquant: %+v", by[FactDocType])
|
||||
}
|
||||
if !hasText(by[FactDate], "2026-03-03") {
|
||||
t.Errorf("date manquante: %+v", by[FactDate])
|
||||
}
|
||||
if len(by[FactAmount]) != 0 {
|
||||
t.Errorf("1000mg ne doit pas être un montant: %+v", by[FactAmount])
|
||||
}
|
||||
}
|
||||
|
||||
const rib = `RELEVÉ D'IDENTITÉ BANCAIRE
|
||||
Titulaire : DUPONT Jean
|
||||
IBAN : FR76 1234 5678 9012 3456 7890 123
|
||||
BIC : AGRIFRPP
|
||||
`
|
||||
|
||||
func TestRIBMasque(t *testing.T) {
|
||||
by := factsByType(FromText(rib))
|
||||
if !hasText(by[FactDocType], "rib") {
|
||||
t.Errorf("doc_type rib manquant: %+v", by[FactDocType])
|
||||
}
|
||||
if !hasText(by[FactReference], "rib:••••0123") {
|
||||
t.Errorf("IBAN masqué manquant: %+v", by[FactReference])
|
||||
}
|
||||
// L'IBAN complet ne doit jamais fuiter dans les faits.
|
||||
for _, f := range by[FactReference] {
|
||||
if len(f.ValueText) > 20 {
|
||||
t.Errorf("fuite IBAN en clair: %q", f.ValueText)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTexteVideEtBruit(t *testing.T) {
|
||||
if got := FromText(""); len(got) != 0 {
|
||||
t.Errorf("vide → %d faits, attendu 0", len(got))
|
||||
}
|
||||
// Bruit OCR sans structure : pas de doc_type, pas de montant.
|
||||
by := factsByType(FromText("lorem ipsum\nbla bla 99\n///"))
|
||||
if len(by[FactDocType]) != 0 {
|
||||
t.Errorf("doc_type sur bruit: %+v", by[FactDocType])
|
||||
}
|
||||
if len(by[FactAmount]) != 0 {
|
||||
t.Errorf("montant sur bruit: %+v", by[FactAmount])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCourrierSimpleSansType(t *testing.T) {
|
||||
// Tous les documents comptent : un courrier sans mots-clés métier donne
|
||||
// des faits (date) mais aucun doc_type — jamais pénalisé.
|
||||
by := factsByType(FromText("Bonjour,\nmerci de me recontacter.\nCordialement.\nParis, le 05/06/2026\n"))
|
||||
if !hasText(by[FactDate], "2026-06-05") {
|
||||
t.Errorf("date courrier manquante: %+v", by[FactDate])
|
||||
}
|
||||
if len(by[FactDocType]) != 0 {
|
||||
t.Errorf("pas de doc_type attendu: %+v", by[FactDocType])
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnneesCourtesEtFormats(t *testing.T) {
|
||||
by := factsByType(FromText("Fait le 01.02.99\nÉchéance 15-08-26\n"))
|
||||
if !hasText(by[FactDate], "1999-02-01") {
|
||||
t.Errorf("année courte 99 → 1999: %+v", by[FactDate])
|
||||
}
|
||||
if !hasText(by[FactDueDate], "2026-08-15") {
|
||||
t.Errorf("année courte 26 → 2026: %+v", by[FactDueDate])
|
||||
}
|
||||
}
|
||||
|
||||
func TestBornes(t *testing.T) {
|
||||
// Dates invalides ignorées, pas de crash.
|
||||
by := factsByType(FromText("le 45/13/2026 et le 00/00/0000"))
|
||||
if len(by[FactDate])+len(by[FactDueDate]) != 0 {
|
||||
t.Errorf("dates invalides extraites: %+v", by)
|
||||
}
|
||||
// Confiances toujours dans [0,1].
|
||||
for _, f := range FromText(edfBill + bulletin + ordonnance + rib) {
|
||||
if f.Confidence < 0 || f.Confidence > 1 {
|
||||
t.Errorf("confiance hors bornes: %+v", f)
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
-4
@@ -7,11 +7,15 @@ require (
|
||||
github.com/gin-gonic/gin v1.12.0
|
||||
github.com/golang-migrate/migrate/v4 v4.19.1
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/ledongthuc/pdf v0.0.0-20260907135840-6c8c28e0e8a0
|
||||
github.com/lib/pq v1.12.3
|
||||
golang.org/x/crypto v0.52.0
|
||||
golang.org/x/image v0.46.0
|
||||
)
|
||||
|
||||
require (
|
||||
aidanwoods.dev/go-result v0.3.1 // indirect
|
||||
github.com/alexmullins/zip v0.0.0-20180717182244-4affb64b04d0 // indirect
|
||||
github.com/bytedance/gopkg v0.1.3 // indirect
|
||||
github.com/bytedance/sonic v1.15.0 // indirect
|
||||
github.com/bytedance/sonic/loader v0.5.0 // indirect
|
||||
@@ -25,7 +29,6 @@ require (
|
||||
github.com/goccy/go-yaml v1.19.2 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
|
||||
github.com/ledongthuc/pdf v0.0.0-20260907135840-6c8c28e0e8a0 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
@@ -37,9 +40,8 @@ require (
|
||||
github.com/ugorji/go/codec v1.3.1 // indirect
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect
|
||||
golang.org/x/arch v0.22.0 // indirect
|
||||
golang.org/x/crypto v0.52.0 // indirect
|
||||
golang.org/x/net v0.54.0 // indirect
|
||||
golang.org/x/sys v0.45.0 // indirect
|
||||
golang.org/x/text v0.37.0 // indirect
|
||||
golang.org/x/sys v0.48.0 // indirect
|
||||
golang.org/x/text v0.42.0 // indirect
|
||||
google.golang.org/protobuf v1.36.10 // indirect
|
||||
)
|
||||
|
||||
+8
-4
@@ -6,6 +6,8 @@ github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25
|
||||
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
|
||||
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
|
||||
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
|
||||
github.com/alexmullins/zip v0.0.0-20180717182244-4affb64b04d0 h1:BVts5dexXf4i+JX8tXlKT0aKoi38JwTXSe+3WUneX0k=
|
||||
github.com/alexmullins/zip v0.0.0-20180717182244-4affb64b04d0/go.mod h1:FDIQmoMNJJl5/k7upZEnGvgWVZfFeE6qHeN7iCMbCsA=
|
||||
github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
|
||||
github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
|
||||
github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE=
|
||||
@@ -136,13 +138,15 @@ golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI=
|
||||
golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A=
|
||||
golang.org/x/crypto v0.52.0 h1:RMs7fP2rXdep0CftQlK8Uf+kibLm7qkCcradZWYz988=
|
||||
golang.org/x/crypto v0.52.0/go.mod h1:1QgfPxDqh0T2M/elOJtp9RvuR95kVjir0e6/BvEmGbc=
|
||||
golang.org/x/image v0.46.0 h1:b1+oYj0Jbp6K5MDT4i4/eZpYlk3V8SJhhDKh6LBHAyQ=
|
||||
golang.org/x/image v0.46.0/go.mod h1:3B3W05VGVQyuXucLINLjXKrqISASfi4Xj+iCVkLMwew=
|
||||
golang.org/x/net v0.54.0 h1:2zJIZAxAHV/OHCDTCOHAYehQzLfSXuf/5SoL/Dv6w/w=
|
||||
golang.org/x/net v0.54.0/go.mod h1:Sj4oj8jK6XmHpBZU/zWHw3BV3abl4Kvi+Ut7cQcY+cQ=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY=
|
||||
golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
|
||||
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
|
||||
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
||||
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
|
||||
golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
|
||||
google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE=
|
||||
google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
||||
@@ -2,6 +2,7 @@ package handlers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -156,7 +157,7 @@ func AuthLogin(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
token, expiresAt, err := Auth.IssueWithExpiry(user.ID, req.DeviceID)
|
||||
token, expiresAt, err := Auth.IssueWithExpiry(user.ID, req.DeviceID, user.TokenVersion)
|
||||
if err != nil {
|
||||
api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not issue token")
|
||||
return
|
||||
@@ -179,8 +180,9 @@ type changePasswordRequest struct {
|
||||
}
|
||||
|
||||
// ChangePassword met à jour le hash du compte connecté. Le mot de passe
|
||||
// courant est requis. Limite connue V1 : les tokens déjà émis restent valides
|
||||
// jusqu'à expiration (7 j, pas de révocation) — cf. docs/api-v1.md §7.
|
||||
// courant est requis. V3 S6 : le changement bumpe `token_version` — tous les
|
||||
// autres tokens (autres devices, sessions volées) sont révoqués aussitôt
|
||||
// (le token courant aussi : le client doit se reconnecter).
|
||||
func ChangePassword(c *gin.Context) {
|
||||
if Store == nil || Store.Repository == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
@@ -217,6 +219,11 @@ func ChangePassword(c *gin.Context) {
|
||||
api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not update password")
|
||||
return
|
||||
}
|
||||
// Révocation (V3 S6) : bump best-effort — un échec ici ne doit pas faire
|
||||
// croire que le password n'a pas changé (il l'est, ligne ci-dessus).
|
||||
if err := Store.Repository.Users.BumpTokenVersion(userID); err != nil {
|
||||
log.Printf("auth: bump token_version(%s): %v", userID, err)
|
||||
}
|
||||
|
||||
api.OK(c, gin.H{"id": userID})
|
||||
}
|
||||
|
||||
@@ -105,3 +105,51 @@ func DevicesRegister(c *gin.Context) {
|
||||
api.OK(c, gin.H{"deviceId": req.DeviceID})
|
||||
|
||||
}
|
||||
|
||||
// DeviceDTO expose un device du compte (A1 — voir / révoquer).
|
||||
type DeviceDTO struct {
|
||||
DeviceID string `json:"deviceId"`
|
||||
LastSeenAt *int64 `json:"lastSeenAt"`
|
||||
}
|
||||
|
||||
// DevicesList renvoie les devices du compte connecté (`GET /devices`,
|
||||
// dernier login = ce compte, les plus récemment vus d'abord).
|
||||
func DevicesList(c *gin.Context) {
|
||||
if Store == nil || Store.Repository == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
rows, err := Store.Repository.Devices.ListByUser(userID)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
out := make([]DeviceDTO, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
out = append(out, DeviceDTO{DeviceID: row.DeviceID, LastSeenAt: row.LastSeenAt})
|
||||
}
|
||||
api.OK(c, out)
|
||||
}
|
||||
|
||||
// DeviceRevoke révoque un device du compte (`DELETE /devices/:id`, A1 —
|
||||
// téléphone perdu). Avec EnforceDeviceExists, ses tokens deviennent 401 et
|
||||
// son login est refusé (ré-enregistrement explicite requis). Ligne absente
|
||||
// ou d'un autre compte → 404 (pas d'énumération).
|
||||
func DeviceRevoke(c *gin.Context) {
|
||||
if Store == nil || Store.Repository == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "device not found")
|
||||
return
|
||||
}
|
||||
if err := Store.Repository.Devices.Revoke(userID, id); err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, gin.H{"id": id})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
)
|
||||
|
||||
type exportRequest struct {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
// ExportFull streame un zip AES-256 du compte (V3 S7 C3) :
|
||||
// `POST /export { password }` (mot de passe en corps JSON, jamais en query
|
||||
// — pas de fuite dans les logs). Chaque entrée (fichiers + manifest.json)
|
||||
// est chiffrée. Scope : fichiers vifs possédés uniquement.
|
||||
// Le mot de passe exige 8+ caractères (400 sinon) ; le zip peut être long
|
||||
// (pas de cap S7 — donnée de l'utilisateur, self-hosté).
|
||||
func ExportFull(c *gin.Context) {
|
||||
if Store == nil || Store.Repository == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
var req exportRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid JSON body (expected { password })")
|
||||
return
|
||||
}
|
||||
if len(req.Password) < 8 {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "password must be at least 8 characters")
|
||||
return
|
||||
}
|
||||
filename := fmt.Sprintf("kazier-export-%s.zip", time.Now().UTC().Format("20060102-150405"))
|
||||
c.Header("Content-Type", "application/zip")
|
||||
c.Header("Content-Disposition", "attachment; filename=\""+filename+"\"")
|
||||
// Erreur mid-stream : headers déjà partis — log + abort (zip tronqué,
|
||||
// documenté ; le manifest chiffré en fin d'archive manque alors).
|
||||
if err := Store.Export(c.Writer, userID, req.Password); err != nil {
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// TestExportEndpoint : POST /export exige un mot de passe 8+ (400 sinon) et
|
||||
// streame un zip (200 + application/zip).
|
||||
func TestExportEndpoint(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "ex"), "export-test-password", device)
|
||||
|
||||
for _, body := range []string{`{}`, `{"password":"court"}`, `not-json`} {
|
||||
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/export", token, []byte(body), "application/json")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "export-400:"+body)
|
||||
}
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/export", token, []byte(`{"password":"motdepasse-8"}`), "application/json")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("export: attendu 200, got %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if ct := rec.Header().Get("Content-Type"); ct != "application/zip" {
|
||||
t.Fatalf("content-type = %q", ct)
|
||||
}
|
||||
if cd := rec.Header().Get("Content-Disposition"); cd == "" {
|
||||
t.Fatal("content-disposition manquant")
|
||||
}
|
||||
if rec.Body.Len() == 0 {
|
||||
t.Fatal("zip vide")
|
||||
}
|
||||
if rec.Body.Bytes()[0] != 'P' || rec.Body.Bytes()[1] != 'K' {
|
||||
t.Fatal("pas une archive zip (signature PK manquante)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
"github.com/kazier/backend/service"
|
||||
)
|
||||
|
||||
// TestFactsEndpoint : GET /files/:id/facts — faits auto visibles (viewer+),
|
||||
// [] si rien d'extractible, 404 si trashé/absent/étranger.
|
||||
func TestFactsEndpoint(t *testing.T) {
|
||||
r, store, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "fa"), "facts-test-password", device)
|
||||
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(userID, fileID, "facture.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
if err := repo.Resources.UpdateOcrText(userID, fileID, "EDF\nFacture n° F-1 du 01/02/2026\nTotal : 10,00 €\n"); err != nil {
|
||||
t.Fatalf("ocr text: %v", err)
|
||||
}
|
||||
extractSvc := service.NewExtract(repo)
|
||||
if err := extractSvc.ProcessText(userID, fileID, "EDF\nFacture n° F-1 du 01/02/2026\nTotal : 10,00 €\n"); err != nil {
|
||||
t.Fatalf("extract: %v", err)
|
||||
}
|
||||
_ = store
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/facts", token, nil, "")
|
||||
env := expectOK(t, rec, "facts-get")
|
||||
var facts []struct {
|
||||
FactType string `json:"factType"`
|
||||
ValueText string `json:"valueText"`
|
||||
ValueNum *float64 `json:"valueNum"`
|
||||
Confidence float64 `json:"confidence"`
|
||||
Origin string `json:"origin"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &facts); err != nil {
|
||||
t.Fatalf("facts unmarshal: %v", err)
|
||||
}
|
||||
if len(facts) == 0 {
|
||||
t.Fatal("faits attendus, got []")
|
||||
}
|
||||
types := map[string]bool{}
|
||||
for _, f := range facts {
|
||||
types[f.FactType] = true
|
||||
if f.Origin != "auto" {
|
||||
t.Errorf("origine = %q, attendu auto", f.Origin)
|
||||
}
|
||||
}
|
||||
for _, want := range []string{"reference", "amount", "date", "doc_type"} {
|
||||
if !types[want] {
|
||||
t.Errorf("fait %q manquant: %+v", want, facts)
|
||||
}
|
||||
}
|
||||
|
||||
// Fichier visible sans faits → [] (200, jamais 404).
|
||||
emptyID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(userID, emptyID, "vide.txt", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert empty: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+emptyID+"/facts", token, nil, "")
|
||||
env = expectOK(t, rec, "facts-empty")
|
||||
if string(env.Data) != "[]" {
|
||||
t.Fatalf("sans faits : attendu [], got %s", env.Data)
|
||||
}
|
||||
|
||||
// Trashé → 404 (comme l'OCR).
|
||||
if _, err := store.Trash(userID, fileID); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/facts", token, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "facts-trashed")
|
||||
|
||||
// Étranger → 404.
|
||||
deviceB := repository.NewID()
|
||||
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "fb"), "facts-test-password", deviceB)
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+emptyID+"/facts", tokenB, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "facts-foreign")
|
||||
}
|
||||
|
||||
// TestCorrectFactsEndpoint : PATCH /files/:id/facts — remplace le jeu user
|
||||
// (editor+ = owner ici), renvoie le jeu complet ; 400 sur corps invalide ;
|
||||
// 404 si trashé/étranger.
|
||||
func TestCorrectFactsEndpoint(t *testing.T) {
|
||||
r, store, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "fc"), "facts-test-password", device)
|
||||
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(userID, fileID, "facture.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
extractSvc := service.NewExtract(repo)
|
||||
if err := extractSvc.ProcessText(userID, fileID, "EDF\nFacture n° F-1 du 01/02/2026\nTotal : 10,00 €\n"); err != nil {
|
||||
t.Fatalf("extract: %v", err)
|
||||
}
|
||||
_ = store
|
||||
|
||||
patch := func(body string) (int, envelope) {
|
||||
t.Helper()
|
||||
rec, _ := doRequest(t, r, http.MethodPatch, "/api/v1/files/"+fileID+"/facts", token, []byte(body), "application/json")
|
||||
var env envelope
|
||||
if rec.Code == http.StatusOK {
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &env); err != nil {
|
||||
t.Fatalf("patch unmarshal: %v body=%s", err, rec.Body.String())
|
||||
}
|
||||
}
|
||||
return rec.Code, env
|
||||
}
|
||||
|
||||
// Correction : émetteur fixé par l'utilisateur.
|
||||
code, env := patch(`{"facts":[{"factType":"issuer","valueText":"Vrai Fournisseur","confidence":1}]}`)
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("patch: attendu 200, got %d", code)
|
||||
}
|
||||
var facts []struct {
|
||||
FactType string `json:"factType"`
|
||||
ValueText string `json:"valueText"`
|
||||
Origin string `json:"origin"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &facts); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
foundUser, foundAuto := false, false
|
||||
for _, f := range facts {
|
||||
if f.FactType == "issuer" && f.Origin == "user" && f.ValueText == "Vrai Fournisseur" {
|
||||
foundUser = true
|
||||
}
|
||||
if f.Origin == "auto" {
|
||||
foundAuto = true
|
||||
}
|
||||
}
|
||||
if !foundUser || !foundAuto {
|
||||
t.Fatalf("jeu après correction inattendu: %+v", facts)
|
||||
}
|
||||
|
||||
// Corps invalides → 400.
|
||||
for _, body := range []string{
|
||||
`{}`,
|
||||
`{"facts":[{"factType":"couleur","valueText":"bleu"}]}`,
|
||||
`{"facts":[{"factType":"date","valueDate":"12/03/2026"}]}`,
|
||||
`{"facts":[{"factType":"issuer"}]}`,
|
||||
`not-json`,
|
||||
} {
|
||||
rec, _ := doRequest(t, r, http.MethodPatch, "/api/v1/files/"+fileID+"/facts", token, []byte(body), "application/json")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "facts-patch-400:"+body)
|
||||
}
|
||||
|
||||
// Étranger (viewer simulé : partage + snapshot hors scope ici, un autre
|
||||
// user sans grant suffit — CorrectFacts exige editor+) → 404.
|
||||
deviceB := repository.NewID()
|
||||
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "fd"), "facts-test-password", deviceB)
|
||||
rec, _ := doRequest(t, r, http.MethodPatch, "/api/v1/files/"+fileID+"/facts", tokenB,
|
||||
[]byte(`{"facts":[{"factType":"issuer","valueText":"X"}]}`), "application/json")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "facts-patch-foreign")
|
||||
|
||||
// Trashé → 404.
|
||||
if _, err := store.Trash(userID, fileID); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodPatch, "/api/v1/files/"+fileID+"/facts", token,
|
||||
[]byte(`{"facts":[{"factType":"issuer","valueText":"X"}]}`), "application/json")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "facts-patch-trashed")
|
||||
}
|
||||
+338
-2
@@ -5,9 +5,11 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
"github.com/kazier/backend/repository"
|
||||
"github.com/kazier/backend/service"
|
||||
)
|
||||
|
||||
@@ -107,6 +109,169 @@ func FilesGetOcr(c *gin.Context) {
|
||||
api.OK(c, dto)
|
||||
}
|
||||
|
||||
// FilesGetFacts expose les faits génériques d'un fichier visible (V3 S2) :
|
||||
// GET /files/:id/facts. Même portée que l'OCR (viewer+, own ou shared) —
|
||||
// les faits dérivent du texte. `[]` si rien d'extractible (jamais 404 pour
|
||||
// un fichier visible sans faits) ; 404 si trashé/absent/étranger.
|
||||
func FilesGetFacts(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
facts, err := Store.GetFacts(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
if facts == nil {
|
||||
facts = []service.FactDTO{}
|
||||
}
|
||||
api.OK(c, facts)
|
||||
}
|
||||
|
||||
// FilesCorrectFacts remplace le jeu `user` des faits (V3 S3) :
|
||||
// PATCH /files/:id/facts `{ facts: [...] }` (full-replace, jamais de diff).
|
||||
// Nécessite editor+ (comme update_metadata) ; viewer- → 404 NOT_FOUND.
|
||||
// Corps invalide (type inconnu, date non ISO, fait sans valeur) → 400.
|
||||
func FilesCorrectFacts(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Facts []service.FactDTO `json:"facts"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid JSON body (expected { facts: [...] })")
|
||||
return
|
||||
}
|
||||
if req.Facts == nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "missing required field `facts`")
|
||||
return
|
||||
}
|
||||
if len(req.Facts) > 100 {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "`facts` too long (max 100)")
|
||||
return
|
||||
}
|
||||
if err := Store.CorrectFacts(userID, id, req.Facts); err != nil {
|
||||
// Erreurs de validation (type/date/valeur) → 400 explicite, pas 500 :
|
||||
// ce sont des fautes client, jamais des pannes (cf. FEEDBACK #8).
|
||||
if errors.Is(err, service.ErrInvalidFact) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", err.Error())
|
||||
return
|
||||
}
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
facts, err := Store.GetFacts(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, facts)
|
||||
}
|
||||
|
||||
// RelationsList expose le graphe documentaire d'une ressource possédée
|
||||
// (V3 S5) : `GET /files/:id/relations`. Réconcilie d'abord (les arêtes
|
||||
// déterministes convergent à la lecture), puis renvoie les arêtes vivantes
|
||||
// (l'autre bout non trashé), confiance décroissante. Owner-only : le graphe
|
||||
// d'autrui ne s'énumère pas (404, comme `/shares`).
|
||||
func RelationsList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
relations, err := Store.ListRelations(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
if relations == nil {
|
||||
relations = []service.RelationDTO{}
|
||||
}
|
||||
api.OK(c, relations)
|
||||
}
|
||||
|
||||
// RelationVerdict enregistre un verdict explicite sur une arête (V3 S5) :
|
||||
// `POST /files/:id/relations/verdict { otherId, linkType, confirmed }`.
|
||||
// Confirmé → 1.0, infirmé → 0.0 (conservé, bloque la re-création auto).
|
||||
// Owner-only, les deux bouts possédés et vifs, sinon 404 ; type/paire
|
||||
// invalide → 400.
|
||||
func RelationVerdict(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
OtherID string `json:"otherId"`
|
||||
LinkType string `json:"linkType"`
|
||||
Confirmed bool `json:"confirmed"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid JSON body (expected { otherId, linkType, confirmed })")
|
||||
return
|
||||
}
|
||||
if !deviceIDPattern.MatchString(req.OtherID) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid `otherId` (32 lowercase hex)")
|
||||
return
|
||||
}
|
||||
if err := Store.LinkVerdict(userID, id, req.OtherID, req.LinkType, req.Confirmed); err != nil {
|
||||
if errors.Is(err, service.ErrInvalidLinkType) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", err.Error())
|
||||
return
|
||||
}
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
relations, err := Store.ListRelations(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, relations)
|
||||
}
|
||||
|
||||
// DuplicatesList expose les doublons d'octets du compte (V3 S5 B2) :
|
||||
// `GET /duplicates`. Groupes par hash, plus gros d'abord, avec le gain
|
||||
// récupérable (« garder un seul »). Scope owner — `[]` quand propre.
|
||||
func DuplicatesList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
groups, err := Store.ListDuplicates(userID)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, groups)
|
||||
}
|
||||
|
||||
// FilesContent streame les octets physiques d'un fichier visible (viewer+,
|
||||
// own ou shared) : GET /files/:id/content. Réponse binaire (Content-Type du
|
||||
// fichier, Content-Disposition inline) — jamais d'enveloppe {data}. Supporte
|
||||
@@ -134,6 +299,34 @@ func FilesContent(c *gin.Context) {
|
||||
c.File(info.Path)
|
||||
}
|
||||
|
||||
// FilesPreview sert la vignette JPEG d'un fichier visible (V3 S7) :
|
||||
// `GET /files/:id/preview` (binaire brut, hors enveloppe, `inline`).
|
||||
// Générée à la demande (images pur Go, PDF page 1 via `pdftoppm`) et cachée
|
||||
// jusqu'au prochain changement (taille/`updated_at`). 404 NOT_FOUND si
|
||||
// invisible/metadata-only (indistinguables, pas d'énumération), 404
|
||||
// NO_PREVIEW si le type ne se prévisualise pas (l'UI montre l'icône).
|
||||
func FilesPreview(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
path, err := Store.OpenPreview(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
c.Header("Content-Type", "image/jpeg")
|
||||
c.Header("Content-Disposition", "inline; filename=\""+id+".jpg\"")
|
||||
c.Header("Cache-Control", "private, max-age=3600")
|
||||
c.File(path)
|
||||
}
|
||||
|
||||
func FilesDelete(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
@@ -145,7 +338,19 @@ func FilesDelete(c *gin.Context) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
deletedID, err := Store.DeleteFile(userID, id)
|
||||
// V3 corbeille : DELETE = trash par défaut ; ?mode=purge = destruction
|
||||
// définitive (lignes + versions + octets). Mode inconnu → 400.
|
||||
var deletedID string
|
||||
var err error
|
||||
switch c.Query("mode") {
|
||||
case "", "trash":
|
||||
deletedID, err = Store.Trash(userID, id)
|
||||
case "purge":
|
||||
deletedID, err = Store.Purge(userID, id)
|
||||
default:
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid query param `mode` (trash|purge)")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
@@ -153,6 +358,94 @@ func FilesDelete(c *gin.Context) {
|
||||
api.OK(c, gin.H{"id": deletedID})
|
||||
}
|
||||
|
||||
// FilesRestore restaure une ressource trashée (POST /files/:id/restore).
|
||||
// Absente/étrangère → 404 NOT_FOUND ; déjà vivante → succès idempotent.
|
||||
func FilesRestore(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
restoredID, err := Store.Restore(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, gin.H{"id": restoredID})
|
||||
}
|
||||
|
||||
// TrashList expose la corbeille (GET /trash) : ressources trashées du user,
|
||||
// les plus récentes d'abord, paginé comme les autres listes.
|
||||
func TrashList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
page, pageSize, ok := parsePageParams(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
items, total, err := Store.ListTrashed(userID, page, pageSize)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OKList(c, items, page, pageSize, total)
|
||||
}
|
||||
|
||||
// VersionsList expose l'historique des octets (GET /files/:id/versions),
|
||||
// le plus récent d'abord. Ressource trashée/absente/étrangère → 404.
|
||||
func VersionsList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
versions, err := Store.ListVersions(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, versions)
|
||||
}
|
||||
|
||||
// VersionRestore recopie la génération n vers le live
|
||||
// (POST /files/:id/versions/:n/restore). Génération inconnue → 404.
|
||||
func VersionRestore(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
n, err := strconv.Atoi(c.Param("n"))
|
||||
if err != nil || n < 1 {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "version not found")
|
||||
return
|
||||
}
|
||||
dto, err := Store.RestoreVersion(userID, id, n)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, dto)
|
||||
}
|
||||
|
||||
func FilesUpload(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
@@ -235,10 +528,53 @@ func FilesSearch(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
files, total, err := Store.SearchFiles(userID, q, page, pageSize)
|
||||
facets, ok := parseSearchFacets(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
files, total, err := Store.SearchFiles(userID, q, page, pageSize, facets)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OKList(c, files, page, pageSize, total)
|
||||
}
|
||||
|
||||
// parseSearchFacets lit les facettes V3 (`issuer`, `from`/`to` ISO,
|
||||
// `min_amount`/`max_amount` numériques, `doc_type`) — 400 si malformées.
|
||||
func parseSearchFacets(c *gin.Context) (repository.SearchFacets, bool) {
|
||||
var facets repository.SearchFacets
|
||||
facets.Issuer = strings.TrimSpace(c.Query("issuer"))
|
||||
facets.DocType = strings.TrimSpace(c.Query("doc_type"))
|
||||
if v := strings.TrimSpace(c.Query("from")); v != "" {
|
||||
if _, err := time.Parse("2006-01-02", v); err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "query param `from` must be ISO YYYY-MM-DD")
|
||||
return facets, false
|
||||
}
|
||||
facets.From = v
|
||||
}
|
||||
if v := strings.TrimSpace(c.Query("to")); v != "" {
|
||||
if _, err := time.Parse("2006-01-02", v); err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "query param `to` must be ISO YYYY-MM-DD")
|
||||
return facets, false
|
||||
}
|
||||
facets.To = v
|
||||
}
|
||||
if v := strings.TrimSpace(c.Query("min_amount")); v != "" {
|
||||
f, err := strconv.ParseFloat(strings.ReplaceAll(v, ",", "."), 64)
|
||||
if err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "query param `min_amount` must be numeric")
|
||||
return facets, false
|
||||
}
|
||||
facets.MinAmount = &f
|
||||
}
|
||||
if v := strings.TrimSpace(c.Query("max_amount")); v != "" {
|
||||
f, err := strconv.ParseFloat(strings.ReplaceAll(v, ",", "."), 64)
|
||||
if err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "query param `max_amount` must be numeric")
|
||||
return facets, false
|
||||
}
|
||||
facets.MaxAmount = &f
|
||||
}
|
||||
return facets, true
|
||||
}
|
||||
|
||||
@@ -76,6 +76,7 @@ func setup(t *testing.T) (*gin.Engine, *service.Resources, *repository.Repositor
|
||||
handlers.Auth = manager
|
||||
handlers.Store = store
|
||||
handlers.ResetDevicesRateLimit()
|
||||
handlers.ResetLoginRateLimit()
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
@@ -359,6 +360,83 @@ func TestSearchFiles(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestSearchFacets : filtres V3 sur faits + 400 si malformés.
|
||||
func TestSearchFacets(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, user := registerAndLogin(t, r, repo, testUserUsername(device, "sf"), "search-test-password", device)
|
||||
|
||||
edfID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(user, edfID, "scan_1.pdf", "", 100, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
if err := repo.Resources.UpdateOcrText(user, edfID, "Facture EDF de mars"); err != nil {
|
||||
t.Fatalf("ocr: %v", err)
|
||||
}
|
||||
num := 84.20
|
||||
if err := repo.Resources.UpsertAutoFacts(edfID, []repository.FactParam{
|
||||
{Type: "issuer", ValueText: "EDF", Confidence: 0.9},
|
||||
{Type: "amount", ValueText: "84.20 €", ValueNum: &num, Confidence: 0.9},
|
||||
{Type: "doc_type", ValueText: "facture", Confidence: 0.8},
|
||||
}); err != nil {
|
||||
t.Fatalf("facts: %v", err)
|
||||
}
|
||||
otherID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(user, otherID, "scan_2.pdf", "", 100, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
|
||||
get := func(query string) []fileDTO {
|
||||
t.Helper()
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/search"+query, token, nil, "")
|
||||
env := expectOK(t, rec, "search-facets:"+query)
|
||||
var files []fileDTO
|
||||
if err := json.Unmarshal(env.Data, &files); err != nil {
|
||||
t.Fatalf("unmarshal %s: %v", query, err)
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
// FTS via OCR (nom opaque).
|
||||
if files := get("?q=EDF"); len(files) == 0 || files[0].ID != edfID {
|
||||
t.Errorf("q=EDF via OCR: %+v", files)
|
||||
}
|
||||
// Facette émetteur : filtre.
|
||||
if files := get("?q=facture&issuer=edf"); len(files) != 1 || files[0].ID != edfID {
|
||||
t.Errorf("issuer=edf: %+v", files)
|
||||
}
|
||||
if files := get("?q=facture&issuer=engie"); len(files) != 0 {
|
||||
t.Errorf("issuer=engie doit filtrer: %+v", files)
|
||||
}
|
||||
// Facette montant.
|
||||
if files := get("?q=facture&min_amount=100"); len(files) != 0 {
|
||||
t.Errorf("min_amount=100 doit exclure 84.20: %+v", files)
|
||||
}
|
||||
if files := get("?q=facture&max_amount=100"); len(files) != 1 {
|
||||
t.Errorf("max_amount=100: %+v", files)
|
||||
}
|
||||
// Facette doc_type.
|
||||
if files := get("?q=scan&doc_type=facture"); len(files) != 1 || files[0].ID != edfID {
|
||||
t.Errorf("doc_type=facture: %+v", files)
|
||||
}
|
||||
|
||||
// Malformées → 400.
|
||||
for _, query := range []string{
|
||||
"?q=facture&from=12/03/2026",
|
||||
"?q=facture&to=demain",
|
||||
"?q=facture&min_amount=beaucoup",
|
||||
"?q=facture&max_amount=",
|
||||
} {
|
||||
// max_amount vide = absent (pas d'erreur) — seul le cas non-vide
|
||||
// non-numérique est rejeté ; on l'exclut ici.
|
||||
if query == "?q=facture&max_amount=" {
|
||||
continue
|
||||
}
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/search"+query, token, nil, "")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "search-facets-400:"+query)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchFilesEscapesPercent(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
|
||||
@@ -60,7 +60,7 @@ func RequireAuth(c *gin.Context) {
|
||||
// Le store peut être nil dans les tests middleware purs ; sinon on vérifie
|
||||
// que le compte existe toujours (supprimé → accès refusé).
|
||||
if Store != nil && Store.Repository != nil {
|
||||
_, err := Store.Repository.Users.GetByID(identity.UserID)
|
||||
user, err := Store.Repository.Users.GetByID(identity.UserID)
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "account no longer exists")
|
||||
c.Abort()
|
||||
@@ -71,6 +71,14 @@ func RequireAuth(c *gin.Context) {
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
// V3 S6 : révocation — un changement de mot de passe (bump de
|
||||
// token_version) invalide tous les tokens antérieurs, même non expirés.
|
||||
if identity.TokenVersion != user.TokenVersion {
|
||||
log.Printf("auth: token révoqué rejeté (user=%s tv=%d, attendu %d)", identity.UserID, identity.TokenVersion, user.TokenVersion)
|
||||
api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "token revoked")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if EnforceDeviceExists && Store != nil && Store.Repository != nil {
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// previewImageBytes génère un JPEG 640x480 uni (décodable sans fixture).
|
||||
func previewImageBytes(t *testing.T) []byte {
|
||||
t.Helper()
|
||||
img := image.NewRGBA(image.Rect(0, 0, 640, 480))
|
||||
for y := 0; y < 480; y++ {
|
||||
for x := 0; x < 640; x++ {
|
||||
img.Set(x, y, color.RGBA{uint8(x % 256), 128, 64, 255})
|
||||
}
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
if err := jpeg.Encode(&buf, img, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
// TestFilesPreview : JPEG 200 + image/jpeg pour une image, NO_PREVIEW pour
|
||||
// un txt, NOT_FOUND si trashé/étranger. L'upload HTTP de test est capé à
|
||||
// 10 o : le physique est écrit directement (même layout UPLOAD_DIR).
|
||||
func TestFilesPreview(t *testing.T) {
|
||||
r, store, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "pv"), "preview-test-password", device)
|
||||
|
||||
createdID := seedPhysical(t, store.UploadDir, repo, userID, "photo.jpg", previewImageBytes(t))
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+createdID+"/preview", token, nil, "")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("preview: attendu 200, got %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if ct := rec.Header().Get("Content-Type"); ct != "image/jpeg" {
|
||||
t.Fatalf("content-type = %q, attendu image/jpeg", ct)
|
||||
}
|
||||
if _, err := jpeg.Decode(bytes.NewReader(rec.Body.Bytes())); err != nil {
|
||||
t.Fatalf("corps non-JPEG: %v", err)
|
||||
}
|
||||
|
||||
// Txt → NO_PREVIEW (404 typé, l'UI montre l'icône).
|
||||
txtID := seedPhysical(t, store.UploadDir, repo, userID, "notes.txt", []byte("hello"))
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+txtID+"/preview", token, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NO_PREVIEW", "preview-txt")
|
||||
|
||||
// Trashé → NOT_FOUND (invisible d'abord, pas NO_PREVIEW).
|
||||
if err := repo.Resources.TrashResource(userID, createdID); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+createdID+"/preview", token, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "preview-trashed")
|
||||
|
||||
// Étranger → NOT_FOUND.
|
||||
deviceB := repository.NewID()
|
||||
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "pb"), "preview-test-password", deviceB)
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+txtID+"/preview", tokenB, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "preview-foreign")
|
||||
}
|
||||
|
||||
// seedPhysical insère la métadonnée + écrit les octets (même layout que
|
||||
// l'upload, sans passer par le multipart capé des tests).
|
||||
func seedPhysical(t *testing.T, uploadDir string, repo *repository.Repository, userID, name string, content []byte) string {
|
||||
t.Helper()
|
||||
id := repository.NewID()
|
||||
ext := ""
|
||||
if i := strings.LastIndex(name, "."); i >= 0 {
|
||||
ext = name[i+1:]
|
||||
}
|
||||
var extPtr *string
|
||||
if ext != "" {
|
||||
extPtr = &ext
|
||||
}
|
||||
mime := "application/octet-stream"
|
||||
if ext == "jpg" {
|
||||
mime = "image/jpeg"
|
||||
}
|
||||
if err := repo.Resources.InsertFile(userID, id, name, "", int64(len(content)), &mime, extPtr); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
dir := filepath.Join(uploadDir, userID)
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
}
|
||||
dest := id
|
||||
if ext != "" {
|
||||
dest = id + "." + ext
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, dest), content, 0o600); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
if err := repo.Resources.SetHasContent(userID, id, true); err != nil {
|
||||
t.Fatalf("has_content: %v", err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// TestRelationsEndpoints : GET /files/:id/relations (réconciliation à la
|
||||
// lecture) + POST verdict (confirmé/infirmé, 400/404).
|
||||
func TestRelationsEndpoints(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "rl"), "relations-test-password", device)
|
||||
|
||||
session := repository.NewID()
|
||||
page1, page2 := repository.NewID(), repository.NewID()
|
||||
for _, id := range []string{page1, page2} {
|
||||
if err := repo.Resources.InsertFile(userID, id, id+".png", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
if err := repo.Resources.SetScanSession(userID, id, session); err != nil {
|
||||
t.Fatalf("session: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+page1+"/relations", token, nil, "")
|
||||
env := expectOK(t, rec, "relations-get")
|
||||
var rels []struct {
|
||||
ID string `json:"id"`
|
||||
OtherID string `json:"otherId"`
|
||||
Type string `json:"type"`
|
||||
Confidence float64 `json:"confidence"`
|
||||
CreatedBy string `json:"createdBy"`
|
||||
OtherName string `json:"otherName"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &rels); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if len(rels) != 1 || rels[0].Type != "same_scan" || rels[0].OtherID != page2 {
|
||||
t.Fatalf("relations inattendues: %+v", rels)
|
||||
}
|
||||
|
||||
// Verdict infirmé : arête conservée à 0.0, origine user.
|
||||
body := fmt.Sprintf(`{"otherId":%q,"linkType":"same_scan","confirmed":false}`, page2)
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+page1+"/relations/verdict", token, []byte(body), "application/json")
|
||||
env = expectOK(t, rec, "relations-verdict")
|
||||
rels = nil
|
||||
if err := json.Unmarshal(env.Data, &rels); err != nil {
|
||||
t.Fatalf("unmarshal verdict: %v", err)
|
||||
}
|
||||
if len(rels) != 1 || rels[0].Confidence != 0 || rels[0].CreatedBy != "user" {
|
||||
t.Fatalf("verdict inattendu: %+v", rels)
|
||||
}
|
||||
|
||||
// Type inconnu → 400 ; paire non-hex → 400 ; étranger → 404.
|
||||
for _, tc := range []struct {
|
||||
name, body string
|
||||
status int
|
||||
code string
|
||||
}{
|
||||
{"type", fmt.Sprintf(`{"otherId":%q,"linkType":"copains","confirmed":true}`, page2), http.StatusBadRequest, "INVALID_REQUEST"},
|
||||
{"hex", `{"otherId":"nope","linkType":"same_scan","confirmed":true}`, http.StatusBadRequest, "INVALID_REQUEST"},
|
||||
} {
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+page1+"/relations/verdict", token, []byte(tc.body), "application/json")
|
||||
expectError(t, rec, tc.status, tc.code, "relations-verdict-400:"+tc.name)
|
||||
}
|
||||
|
||||
deviceB := repository.NewID()
|
||||
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "rb"), "relations-test-password", deviceB)
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+page1+"/relations", tokenB, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "relations-foreign")
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+page1+"/relations/verdict", tokenB,
|
||||
[]byte(fmt.Sprintf(`{"otherId":%q,"linkType":"same_scan","confirmed":true}`, page2)), "application/json")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "relations-verdict-foreign")
|
||||
|
||||
// Trashé → 404 (l'autre bout trashé disparaît aussi de la liste).
|
||||
if err := repo.Resources.TrashResource(userID, page2); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+page1+"/relations", token, nil, "")
|
||||
env = expectOK(t, rec, "relations-trashed-other")
|
||||
rels = nil
|
||||
if err := json.Unmarshal(env.Data, &rels); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if len(rels) != 0 {
|
||||
t.Fatalf("bout trashé encore listé: %+v", rels)
|
||||
}
|
||||
}
|
||||
@@ -33,15 +33,30 @@ func RegisterRoutes(r *gin.Engine) {
|
||||
protected.GET("/files/folders", FoldersList)
|
||||
protected.GET("/files/:id", FilesGet)
|
||||
protected.GET("/files/:id/ocr", FilesGetOcr)
|
||||
protected.GET("/files/:id/facts", FilesGetFacts)
|
||||
protected.PATCH("/files/:id/facts", FilesCorrectFacts)
|
||||
protected.GET("/files/:id/content", FilesContent)
|
||||
protected.GET("/files/:id/preview", FilesPreview)
|
||||
protected.GET("/files/:id/shares", SharesList)
|
||||
protected.GET("/files/:id/links", LinksList)
|
||||
protected.GET("/files/:id/placements", PlacementsList)
|
||||
protected.GET("/files/:id/versions", VersionsList)
|
||||
protected.GET("/files/:id/relations", RelationsList)
|
||||
protected.POST("/files/:id/relations/verdict", RelationVerdict)
|
||||
protected.GET("/duplicates", DuplicatesList)
|
||||
protected.POST("/export", ExportFull)
|
||||
protected.POST("/files/:id/restore", FilesRestore)
|
||||
protected.POST("/files/:id/versions/:n/restore", VersionRestore)
|
||||
protected.GET("/trash", TrashList)
|
||||
protected.DELETE("/files/:id", FilesDelete)
|
||||
protected.POST("/files/upload", FilesUpload)
|
||||
|
||||
protected.GET("/users/resolve", ResolveUser)
|
||||
protected.PATCH("/users/me/password", ChangePassword)
|
||||
protected.GET("/users/me/usage", UsersUsage)
|
||||
|
||||
protected.GET("/devices", DevicesList)
|
||||
protected.DELETE("/devices/:id", DeviceRevoke)
|
||||
|
||||
protected.POST("/groups", GroupsCreate)
|
||||
protected.GET("/groups", GroupsList)
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/handlers"
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// TestPasswordChangeRevokesTokens : le bump de token_version invalide tous
|
||||
// les tokens antérieurs (V3 S6) — l'ancien token devient 401, un nouveau
|
||||
// login fonctionne.
|
||||
func TestPasswordChangeRevokesTokens(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "rv"), "revoke-test-password", device)
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files", token, nil, "")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("token initial rejeté: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
body := `{"current_password":"revoke-test-password","new_password":"revoke-test-password-2"}`
|
||||
rec, _ = doRequest(t, r, http.MethodPatch, "/api/v1/users/me/password", token, []byte(body), "application/json")
|
||||
expectOK(t, rec, "password-change")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files", token, nil, "")
|
||||
expectError(t, rec, http.StatusUnauthorized, "UNAUTHORIZED", "revoked-token")
|
||||
|
||||
// Nouveau login (nouvelle version) : fonctionne.
|
||||
body = fmt.Sprintf(`{"username":%q,"password":"revoke-test-password-2","device_id":%q}`,
|
||||
testUserUsername(device, "rv"), device)
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/auth/login", "", []byte(body), "application/json")
|
||||
expectOK(t, rec, "relogin")
|
||||
var login struct {
|
||||
Data struct {
|
||||
Token string `json:"token"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &login); err != nil || login.Data.Token == "" {
|
||||
t.Fatalf("relogin sans token: %s", rec.Body.String())
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files", login.Data.Token, nil, "")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("nouveau token rejeté: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestDevicesListAndRevoke : liste les devices du compte, révoque (A1) —
|
||||
// avec EnforceDeviceExists, le token du device révoqué devient 401 et son
|
||||
// login est refusé (ré-enregistrement requis).
|
||||
func TestDevicesListAndRevoke(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
handlers.EnforceDeviceExists = true
|
||||
defer func() { handlers.EnforceDeviceExists = false }()
|
||||
device := repository.NewID()
|
||||
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "dv"), "devices-test-password", device)
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/devices", token, nil, "")
|
||||
env := expectOK(t, rec, "devices-list")
|
||||
var devices []struct {
|
||||
DeviceID string `json:"deviceId"`
|
||||
LastSeenAt *int64 `json:"lastSeenAt"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &devices); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
found := false
|
||||
for _, d := range devices {
|
||||
if d.DeviceID == device {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("device absent de la liste: %+v", devices)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/devices/"+device, token, nil, "")
|
||||
expectOK(t, rec, "devices-revoke")
|
||||
|
||||
// Token existant → 401 (device inconnu).
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files", token, nil, "")
|
||||
expectError(t, rec, http.StatusUnauthorized, "UNAUTHORIZED", "revoked-device-token")
|
||||
|
||||
// Login refusé (device inconnu, indistinguable — 401).
|
||||
body := fmt.Sprintf(`{"username":%q,"password":"devices-test-password","device_id":%q}`,
|
||||
testUserUsername(device, "dv"), device)
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/auth/login", "", []byte(body), "application/json")
|
||||
expectError(t, rec, http.StatusUnauthorized, "UNAUTHORIZED", "revoked-device-login")
|
||||
|
||||
// Révoquer l'inexistant / malformé → 404 (avec un token encore valide).
|
||||
deviceB := repository.NewID()
|
||||
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "dv2"), "devices-test-password", deviceB)
|
||||
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/devices/"+repository.NewID(), tokenB, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "revoke-unknown")
|
||||
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/devices/nope", tokenB, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "revoke-malformed")
|
||||
}
|
||||
|
||||
// TestUsageAndQuota : GET /users/me/usage reflète l'état, et un upload
|
||||
// au-delà du quota est rejeté en 413 QUOTA_EXCEEDED (V3 S6 A2).
|
||||
func TestUsageAndQuota(t *testing.T) {
|
||||
r, store, repo := setup(t)
|
||||
_ = store
|
||||
device := repository.NewID()
|
||||
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "qu"), "quota-test-password", device)
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/users/me/usage", token, nil, "")
|
||||
env := expectOK(t, rec, "usage-empty")
|
||||
var usage struct {
|
||||
UsedBytes int64 `json:"usedBytes"`
|
||||
QuotaBytes int64 `json:"quotaBytes"`
|
||||
FileCount int `json:"fileCount"`
|
||||
TrashCount int `json:"trashCount"`
|
||||
TrashedBytes int64 `json:"trashedBytes"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &usage); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if usage.UsedBytes != 0 || usage.QuotaBytes != 10737418240 || usage.FileCount != 0 {
|
||||
t.Fatalf("usage initial inattendu: %+v", usage)
|
||||
}
|
||||
|
||||
// Quota miniature (10 octets) → l'upload passe sous le seuil puis casse.
|
||||
if _, err := repo.Users.DB.Exec(`UPDATE users SET storage_quota_bytes = 10 WHERE id = $1`, userID); err != nil {
|
||||
t.Fatalf("shrink quota: %v", err)
|
||||
}
|
||||
rec = uploadMultipart(t, r, token, "", "a.txt", []byte("12345"))
|
||||
expectOK(t, rec, "usage-under-quota")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/users/me/usage", token, nil, "")
|
||||
env = expectOK(t, rec, "usage-after")
|
||||
usage = struct {
|
||||
UsedBytes int64 `json:"usedBytes"`
|
||||
QuotaBytes int64 `json:"quotaBytes"`
|
||||
FileCount int `json:"fileCount"`
|
||||
TrashCount int `json:"trashCount"`
|
||||
TrashedBytes int64 `json:"trashedBytes"`
|
||||
}{}
|
||||
if err := json.Unmarshal(env.Data, &usage); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if usage.UsedBytes != 5 || usage.FileCount != 1 {
|
||||
t.Fatalf("usage après upload: %+v", usage)
|
||||
}
|
||||
|
||||
rec = uploadMultipart(t, r, token, "", "b.txt", []byte("123456"))
|
||||
expectError(t, rec, http.StatusRequestEntityTooLarge, "QUOTA_EXCEEDED", "usage-over-quota")
|
||||
}
|
||||
@@ -30,6 +30,10 @@ func writeError(c *gin.Context, err error) {
|
||||
api.Error(c, 409, "NAME_CONFLICT", "a resource with this name already exists here")
|
||||
case errors.Is(err, service.ErrFileTooLarge):
|
||||
api.Error(c, 413, "FILE_TOO_LARGE", "file exceeds the maximum allowed size")
|
||||
case errors.Is(err, service.ErrQuotaExceeded):
|
||||
api.Error(c, 413, "QUOTA_EXCEEDED", "storage quota exceeded")
|
||||
case errors.Is(err, service.ErrNoPreview):
|
||||
api.Error(c, 404, "NO_PREVIEW", "no preview available for this file")
|
||||
default:
|
||||
// Jamais de err.Error() brut au client (fuite SQL/FS/paths, cf.
|
||||
// FEEDBACK #8) : message générique, détail au log serveur.
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// TestTrashEndpoints couvre le cycle corbeille via HTTP : DELETE (trash) →
|
||||
// 404 en lecture → GET /trash → POST /files/:id/restore → visible →
|
||||
// DELETE ?mode=purge → corbeille vide, restore → 404.
|
||||
func TestTrashEndpoints(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "tr"), "trash-test-password", device)
|
||||
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(userID, fileID, "acte.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodDelete, "/api/v1/files/"+fileID, token, nil, "")
|
||||
expectOK(t, rec, "trash-delete")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID, token, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "trash-get-hidden")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/trash", token, nil, "")
|
||||
env := expectOK(t, rec, "trash-list")
|
||||
var items []struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
TrashedAt string `json:"trashedAt"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &items); err != nil {
|
||||
t.Fatalf("trash list unmarshal: %v", err)
|
||||
}
|
||||
if len(items) != 1 || items[0].ID != fileID || items[0].TrashedAt == "" {
|
||||
t.Fatalf("corbeille inattendue: %+v", items)
|
||||
}
|
||||
if env.Meta == nil || env.Meta.Total != 1 {
|
||||
t.Fatalf("meta corbeille: %+v", env.Meta)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+fileID+"/restore", token, nil, "")
|
||||
expectOK(t, rec, "trash-restore")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID, token, nil, "")
|
||||
expectOK(t, rec, "trash-get-restored")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/files/"+fileID+"?mode=purge", token, nil, "")
|
||||
expectOK(t, rec, "trash-purge")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/trash", token, nil, "")
|
||||
env = expectOK(t, rec, "trash-list-empty")
|
||||
if env.Meta == nil || env.Meta.Total != 0 {
|
||||
t.Fatalf("corbeille doit être vide: %+v", env.Meta)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+fileID+"/restore", token, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "trash-restore-purged")
|
||||
}
|
||||
|
||||
// TestTrashInvalidMode : ?mode inconnu → 400, la ressource reste vivante.
|
||||
func TestTrashInvalidMode(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "tm"), "trash-test-password", device)
|
||||
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(userID, fileID, "doc.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodDelete, "/api/v1/files/"+fileID+"?mode=bogus", token, nil, "")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "trash-bad-mode")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID, token, nil, "")
|
||||
expectOK(t, rec, "trash-bad-mode-keeps-live")
|
||||
}
|
||||
|
||||
// TestTrashIsolation : un user ne voit ni ne touche la corbeille d'autrui.
|
||||
func TestTrashIsolation(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
deviceA := repository.NewID()
|
||||
deviceB := repository.NewID()
|
||||
tokenA, userA := registerAndLogin(t, r, repo, testUserUsername(deviceA, "ta"), "trash-test-password", deviceA)
|
||||
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "tb"), "trash-test-password", deviceB)
|
||||
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(userA, fileID, "prive.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodDelete, "/api/v1/files/"+fileID, tokenB, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "trash-foreign")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/trash", tokenB, nil, "")
|
||||
env := expectOK(t, rec, "trash-foreign-list")
|
||||
if env.Meta == nil || env.Meta.Total != 0 {
|
||||
t.Fatalf("corbeille d'autrui doit être vide: %+v", env.Meta)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/files/"+fileID, tokenA, nil, "")
|
||||
expectOK(t, rec, "trash-owner")
|
||||
}
|
||||
|
||||
// TestVersionsEndpoints : upload miniature (MaxFileSize=10 en test) puis
|
||||
// re-upload ciblé → GET /files/:id/versions (1 entrée) → restore → contenu
|
||||
// d'origine. Trashée → versions en 404.
|
||||
func TestVersionsEndpoints(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "ve"), "trash-test-password", device)
|
||||
|
||||
rec := uploadMultipart(t, r, token, "", "doc.txt", []byte("AAA"))
|
||||
env := expectOK(t, rec, "versions-upload")
|
||||
var created fileDTO
|
||||
if err := json.Unmarshal(env.Data, &created); err != nil {
|
||||
t.Fatalf("upload unmarshal: %v", err)
|
||||
}
|
||||
|
||||
rec = uploadMultipartToResource(t, r, token, created.ID, "doc.txt", []byte("BBBB"))
|
||||
expectOK(t, rec, "versions-overwrite")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+created.ID+"/versions", token, nil, "")
|
||||
env = expectOK(t, rec, "versions-list")
|
||||
var vers []struct {
|
||||
VersionNo int `json:"versionNo"`
|
||||
Size int64 `json:"size"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &vers); err != nil {
|
||||
t.Fatalf("versions unmarshal: %v", err)
|
||||
}
|
||||
if len(vers) != 1 || vers[0].Size != 3 {
|
||||
t.Fatalf("versions inattendues: %+v", vers)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+created.ID+"/versions/1/restore", token, nil, "")
|
||||
env = expectOK(t, rec, "versions-restore")
|
||||
var restored fileDTO
|
||||
if err := json.Unmarshal(env.Data, &restored); err != nil {
|
||||
t.Fatalf("restore unmarshal: %v", err)
|
||||
}
|
||||
if restored.Size != 3 {
|
||||
t.Fatalf("live restauré taille=%d, attendu 3", restored.Size)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+created.ID+"/versions/99/restore", token, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "versions-restore-unknown")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/files/"+created.ID, token, nil, "")
|
||||
expectOK(t, rec, "versions-trash")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+created.ID+"/versions", token, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "versions-trashed-hidden")
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
)
|
||||
|
||||
// UsersUsage expose l'empreinte du compte (V3 S6 A2) : `GET /users/me/usage`
|
||||
// `{ usedBytes, quotaBytes, fileCount, trashCount, trashedBytes }` —
|
||||
// calculé, jamais stocké. La corbeille est détaillée (octets récupérables).
|
||||
func UsersUsage(c *gin.Context) {
|
||||
if Store == nil || Store.Repository == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
usage, err := Store.Usage(userID)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, usage)
|
||||
}
|
||||
+26
-10
@@ -4,6 +4,7 @@ import (
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"aidanwoods.dev/go-paseto"
|
||||
@@ -17,11 +18,14 @@ var ErrInvalidToken = errors.New("invalid token")
|
||||
|
||||
var hex32Pattern = regexp.MustCompile(`^[0-9a-f]{32}$`)
|
||||
|
||||
// Identity est le résultat de Verify : l'utilisateur (sujet du token, autorisant)
|
||||
// et le device (claim secondaire, porté mais NON autorisant seul).
|
||||
// Identity est le résultat de Verify : l'utilisateur (sujet du token, autorisant),
|
||||
// le device (claim secondaire, porté mais NON autorisant seul) et la version
|
||||
// de révocation (claim `tv`, V3 S6 — le middleware la compare à
|
||||
// users.token_version).
|
||||
type Identity struct {
|
||||
UserID string
|
||||
DeviceID string
|
||||
TokenVersion int
|
||||
}
|
||||
|
||||
// Manager issues and verifies paseto v4-local bearer tokens.
|
||||
@@ -40,16 +44,19 @@ func NewManager(secret string) (*Manager, error) {
|
||||
return &Manager{key: key}, nil
|
||||
}
|
||||
|
||||
// Issue émet un token lié à un utilisateur ET à un device.
|
||||
// Issue émet un token lié à un utilisateur ET à un device (version 0 —
|
||||
// compat pré-V3, cf. IssueWithExpiry).
|
||||
func (m *Manager) Issue(userID, deviceID string) (string, error) {
|
||||
token, _, err := m.IssueWithExpiry(userID, deviceID)
|
||||
token, _, err := m.IssueWithExpiry(userID, deviceID, 0)
|
||||
return token, err
|
||||
}
|
||||
|
||||
// IssueWithExpiry émet un token et retourne son expiration réelle (le `exp`
|
||||
// embarqué). Les appelants doivent exposer expiresAt, jamais le recalculer
|
||||
// via Now()+TTL (dérive d'horloge, cf. FEEDBACK A7).
|
||||
func (m *Manager) IssueWithExpiry(userID, deviceID string) (string, time.Time, error) {
|
||||
// via Now()+TTL (dérive d'horloge, cf. FEEDBACK A7). `tokenVersion` est le
|
||||
// compteur de révocation du compte (V3 S6) — bumpé à chaque changement de
|
||||
// mot de passe, vérifié par le middleware.
|
||||
func (m *Manager) IssueWithExpiry(userID, deviceID string, tokenVersion int) (string, time.Time, error) {
|
||||
now := time.Now()
|
||||
expiresAt := now.Add(TokenTTL)
|
||||
token := paseto.NewToken()
|
||||
@@ -59,12 +66,14 @@ func (m *Manager) IssueWithExpiry(userID, deviceID string) (string, time.Time, e
|
||||
token.SetSubject(userID)
|
||||
// v1.6.0 : SetString n'expose pas d'erreur (panic si non sérialisable).
|
||||
token.SetString("device_id", deviceID)
|
||||
token.SetString("tv", strconv.Itoa(tokenVersion))
|
||||
return token.V4Encrypt(m.key, nil), expiresAt, nil
|
||||
}
|
||||
|
||||
// Verify décode et contrôle le token, retourne l'identité (user + device).
|
||||
// Les deux identifiants doivent être 32-hex (cf. docs/api-v1.md) — rejet
|
||||
// sinon pour préserver les invariants UNIQUE(device_id,operation_id)/CHECK.
|
||||
// Verify décode et contrôle le token, retourne l'identité (user + device +
|
||||
// version). Les deux identifiants doivent être 32-hex (cf. docs/api-v1.md) —
|
||||
// rejet sinon pour préserver les invariants UNIQUE(device_id,operation_id)/CHECK.
|
||||
// `tv` absent (tokens pré-V3) → 0 ; non numérique → rejet.
|
||||
func (m *Manager) Verify(signed string) (Identity, error) {
|
||||
parsed, err := paseto.NewParserForValidNow().ParseV4Local(m.key, signed, nil)
|
||||
if err != nil {
|
||||
@@ -78,5 +87,12 @@ func (m *Manager) Verify(signed string) (Identity, error) {
|
||||
if err != nil || !hex32Pattern.MatchString(deviceID) {
|
||||
return Identity{}, ErrInvalidToken
|
||||
}
|
||||
return Identity{UserID: userID, DeviceID: deviceID}, nil
|
||||
tv := 0
|
||||
if raw, err := parsed.GetString("tv"); err == nil && raw != "" {
|
||||
tv, err = strconv.Atoi(raw)
|
||||
if err != nil || tv < 0 {
|
||||
return Identity{}, ErrInvalidToken
|
||||
}
|
||||
}
|
||||
return Identity{UserID: userID, DeviceID: deviceID, TokenVersion: tv}, nil
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ func TestIssueSetsExpiration(t *testing.T) {
|
||||
|
||||
func TestIssueWithExpiryMatchesTokenExp(t *testing.T) {
|
||||
m, _ := NewManager("test-secret")
|
||||
signed, expiresAt, err := m.IssueWithExpiry(testUserID, testDeviceID)
|
||||
signed, expiresAt, err := m.IssueWithExpiry(testUserID, testDeviceID, 3)
|
||||
if err != nil {
|
||||
t.Fatalf("IssueWithExpiry: %v", err)
|
||||
}
|
||||
@@ -177,4 +177,28 @@ func TestIssueWithExpiryMatchesTokenExp(t *testing.T) {
|
||||
if exp.Unix() != expiresAt.Unix() {
|
||||
t.Errorf("expiresAt = %v, exp token = %v", expiresAt, exp)
|
||||
}
|
||||
id, err := m.Verify(signed)
|
||||
if err != nil {
|
||||
t.Fatalf("Verify: %v", err)
|
||||
}
|
||||
if id.TokenVersion != 3 {
|
||||
t.Errorf("tv = %d, attendu 3", id.TokenVersion)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyLegacyTokenWithoutTv(t *testing.T) {
|
||||
m, _ := NewManager("test-secret")
|
||||
// Tokens pré-V3 (Issue, tv=0) : Verify lit 0 — valides tant que le
|
||||
// compte n'a jamais bumpé (montée en version sans coupure).
|
||||
signed, err := m.Issue(testUserID, testDeviceID)
|
||||
if err != nil {
|
||||
t.Fatalf("Issue: %v", err)
|
||||
}
|
||||
id, err := m.Verify(signed)
|
||||
if err != nil {
|
||||
t.Fatalf("Verify legacy: %v", err)
|
||||
}
|
||||
if id.TokenVersion != 0 {
|
||||
t.Errorf("tv legacy = %d, attendu 0", id.TokenVersion)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,3 +52,61 @@ func (d *Devices) MarkUser(deviceID, userID string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeviceRow is one registered device of an account (A1 — voir / révoquer).
|
||||
type DeviceRow struct {
|
||||
DeviceID string
|
||||
LastSeenAt *int64 // ms epoch, NULL si jamais vu depuis l'enregistrement
|
||||
}
|
||||
|
||||
// ListByUser returns the devices whose last login was this account
|
||||
// (devices.user_id informatif, jamais autorisant).
|
||||
func (d *Devices) ListByUser(userID string) ([]DeviceRow, error) {
|
||||
rows, err := d.DB.Query(
|
||||
`SELECT device_id, (EXTRACT(EPOCH FROM last_seen_at) * 1000)::bigint
|
||||
FROM devices WHERE user_id = $1 ORDER BY last_seen_at DESC NULLS LAST`,
|
||||
userID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []DeviceRow{}
|
||||
for rows.Next() {
|
||||
var row DeviceRow
|
||||
if err := rows.Scan(&row.DeviceID, &row.LastSeenAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, row)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// Revoke deletes a device registration of this account (A1 — téléphone
|
||||
// perdu). Avec EnforceDeviceExists, les tokens du device deviennent 401 et
|
||||
// le login refuse le device (ré-enregistrement explicite requis).
|
||||
// Ligne absente ou d'un autre compte → ErrNotFound (pas d'énumération).
|
||||
// Les hints de présence (`storage_locations`) suivent.
|
||||
func (d *Devices) Revoke(userID, deviceID string) error {
|
||||
result, err := d.DB.Exec(
|
||||
`DELETE FROM devices WHERE device_id = $1 AND user_id = $2`,
|
||||
deviceID, userID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
if _, err := d.DB.Exec(
|
||||
`DELETE FROM storage_locations WHERE device_id = $1 AND user_id = $2`,
|
||||
deviceID, userID,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// FactParam is one generic fact to persist (V3 S2, migration 000019).
|
||||
// ValueNum/ValueDate nil when the type carries no numeric/date value.
|
||||
type FactParam struct {
|
||||
Type string
|
||||
ValueText string
|
||||
ValueNum *float64
|
||||
ValueDate *string // ISO YYYY-MM-DD
|
||||
Confidence float64
|
||||
}
|
||||
|
||||
// FactRow is one persisted fact (auto-extracted or user-corrected).
|
||||
type FactRow struct {
|
||||
ID string
|
||||
Type string
|
||||
ValueText string
|
||||
ValueNum *float64
|
||||
ValueDate *string
|
||||
Confidence float64
|
||||
Origin string
|
||||
}
|
||||
|
||||
// UpsertAutoFacts replaces the origin='auto' facts of a resource with a fresh
|
||||
// extraction. Rows with origin='user' (S3 corrections) are never touched.
|
||||
// An empty params clears stale auto facts (e.g. OCR re-run yielding nothing).
|
||||
func (r *Resources) UpsertAutoFacts(resourceID string, params []FactParam) error {
|
||||
tx, err := r.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback() //nolint:errcheck — no-op after Commit.
|
||||
if _, err := tx.Exec(
|
||||
`DELETE FROM resource_facts WHERE resource_id = $1 AND origin = 'auto'`,
|
||||
resourceID,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, p := range params {
|
||||
id, err := NewIDErr()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(
|
||||
`INSERT INTO resource_facts
|
||||
(id, resource_id, fact_type, value_text, value_num, value_date, confidence, origin)
|
||||
VALUES ($1, $2, $3, NULLIF($4, ''), $5, NULLIF($6, '')::date, $7, 'auto')`,
|
||||
id, resourceID, p.Type, p.ValueText, nullableFloat(p.ValueNum), derefStr(p.ValueDate), clamp01(p.Confidence),
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// ListFacts returns the facts of a resource, user-corrected first then auto
|
||||
// (stable order by type). No visibility check — the caller enforces viewer+
|
||||
// (GetFileVisible) like GetFileOcr.
|
||||
func (r *Resources) ListFacts(resourceID string) ([]FactRow, error) {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT id, fact_type, COALESCE(value_text, ''), value_num,
|
||||
COALESCE(value_date::text, ''), confidence, origin
|
||||
FROM resource_facts WHERE resource_id = $1
|
||||
ORDER BY CASE origin WHEN 'user' THEN 0 ELSE 1 END, fact_type, value_text`,
|
||||
resourceID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make([]FactRow, 0)
|
||||
for rows.Next() {
|
||||
var row FactRow
|
||||
var num sql.NullFloat64
|
||||
var dat string
|
||||
if err := rows.Scan(&row.ID, &row.Type, &row.ValueText, &num, &dat, &row.Confidence, &row.Origin); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if num.Valid {
|
||||
v := num.Float64
|
||||
row.ValueNum = &v
|
||||
}
|
||||
if dat != "" {
|
||||
d := dat
|
||||
row.ValueDate = &d
|
||||
}
|
||||
out = append(out, row)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// CorrectFacts fully replaces the origin='user' set of a resource (S3 :
|
||||
// correction en un tap — le client envoie le jeu corrigé complet, pas un
|
||||
// diff). Auto facts are never touched. Empty params clears user corrections
|
||||
// (back to pure auto).
|
||||
func (r *Resources) CorrectFacts(resourceID string, params []FactParam) error {
|
||||
tx, err := r.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback() //nolint:errcheck — no-op after Commit.
|
||||
if _, err := tx.Exec(
|
||||
`DELETE FROM resource_facts WHERE resource_id = $1 AND origin = 'user'`,
|
||||
resourceID,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, p := range params {
|
||||
id, err := NewIDErr()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(
|
||||
`INSERT INTO resource_facts
|
||||
(id, resource_id, fact_type, value_text, value_num, value_date, confidence, origin)
|
||||
VALUES ($1, $2, $3, NULLIF($4, ''), $5, NULLIF($6, '')::date, $7, 'user')`,
|
||||
id, resourceID, p.Type, p.ValueText, nullableFloat(p.ValueNum), derefStr(p.ValueDate), clamp01(p.Confidence),
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
type factNeedingExtract struct {
|
||||
UserID string
|
||||
ResourceID string
|
||||
Text string
|
||||
}
|
||||
|
||||
// ListNeedingExtract returns up to limit files with non-empty ocr_text and no
|
||||
// origin='auto' facts (boot backfill, S2). Live rows only.
|
||||
func (r *Resources) ListNeedingExtract(limit int) ([]factNeedingExtract, error) {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT user_id, resource_id, ocr_text FROM resources
|
||||
WHERE type = 'file' AND deleted_at IS NULL AND user_id IS NOT NULL
|
||||
AND ocr_text IS NOT NULL AND ocr_text <> ''
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM resource_facts
|
||||
WHERE resource_facts.resource_id = resources.resource_id
|
||||
AND origin = 'auto')
|
||||
ORDER BY updated_at ASC LIMIT $1`,
|
||||
limit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []factNeedingExtract
|
||||
for rows.Next() {
|
||||
var row factNeedingExtract
|
||||
if err := rows.Scan(&row.UserID, &row.ResourceID, &row.Text); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, row)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func nullableFloat(v *float64) any {
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
return *v
|
||||
}
|
||||
|
||||
func derefStr(v *string) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
}
|
||||
return *v
|
||||
}
|
||||
|
||||
func clamp01(v float64) float64 {
|
||||
if v < 0 {
|
||||
return 0
|
||||
}
|
||||
if v > 1 {
|
||||
return 1
|
||||
}
|
||||
return v
|
||||
}
|
||||
@@ -0,0 +1,355 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// Link types — mirror of the resource_links CHECK (migration 000021).
|
||||
// All types are symmetric : stored once with from_id < to_id (canonical
|
||||
// order), read from either end. version_of is intentionally absent :
|
||||
// versions live INSIDE a resource (resource_versions, 000018).
|
||||
const (
|
||||
LinkSameScan = "same_scan"
|
||||
LinkDuplicateOf = "duplicate_of"
|
||||
LinkSameIssuer = "same_issuer"
|
||||
LinkSameReference = "same_reference"
|
||||
)
|
||||
|
||||
// LinkRow is one edge viewed from the queried resource.
|
||||
type LinkRow struct {
|
||||
ID string
|
||||
OtherID string
|
||||
Type string
|
||||
Confidence float64
|
||||
CreatedBy string
|
||||
OtherName string
|
||||
OtherType string
|
||||
OtherSize int64
|
||||
}
|
||||
|
||||
// canonicalPair orders an unordered pair for symmetric storage.
|
||||
func canonicalPair(a, b string) (string, string) {
|
||||
if a < b {
|
||||
return a, b
|
||||
}
|
||||
return b, a
|
||||
}
|
||||
|
||||
// ListLinks returns the live edges touching resourceID (either direction),
|
||||
// joined with the other end's identity. Trashed others are excluded.
|
||||
// No ownership check — the caller scopes (owner-only in S5).
|
||||
func (r *Resources) ListLinks(resourceID string) ([]LinkRow, error) {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT l.id,
|
||||
CASE WHEN l.from_id = $1 THEN l.to_id ELSE l.from_id END AS other_id,
|
||||
l.link_type, l.confidence, l.created_by,
|
||||
o.name, o.type, COALESCE(o.size_bytes, 0)
|
||||
FROM resource_links l
|
||||
JOIN resources o ON o.resource_id = CASE WHEN l.from_id = $1 THEN l.to_id ELSE l.from_id END
|
||||
WHERE (l.from_id = $1 OR l.to_id = $1)
|
||||
AND o.deleted_at IS NULL
|
||||
ORDER BY l.confidence DESC, o.name ASC`,
|
||||
resourceID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make([]LinkRow, 0)
|
||||
for rows.Next() {
|
||||
var row LinkRow
|
||||
if err := rows.Scan(&row.ID, &row.OtherID, &row.Type, &row.Confidence, &row.CreatedBy, &row.OtherName, &row.OtherType, &row.OtherSize); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, row)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// ensureAutoLink inserts a symmetric auto edge (canonical order). Any
|
||||
// existing row — auto or user verdict — blocks recreation (an invalidated
|
||||
// edge never comes back by itself).
|
||||
func (r *Resources) ensureAutoLink(from, to, linkType string, confidence float64) error {
|
||||
a, b := canonicalPair(from, to)
|
||||
id, err := NewIDErr()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = r.DB.Exec(
|
||||
`INSERT INTO resource_links (id, from_id, to_id, link_type, confidence, created_by)
|
||||
VALUES ($1, $2, $3, $4, $5, 'auto')
|
||||
ON CONFLICT (from_id, to_id, link_type) DO NOTHING`,
|
||||
id, a, b, linkType, clamp01(confidence),
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
// SetUserVerdict records an explicit verdict (created_by='user') : confirm →
|
||||
// confidence 1.0, invalidate → 0.0. Upserts over any previous row (auto or
|
||||
// older verdict) — the latest human word wins.
|
||||
func (r *Resources) SetUserVerdict(one, other, linkType string, confirmed bool) error {
|
||||
a, b := canonicalPair(one, other)
|
||||
conf := 0.0
|
||||
if confirmed {
|
||||
conf = 1.0
|
||||
}
|
||||
id, err := NewIDErr()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = r.DB.Exec(
|
||||
`INSERT INTO resource_links (id, from_id, to_id, link_type, confidence, created_by)
|
||||
VALUES ($1, $2, $3, $4, $5, 'user')
|
||||
ON CONFLICT (from_id, to_id, link_type)
|
||||
DO UPDATE SET confidence = EXCLUDED.confidence, created_by = 'user'`,
|
||||
id, a, b, linkType, conf,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
// pruneAutoLinks drops auto edges of the symmetric types touching resourceID
|
||||
// whose pair is no longer a candidate (facts/session edited). keep holds
|
||||
// canonical "a\x00b\x00type" keys to preserve. User verdicts are never pruned.
|
||||
func (r *Resources) pruneAutoLinks(resourceID string, keep map[string]bool) error {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT id, from_id, to_id, link_type FROM resource_links
|
||||
WHERE (from_id = $1 OR to_id = $1) AND created_by = 'auto'`,
|
||||
resourceID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var stale []string
|
||||
for rows.Next() {
|
||||
var id, a, b, typ string
|
||||
if err := rows.Scan(&id, &a, &b, &typ); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
if !keep[a+"\x00"+b+"\x00"+typ] {
|
||||
stale = append(stale, id)
|
||||
}
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, id := range stale {
|
||||
if _, err := r.DB.Exec(`DELETE FROM resource_links WHERE id = $1`, id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// linkCandidate is one deterministic pair found by reconciliation.
|
||||
type linkCandidate struct {
|
||||
other string
|
||||
typ string
|
||||
confidence float64
|
||||
}
|
||||
|
||||
// ReconcileLinks recomputes the deterministic auto edges of a live owned
|
||||
// resource (same_scan, duplicate_of, same_issuer, same_reference), creates
|
||||
// missing ones and prunes stale auto ones. User verdicts are preserved and
|
||||
// block recreation. Idempotent — safe to call on every read (GET links).
|
||||
func (r *Resources) ReconcileLinks(ownerID, resourceID string) error {
|
||||
var session sql.NullString
|
||||
var hash sql.NullString
|
||||
var size int64
|
||||
err := r.DB.QueryRow(
|
||||
`SELECT COALESCE(scan_session_id, ''), COALESCE(content_hash, ''), COALESCE(size_bytes, 0)
|
||||
FROM resources WHERE resource_id = $1 AND user_id = $2 AND deleted_at IS NULL`,
|
||||
resourceID, ownerID,
|
||||
).Scan(&session, &hash, &size)
|
||||
if err == sql.ErrNoRows {
|
||||
return ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var cands []linkCandidate
|
||||
|
||||
// same_scan : same opaque session, same user, live, not self.
|
||||
if session.Valid && session.String != "" {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT resource_id FROM resources
|
||||
WHERE user_id = $1 AND scan_session_id = $2 AND resource_id != $3 AND deleted_at IS NULL`,
|
||||
ownerID, session.String, resourceID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for rows.Next() {
|
||||
var other string
|
||||
if err := rows.Scan(&other); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
cands = append(cands, linkCandidate{other, LinkSameScan, 1.0})
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// duplicate_of : same content hash (non-empty), size > 0, same user, live.
|
||||
if hash.Valid && hash.String != "" && size > 0 {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT resource_id FROM resources
|
||||
WHERE user_id = $1 AND content_hash = $2 AND size_bytes > 0
|
||||
AND resource_id != $3 AND deleted_at IS NULL`,
|
||||
ownerID, hash.String, resourceID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for rows.Next() {
|
||||
var other string
|
||||
if err := rows.Scan(&other); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
cands = append(cands, linkCandidate{other, LinkDuplicateOf, 1.0})
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// same_issuer / same_reference : shared fact values (case-insensitive
|
||||
// for issuers, exact for reference codes), any origin (user corrections
|
||||
// count — they ARE the household truth). Effective facts only : un fait
|
||||
// auto est ignoré quand un fait user du même type existe sur la même
|
||||
// ressource (miroir de `displayFacts` côté mobile — sinon une correction
|
||||
// laisserait l'arête auto périmée).
|
||||
for _, typ := range []string{"issuer", "reference"} {
|
||||
linkTyp := LinkSameIssuer
|
||||
if typ == "reference" {
|
||||
linkTyp = LinkSameReference
|
||||
}
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT DISTINCT o.resource_id, LEAST(f.confidence, g.confidence)
|
||||
FROM resource_facts f
|
||||
JOIN resource_facts g ON g.fact_type = f.fact_type
|
||||
AND g.resource_id != f.resource_id
|
||||
AND CASE WHEN f.fact_type = 'issuer'
|
||||
THEN LOWER(g.value_text) = LOWER(f.value_text)
|
||||
ELSE g.value_text = f.value_text END
|
||||
JOIN resources o ON o.resource_id = g.resource_id
|
||||
WHERE f.resource_id = $1 AND f.fact_type = $2 AND f.value_text IS NOT NULL
|
||||
AND (f.origin = 'user' OR NOT EXISTS (
|
||||
SELECT 1 FROM resource_facts u WHERE u.resource_id = f.resource_id
|
||||
AND u.fact_type = f.fact_type AND u.origin = 'user'))
|
||||
AND (g.origin = 'user' OR NOT EXISTS (
|
||||
SELECT 1 FROM resource_facts u WHERE u.resource_id = g.resource_id
|
||||
AND u.fact_type = g.fact_type AND u.origin = 'user'))
|
||||
AND o.user_id = $3 AND o.deleted_at IS NULL`,
|
||||
resourceID, typ, ownerID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for rows.Next() {
|
||||
var other string
|
||||
var conf float64
|
||||
if err := rows.Scan(&other, &conf); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
cands = append(cands, linkCandidate{other, linkTyp, conf})
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
keep := make(map[string]bool, len(cands))
|
||||
for _, c := range cands {
|
||||
a, b := canonicalPair(resourceID, c.other)
|
||||
keep[a+"\x00"+b+"\x00"+c.typ] = true
|
||||
if err := r.ensureAutoLink(resourceID, c.other, c.typ, c.confidence); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return r.pruneAutoLinks(resourceID, keep)
|
||||
}
|
||||
|
||||
// DuplicateGroup is one set of byte-identical live files (B2 screen).
|
||||
type DuplicateGroup struct {
|
||||
ContentHash string
|
||||
Count int
|
||||
TotalBytes int64
|
||||
ReclaimableBytes int64 // total - biggest member (garder un seul)
|
||||
Files []DuplicateFile
|
||||
}
|
||||
|
||||
// DuplicateFile is one member of a duplicate group, newest first.
|
||||
type DuplicateFile struct {
|
||||
ID string
|
||||
Name string
|
||||
Size int64
|
||||
UpdatedAt int64 // ms epoch
|
||||
}
|
||||
|
||||
// ListDuplicateGroups returns the owner's byte-identical file sets
|
||||
// (content_hash non-empty, size > 0, live only), biggest groups first.
|
||||
func (r *Resources) ListDuplicateGroups(ownerID string) ([]DuplicateGroup, error) {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT content_hash, COUNT(*), COALESCE(SUM(size_bytes), 0)
|
||||
FROM resources
|
||||
WHERE user_id = $1 AND type = 'file' AND deleted_at IS NULL
|
||||
AND content_hash IS NOT NULL AND content_hash <> '' AND size_bytes > 0
|
||||
GROUP BY content_hash HAVING COUNT(*) > 1
|
||||
ORDER BY SUM(size_bytes) DESC`,
|
||||
ownerID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []DuplicateGroup{}
|
||||
for rows.Next() {
|
||||
var g DuplicateGroup
|
||||
if err := rows.Scan(&g.ContentHash, &g.Count, &g.TotalBytes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
members, err := r.DB.Query(
|
||||
`SELECT resource_id, name, COALESCE(size_bytes, 0),
|
||||
(EXTRACT(EPOCH FROM updated_at) * 1000)::bigint
|
||||
FROM resources
|
||||
WHERE user_id = $1 AND type = 'file' AND deleted_at IS NULL AND content_hash = $2
|
||||
ORDER BY updated_at DESC`,
|
||||
ownerID, g.ContentHash,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var biggest int64
|
||||
for members.Next() {
|
||||
var f DuplicateFile
|
||||
if err := members.Scan(&f.ID, &f.Name, &f.Size, &f.UpdatedAt); err != nil {
|
||||
members.Close()
|
||||
return nil, err
|
||||
}
|
||||
if f.Size > biggest {
|
||||
biggest = f.Size
|
||||
}
|
||||
g.Files = append(g.Files, f)
|
||||
}
|
||||
members.Close()
|
||||
if err := members.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
g.ReclaimableBytes = g.TotalBytes - biggest
|
||||
out = append(out, g)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
|
||||
// PreviewRow mémorise la vignette servie (V3 S7, migration 000024).
|
||||
type PreviewRow struct {
|
||||
SrcSize int64
|
||||
SrcUpdatedAt time.Time
|
||||
}
|
||||
|
||||
// GetPreview lit la génération servie. Absente → sql.ErrNoRows (régénérer).
|
||||
func (r *Resources) GetPreview(resourceID string) (PreviewRow, error) {
|
||||
var row PreviewRow
|
||||
err := r.DB.QueryRow(
|
||||
`SELECT src_size_bytes, src_updated_at FROM resource_previews WHERE resource_id = $1`,
|
||||
resourceID,
|
||||
).Scan(&row.SrcSize, &row.SrcUpdatedAt)
|
||||
if err == sql.ErrNoRows {
|
||||
return PreviewRow{}, err
|
||||
}
|
||||
return row, err
|
||||
}
|
||||
|
||||
// UpsertPreview mémorise la génération servie (taille + updated_at sources).
|
||||
func (r *Resources) UpsertPreview(resourceID string, srcSize int64, srcUpdatedAt time.Time) error {
|
||||
_, err := r.DB.Exec(
|
||||
`INSERT INTO resource_previews (resource_id, src_size_bytes, src_updated_at)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (resource_id)
|
||||
DO UPDATE SET src_size_bytes = EXCLUDED.src_size_bytes,
|
||||
src_updated_at = EXCLUDED.src_updated_at,
|
||||
created_at = NOW()`,
|
||||
resourceID, srcSize, srcUpdatedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
@@ -51,6 +51,112 @@ func isForeignKeyViolation(err error) bool {
|
||||
return errors.As(err, &pqErr) && pqErr.Code == "23503"
|
||||
}
|
||||
|
||||
// SetContentHash stores the SHA-256 of the live bytes (V3 S5 duplicates).
|
||||
// Owner-scoped, live rows. Best-effort côté service (jamais d'erreur vers
|
||||
// le client).
|
||||
func (r *Resources) SetContentHash(ownerID, resourceID, hash string) error {
|
||||
result, err := r.DB.Exec(
|
||||
`UPDATE resources SET content_hash = NULLIF($3, ''), updated_at = NOW()
|
||||
WHERE resource_id = $1 AND user_id = $2 AND type = 'file' AND deleted_at IS NULL`,
|
||||
resourceID, ownerID, hash,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetScanSession records the opaque client scan-session of a resource
|
||||
// (V3 S5 same_scan — drift informatif, jamais autorisant). Owner-scoped.
|
||||
func (r *Resources) SetScanSession(ownerID, resourceID, sessionID string) error {
|
||||
result, err := r.DB.Exec(
|
||||
`UPDATE resources SET scan_session_id = NULLIF($3, ''), updated_at = NOW()
|
||||
WHERE resource_id = $1 AND user_id = $2 AND deleted_at IS NULL`,
|
||||
resourceID, ownerID, sessionID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MissingHash identifies a live file without content_hash (boot backfill).
|
||||
type MissingHash struct {
|
||||
UserID string
|
||||
ResourceID string
|
||||
}
|
||||
|
||||
// ListMissingHashes returns up to limit live files with bytes but no hash.
|
||||
func (r *Resources) ListMissingHashes(limit int) ([]MissingHash, error) {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT user_id, resource_id FROM resources
|
||||
WHERE type = 'file' AND deleted_at IS NULL AND user_id IS NOT NULL AND has_content = TRUE
|
||||
AND (content_hash IS NULL OR content_hash = '')
|
||||
ORDER BY updated_at ASC LIMIT $1`,
|
||||
limit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []MissingHash
|
||||
for rows.Next() {
|
||||
var m MissingHash
|
||||
if err := rows.Scan(&m.UserID, &m.ResourceID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, m)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// StorageUsage aggregates a user's footprint (V3 S6 A2 — calculé, jamais
|
||||
// stocké). Les trashés sont exclus (octets récupérables à la purge).
|
||||
type StorageUsage struct {
|
||||
UsedBytes int64
|
||||
FileCount int
|
||||
TrashCount int
|
||||
TrashedBytes int64
|
||||
}
|
||||
|
||||
// StorageUsage sums live file bytes + counts (live + trashed) for a user.
|
||||
func (r *Resources) StorageUsage(ownerID string) (StorageUsage, error) {
|
||||
var u StorageUsage
|
||||
err := r.DB.QueryRow(
|
||||
`SELECT COALESCE(SUM(size_bytes), 0), COUNT(*)
|
||||
FROM resources
|
||||
WHERE user_id = $1 AND type = 'file' AND deleted_at IS NULL`,
|
||||
ownerID,
|
||||
).Scan(&u.UsedBytes, &u.FileCount)
|
||||
if err != nil {
|
||||
return StorageUsage{}, err
|
||||
}
|
||||
err = r.DB.QueryRow(
|
||||
`SELECT COUNT(*), COALESCE(SUM(size_bytes), 0)
|
||||
FROM resources
|
||||
WHERE user_id = $1 AND deleted_at IS NOT NULL`,
|
||||
ownerID,
|
||||
).Scan(&u.TrashCount, &u.TrashedBytes)
|
||||
if err != nil {
|
||||
return StorageUsage{}, err
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
|
||||
type FileRow struct {
|
||||
ID string
|
||||
Name string
|
||||
@@ -569,13 +675,12 @@ func (r *Resources) UpdateOcrText(ownerID, resourceID, text string) error {
|
||||
|
||||
// SyncDelete soft-deletes a resource; absence is NOT an error (idempotent
|
||||
// terminal state for the outbox).
|
||||
//
|
||||
// Deprecated: V3 trash — préférer TrashResource (récursif sur le subtree).
|
||||
// Conservé comme alias pour compatibilité (même effet sur une ressource
|
||||
// sans enfant : TrashResource ne touche que deleted_at IS NULL).
|
||||
func (r *Resources) SyncDelete(ownerID, resourceID string) error {
|
||||
_, err := r.DB.Exec(
|
||||
`UPDATE resources SET deleted_at = NOW(), updated_at = NOW()
|
||||
WHERE resource_id = $1 AND user_id = $2 AND deleted_at IS NULL`,
|
||||
resourceID, ownerID,
|
||||
)
|
||||
return err
|
||||
return r.TrashResource(ownerID, resourceID)
|
||||
}
|
||||
|
||||
// ExistsOwner reports whether a non-deleted resource belongs to the device.
|
||||
@@ -628,6 +733,24 @@ func (r *Resources) TouchResource(resourceID string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// LiveNameAndUpdatedAt returns the current name and updated_at (ms epoch) of
|
||||
// a live resource, any type, unscoped — the caller (sync) has already
|
||||
// verified effective access. Absent/trashed → ErrNotFound. Sert à la
|
||||
// détection de conflits de rename (S6 A4 : baseUpdatedAt périmé).
|
||||
func (r *Resources) LiveNameAndUpdatedAt(resourceID string) (string, int64, error) {
|
||||
var name string
|
||||
var ms int64
|
||||
err := r.DB.QueryRow(
|
||||
`SELECT name, (EXTRACT(EPOCH FROM updated_at) * 1000)::bigint
|
||||
FROM resources WHERE resource_id = $1 AND deleted_at IS NULL`,
|
||||
resourceID,
|
||||
).Scan(&name, &ms)
|
||||
if err == sql.ErrNoRows {
|
||||
return "", 0, ErrNotFound
|
||||
}
|
||||
return name, ms, err
|
||||
}
|
||||
|
||||
// OwnedRow is a snapshot row: resource identity + freshness.
|
||||
type OwnedRow struct {
|
||||
ID string
|
||||
@@ -708,3 +831,68 @@ func (r *Resources) ListRootFoldersVisible(userID string) ([]FolderRow, error) {
|
||||
}
|
||||
return folders, rows.Err()
|
||||
}
|
||||
|
||||
// ExportFileRow is one live file for full export (V3 S7 C3).
|
||||
type ExportFileRow struct {
|
||||
ID string
|
||||
Name string
|
||||
ParentID string
|
||||
Size int64
|
||||
MimeType string
|
||||
Extension string
|
||||
Hash string
|
||||
OcrText string
|
||||
HasContent bool
|
||||
}
|
||||
|
||||
// ExportFiles returns ALL live files of a user (bounded — full export).
|
||||
func (r *Resources) ExportFiles(ownerID string, limit int) ([]ExportFileRow, error) {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT resource_id, name, COALESCE(parent_id, ''), COALESCE(size_bytes, 0),
|
||||
COALESCE(mime_type, ''), COALESCE(extension, ''),
|
||||
COALESCE(content_hash, ''), COALESCE(ocr_text, ''),
|
||||
COALESCE(has_content, FALSE)
|
||||
FROM resources
|
||||
WHERE user_id = $1 AND type = 'file' AND deleted_at IS NULL
|
||||
ORDER BY created_at ASC LIMIT $2`,
|
||||
ownerID, limit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make([]ExportFileRow, 0)
|
||||
for rows.Next() {
|
||||
var row ExportFileRow
|
||||
if err := rows.Scan(&row.ID, &row.Name, &row.ParentID, &row.Size, &row.MimeType,
|
||||
&row.Extension, &row.Hash, &row.OcrText, &row.HasContent); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, row)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// ExportFolders returns ALL live folders of a user (bounded — full export).
|
||||
func (r *Resources) ExportFolders(ownerID string, limit int) ([]FolderRow, error) {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT resource_id, name, COALESCE(parent_id, '')
|
||||
FROM resources
|
||||
WHERE user_id = $1 AND type = 'folder' AND deleted_at IS NULL
|
||||
ORDER BY created_at ASC LIMIT $2`,
|
||||
ownerID, limit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make([]FolderRow, 0)
|
||||
for rows.Next() {
|
||||
var folder FolderRow
|
||||
if err := rows.Scan(&folder.ID, &folder.Name, &folder.ParentID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, folder)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SearchFacets filters document search on extracted facts (V3 S4).
|
||||
// Empty = no filter. From/To are ISO dates (YYYY-MM-DD) applied to
|
||||
// date/due_date facts and overlapped with period facts (YYYY-MM).
|
||||
type SearchFacets struct {
|
||||
Issuer string
|
||||
From, To string
|
||||
MinAmount, MaxAmount *float64
|
||||
DocType string
|
||||
}
|
||||
|
||||
// searchFileColumns mirrors fileColumns qualified for `resources r`.
|
||||
const searchFileColumns = `r.resource_id, r.name, r.size_bytes, COALESCE(r.mime_type, ''), COALESCE(r.parent_id, ''), r.created_at, r.updated_at, COALESCE(r.ocr_text, ''), COALESCE(r.has_content, FALSE)`
|
||||
|
||||
// SearchDocumentsVisible returns the user's accessible files (owned or
|
||||
// shared, viewer+) matching q across name (ILIKE, compat), full-text
|
||||
// (french TS, name+OCR) and extracted facts (ILIKE on value_text),
|
||||
// optionally narrowed by facets. Ranked: TS rank + fact bonus (high-
|
||||
// confidence fact match +1.0, low-confidence +0.5 — humble by design :
|
||||
// low-confidence demotes, never excludes), then name.
|
||||
func (r *Resources) SearchDocumentsVisible(userID, q string, facets SearchFacets, limit, offset int) ([]FileRow, int, error) {
|
||||
where, order, args := r.searchDocumentsClauses(userID, q, facets)
|
||||
base := `WITH RECURSIVE ` + grantedCTE + ` SELECT %s FROM resources r WHERE ` + where
|
||||
|
||||
var total int
|
||||
if err := r.DB.QueryRow(fmt.Sprintf(base, "COUNT(*)"), args...).Scan(&total); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
rows, err := r.DB.Query(
|
||||
fmt.Sprintf(base+` ORDER BY %s, r.name ASC LIMIT $%d OFFSET $%d`, searchFileColumns, order, len(args)+1, len(args)+2),
|
||||
append(args, limit, offset)...,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
files := make([]FileRow, 0)
|
||||
for rows.Next() {
|
||||
row, err := r.scanFile(rows.Scan)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
files = append(files, row)
|
||||
}
|
||||
return files, total, rows.Err()
|
||||
}
|
||||
|
||||
// searchDocumentsClauses builds the WHERE (match) and ORDER BY (rank)
|
||||
// fragments plus bound args. $1 is always userID (grantedCTE).
|
||||
func (r *Resources) searchDocumentsClauses(userID, q string, facets SearchFacets) (where, order string, args []any) {
|
||||
args = []any{userID}
|
||||
next := 2
|
||||
ph := func(v any) string {
|
||||
args = append(args, v)
|
||||
p := fmt.Sprintf("$%d", next)
|
||||
next++
|
||||
return p
|
||||
}
|
||||
|
||||
// plainto_tsquery placeholder partagé match/rang (le COUNT ne voit que
|
||||
// le WHERE : aucun placeholder ne doit n'exister que dans le ORDER BY).
|
||||
// Même règle pour le pattern LIKE du nom (bonus de rang).
|
||||
tsPh := ph(q)
|
||||
namePh := ph(`%` + escapeLike(q) + `%`)
|
||||
matchAlts := []string{
|
||||
`r.name ILIKE ` + namePh + ` ESCAPE '\'`,
|
||||
`r.search_tsv @@ plainto_tsquery('french', ` + tsPh + `)`,
|
||||
}
|
||||
|
||||
// Fact word matches (words >= 3 chars — shorter are French stopwords).
|
||||
// `pat` est partagé entre le match et le bonus du rang : le COUNT ne voit
|
||||
// que le WHERE, aucun placeholder ne doit n'exister que dans le ORDER BY.
|
||||
words := longWords(q, 3, 5)
|
||||
var wordConds, wordCondsHigh []string
|
||||
for _, w := range words {
|
||||
pat := ph(`%` + escapeLike(w) + `%`)
|
||||
wordConds = append(wordConds,
|
||||
`EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = r.resource_id AND f.value_text ILIKE `+pat+` ESCAPE '\')`)
|
||||
wordCondsHigh = append(wordCondsHigh,
|
||||
`EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = r.resource_id AND f.confidence >= 0.5 AND f.value_text ILIKE `+pat+` ESCAPE '\')`)
|
||||
}
|
||||
if len(wordConds) > 0 {
|
||||
matchAlts = append(matchAlts, `(`+strings.Join(wordConds, " OR ")+`)`)
|
||||
}
|
||||
|
||||
rankParts := []string{
|
||||
// Match nom exact : bonus dominant (2.0) — un nom trouvé bat
|
||||
// toujours un fait douteux (0.5), humble par design.
|
||||
`CASE WHEN r.name ILIKE ` + namePh + ` ESCAPE '\' THEN 2.0 ELSE 0 END`,
|
||||
`COALESCE(ts_rank_cd(r.search_tsv, plainto_tsquery('french', ` + tsPh + `)), 0)`,
|
||||
}
|
||||
if len(wordCondsHigh) > 0 {
|
||||
rankParts = append(rankParts,
|
||||
`CASE WHEN (`+strings.Join(wordCondsHigh, " OR ")+`) THEN 1.0 WHEN (`+strings.Join(wordConds, " OR ")+`) THEN 0.5 ELSE 0 END`)
|
||||
}
|
||||
|
||||
conds := []string{
|
||||
`r.type = 'file'`,
|
||||
`r.deleted_at IS NULL`,
|
||||
`r.resource_id IN (SELECT id FROM granted)`,
|
||||
`(` + strings.Join(matchAlts, " OR ") + `)`,
|
||||
}
|
||||
|
||||
// Facets (AND).
|
||||
if facets.Issuer != "" {
|
||||
conds = append(conds,
|
||||
`EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = r.resource_id AND f.fact_type = 'issuer' AND f.value_text ILIKE `+ph(`%`+escapeLike(facets.Issuer)+`%`)+` ESCAPE '\')`)
|
||||
}
|
||||
if facets.DocType != "" {
|
||||
conds = append(conds,
|
||||
`EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = r.resource_id AND f.fact_type = 'doc_type' AND f.value_text = `+ph(facets.DocType)+`)`)
|
||||
}
|
||||
if facets.MinAmount != nil {
|
||||
conds = append(conds,
|
||||
`EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = r.resource_id AND f.fact_type = 'amount' AND f.value_num >= `+ph(*facets.MinAmount)+`)`)
|
||||
}
|
||||
if facets.MaxAmount != nil {
|
||||
conds = append(conds,
|
||||
`EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = r.resource_id AND f.fact_type = 'amount' AND f.value_num <= `+ph(*facets.MaxAmount)+`)`)
|
||||
}
|
||||
if facets.From != "" || facets.To != "" {
|
||||
from := facets.From
|
||||
if from == "" {
|
||||
from = "0001-01-01"
|
||||
}
|
||||
to := facets.To
|
||||
if to == "" {
|
||||
to = "9999-12-31"
|
||||
}
|
||||
fromMonth, toMonth := from, to
|
||||
if len(fromMonth) >= 7 {
|
||||
fromMonth = fromMonth[:7]
|
||||
}
|
||||
if len(toMonth) >= 7 {
|
||||
toMonth = toMonth[:7]
|
||||
}
|
||||
conds = append(conds, `(
|
||||
EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = r.resource_id AND f.fact_type IN ('date', 'due_date') AND f.value_date >= `+ph(from)+`::date AND f.value_date <= `+ph(to)+`::date)
|
||||
OR EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = r.resource_id AND f.fact_type = 'period' AND f.value_text >= `+ph(fromMonth)+` AND f.value_text <= `+ph(toMonth)+`)
|
||||
)`)
|
||||
}
|
||||
|
||||
where = strings.Join(conds, " AND ")
|
||||
order = strings.Join(rankParts, " + ") + " DESC"
|
||||
return where, order, args
|
||||
}
|
||||
|
||||
// longWords splits q into words of at least minLen runes, capped at max.
|
||||
func longWords(q string, minLen, max int) []string {
|
||||
var out []string
|
||||
seen := map[string]bool{}
|
||||
for _, w := range strings.Fields(q) {
|
||||
if len([]rune(w)) < minLen || seen[w] {
|
||||
continue
|
||||
}
|
||||
seen[w] = true
|
||||
out = append(out, w)
|
||||
if len(out) >= max {
|
||||
break
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/dbtest"
|
||||
)
|
||||
|
||||
const searchTestURL = "postgres://kazier:kazier@localhost:5432/kazier_search_test?sslmode=disable"
|
||||
|
||||
func newSearchRepo(t *testing.T) *Repository {
|
||||
t.Helper()
|
||||
return NewRepository(dbtest.OpenTestDatabase(t, searchTestURL))
|
||||
}
|
||||
|
||||
func mustSearchUser(t *testing.T, repo *Repository, username string) string {
|
||||
t.Helper()
|
||||
id, err := repo.Users.Create(username, username, "test-hash", false)
|
||||
if err != nil {
|
||||
t.Fatalf("create user: %v", err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func seedDoc(t *testing.T, repo *Repository, userID, name, ocr string, facts []FactParam) string {
|
||||
t.Helper()
|
||||
id := NewID()
|
||||
if err := repo.Resources.InsertFile(userID, id, name, "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert %q: %v", name, err)
|
||||
}
|
||||
if ocr != "" {
|
||||
if err := repo.Resources.UpdateOcrText(userID, id, ocr); err != nil {
|
||||
t.Fatalf("ocr %q: %v", name, err)
|
||||
}
|
||||
}
|
||||
if len(facts) > 0 {
|
||||
if err := repo.Resources.UpsertAutoFacts(id, facts); err != nil {
|
||||
t.Fatalf("facts %q: %v", name, err)
|
||||
}
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func f64(v float64) *float64 { return &v }
|
||||
func fstr(v string) *string { return &v }
|
||||
|
||||
func searchIDs(t *testing.T, repo *Repository, userID, q string, facets SearchFacets) []string {
|
||||
t.Helper()
|
||||
rows, _, err := repo.Resources.SearchDocumentsVisible(userID, q, facets, 50, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("search %q: %v", q, err)
|
||||
}
|
||||
ids := make([]string, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
ids = append(ids, r.ID)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func containsID(ids []string, want string) bool {
|
||||
for _, id := range ids {
|
||||
if id == want {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func TestSearchDocumentsFTS(t *testing.T) {
|
||||
repo := newSearchRepo(t)
|
||||
userID := mustSearchUser(t, repo, "search-fts")
|
||||
|
||||
// Nom opaque : trouvable uniquement via l'OCR (TS français).
|
||||
scan := seedDoc(t, repo, userID, "scan_042.pdf",
|
||||
"Attestation d'assurance MMA pour l'année 2026", nil)
|
||||
plain := seedDoc(t, repo, userID, "assurance-vie.txt", "", nil)
|
||||
|
||||
ids := searchIDs(t, repo, userID, "assurance", SearchFacets{})
|
||||
if !containsID(ids, scan) {
|
||||
t.Errorf("scan_042.pdf introuvable via OCR: %v", ids)
|
||||
}
|
||||
if !containsID(ids, plain) {
|
||||
t.Errorf("assurance-vie.txt introuvable via nom: %v", ids)
|
||||
}
|
||||
|
||||
// Stemming français : "factures" matche "facture".
|
||||
fact := seedDoc(t, repo, userID, "doc.pdf", "Votre facture de mars", nil)
|
||||
ids = searchIDs(t, repo, userID, "factures", SearchFacets{})
|
||||
if !containsID(ids, fact) {
|
||||
t.Errorf("stemming FR manquant: %v", ids)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchDocumentsFacets(t *testing.T) {
|
||||
repo := newSearchRepo(t)
|
||||
userID := mustSearchUser(t, repo, "search-facets")
|
||||
|
||||
edf := seedDoc(t, repo, userID, "edf.pdf", "Facture EDF", []FactParam{
|
||||
{Type: "issuer", ValueText: "EDF", Confidence: 0.9},
|
||||
{Type: "amount", ValueText: "84.20 €", ValueNum: f64(84.20), Confidence: 0.9},
|
||||
{Type: "due_date", ValueText: "2026-04-12", ValueDate: fstr("2026-04-12"), Confidence: 0.8},
|
||||
{Type: "doc_type", ValueText: "facture", Confidence: 0.8},
|
||||
})
|
||||
engie := seedDoc(t, repo, userID, "engie.pdf", "Facture Engie", []FactParam{
|
||||
{Type: "issuer", ValueText: "ENGIE", Confidence: 0.9},
|
||||
{Type: "amount", ValueText: "120.00 €", ValueNum: f64(120), Confidence: 0.9},
|
||||
{Type: "doc_type", ValueText: "facture", Confidence: 0.8},
|
||||
})
|
||||
|
||||
ids := searchIDs(t, repo, userID, "facture", SearchFacets{Issuer: "edf"})
|
||||
if !containsID(ids, edf) || containsID(ids, engie) {
|
||||
t.Errorf("facette issuer: %v", ids)
|
||||
}
|
||||
|
||||
ids = searchIDs(t, repo, userID, "facture", SearchFacets{MinAmount: f64(100)})
|
||||
if !containsID(ids, engie) || containsID(ids, edf) {
|
||||
t.Errorf("facette min_amount: %v", ids)
|
||||
}
|
||||
|
||||
ids = searchIDs(t, repo, userID, "facture", SearchFacets{DocType: "facture"})
|
||||
if !containsID(ids, edf) || !containsID(ids, engie) {
|
||||
t.Errorf("facette doc_type: %v", ids)
|
||||
}
|
||||
|
||||
ids = searchIDs(t, repo, userID, "facture", SearchFacets{From: "2026-04-01", To: "2026-04-30"})
|
||||
if !containsID(ids, edf) || containsID(ids, engie) {
|
||||
t.Errorf("facette période: %v", ids)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchDocumentsLowConfidenceDemotes(t *testing.T) {
|
||||
repo := newSearchRepo(t)
|
||||
userID := mustSearchUser(t, repo, "search-humble")
|
||||
|
||||
// Fait douteux (0.3) : trouvé quand même (jamais exclu), mais après le
|
||||
// match franc (nom) — humble par design.
|
||||
sure := seedDoc(t, repo, userID, "EDF-contrat.pdf", "", nil)
|
||||
doubt := seedDoc(t, repo, userID, "scan_x.pdf", "courrier", []FactParam{
|
||||
{Type: "issuer", ValueText: "EDF", Confidence: 0.3},
|
||||
})
|
||||
|
||||
ids := searchIDs(t, repo, userID, "EDF", SearchFacets{})
|
||||
if !containsID(ids, doubt) {
|
||||
t.Fatalf("fait douteux exclu (interdit): %v", ids)
|
||||
}
|
||||
if len(ids) < 2 || ids[0] != sure || ids[1] != doubt {
|
||||
t.Errorf("ordre de rang inattendu (franc avant douteux): %v", ids)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchDocumentsTrashedExcluded(t *testing.T) {
|
||||
repo := newSearchRepo(t)
|
||||
userID := mustSearchUser(t, repo, "search-trash")
|
||||
id := seedDoc(t, repo, userID, "poubelle.pdf", "contenu unique xyz", nil)
|
||||
|
||||
if ids := searchIDs(t, repo, userID, "xyz", SearchFacets{}); !containsID(ids, id) {
|
||||
t.Fatalf("introuvable avant trash: %v", ids)
|
||||
}
|
||||
if err := repo.Resources.TrashResource(userID, id); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
if ids := searchIDs(t, repo, userID, "xyz", SearchFacets{}); containsID(ids, id) {
|
||||
t.Errorf("trashé encore visible: %v", ids)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TrashedRow is a trashed resource for GET /trash.
|
||||
type TrashedRow struct {
|
||||
ID string
|
||||
Type string
|
||||
Name string
|
||||
Size int64
|
||||
TrashedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// subtreeCTE selects a resource and all its descendants (any depth).
|
||||
const subtreeCTE = `
|
||||
subtree(id) AS (
|
||||
SELECT $2::text
|
||||
UNION ALL
|
||||
SELECT r.resource_id FROM resources r JOIN subtree s ON r.parent_id = s.id
|
||||
)`
|
||||
|
||||
// ancestorsCTE selects a resource and all its ancestors up to the root.
|
||||
const ancestorsCTE = `
|
||||
ancestors(id) AS (
|
||||
SELECT $2::text
|
||||
UNION ALL
|
||||
SELECT r.parent_id FROM resources r JOIN ancestors a ON r.resource_id = a.id
|
||||
WHERE r.parent_id IS NOT NULL
|
||||
)`
|
||||
|
||||
// TrashResource soft-deletes a resource and its whole subtree
|
||||
// (`deleted_at = NOW()`), owner-scoped. Already-trashed rows are untouched.
|
||||
// The target must be verified owned beforehand (ExistsOwner / GetResourceOwner)
|
||||
// for the NOT_FOUND halt semantics — this method only applies.
|
||||
func (r *Resources) TrashResource(ownerID, resourceID string) error {
|
||||
_, err := r.DB.Exec(
|
||||
`WITH RECURSIVE `+subtreeCTE+`
|
||||
UPDATE resources SET deleted_at = NOW(), updated_at = NOW()
|
||||
WHERE resource_id IN (SELECT id FROM subtree)
|
||||
AND user_id = $1 AND deleted_at IS NULL`,
|
||||
ownerID, resourceID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
// RestoreResource clears the trash flag on a resource, its subtree AND its
|
||||
// ancestor chain (a file whose folder is still trashed would stay
|
||||
// unreachable otherwise). Independently-trashed descendants are restored too
|
||||
// — documented V3 behaviour (restoring the folder restores what it contains).
|
||||
func (r *Resources) RestoreResource(ownerID, resourceID string) error {
|
||||
_, err := r.DB.Exec(
|
||||
`WITH RECURSIVE `+subtreeCTE+`, `+ancestorsCTE+`
|
||||
UPDATE resources SET deleted_at = NULL, updated_at = NOW()
|
||||
WHERE (resource_id IN (SELECT id FROM subtree)
|
||||
OR resource_id IN (SELECT id FROM ancestors))
|
||||
AND user_id = $1 AND deleted_at IS NOT NULL`,
|
||||
ownerID, resourceID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
// ExistsOwnerAny reports ownership regardless of trash state (for purge :
|
||||
// a trashed resource is still owned).
|
||||
func (r *Resources) ExistsOwnerAny(ownerID, resourceID string) (bool, error) {
|
||||
var exists int
|
||||
err := r.DB.QueryRow(
|
||||
`SELECT 1 FROM resources WHERE resource_id = $1 AND user_id = $2`,
|
||||
resourceID, ownerID,
|
||||
).Scan(&exists)
|
||||
if err == sql.ErrNoRows {
|
||||
return false, nil
|
||||
}
|
||||
return err == nil, err
|
||||
}
|
||||
|
||||
// PurgeResource hard-deletes a resource and its subtree (FK CASCADE on
|
||||
// parent_id + resource_versions), returning every purged resource_id so the
|
||||
// caller can remove physical files best-effort. Owner-scoped.
|
||||
func (r *Resources) PurgeResource(ownerID, resourceID string) ([]string, error) {
|
||||
rows, err := r.DB.Query(
|
||||
`WITH RECURSIVE subtree(id) AS (
|
||||
SELECT $1::text
|
||||
UNION ALL
|
||||
SELECT r.resource_id FROM resources r JOIN subtree s ON r.parent_id = s.id
|
||||
)
|
||||
SELECT id FROM subtree`,
|
||||
resourceID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var ids []string
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := r.DB.Exec(
|
||||
`DELETE FROM resources WHERE resource_id = $1 AND user_id = $2`,
|
||||
resourceID, ownerID,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// ListTrashed returns the owner's trashed resources, most recently trashed
|
||||
// first, paginated.
|
||||
func (r *Resources) ListTrashed(ownerID string, limit, offset int) ([]TrashedRow, int, error) {
|
||||
var total int
|
||||
if err := r.DB.QueryRow(
|
||||
`SELECT COUNT(*) FROM resources WHERE user_id = $1 AND deleted_at IS NOT NULL`,
|
||||
ownerID,
|
||||
).Scan(&total); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT resource_id, type, name, COALESCE(size_bytes, 0), deleted_at, updated_at
|
||||
FROM resources WHERE user_id = $1 AND deleted_at IS NOT NULL
|
||||
ORDER BY deleted_at DESC LIMIT $2 OFFSET $3`,
|
||||
ownerID, limit, offset,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make([]TrashedRow, 0)
|
||||
for rows.Next() {
|
||||
var row TrashedRow
|
||||
if err := rows.Scan(&row.ID, &row.Type, &row.Name, &row.Size, &row.TrashedAt, &row.UpdatedAt); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
out = append(out, row)
|
||||
}
|
||||
return out, total, rows.Err()
|
||||
}
|
||||
|
||||
// PurgedResource identifies a row destroyed by the retention pass.
|
||||
type PurgedResource struct {
|
||||
UserID string
|
||||
ResourceID string
|
||||
}
|
||||
|
||||
// PurgeExpiredTrash hard-deletes every resource trashed before cutoff
|
||||
// (corbeille 30j), oldest first, bounded by limit. Returns the destroyed
|
||||
// identities for physical-file cleanup (best-effort, caller-side).
|
||||
func (r *Resources) PurgeExpiredTrash(cutoff time.Time, limit int) ([]PurgedResource, error) {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT user_id, resource_id FROM resources
|
||||
WHERE deleted_at IS NOT NULL AND deleted_at < $1
|
||||
ORDER BY deleted_at ASC LIMIT $2`,
|
||||
cutoff, limit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out []PurgedResource
|
||||
for rows.Next() {
|
||||
var p PurgedResource
|
||||
if err := rows.Scan(&p.UserID, &p.ResourceID); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, p)
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, p := range out {
|
||||
// Hard delete cascades to children (parent_id FK) and versions.
|
||||
if _, err := r.DB.Exec(
|
||||
`DELETE FROM resources WHERE resource_id = $1 AND user_id = $2 AND deleted_at IS NOT NULL AND deleted_at < $3`,
|
||||
p.ResourceID, p.UserID, cutoff,
|
||||
); err != nil {
|
||||
return out, err
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -13,6 +13,10 @@ type UserRow struct {
|
||||
PasswordHash string
|
||||
IsAdmin bool
|
||||
CreatedAt time.Time
|
||||
// V3 S6 : révocation des tokens (bump au changement de password) et
|
||||
// quota de stockage (garde-fou upload).
|
||||
TokenVersion int
|
||||
QuotaBytes int64
|
||||
}
|
||||
|
||||
// Users persiste les comptes utilisateurs (auth par login, cf. tranche identité).
|
||||
@@ -33,7 +37,8 @@ func (u *Users) Count() (int, error) {
|
||||
// (lowercase, unique). Absent → ErrNotFound.
|
||||
func (u *Users) GetByUsernameNormalized(normalized string) (*UserRow, error) {
|
||||
row := u.DB.QueryRow(
|
||||
`SELECT id, username, username_normalized, password_hash, is_admin, created_at
|
||||
`SELECT id, username, username_normalized, password_hash, is_admin, created_at,
|
||||
token_version, storage_quota_bytes
|
||||
FROM users
|
||||
WHERE username_normalized = $1 AND deleted_at IS NULL`,
|
||||
normalized,
|
||||
@@ -48,7 +53,8 @@ func (u *Users) GetByUsernameNormalized(normalized string) (*UserRow, error) {
|
||||
// GetByID charge un compte actif par id. Absent → ErrNotFound.
|
||||
func (u *Users) GetByID(id string) (*UserRow, error) {
|
||||
row := u.DB.QueryRow(
|
||||
`SELECT id, username, username_normalized, password_hash, is_admin, created_at
|
||||
`SELECT id, username, username_normalized, password_hash, is_admin, created_at,
|
||||
token_version, storage_quota_bytes
|
||||
FROM users
|
||||
WHERE id = $1 AND deleted_at IS NULL`,
|
||||
id,
|
||||
@@ -131,6 +137,27 @@ func (u *Users) MarkDeleted(id string) error {
|
||||
|
||||
func scanUser(scan func(...any) error) (*UserRow, error) {
|
||||
var user UserRow
|
||||
err := scan(&user.ID, &user.Username, &user.UsernameNormalized, &user.PasswordHash, &user.IsAdmin, &user.CreatedAt)
|
||||
err := scan(&user.ID, &user.Username, &user.UsernameNormalized, &user.PasswordHash, &user.IsAdmin, &user.CreatedAt, &user.TokenVersion, &user.QuotaBytes)
|
||||
return &user, err
|
||||
}
|
||||
|
||||
// BumpTokenVersion invalide tous les tokens émis (V3 S6) : le middleware
|
||||
// rejette les tokens dont le claim `tv` diffère. Compte absent → ErrNotFound.
|
||||
func (u *Users) BumpTokenVersion(id string) error {
|
||||
result, err := u.DB.Exec(
|
||||
`UPDATE users SET token_version = token_version + 1, updated_at = NOW()
|
||||
WHERE id = $1 AND deleted_at IS NULL`,
|
||||
id,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
// VersionRow is one frozen byte-generation of a resource.
|
||||
type VersionRow struct {
|
||||
ID string
|
||||
VersionNo int
|
||||
Size int64
|
||||
MimeType string
|
||||
Extension string
|
||||
StoredPath string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// MaxVersions caps the per-resource history (V3 P1 : N=3).
|
||||
const MaxVersions = 3
|
||||
|
||||
// NextVersionNo returns 1 + the highest version number (monotonic, never
|
||||
// reused even after pruning).
|
||||
func (r *Resources) NextVersionNo(resourceID string) (int, error) {
|
||||
var next int
|
||||
err := r.DB.QueryRow(
|
||||
`SELECT COALESCE(MAX(version_no), 0) + 1 FROM resource_versions WHERE resource_id = $1`,
|
||||
resourceID,
|
||||
).Scan(&next)
|
||||
return next, err
|
||||
}
|
||||
|
||||
// InsertVersion freezes one byte-generation. ID must be NewID() 32-hex.
|
||||
func (r *Resources) InsertVersion(id, resourceID string, versionNo int, size int64, mimeType, extension *string, storedPath string) error {
|
||||
_, err := r.DB.Exec(
|
||||
`INSERT INTO resource_versions (id, resource_id, version_no, size_bytes, mime_type, extension, stored_path)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
|
||||
id, resourceID, versionNo, size, mimeType, extension, storedPath,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
// ListVersions returns the history of a resource owned by ownerID
|
||||
// (ownership checked via the parent row), newest first.
|
||||
func (r *Resources) ListVersions(ownerID, resourceID string) ([]VersionRow, error) {
|
||||
if _, err := r.GetFile(ownerID, resourceID); err != nil {
|
||||
// Folders can be versioned too (metadata only has no bytes — in
|
||||
// practice only files carry versions, but don't 404 on type).
|
||||
if _, ferr := r.GetFolder(ownerID, resourceID); ferr != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT v.id, v.version_no, v.size_bytes, COALESCE(v.mime_type, ''),
|
||||
COALESCE(v.extension, ''), v.stored_path, v.created_at
|
||||
FROM resource_versions v
|
||||
JOIN resources r ON r.resource_id = v.resource_id
|
||||
WHERE v.resource_id = $1 AND r.user_id = $2 AND r.deleted_at IS NULL
|
||||
ORDER BY v.version_no DESC`,
|
||||
resourceID, ownerID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make([]VersionRow, 0)
|
||||
for rows.Next() {
|
||||
var row VersionRow
|
||||
if err := rows.Scan(&row.ID, &row.VersionNo, &row.Size, &row.MimeType, &row.Extension, &row.StoredPath, &row.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, row)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// GetVersion returns one frozen generation (owner-scoped via parent row).
|
||||
func (r *Resources) GetVersion(ownerID, resourceID string, versionNo int) (VersionRow, error) {
|
||||
var row VersionRow
|
||||
err := r.DB.QueryRow(
|
||||
`SELECT v.id, v.version_no, v.size_bytes, COALESCE(v.mime_type, ''),
|
||||
COALESCE(v.extension, ''), v.stored_path, v.created_at
|
||||
FROM resource_versions v
|
||||
JOIN resources r ON r.resource_id = v.resource_id
|
||||
WHERE v.resource_id = $1 AND v.version_no = $2 AND r.user_id = $3 AND r.deleted_at IS NULL`,
|
||||
resourceID, versionNo, ownerID,
|
||||
).Scan(&row.ID, &row.VersionNo, &row.Size, &row.MimeType, &row.Extension, &row.StoredPath, &row.CreatedAt)
|
||||
if err == sql.ErrNoRows {
|
||||
return VersionRow{}, ErrNotFound
|
||||
}
|
||||
return row, err
|
||||
}
|
||||
|
||||
// PruneVersions deletes generations beyond the newest keepN, returning their
|
||||
// stored paths for physical cleanup (best-effort, caller-side).
|
||||
func (r *Resources) PruneVersions(resourceID string, keepN int) ([]string, error) {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT stored_path FROM resource_versions
|
||||
WHERE resource_id = $1
|
||||
ORDER BY version_no DESC OFFSET $2`,
|
||||
resourceID, keepN,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var paths []string
|
||||
for rows.Next() {
|
||||
var p string
|
||||
if err := rows.Scan(&p); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
paths = append(paths, p)
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(paths) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
if _, err := r.DB.Exec(
|
||||
`DELETE FROM resource_versions WHERE resource_id = $1 AND stored_path = ANY($2)`,
|
||||
resourceID, pq.Array(paths),
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return paths, nil
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/alexmullins/zip"
|
||||
)
|
||||
|
||||
// Export password floor (V3 S7 C3) : un backup chiffré avec "1234" est un
|
||||
// placebo — même seuil que les mots de passe compte (8).
|
||||
const exportMinPasswordLength = 8
|
||||
|
||||
// ExportMaxResources bounds a full export (V3 S7 C3) : au-delà, l'export est
|
||||
// tronqué honnêtement (manifest.truncated = true) plutôt que de gonfler sans
|
||||
// fin la mémoire/temps d'une requête HTTP.
|
||||
const ExportMaxResources = 20000
|
||||
|
||||
// ExportManifestFile is the encrypted manifest inside the zip.
|
||||
type ExportManifestFile struct {
|
||||
Version int `json:"version"`
|
||||
ExportedAt string `json:"exportedAt"`
|
||||
UserID string `json:"userId"`
|
||||
Truncated bool `json:"truncated"`
|
||||
Folders []ExportFolderEntry `json:"folders"`
|
||||
Files []ExportFileEntry `json:"files"`
|
||||
}
|
||||
|
||||
// ExportFolderEntry mirrors a live folder (tree rebuilt via parentId).
|
||||
type ExportFolderEntry struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
ParentID string `json:"parentId,omitempty"`
|
||||
}
|
||||
|
||||
// ExportFileEntry mirrors a live file (+ extracted knowledge for re-import).
|
||||
type ExportFileEntry struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
ParentID string `json:"parentId,omitempty"`
|
||||
Size int64 `json:"size"`
|
||||
MimeType string `json:"mimeType,omitempty"`
|
||||
Extension string `json:"extension,omitempty"`
|
||||
Hash string `json:"hash,omitempty"`
|
||||
OcrText string `json:"ocrText,omitempty"`
|
||||
Facts []FactDTO `json:"facts,omitempty"`
|
||||
Path string `json:"path,omitempty"` // files/<id>.<ext> — absent si metadata-only
|
||||
HasBytes bool `json:"hasBytes"`
|
||||
}
|
||||
|
||||
// Export streams an AES-256 encrypted zip of the account (V3 S7 C3) : every
|
||||
// entry — files AND manifest — is encrypted with password (WinZip AES, cf.
|
||||
// alexmullins/zip). Scope : own live resources only (ni trashés, ni
|
||||
// partagés d'autrui). Metadata-only files appear in the manifest without
|
||||
// bytes (Path vide, HasBytes false). Password < 8 → error (400 côté handler).
|
||||
func (s *Resources) Export(w io.Writer, ownerID, password string) error {
|
||||
if len(password) < exportMinPasswordLength {
|
||||
return fmt.Errorf("export password must be at least %d characters", exportMinPasswordLength)
|
||||
}
|
||||
folders, err := s.Repo.ExportFolders(ownerID, ExportMaxResources)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
files, err := s.Repo.ExportFiles(ownerID, ExportMaxResources)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
truncated := len(folders) == ExportMaxResources || len(files) == ExportMaxResources
|
||||
|
||||
manifest := ExportManifestFile{
|
||||
Version: 1, ExportedAt: time.Now().UTC().Format(time.RFC3339),
|
||||
UserID: ownerID, Truncated: truncated,
|
||||
Folders: make([]ExportFolderEntry, 0, len(folders)),
|
||||
Files: make([]ExportFileEntry, 0, len(files)),
|
||||
}
|
||||
for _, f := range folders {
|
||||
manifest.Folders = append(manifest.Folders, ExportFolderEntry{
|
||||
ID: f.ID, Name: f.Name, ParentID: f.ParentID,
|
||||
})
|
||||
}
|
||||
|
||||
zw := zip.NewWriter(w)
|
||||
writeEncrypted := func(name string, data []byte) error {
|
||||
fh := &zip.FileHeader{Name: name, Method: zip.Deflate}
|
||||
fh.SetPassword(password)
|
||||
fw, err := zw.CreateHeader(fh)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = fw.Write(data)
|
||||
return err
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
entry := ExportFileEntry{
|
||||
ID: f.ID, Name: f.Name, ParentID: f.ParentID, Size: f.Size,
|
||||
MimeType: f.MimeType, Extension: f.Extension, Hash: f.Hash,
|
||||
OcrText: f.OcrText,
|
||||
}
|
||||
// Faits joints (auto + corrections — la connaissance voyage avec).
|
||||
if facts, ferr := s.Repository.Resources.ListFacts(f.ID); ferr == nil {
|
||||
for _, fact := range facts {
|
||||
entry.Facts = append(entry.Facts, FactDTO{
|
||||
FactType: fact.Type, ValueText: fact.ValueText,
|
||||
ValueNum: fact.ValueNum, ValueDate: derefString(fact.ValueDate),
|
||||
Confidence: fact.Confidence, Origin: fact.Origin,
|
||||
})
|
||||
}
|
||||
}
|
||||
src, serr := s.resolvePhysical(ownerID, f.ID)
|
||||
if serr != nil {
|
||||
entry.HasBytes = false
|
||||
manifest.Files = append(manifest.Files, entry)
|
||||
continue
|
||||
}
|
||||
zipName := "files/" + f.ID
|
||||
if f.Extension != "" {
|
||||
zipName += "." + f.Extension
|
||||
}
|
||||
entry.Path = zipName
|
||||
entry.HasBytes = true
|
||||
manifest.Files = append(manifest.Files, entry)
|
||||
|
||||
in, err := os.Open(src)
|
||||
if err != nil {
|
||||
log.Printf("export: open %s: %v", f.ID, err)
|
||||
continue
|
||||
}
|
||||
fh := &zip.FileHeader{Name: zipName, Method: zip.Deflate}
|
||||
fh.SetPassword(password)
|
||||
fw, err := zw.CreateHeader(fh)
|
||||
if err != nil {
|
||||
in.Close()
|
||||
return err
|
||||
}
|
||||
if _, err := io.Copy(fw, in); err != nil {
|
||||
in.Close()
|
||||
return err
|
||||
}
|
||||
in.Close()
|
||||
}
|
||||
|
||||
raw, err := json.MarshalIndent(manifest, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := writeEncrypted("manifest.json", raw); err != nil {
|
||||
return err
|
||||
}
|
||||
return zw.Close()
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
azip "github.com/alexmullins/zip"
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// TestExportRoundTrip : zip déchiffrable avec le mot de passe, manifest
|
||||
// cohérent (fichiers + faits + dossiers), octets intacts, trashés exclus.
|
||||
func TestExportRoundTrip(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "export-roundtrip")
|
||||
|
||||
folderID := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFolder(userID, folderID, "Papiers", ""); err != nil {
|
||||
t.Fatalf("insert folder: %v", err)
|
||||
}
|
||||
a, err := s.Upload(userID, multipartFileHeader(t, "a.txt", []byte("contenu-a")), folderID, "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload a: %v", err)
|
||||
}
|
||||
b, err := s.Upload(userID, multipartFileHeader(t, "b.txt", []byte("contenu-b")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload b: %v", err)
|
||||
}
|
||||
// Trashé : exclu de l'export.
|
||||
if _, err := s.Trash(userID, b.ID); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
// Metadata-only (jamais uploadé) : manifest sans octets.
|
||||
metaID := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFile(userID, metaID, "fantome.txt", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert meta: %v", err)
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := s.Export(&buf, userID, "motdepasse-8"); err != nil {
|
||||
t.Fatalf("export: %v", err)
|
||||
}
|
||||
if buf.Len() == 0 {
|
||||
t.Fatal("zip vide")
|
||||
}
|
||||
|
||||
// Lecture avec le bon mot de passe (lib standard : échoue sur AES —
|
||||
// on relit avec la même lib, comme un outil compatible WinZip AES).
|
||||
zr, err := azip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
|
||||
if err != nil {
|
||||
t.Fatalf("open zip: %v", err)
|
||||
}
|
||||
names := map[string]bool{}
|
||||
var manifestRaw []byte
|
||||
for _, f := range zr.File {
|
||||
names[f.Name] = true
|
||||
if !f.IsEncrypted() {
|
||||
t.Errorf("entrée non chiffrée: %s", f.Name)
|
||||
}
|
||||
f.SetPassword("motdepasse-8")
|
||||
rc, err := f.Open()
|
||||
if err != nil {
|
||||
t.Fatalf("open %s: %v", f.Name, err)
|
||||
}
|
||||
var content bytes.Buffer
|
||||
if _, err := content.ReadFrom(rc); err != nil {
|
||||
rc.Close()
|
||||
t.Fatalf("read %s: %v", f.Name, err)
|
||||
}
|
||||
rc.Close()
|
||||
switch {
|
||||
case f.Name == "manifest.json":
|
||||
manifestRaw = content.Bytes()
|
||||
case len(content.Bytes()) > 0 && f.Name != "manifest.json":
|
||||
_ = content
|
||||
}
|
||||
}
|
||||
var manifest ExportManifestFile
|
||||
if err := json.Unmarshal(manifestRaw, &manifest); err != nil {
|
||||
t.Fatalf("manifest: %v", err)
|
||||
}
|
||||
if manifest.Version != 1 || manifest.UserID != userID || manifest.Truncated {
|
||||
t.Fatalf("manifest inattendu: %+v", manifest)
|
||||
}
|
||||
if len(manifest.Folders) != 1 || manifest.Folders[0].Name != "Papiers" {
|
||||
t.Fatalf("dossiers: %+v", manifest.Folders)
|
||||
}
|
||||
if len(manifest.Files) != 2 {
|
||||
t.Fatalf("fichiers manifest: %+v (attendu a + fantome, b trashé exclu)", manifest.Files)
|
||||
}
|
||||
byID := map[string]ExportFileEntry{}
|
||||
for _, f := range manifest.Files {
|
||||
byID[f.ID] = f
|
||||
}
|
||||
if byID[a.ID].Path == "" || !byID[a.ID].HasBytes {
|
||||
t.Errorf("a doit avoir des octets: %+v", byID[a.ID])
|
||||
}
|
||||
if byID[metaID].Path != "" || byID[metaID].HasBytes {
|
||||
t.Errorf("fantome sans octets: %+v", byID[metaID])
|
||||
}
|
||||
if !names["manifest.json"] || !names[byID[a.ID].Path] {
|
||||
t.Fatalf("entrées zip manquantes: %v", names)
|
||||
}
|
||||
|
||||
// Contenu chiffré : illisible sans mot de passe (lib standard échoue),
|
||||
// et mauvais mot de passe refusé.
|
||||
stdReader, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
|
||||
if err != nil {
|
||||
t.Fatalf("open std: %v", err)
|
||||
}
|
||||
for _, f := range stdReader.File {
|
||||
rc, err := f.Open()
|
||||
if err == nil {
|
||||
var tmp bytes.Buffer
|
||||
_, copyErr := tmp.ReadFrom(rc)
|
||||
rc.Close()
|
||||
if copyErr == nil && tmp.Len() > 0 {
|
||||
t.Errorf("contenu lisible sans mot de passe: %s", f.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
zr2, err := azip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
|
||||
if err != nil {
|
||||
t.Fatalf("reopen: %v", err)
|
||||
}
|
||||
for _, f := range zr2.File {
|
||||
if f.Name != "manifest.json" {
|
||||
continue
|
||||
}
|
||||
f.SetPassword("mauvais-mot-de-passe")
|
||||
rc, err := f.Open()
|
||||
if err == nil {
|
||||
var tmp bytes.Buffer
|
||||
_, copyErr := tmp.ReadFrom(rc)
|
||||
rc.Close()
|
||||
if copyErr == nil {
|
||||
t.Error("manifest déchiffré avec un mauvais mot de passe")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportPasswordFloor(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "export-pw")
|
||||
var buf bytes.Buffer
|
||||
if err := s.Export(&buf, userID, "court"); err == nil {
|
||||
t.Fatal("mot de passe court accepté")
|
||||
}
|
||||
// Compte vide : zip valide avec manifest seul.
|
||||
if err := s.Export(&buf, userID, "motdepasse-8"); err != nil {
|
||||
t.Fatalf("export vide: %v", err)
|
||||
}
|
||||
zr, err := azip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
|
||||
if err != nil {
|
||||
t.Fatalf("open: %v", err)
|
||||
}
|
||||
if len(zr.File) != 1 || zr.File[0].Name != "manifest.json" {
|
||||
t.Fatalf("export vide inattendu: %v", zr.File)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/kazier/backend/extract"
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// Extract derives generic facts from OCR text (V3 S2) — dates, amounts,
|
||||
// issuers, references, suggested doc type. Rules-based (package extract),
|
||||
// no ML, no network : it runs inside the OCR worker right after Tesseract
|
||||
// and stores origin='auto' rows (user corrections, origin='user', survive
|
||||
// every re-run — see UpsertAutoFacts).
|
||||
type Extract struct {
|
||||
Repository *repository.Repository
|
||||
}
|
||||
|
||||
func NewExtract(repo *repository.Repository) *Extract {
|
||||
return &Extract{Repository: repo}
|
||||
}
|
||||
|
||||
// FactDTO serializes one row of GET /files/:id/facts.
|
||||
type FactDTO struct {
|
||||
FactType string `json:"factType"`
|
||||
ValueText string `json:"valueText,omitempty"`
|
||||
ValueNum *float64 `json:"valueNum,omitempty"`
|
||||
ValueDate string `json:"valueDate,omitempty"`
|
||||
Confidence float64 `json:"confidence"`
|
||||
Origin string `json:"origin"`
|
||||
}
|
||||
|
||||
// ProcessText extracts facts from text and replaces the resource's auto
|
||||
// facts. Empty text clears stale auto facts. Never touches user corrections.
|
||||
// Afterwards the deterministic links are reconciled (same_issuer /
|
||||
// same_reference may have appeared or vanished) — best-effort.
|
||||
func (e *Extract) ProcessText(ownerID, resourceID, text string) error {
|
||||
raw := extract.FromText(text)
|
||||
params := make([]repository.FactParam, 0, len(raw))
|
||||
for _, f := range raw {
|
||||
var num *float64
|
||||
if f.HasNum {
|
||||
n := f.ValueNum
|
||||
num = &n
|
||||
}
|
||||
var dat *string
|
||||
if f.ValueDate != "" {
|
||||
d := f.ValueDate
|
||||
dat = &d
|
||||
}
|
||||
text := f.ValueText
|
||||
// Amounts carry a display string ("1234.56 €") — keep the numeric
|
||||
// column as the machine value, the text as the human one.
|
||||
params = append(params, repository.FactParam{
|
||||
Type: string(f.Type),
|
||||
ValueText: text,
|
||||
ValueNum: num,
|
||||
ValueDate: dat,
|
||||
Confidence: f.Confidence,
|
||||
})
|
||||
}
|
||||
if err := e.Repository.Resources.UpsertAutoFacts(resourceID, params); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := e.Repository.Resources.ReconcileLinks(ownerID, resourceID); err != nil {
|
||||
log.Printf("extract: reconcile %s: %v", resourceID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Backfill processes files with OCR text but no auto facts yet (one boot
|
||||
// pass, bounded batches). Returns the number of resources processed.
|
||||
func (e *Extract) Backfill(batch int) (int, error) {
|
||||
total := 0
|
||||
for {
|
||||
pending, err := e.Repository.Resources.ListNeedingExtract(batch)
|
||||
if err != nil {
|
||||
return total, err
|
||||
}
|
||||
if len(pending) == 0 {
|
||||
return total, nil
|
||||
}
|
||||
for _, p := range pending {
|
||||
if err := e.ProcessText(p.UserID, p.ResourceID, p.Text); err != nil {
|
||||
log.Printf("extract: backfill %s: %v", p.ResourceID, err)
|
||||
continue
|
||||
}
|
||||
total++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetFacts returns the facts of a visible resource (viewer+, own or shared —
|
||||
// same scope as GetFileOcr, facts derive from the OCR text). Trashed/absent/
|
||||
// foreign → repository.ErrNotFound.
|
||||
func (s *Resources) GetFacts(userID, resourceID string) ([]FactDTO, error) {
|
||||
if _, err := s.Repo.GetFileVisible(userID, resourceID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := s.Repository.Resources.ListFacts(resourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]FactDTO, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
out = append(out, FactDTO{
|
||||
FactType: row.Type,
|
||||
ValueText: row.ValueText,
|
||||
ValueNum: row.ValueNum,
|
||||
ValueDate: derefString(row.ValueDate),
|
||||
Confidence: row.Confidence,
|
||||
Origin: row.Origin,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// validFactTypes mirrors the resource_facts CHECK (migration 000019).
|
||||
// Validated in Go so violations map to INVALID_REQUEST, never 500.
|
||||
var validFactTypes = map[string]bool{
|
||||
"date": true, "due_date": true, "period": true, "amount": true,
|
||||
"issuer": true, "reference": true, "doc_type": true,
|
||||
}
|
||||
|
||||
// ErrInvalidFact marks client fact-validation failures (400 INVALID_REQUEST,
|
||||
// never 500 — cf. FEEDBACK #8). Wrapped by validateFactParam.
|
||||
var ErrInvalidFact = fmt.Errorf("invalid fact")
|
||||
|
||||
// CorrectFacts replaces the user's correction set on a resource (S3 :
|
||||
// full-replace, not a diff). Requires editor+ like update_metadata
|
||||
// (A8-style : viewer- ne peut pas corriger → NOT_FOUND halt, pas d'oracle).
|
||||
// Absent/trashed/foreign → repository.ErrNotFound.
|
||||
func (s *Resources) CorrectFacts(userID, resourceID string, facts []FactDTO) error {
|
||||
access, err := s.Repository.Shares.EffectiveAccess(userID, resourceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if access.EffectiveRank < repository.AccessEditor {
|
||||
return repository.ErrNotFound
|
||||
}
|
||||
params := make([]repository.FactParam, 0, len(facts))
|
||||
for i, f := range facts {
|
||||
p, err := validateFactParam(i, f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params = append(params, p)
|
||||
}
|
||||
if err := s.Repository.Resources.CorrectFacts(resourceID, params); err != nil {
|
||||
return err
|
||||
}
|
||||
// Les faits ont changé : les arêtes same_issuer/same_reference suivent.
|
||||
if err := s.Repository.Resources.ReconcileLinks(userID, resourceID); err != nil {
|
||||
log.Printf("extract: reconcile %s: %v", resourceID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateFactParam(i int, f FactDTO) (repository.FactParam, error) {
|
||||
if !validFactTypes[f.FactType] {
|
||||
return repository.FactParam{}, fmt.Errorf("facts[%d].factType invalide: %w", i, ErrInvalidFact)
|
||||
}
|
||||
if f.Confidence < 0 || f.Confidence > 1 {
|
||||
return repository.FactParam{}, fmt.Errorf("facts[%d].confidence hors bornes [0,1]: %w", i, ErrInvalidFact)
|
||||
}
|
||||
var num *float64
|
||||
if f.ValueNum != nil {
|
||||
n := *f.ValueNum
|
||||
num = &n
|
||||
}
|
||||
var dat *string
|
||||
if f.ValueDate != "" {
|
||||
if _, err := time.Parse("2006-01-02", f.ValueDate); err != nil {
|
||||
return repository.FactParam{}, fmt.Errorf("facts[%d].valueDate doit être ISO YYYY-MM-DD: %w", i, ErrInvalidFact)
|
||||
}
|
||||
d := f.ValueDate
|
||||
dat = &d
|
||||
}
|
||||
if f.ValueText == "" && num == nil && dat == nil {
|
||||
return repository.FactParam{}, fmt.Errorf("facts[%d] sans valeur (valueText|valueNum|valueDate requis): %w", i, ErrInvalidFact)
|
||||
}
|
||||
conf := f.Confidence
|
||||
if conf == 0 {
|
||||
// Correction manuelle sans confiance explicite = affirmation.
|
||||
conf = 1.0
|
||||
}
|
||||
return repository.FactParam{
|
||||
Type: f.FactType,
|
||||
ValueText: f.ValueText,
|
||||
ValueNum: num,
|
||||
ValueDate: dat,
|
||||
Confidence: conf,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,363 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
func newExtractStore(t *testing.T) (*Resources, *Extract) {
|
||||
t.Helper()
|
||||
s := newServiceStore(t)
|
||||
return s, NewExtract(s.Repository)
|
||||
}
|
||||
|
||||
const extractCorpus = `EDF - SERVICE CLIENTS
|
||||
Facture n° FA-2026-0031 du 12/03/2026
|
||||
Montant total TTC : 84,20 €
|
||||
À payer avant le 12/04/2026
|
||||
`
|
||||
|
||||
func TestProcessTextPersistsAutoFacts(t *testing.T) {
|
||||
s, ex := newExtractStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "extract-auto")
|
||||
fileID := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFile(userID, fileID, "facture.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
|
||||
if err := ex.ProcessText(userID, fileID, extractCorpus); err != nil {
|
||||
t.Fatalf("process: %v", err)
|
||||
}
|
||||
facts, err := s.GetFacts(userID, fileID)
|
||||
if err != nil {
|
||||
t.Fatalf("get facts: %v", err)
|
||||
}
|
||||
byType := map[string][]FactDTO{}
|
||||
for _, f := range facts {
|
||||
byType[f.FactType] = append(byType[f.FactType], f)
|
||||
if f.Origin != "auto" {
|
||||
t.Errorf("origine = %q, attendu auto", f.Origin)
|
||||
}
|
||||
}
|
||||
if len(byType["reference"]) == 0 || len(byType["amount"]) == 0 ||
|
||||
len(byType["date"]) == 0 || len(byType["due_date"]) == 0 ||
|
||||
len(byType["doc_type"]) == 0 {
|
||||
t.Fatalf("faits manquants: %+v", byType)
|
||||
}
|
||||
var amount float64
|
||||
for _, f := range byType["amount"] {
|
||||
if f.ValueNum != nil {
|
||||
amount = *f.ValueNum
|
||||
}
|
||||
}
|
||||
if amount != 84.20 {
|
||||
t.Errorf("montant = %v, attendu 84.20", amount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessTextPreservesUserCorrections(t *testing.T) {
|
||||
s, ex := newExtractStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "extract-user")
|
||||
fileID := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFile(userID, fileID, "facture.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
|
||||
// Correction manuelle (S3, simulée en SQL) : l'auto ne doit jamais l'écraser.
|
||||
if _, err := s.Repository.Resources.DB.Exec(
|
||||
`INSERT INTO resource_facts (id, resource_id, fact_type, value_text, confidence, origin)
|
||||
VALUES ($1, $2, 'issuer', 'Vrai Fournisseur', 1.0, 'user')`,
|
||||
repository.NewID(), fileID,
|
||||
); err != nil {
|
||||
t.Fatalf("seed user fact: %v", err)
|
||||
}
|
||||
|
||||
if err := ex.ProcessText(userID, fileID, extractCorpus); err != nil {
|
||||
t.Fatalf("process: %v", err)
|
||||
}
|
||||
// Re-passe (idempotence du remplacement auto).
|
||||
if err := ex.ProcessText(userID, fileID, extractCorpus); err != nil {
|
||||
t.Fatalf("re-process: %v", err)
|
||||
}
|
||||
facts, err := s.GetFacts(userID, fileID)
|
||||
if err != nil {
|
||||
t.Fatalf("get facts: %v", err)
|
||||
}
|
||||
issuers := map[string]string{}
|
||||
autoCount := 0
|
||||
for _, f := range facts {
|
||||
if f.FactType == "issuer" {
|
||||
issuers[f.Origin] = f.ValueText
|
||||
}
|
||||
if f.Origin == "auto" {
|
||||
autoCount++
|
||||
}
|
||||
}
|
||||
if issuers["user"] != "Vrai Fournisseur" {
|
||||
t.Errorf("correction user écrasée: %+v", issuers)
|
||||
}
|
||||
if autoCount == 0 {
|
||||
t.Error("aucun fait auto après re-passe")
|
||||
}
|
||||
// Pas de doublons auto après deux passes.
|
||||
seen := map[string]int{}
|
||||
for _, f := range facts {
|
||||
if f.Origin == "auto" {
|
||||
seen[f.FactType+"\x00"+f.ValueText]++
|
||||
}
|
||||
}
|
||||
for k, n := range seen {
|
||||
if n > 1 {
|
||||
t.Errorf("doublon auto %q x%d", k, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessTextEmptyClearsStaleAuto(t *testing.T) {
|
||||
s, ex := newExtractStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "extract-clear")
|
||||
fileID := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFile(userID, fileID, "doc.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
if err := ex.ProcessText(userID, fileID, extractCorpus); err != nil {
|
||||
t.Fatalf("process: %v", err)
|
||||
}
|
||||
if err := ex.ProcessText(userID, fileID, ""); err != nil {
|
||||
t.Fatalf("clear: %v", err)
|
||||
}
|
||||
facts, err := s.GetFacts(userID, fileID)
|
||||
if err != nil {
|
||||
t.Fatalf("get facts: %v", err)
|
||||
}
|
||||
if len(facts) != 0 {
|
||||
t.Fatalf("auto périmés non purgés: %+v", facts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetFactsScope(t *testing.T) {
|
||||
s, ex := newExtractStore(t)
|
||||
owner := mustCreateUser(t, s.Repository, "extract-scope-o")
|
||||
other := mustCreateUser(t, s.Repository, "extract-scope-x")
|
||||
fileID := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFile(owner, fileID, "doc.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
if err := ex.ProcessText(owner, fileID, extractCorpus); err != nil {
|
||||
t.Fatalf("process: %v", err)
|
||||
}
|
||||
|
||||
if _, err := s.GetFacts(other, fileID); err != repository.ErrNotFound {
|
||||
t.Fatalf("étranger : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
if _, err := s.GetFacts(owner, repository.NewID()); err != repository.ErrNotFound {
|
||||
t.Fatalf("inconnu : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
if _, err := s.Trash(owner, fileID); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
if _, err := s.GetFacts(owner, fileID); err != repository.ErrNotFound {
|
||||
t.Fatalf("trashé : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackfill(t *testing.T) {
|
||||
s, ex := newExtractStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "extract-backfill")
|
||||
oldID := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFile(userID, oldID, "vieux.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
// OCR d'avant la V3 : texte présent, aucun fait auto.
|
||||
if err := s.Repository.Resources.UpdateOcrText(userID, oldID, extractCorpus); err != nil {
|
||||
t.Fatalf("ocr text: %v", err)
|
||||
}
|
||||
|
||||
n, err := ex.Backfill(200)
|
||||
if err != nil {
|
||||
t.Fatalf("backfill: %v", err)
|
||||
}
|
||||
if n != 1 {
|
||||
t.Fatalf("backfill = %d, attendu 1", n)
|
||||
}
|
||||
facts, err := s.GetFacts(userID, oldID)
|
||||
if err != nil || len(facts) == 0 {
|
||||
t.Fatalf("faits après backfill: %+v %v", facts, err)
|
||||
}
|
||||
// Deuxième passe : rien à faire (idempotent).
|
||||
n, err = ex.Backfill(200)
|
||||
if err != nil {
|
||||
t.Fatalf("backfill 2: %v", err)
|
||||
}
|
||||
if n != 0 {
|
||||
t.Fatalf("backfill 2 = %d, attendu 0", n)
|
||||
}
|
||||
}
|
||||
|
||||
func correctDTO(factType, valueText string) FactDTO {
|
||||
return FactDTO{FactType: factType, ValueText: valueText, Confidence: 1.0}
|
||||
}
|
||||
|
||||
func TestCorrectFactsReplacesUserSet(t *testing.T) {
|
||||
s, ex := newExtractStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "extract-correct")
|
||||
fileID := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFile(userID, fileID, "facture.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
if err := ex.ProcessText(userID, fileID, extractCorpus); err != nil {
|
||||
t.Fatalf("process: %v", err)
|
||||
}
|
||||
|
||||
// Correction : l'émetteur auto est faux, l'utilisateur le fixe.
|
||||
if err := s.CorrectFacts(userID, fileID, []FactDTO{correctDTO("issuer", "Vrai Fournisseur")}); err != nil {
|
||||
t.Fatalf("correct: %v", err)
|
||||
}
|
||||
facts, err := s.GetFacts(userID, fileID)
|
||||
if err != nil {
|
||||
t.Fatalf("get facts: %v", err)
|
||||
}
|
||||
var userIssuer, autoIssuer int
|
||||
for _, f := range facts {
|
||||
if f.FactType == "issuer" && f.Origin == "user" && f.ValueText == "Vrai Fournisseur" {
|
||||
userIssuer++
|
||||
}
|
||||
if f.FactType == "issuer" && f.Origin == "auto" {
|
||||
autoIssuer++
|
||||
}
|
||||
}
|
||||
if userIssuer != 1 {
|
||||
t.Fatalf("correction user manquante: %+v", facts)
|
||||
}
|
||||
if autoIssuer == 0 {
|
||||
t.Error("l'auto doit survivre à côté du user (les deux sont lus, user d'abord)")
|
||||
}
|
||||
|
||||
// Deuxième correction (jeu vide) : efface les corrections, garde l'auto.
|
||||
if err := s.CorrectFacts(userID, fileID, nil); err != nil {
|
||||
t.Fatalf("clear corrections: %v", err)
|
||||
}
|
||||
facts, _ = s.GetFacts(userID, fileID)
|
||||
for _, f := range facts {
|
||||
if f.Origin == "user" {
|
||||
t.Fatalf("corrections restantes: %+v", facts)
|
||||
}
|
||||
}
|
||||
if len(facts) == 0 {
|
||||
t.Fatal("l'auto doit survivre à l'effacement des corrections")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCorrectFactsValidation(t *testing.T) {
|
||||
s, _ := newExtractStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "extract-correct-val")
|
||||
fileID := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFile(userID, fileID, "doc.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
num := 12.5
|
||||
cases := []struct {
|
||||
name string
|
||||
facts []FactDTO
|
||||
}{
|
||||
{"type inconnu", []FactDTO{{FactType: "couleur", ValueText: "bleu"}}},
|
||||
{"confiance hors bornes", []FactDTO{{FactType: "issuer", ValueText: "X", Confidence: 2}}},
|
||||
{"date non ISO", []FactDTO{{FactType: "date", ValueDate: "12/03/2026"}}},
|
||||
{"sans valeur", []FactDTO{{FactType: "issuer"}}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if err := s.CorrectFacts(userID, fileID, tc.facts); err == nil {
|
||||
t.Errorf("%s : erreur attendue", tc.name)
|
||||
} else if !errors.Is(err, ErrInvalidFact) {
|
||||
t.Errorf("%s : attendu ErrInvalidFact, got %v", tc.name, err)
|
||||
}
|
||||
}
|
||||
// Valides : date ISO + montant numérique.
|
||||
if err := s.CorrectFacts(userID, fileID, []FactDTO{
|
||||
{FactType: "due_date", ValueDate: "2026-04-12"},
|
||||
{FactType: "amount", ValueNum: &num},
|
||||
}); err != nil {
|
||||
t.Fatalf("faits valides rejetés: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCorrectFactsScope(t *testing.T) {
|
||||
s, _ := newExtractStore(t)
|
||||
owner := mustCreateUser(t, s.Repository, "extract-corr-o")
|
||||
viewer := mustCreateUser(t, s.Repository, "extract-corr-v")
|
||||
fileID := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFile(owner, fileID, "doc.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
|
||||
// Étranger (même viewer via partage simulé plus bas) → NOT_FOUND.
|
||||
if err := s.CorrectFacts(viewer, fileID, nil); err != repository.ErrNotFound {
|
||||
t.Fatalf("étranger : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
// Absent → NOT_FOUND (EffectiveAccess sur ressource inconnue).
|
||||
if err := s.CorrectFacts(owner, repository.NewID(), []FactDTO{correctDTO("issuer", "X")}); err != repository.ErrNotFound {
|
||||
t.Fatalf("absent : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncOpCorrectFacts(t *testing.T) {
|
||||
s, ex := newExtractStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "extract-op")
|
||||
other := mustCreateUser(t, s.Repository, "extract-op-x")
|
||||
device := mustRegisterDevice(t, s.Repository, "c0000000000000000000000000000001")
|
||||
fileID := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFile(userID, fileID, "facture.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
if err := ex.ProcessText(userID, fileID, extractCorpus); err != nil {
|
||||
t.Fatalf("process: %v", err)
|
||||
}
|
||||
foreignID := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFile(other, foreignID, "autre.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert foreign: %v", err)
|
||||
}
|
||||
|
||||
apply := func(resID string, facts []FactDTO) SyncResult {
|
||||
t.Helper()
|
||||
raw := map[string]any{"facts": facts}
|
||||
res, err := s.ApplyBatch(userID, device, []SyncOperation{
|
||||
syncOp(repository.NewID(), resID, OpCorrectFacts, "file", raw),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("apply: %v", err)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
if res := apply(fileID, []FactDTO{correctDTO("issuer", "Corrigé")}); res.Applied != 1 || res.Failed != nil {
|
||||
t.Fatalf("correct op: %+v", res)
|
||||
}
|
||||
facts, _ := s.GetFacts(userID, fileID)
|
||||
found := false
|
||||
for _, f := range facts {
|
||||
if f.FactType == "issuer" && f.Origin == "user" && f.ValueText == "Corrigé" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("correction via op manquante: %+v", facts)
|
||||
}
|
||||
// Op étrangère → halt NOT_FOUND.
|
||||
if res := apply(foreignID, []FactDTO{correctDTO("issuer", "X")}); res.Failed == nil || res.Failed.Code != "NOT_FOUND" {
|
||||
t.Fatalf("correct autrui doit halter NOT_FOUND: %+v", res)
|
||||
}
|
||||
// Payload invalide → halt INVALID_REQUEST.
|
||||
res, err := s.ApplyBatch(userID, device, []SyncOperation{
|
||||
syncOp(repository.NewID(), fileID, OpCorrectFacts, "file", map[string]any{
|
||||
"facts": []any{map[string]any{"factType": "couleur", "valueText": "bleu"}},
|
||||
}),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("apply: %v", err)
|
||||
}
|
||||
if res.Failed == nil || res.Failed.Code != "INVALID_REQUEST" {
|
||||
t.Fatalf("payload invalide doit halter INVALID_REQUEST: %+v", res)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// RelationDTO serializes one edge of GET /files/:id/relations (V3 S5).
|
||||
type RelationDTO struct {
|
||||
ID string `json:"id"`
|
||||
OtherID string `json:"otherId"`
|
||||
Type string `json:"type"`
|
||||
Confidence float64 `json:"confidence"`
|
||||
CreatedBy string `json:"createdBy"`
|
||||
OtherName string `json:"otherName"`
|
||||
OtherType string `json:"otherType"`
|
||||
OtherSize int64 `json:"otherSize"`
|
||||
}
|
||||
|
||||
// validLinkTypes mirrors the resource_links CHECK (migration 000021).
|
||||
var validLinkTypes = map[string]bool{
|
||||
repository.LinkSameScan: true,
|
||||
repository.LinkDuplicateOf: true,
|
||||
repository.LinkSameIssuer: true,
|
||||
repository.LinkSameReference: true,
|
||||
}
|
||||
|
||||
// ListRelations reconciles then returns the live edges of an owned resource
|
||||
// (owner-only : shared docs don't expose the owner's graph, S5 scope).
|
||||
func (s *Resources) ListRelations(ownerID, resourceID string) ([]RelationDTO, error) {
|
||||
owned, err := s.Repo.ExistsOwner(ownerID, resourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !owned {
|
||||
return nil, repository.ErrNotFound
|
||||
}
|
||||
if err := s.Repo.ReconcileLinks(ownerID, resourceID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := s.Repo.ListLinks(resourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]RelationDTO, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
out = append(out, RelationDTO{
|
||||
ID: row.ID, OtherID: row.OtherID, Type: row.Type,
|
||||
Confidence: row.Confidence, CreatedBy: row.CreatedBy,
|
||||
OtherName: row.OtherName, OtherType: row.OtherType, OtherSize: row.OtherSize,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// LinkVerdict records an explicit verdict on an edge (owner-only, both ends
|
||||
// owned-live). Confirm → 1.0, invalidate → 0.0 (kept, blocks re-creation).
|
||||
func (s *Resources) LinkVerdict(ownerID, resourceID, otherID, linkType string, confirmed bool) error {
|
||||
if !validLinkTypes[linkType] {
|
||||
return ErrInvalidLinkType
|
||||
}
|
||||
if otherID == resourceID {
|
||||
return ErrInvalidLinkType
|
||||
}
|
||||
for _, id := range []string{resourceID, otherID} {
|
||||
owned, err := s.Repo.ExistsOwner(ownerID, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !owned {
|
||||
return repository.ErrNotFound
|
||||
}
|
||||
}
|
||||
return s.Repo.SetUserVerdict(resourceID, otherID, linkType, confirmed)
|
||||
}
|
||||
|
||||
// DuplicateGroupDTO serializes one set of byte-identical files (B2).
|
||||
type DuplicateGroupDTO struct {
|
||||
ContentHash string `json:"contentHash"`
|
||||
Count int `json:"count"`
|
||||
TotalBytes int64 `json:"totalBytes"`
|
||||
ReclaimableBytes int64 `json:"reclaimableBytes"`
|
||||
Files []DuplicateFileDTO `json:"files"`
|
||||
}
|
||||
|
||||
// DuplicateFileDTO serializes one member (newest first).
|
||||
type DuplicateFileDTO struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Size int64 `json:"size"`
|
||||
UpdatedAt int64 `json:"updatedAt"`
|
||||
}
|
||||
|
||||
// ListDuplicates returns the owner's byte-identical file sets, biggest
|
||||
// first. Owner scope (own files only) — `[]` when clean.
|
||||
func (s *Resources) ListDuplicates(ownerID string) ([]DuplicateGroupDTO, error) {
|
||||
groups, err := s.Repo.ListDuplicateGroups(ownerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]DuplicateGroupDTO, 0, len(groups))
|
||||
for _, g := range groups {
|
||||
dto := DuplicateGroupDTO{
|
||||
ContentHash: g.ContentHash, Count: g.Count,
|
||||
TotalBytes: g.TotalBytes, ReclaimableBytes: g.ReclaimableBytes,
|
||||
Files: make([]DuplicateFileDTO, 0, len(g.Files)),
|
||||
}
|
||||
for _, f := range g.Files {
|
||||
dto.Files = append(dto.Files, DuplicateFileDTO{
|
||||
ID: f.ID, Name: f.Name, Size: f.Size, UpdatedAt: f.UpdatedAt,
|
||||
})
|
||||
}
|
||||
out = append(out, dto)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ErrInvalidLinkType marks client verdict-validation failures (400).
|
||||
var ErrInvalidLinkType = errors.New("invalid link type or pair")
|
||||
|
||||
// hashPhysical computes the SHA-256 of the live bytes (safe resolution,
|
||||
// same pattern as OpenContent). Absent → ErrNotFound.
|
||||
func (s *Resources) hashPhysical(ownerID, resourceID string) (string, error) {
|
||||
resolved, err := s.resolvePhysical(ownerID, resourceID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
f, err := os.Open(resolved)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer f.Close()
|
||||
h := sha256.New()
|
||||
if _, err := io.Copy(h, f); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(h.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// resolvePhysical returns the absolute path of the live bytes (safe :
|
||||
// 32-hex only, regular file, symlink contained in UploadDir).
|
||||
// Absent → repository.ErrNotFound.
|
||||
func (s *Resources) resolvePhysical(ownerID, resourceID string) (string, error) {
|
||||
if !idPattern.MatchString(ownerID) || !idPattern.MatchString(resourceID) {
|
||||
return "", repository.ErrNotFound
|
||||
}
|
||||
base, err := filepath.EvalSymlinks(s.UploadDir)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
matches, err := filepath.Glob(filepath.Join(s.UploadDir, ownerID, resourceID+".*"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, m := range matches {
|
||||
if !strings.HasPrefix(filepath.Base(m), resourceID+".") {
|
||||
continue
|
||||
}
|
||||
fi, err := os.Lstat(m)
|
||||
if err != nil || !fi.Mode().IsRegular() {
|
||||
continue
|
||||
}
|
||||
resolved, err := filepath.EvalSymlinks(m)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
rel, err := filepath.Rel(base, resolved)
|
||||
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
|
||||
continue
|
||||
}
|
||||
return resolved, nil
|
||||
}
|
||||
return "", repository.ErrNotFound
|
||||
}
|
||||
|
||||
// refreshContentHash (re)computes and stores the hash after bytes arrive.
|
||||
// Best-effort : un échec ne fait jamais échouer l'upload (loggé).
|
||||
func (s *Resources) refreshContentHash(ownerID, resourceID string) {
|
||||
hash, err := s.hashPhysical(ownerID, resourceID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err := s.Repo.SetContentHash(ownerID, resourceID, hash); err != nil {
|
||||
log.Printf("content: SetContentHash(%s): %v", resourceID, err)
|
||||
return
|
||||
}
|
||||
// Des octets identiques à une autre ressource = doublon à relier.
|
||||
if err := s.Repo.ReconcileLinks(ownerID, resourceID); err != nil {
|
||||
log.Printf("content: reconcile(%s): %v", resourceID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// BackfillHashes hashes live files missing content_hash (bounded batches,
|
||||
// boot pass — uploads d'avant la V3). Returns files hashed.
|
||||
func (s *Resources) BackfillHashes(batch int) (int, error) {
|
||||
total := 0
|
||||
for {
|
||||
pending, err := s.Repo.ListMissingHashes(batch)
|
||||
if err != nil {
|
||||
return total, err
|
||||
}
|
||||
if len(pending) == 0 {
|
||||
return total, nil
|
||||
}
|
||||
for _, p := range pending {
|
||||
hash, err := s.hashPhysical(p.UserID, p.ResourceID)
|
||||
if err != nil {
|
||||
continue // metadata-only : rien à hasher, re-tenté au prochain upload.
|
||||
}
|
||||
if err := s.Repo.SetContentHash(p.UserID, p.ResourceID, hash); err != nil {
|
||||
log.Printf("content: backfill hash %s: %v", p.ResourceID, err)
|
||||
continue
|
||||
}
|
||||
if err := s.Repo.ReconcileLinks(p.UserID, p.ResourceID); err != nil {
|
||||
log.Printf("content: backfill reconcile %s: %v", p.ResourceID, err)
|
||||
}
|
||||
total++
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
func mustLinkFile(t *testing.T, s *Resources, userID, name string) string {
|
||||
t.Helper()
|
||||
id := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFile(userID, id, name, "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert %q: %v", name, err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func relationTypes(rels []RelationDTO) map[string]int {
|
||||
out := map[string]int{}
|
||||
for _, r := range rels {
|
||||
out[r.Type]++
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestSameScanViaOutbox(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "links-scan")
|
||||
device := mustRegisterDevice(t, s.Repository, "d0000000000000000000000000000001")
|
||||
session := repository.NewID()
|
||||
page1 := repository.NewID()
|
||||
page2 := repository.NewID()
|
||||
|
||||
create := func(opID, resID, name string) {
|
||||
t.Helper()
|
||||
res, err := s.ApplyBatch(userID, device, []SyncOperation{
|
||||
syncOp(opID, resID, OpCreateResource, "file", map[string]any{
|
||||
"name": resID + "-" + name, "scanSessionId": session,
|
||||
}),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("apply: %v", err)
|
||||
}
|
||||
if res.Applied != 1 || res.Failed != nil {
|
||||
t.Fatalf("create %s: %+v", name, res)
|
||||
}
|
||||
}
|
||||
create(repository.NewID(), page1, "p1.png")
|
||||
create(repository.NewID(), page2, "p2.png")
|
||||
|
||||
rels, err := s.ListRelations(userID, page1)
|
||||
if err != nil {
|
||||
t.Fatalf("relations: %v", err)
|
||||
}
|
||||
if relationTypes(rels)[repository.LinkSameScan] != 1 || rels[0].OtherID != page2 {
|
||||
t.Fatalf("same_scan manquant: %+v", rels)
|
||||
}
|
||||
if rels[0].Confidence != 1.0 || rels[0].CreatedBy != "auto" {
|
||||
t.Fatalf("arête same_scan inattendue: %+v", rels[0])
|
||||
}
|
||||
|
||||
// Session invalide → halt INVALID_REQUEST (pas d'écriture partielle).
|
||||
res, err := s.ApplyBatch(userID, device, []SyncOperation{
|
||||
syncOp(repository.NewID(), repository.NewID(), OpCreateResource, "file", map[string]any{
|
||||
"name": "bad.png", "scanSessionId": "nope",
|
||||
}),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("apply: %v", err)
|
||||
}
|
||||
if res.Failed == nil || res.Failed.Code != "INVALID_REQUEST" {
|
||||
t.Fatalf("session invalide doit halter: %+v", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDuplicateOfViaUploadHash(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "links-dupe")
|
||||
|
||||
a, err := s.Upload(userID, multipartFileHeader(t, "a.txt", []byte("meme-contenu")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload a: %v", err)
|
||||
}
|
||||
b, err := s.Upload(userID, multipartFileHeader(t, "b.txt", []byte("meme-contenu")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload b: %v", err)
|
||||
}
|
||||
|
||||
rels, err := s.ListRelations(userID, a.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("relations: %v", err)
|
||||
}
|
||||
if relationTypes(rels)[repository.LinkDuplicateOf] != 1 || rels[0].OtherID != b.ID {
|
||||
t.Fatalf("duplicate_of manquant: %+v", rels)
|
||||
}
|
||||
|
||||
// Contenu différent → pas de doublon.
|
||||
c, err := s.Upload(userID, multipartFileHeader(t, "c.txt", []byte("autre-contenu")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload c: %v", err)
|
||||
}
|
||||
relsC, err := s.ListRelations(userID, c.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("relations c: %v", err)
|
||||
}
|
||||
if len(relsC) != 0 {
|
||||
t.Fatalf("faux doublon: %+v", relsC)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSameIssuerViaFacts(t *testing.T) {
|
||||
s, ex := newExtractStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "links-issuer")
|
||||
f1 := mustLinkFile(t, s, userID, "f1.pdf")
|
||||
f2 := mustLinkFile(t, s, userID, "f2.pdf")
|
||||
|
||||
if err := ex.ProcessText(userID, f1, "EDF\nFacture 10,00 €\n"); err != nil {
|
||||
t.Fatalf("extract f1: %v", err)
|
||||
}
|
||||
if err := ex.ProcessText(userID, f2, "EDF\nFacture 20,00 €\n"); err != nil {
|
||||
t.Fatalf("extract f2: %v", err)
|
||||
}
|
||||
|
||||
rels, err := s.ListRelations(userID, f1)
|
||||
if err != nil {
|
||||
t.Fatalf("relations: %v", err)
|
||||
}
|
||||
if relationTypes(rels)[repository.LinkSameIssuer] != 1 {
|
||||
t.Fatalf("same_issuer manquant: %+v", rels)
|
||||
}
|
||||
|
||||
// Correction de l'émetteur → l'arête disparaît (prune auto).
|
||||
if err := s.CorrectFacts(userID, f2, []FactDTO{{FactType: "issuer", ValueText: "ENGIE", Confidence: 1}}); err != nil {
|
||||
t.Fatalf("correct: %v", err)
|
||||
}
|
||||
rels, err = s.ListRelations(userID, f1)
|
||||
if err != nil {
|
||||
t.Fatalf("relations 2: %v", err)
|
||||
}
|
||||
if relationTypes(rels)[repository.LinkSameIssuer] != 0 {
|
||||
t.Fatalf("arête périmée non élaguée: %+v", rels)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinkVerdict(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "links-verdict")
|
||||
other := mustCreateUser(t, s.Repository, "links-verdict-x")
|
||||
device := mustRegisterDevice(t, s.Repository, "d0000000000000000000000000000002")
|
||||
session := repository.NewID()
|
||||
p1, p2 := repository.NewID(), repository.NewID()
|
||||
for _, id := range []string{p1, p2} {
|
||||
res, err := s.ApplyBatch(userID, device, []SyncOperation{
|
||||
syncOp(repository.NewID(), id, OpCreateResource, "file", map[string]any{
|
||||
"name": id, "scanSessionId": session,
|
||||
}),
|
||||
})
|
||||
if err != nil || res.Applied != 1 || res.Failed != nil {
|
||||
t.Fatalf("create %s: %+v %v", id, res, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Invalider : l'arête reste (confiance 0, verdict user) mais ne bloque rien.
|
||||
if err := s.LinkVerdict(userID, p1, p2, repository.LinkSameScan, false); err != nil {
|
||||
t.Fatalf("verdict: %v", err)
|
||||
}
|
||||
rels, _ := s.ListRelations(userID, p1)
|
||||
if len(rels) != 1 || rels[0].Confidence != 0 || rels[0].CreatedBy != "user" {
|
||||
t.Fatalf("verdict infirme inattendu: %+v", rels)
|
||||
}
|
||||
// Re-reconcile ne ressuscite pas l'arête invalidée.
|
||||
rels, _ = s.ListRelations(userID, p1)
|
||||
if len(rels) != 1 || rels[0].Confidence != 0 {
|
||||
t.Fatalf("arête invalidée ressuscitée: %+v", rels)
|
||||
}
|
||||
|
||||
// Re-confirmer : retour à 1.0 user.
|
||||
if err := s.LinkVerdict(userID, p1, p2, repository.LinkSameScan, true); err != nil {
|
||||
t.Fatalf("re-verdict: %v", err)
|
||||
}
|
||||
rels, _ = s.ListRelations(userID, p1)
|
||||
if len(rels) != 1 || rels[0].Confidence != 1.0 || rels[0].CreatedBy != "user" {
|
||||
t.Fatalf("re-verdict inattendu: %+v", rels)
|
||||
}
|
||||
|
||||
// Type inconnu → erreur 400-candidate ; paire réflexive idem.
|
||||
if err := s.LinkVerdict(userID, p1, p2, "copains", true); !errors.Is(err, ErrInvalidLinkType) {
|
||||
t.Fatalf("type inconnu : attendu ErrInvalidLinkType, got %v", err)
|
||||
}
|
||||
if err := s.LinkVerdict(userID, p1, p1, repository.LinkSameScan, true); !errors.Is(err, ErrInvalidLinkType) {
|
||||
t.Fatalf("paire réflexive : attendu ErrInvalidLinkType, got %v", err)
|
||||
}
|
||||
// Autrui → NOT_FOUND (pas d'oracle).
|
||||
foreign := mustLinkFile(t, s, other, "x.pdf")
|
||||
if err := s.LinkVerdict(other, foreign, p1, repository.LinkSameScan, true); err != repository.ErrNotFound {
|
||||
t.Fatalf("verdict trans-compte : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
if _, err := s.ListRelations(other, p1); err != repository.ErrNotFound {
|
||||
t.Fatalf("lecture trans-compte : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListDuplicates(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "links-dupes")
|
||||
other := mustCreateUser(t, s.Repository, "links-dupes-x")
|
||||
|
||||
a, err := s.Upload(userID, multipartFileHeader(t, "a.txt", []byte("doublon")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload a: %v", err)
|
||||
}
|
||||
b, err := s.Upload(userID, multipartFileHeader(t, "b.txt", []byte("doublon")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload b: %v", err)
|
||||
}
|
||||
if _, err := s.Upload(userID, multipartFileHeader(t, "c.txt", []byte("unique-contenu")), "", ""); err != nil {
|
||||
t.Fatalf("upload c: %v", err)
|
||||
}
|
||||
// Même contenu chez un autre compte : pas mon doublon.
|
||||
if _, err := s.Upload(other, multipartFileHeader(t, "z.txt", []byte("doublon")), "", ""); err != nil {
|
||||
t.Fatalf("upload other: %v", err)
|
||||
}
|
||||
|
||||
groups, err := s.ListDuplicates(userID)
|
||||
if err != nil {
|
||||
t.Fatalf("duplicates: %v", err)
|
||||
}
|
||||
if len(groups) != 1 || groups[0].Count != 2 {
|
||||
t.Fatalf("groupes inattendus: %+v", groups)
|
||||
}
|
||||
g := groups[0]
|
||||
if g.TotalBytes != 14 || g.ReclaimableBytes != 7 {
|
||||
t.Fatalf("tailles inattendues: %+v", g)
|
||||
}
|
||||
ids := map[string]bool{}
|
||||
for _, f := range g.Files {
|
||||
ids[f.ID] = true
|
||||
}
|
||||
if !ids[a.ID] || !ids[b.ID] {
|
||||
t.Fatalf("membres inattendus: %+v", g.Files)
|
||||
}
|
||||
|
||||
// Compte propre → [] (jamais null).
|
||||
empty, err := s.ListDuplicates(other)
|
||||
if err != nil {
|
||||
t.Fatalf("duplicates other: %v", err)
|
||||
}
|
||||
if len(empty) != 0 {
|
||||
t.Fatalf("compte sans doublon: %+v", empty)
|
||||
}
|
||||
}
|
||||
+25
-1
@@ -41,6 +41,11 @@ type Ocr struct {
|
||||
Lang string
|
||||
Engine ocr.Engine
|
||||
StaleAfter time.Duration
|
||||
// OnText, quand non-nil, reçoit chaque texte extrait avec succès (V3 S2 :
|
||||
// le service Extract en dérive des faits génériques). Appelé dans le
|
||||
// worker, jamais dans un handler ; une erreur est loggée, pas propagée
|
||||
// (le job OCR reste `done` — l'extract est un enrichissement, pas un gate).
|
||||
OnText func(userID, resourceID, text string)
|
||||
}
|
||||
|
||||
func NewOcr(repo *repository.Repository, uploadDir, lang string, engine ocr.Engine) *Ocr {
|
||||
@@ -166,9 +171,10 @@ func (o *Ocr) process(ctx context.Context, row repository.OcrJobRow) {
|
||||
fail(err.Error())
|
||||
return
|
||||
}
|
||||
trimmed := strings.TrimSpace(text)
|
||||
// Le texte atterrit sur la ressource (updated_at bumpé) : consultable par
|
||||
// tous les devices du user et les grantees via GET /files/:id/ocr.
|
||||
if trimmed := strings.TrimSpace(text); trimmed != "" {
|
||||
if trimmed != "" {
|
||||
if err := o.Repository.Resources.UpdateOcrText(userID, row.FileID, trimmed); err != nil {
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
fail("file not readable")
|
||||
@@ -178,9 +184,27 @@ func (o *Ocr) process(ctx context.Context, row repository.OcrJobRow) {
|
||||
return
|
||||
}
|
||||
}
|
||||
// V3 S2 : faits génériques (même texte vide — purge les auto périmés).
|
||||
if o.OnText != nil {
|
||||
// userID capturing : le user courant, pas un owner résolu plus tard.
|
||||
if terr := o.safeOnText(userID, row.FileID, trimmed); terr != nil {
|
||||
log.Printf("ocr: extract %s: %v", row.FileID, terr)
|
||||
}
|
||||
}
|
||||
complete(text)
|
||||
}
|
||||
|
||||
// safeOnText isole un hook OnText paniquant (règle défectueuse) du worker.
|
||||
func (o *Ocr) safeOnText(userID, resourceID, text string) (err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = fmt.Errorf("extract panic: %v", r)
|
||||
}
|
||||
}()
|
||||
o.OnText(userID, resourceID, text)
|
||||
return nil
|
||||
}
|
||||
|
||||
// physicalPath résout UPLOAD_DIR/<user_id>/<resource_id>.<ext> — l'ext est
|
||||
// choisi à l'upload, le fichier réel est retrouvé par préfixe. Durci (A12) :
|
||||
// identifiants 32-hex uniquement (anti `..`), `Lstat` + fichier régulier
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/gif"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/image/draw"
|
||||
"golang.org/x/image/webp"
|
||||
)
|
||||
|
||||
// ErrNoPreview signale qu'aucune vignette n'est possible (type non supporté,
|
||||
// outil absent, octets illisibles). Handlers → 404 NO_PREVIEW (l'UI montre
|
||||
// l'icône de catégorie au lieu de réessayer).
|
||||
var ErrNoPreview = errors.New("no preview available")
|
||||
|
||||
// MaxPreviewEdge borne la vignette (miroir ThumbnailStore mobile, 512 px).
|
||||
const MaxPreviewEdge = 512
|
||||
|
||||
// previewJPEGQuality compresse les vignettes (galerie rapide, artefacts invisibles à 512 px).
|
||||
const previewJPEGQuality = 80
|
||||
|
||||
// pdftoppmTimeout borne le rendu PDF (binaire externe, jamais bloquant).
|
||||
const pdftoppmTimeout = 30 * time.Second
|
||||
|
||||
// OpenPreview résout la vignette JPEG d'un fichier visible (viewer+, own ou
|
||||
// shared) : cache `.previews/<resource_id>.jpg` régénéré quand la source a
|
||||
// changé (taille ou updated_at), sinon servi tel quel. Images : pur Go.
|
||||
// PDF page 1 : `pdftoppm` (poppler, cf. Dockerfile) ; absent → ErrNoPreview.
|
||||
func (s *Resources) OpenPreview(userID, resourceID string) (string, error) {
|
||||
row, err := s.Repo.GetFileVisible(userID, resourceID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
dir := filepath.Join(s.UploadDir, ".previews")
|
||||
cached := filepath.Join(dir, resourceID+".jpg")
|
||||
if prev, err := s.Repo.GetPreview(resourceID); err == nil {
|
||||
if prev.SrcSize == row.Size && prev.SrcUpdatedAt.Equal(row.UpdatedAt) {
|
||||
if fi, serr := os.Stat(cached); serr == nil && fi.Mode().IsRegular() {
|
||||
return cached, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
info, err := s.OpenContent(userID, resourceID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
kind := previewKind(row.MimeType, info.Path)
|
||||
var data []byte
|
||||
switch kind {
|
||||
case "image":
|
||||
data, err = renderImagePreview(info.Path)
|
||||
case "pdf":
|
||||
data, err = renderPDFPreview(info.Path)
|
||||
default:
|
||||
return "", ErrNoPreview
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
return "", fmt.Errorf("preview dir: %w", err)
|
||||
}
|
||||
tmp, err := os.CreateTemp(dir, ".tmp-"+resourceID+".*.jpg")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("preview tmp: %w", err)
|
||||
}
|
||||
tmpName := tmp.Name()
|
||||
if _, err := tmp.Write(data); err != nil {
|
||||
tmp.Close()
|
||||
_ = os.Remove(tmpName)
|
||||
return "", fmt.Errorf("preview write: %w", err)
|
||||
}
|
||||
if err := tmp.Close(); err != nil {
|
||||
_ = os.Remove(tmpName)
|
||||
return "", fmt.Errorf("preview close: %w", err)
|
||||
}
|
||||
if err := os.Chmod(tmpName, 0o600); err != nil {
|
||||
_ = os.Remove(tmpName)
|
||||
return "", fmt.Errorf("preview chmod: %w", err)
|
||||
}
|
||||
if err := os.Rename(tmpName, cached); err != nil {
|
||||
_ = os.Remove(tmpName)
|
||||
return "", fmt.Errorf("preview commit: %w", err)
|
||||
}
|
||||
if err := s.Repo.UpsertPreview(resourceID, row.Size, row.UpdatedAt); err != nil {
|
||||
log.Printf("preview: upsert(%s): %v", resourceID, err)
|
||||
}
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
// previewKind classe la source : images décodables Go vs PDF vs le reste.
|
||||
func previewKind(mimeType, path string) string {
|
||||
mime := strings.ToLower(mimeType)
|
||||
if strings.HasPrefix(mime, "image/") {
|
||||
return "image"
|
||||
}
|
||||
ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(path), "."))
|
||||
switch ext {
|
||||
case "jpg", "jpeg", "png", "gif", "webp", "bmp":
|
||||
return "image"
|
||||
case "pdf":
|
||||
return "pdf"
|
||||
}
|
||||
if mime == "application/pdf" {
|
||||
return "pdf"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// renderImagePreview décode (jpeg/png/gif/webp) et réduit au plus grand côté
|
||||
// MaxPreviewEdge, sortie JPEG. Source déjà à taille → ré-encodée telle quelle
|
||||
// (uniformité du cache, coût négligeable à cette échelle).
|
||||
func renderImagePreview(path string) ([]byte, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, ErrNoPreview
|
||||
}
|
||||
defer f.Close()
|
||||
var img image.Image
|
||||
lower := strings.ToLower(path)
|
||||
switch {
|
||||
case strings.HasSuffix(lower, ".png"):
|
||||
img, err = png.Decode(f)
|
||||
case strings.HasSuffix(lower, ".gif"):
|
||||
img, err = gif.Decode(f)
|
||||
case strings.HasSuffix(lower, ".webp"):
|
||||
img, err = webp.Decode(f)
|
||||
default:
|
||||
img, err = jpeg.Decode(f)
|
||||
if err != nil {
|
||||
// Extension menteuse (ex. .jpg contenant du PNG) : tente le PNG.
|
||||
if _, serr := f.Seek(0, io.SeekStart); serr == nil {
|
||||
img, err = png.Decode(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, ErrNoPreview
|
||||
}
|
||||
return encodeScaled(img)
|
||||
}
|
||||
|
||||
// renderPDFPreview rend la page 1 via `pdftoppm` (poppler) puis réduit.
|
||||
// Binaire absent ou PDF illisible → ErrNoPreview (jamais 500).
|
||||
func renderPDFPreview(path string) ([]byte, error) {
|
||||
if _, err := exec.LookPath("pdftoppm"); err != nil {
|
||||
return nil, ErrNoPreview
|
||||
}
|
||||
dir, err := os.MkdirTemp("", "kazier-preview-*")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("preview tmpdir: %w", err)
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
prefix := filepath.Join(dir, "page")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), pdftoppmTimeout)
|
||||
defer cancel()
|
||||
// -r 50 : assez pour 512 px après réduction, sans pic mémoire.
|
||||
cmd := exec.CommandContext(ctx, "pdftoppm", "-jpeg", "-r", "50", "-f", "1", "-l", "1", "-singlefile", path, prefix)
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
log.Printf("preview: pdftoppm: %v (%s)", err, strings.TrimSpace(string(out)))
|
||||
return nil, ErrNoPreview
|
||||
}
|
||||
rendered := prefix + ".jpg"
|
||||
f, err := os.Open(rendered)
|
||||
if err != nil {
|
||||
return nil, ErrNoPreview
|
||||
}
|
||||
defer f.Close()
|
||||
img, err := jpeg.Decode(f)
|
||||
if err != nil {
|
||||
return nil, ErrNoPreview
|
||||
}
|
||||
return encodeScaled(img)
|
||||
}
|
||||
|
||||
// encodeScaled réduit au plus grand côté MaxPreviewEdge (ratio préservé) et
|
||||
// encode en JPEG. Images déjà petites : recopiées à l'identique.
|
||||
func encodeScaled(img image.Image) ([]byte, error) {
|
||||
bounds := img.Bounds()
|
||||
w, h := bounds.Dx(), bounds.Dy()
|
||||
if w <= 0 || h <= 0 {
|
||||
return nil, ErrNoPreview
|
||||
}
|
||||
scaled := img
|
||||
max := w
|
||||
if h > max {
|
||||
max = h
|
||||
}
|
||||
if max > MaxPreviewEdge {
|
||||
ratio := float64(MaxPreviewEdge) / float64(max)
|
||||
nw, nh := int(float64(w)*ratio+0.5), int(float64(h)*ratio+0.5)
|
||||
if nw < 1 {
|
||||
nw = 1
|
||||
}
|
||||
if nh < 1 {
|
||||
nh = 1
|
||||
}
|
||||
dst := image.NewRGBA(image.Rect(0, 0, nw, nh))
|
||||
draw.CatmullRom.Scale(dst, dst.Bounds(), img, bounds, draw.Over, nil)
|
||||
scaled = dst
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
if err := jpeg.Encode(&buf, scaled, &jpeg.Options{Quality: previewJPEGQuality}); err != nil {
|
||||
return nil, fmt.Errorf("preview encode: %w", err)
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/dbtest"
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// newPreviewStore : comme newServiceStore mais avec un quota d'upload réaliste
|
||||
// (les images de test dépassent les 100 octets du store par défaut).
|
||||
func newPreviewStore(t *testing.T) *Resources {
|
||||
t.Helper()
|
||||
conn := dbtest.OpenTestDatabase(t, serviceTestURL)
|
||||
repo := repository.NewRepository(conn)
|
||||
return NewResources(repo, t.TempDir(), 10<<20)
|
||||
}
|
||||
|
||||
// testImageBytes génère un JPEG 800x600 uni (décodable sans fixture).
|
||||
func testImageBytes(t *testing.T) []byte {
|
||||
t.Helper()
|
||||
img := image.NewRGBA(image.Rect(0, 0, 800, 600))
|
||||
for y := 0; y < 600; y++ {
|
||||
for x := 0; x < 800; x++ {
|
||||
img.Set(x, y, color.RGBA{uint8(x % 256), uint8(y % 256), 128, 255})
|
||||
}
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
if err := jpeg.Encode(&buf, img, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func TestOpenPreviewImage(t *testing.T) {
|
||||
s := newPreviewStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "preview-img")
|
||||
|
||||
dto, err := s.Upload(userID, multipartFileHeader(t, "photo.jpg", testImageBytes(t)), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload: %v", err)
|
||||
}
|
||||
path, err := s.OpenPreview(userID, dto.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("preview: %v", err)
|
||||
}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
t.Fatalf("open preview: %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
img, err := jpeg.Decode(f)
|
||||
if err != nil {
|
||||
t.Fatalf("preview n'est pas un JPEG: %v", err)
|
||||
}
|
||||
b := img.Bounds()
|
||||
if b.Dx() > MaxPreviewEdge || b.Dy() > MaxPreviewEdge {
|
||||
t.Fatalf("vignette trop grande: %dx%d", b.Dx(), b.Dy())
|
||||
}
|
||||
if b.Dx() != MaxPreviewEdge {
|
||||
t.Fatalf("plus grand côté = %d, attendu %d", b.Dx(), MaxPreviewEdge)
|
||||
}
|
||||
|
||||
// Second appel : cache (même fichier, pas de régénération).
|
||||
fi1, _ := os.Stat(path)
|
||||
path2, err := s.OpenPreview(userID, dto.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("preview 2: %v", err)
|
||||
}
|
||||
fi2, _ := os.Stat(path2)
|
||||
if !os.SameFile(fi1, fi2) {
|
||||
t.Fatal("le cache aurait dû être servi")
|
||||
}
|
||||
|
||||
// Overwrite → la vignette suit les nouveaux octets.
|
||||
dto2, err := s.Upload(userID, multipartFileHeader(t, "photo.jpg", testImageBytes(t)), "", dto.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("overwrite: %v", err)
|
||||
}
|
||||
_ = dto2
|
||||
path3, err := s.OpenPreview(userID, dto.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("preview 3: %v", err)
|
||||
}
|
||||
fi3, _ := os.Stat(path3)
|
||||
if os.SameFile(fi2, fi3) {
|
||||
t.Fatal("la vignette aurait dû être régénérée après overwrite")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenPreviewUnsupported(t *testing.T) {
|
||||
s := newPreviewStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "preview-no")
|
||||
|
||||
dto, err := s.Upload(userID, multipartFileHeader(t, "notes.txt", []byte("hello")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload: %v", err)
|
||||
}
|
||||
if _, err := s.OpenPreview(userID, dto.ID); err != ErrNoPreview {
|
||||
t.Fatalf("txt : attendu ErrNoPreview, got %v", err)
|
||||
}
|
||||
|
||||
// Trashé → NOT_FOUND (pas NO_PREVIEW : invisible d'abord).
|
||||
if _, err := s.Trash(userID, dto.ID); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
if _, err := s.OpenPreview(userID, dto.ID); err != repository.ErrNotFound {
|
||||
t.Fatalf("trashé : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
|
||||
// Étranger → NOT_FOUND.
|
||||
other := mustCreateUser(t, s.Repository, "preview-no-x")
|
||||
dto2, err := s.Upload(other, multipartFileHeader(t, "img.png", testPNGBytes(t)), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload other: %v", err)
|
||||
}
|
||||
if _, err := s.OpenPreview(userID, dto2.ID); err != repository.ErrNotFound {
|
||||
t.Fatalf("étranger : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func testPNGBytes(t *testing.T) []byte {
|
||||
t.Helper()
|
||||
img := image.NewRGBA(image.Rect(0, 0, 100, 100))
|
||||
var buf bytes.Buffer
|
||||
if err := png.Encode(&buf, img); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func TestOpenPreviewPDF(t *testing.T) {
|
||||
if _, err := exec.LookPath("pdftoppm"); err != nil {
|
||||
t.Skip("pdftoppm absent (hors Docker) — rendu PDF non testé ici")
|
||||
}
|
||||
s := newPreviewStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "preview-pdf")
|
||||
|
||||
pdf := []byte("%PDF-1.4\n1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj\n2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj\n3 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 200 200]>>endobj\ntrailer<</Root 1 0 R>>\n")
|
||||
dto, err := s.Upload(userID, multipartFileHeader(t, "doc.pdf", pdf), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload: %v", err)
|
||||
}
|
||||
path, err := s.OpenPreview(userID, dto.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("preview pdf: %v", err)
|
||||
}
|
||||
if fi, err := os.Stat(path); err != nil || fi.Size() == 0 {
|
||||
t.Fatalf("vignette pdf absente: %v", err)
|
||||
}
|
||||
_ = filepath.Separator
|
||||
}
|
||||
@@ -18,6 +18,10 @@ import (
|
||||
// ErrFileTooLarge signals an upload above MaxFileSize.
|
||||
var ErrFileTooLarge = errors.New("file too large")
|
||||
|
||||
// ErrQuotaExceeded signals an upload that would overflow the account quota
|
||||
// (V3 S6 A2). Handlers map it to 413 QUOTA_EXCEEDED (never 500).
|
||||
var ErrQuotaExceeded = errors.New("storage quota exceeded")
|
||||
|
||||
// ErrInvalidUpload signals a rejected upload (display name or path component).
|
||||
// Handlers map it to 400 INVALID_REQUEST (never 500).
|
||||
var ErrInvalidUpload = errors.New("invalid upload name")
|
||||
@@ -233,10 +237,6 @@ func (s *Resources) GetFileOcr(ownerID, id string) (FileOcrDTO, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Resources) DeleteFile(ownerID, id string) (string, error) {
|
||||
return s.Repo.DeleteFile(ownerID, id)
|
||||
}
|
||||
|
||||
func (s *Resources) ListRootFolders(ownerID string) ([]FolderDTO, error) {
|
||||
rows, err := s.Repo.ListRootFoldersVisible(ownerID)
|
||||
if err != nil {
|
||||
@@ -249,8 +249,8 @@ func (s *Resources) ListRootFolders(ownerID string) ([]FolderDTO, error) {
|
||||
return folders, nil
|
||||
}
|
||||
|
||||
func (s *Resources) SearchFiles(ownerID, q string, page, pageSize int) ([]FileDTO, int, error) {
|
||||
rows, total, err := s.Repo.SearchFilesVisible(ownerID, q, pageSize, (page-1)*pageSize)
|
||||
func (s *Resources) SearchFiles(ownerID, q string, page, pageSize int, facets repository.SearchFacets) ([]FileDTO, int, error) {
|
||||
rows, total, err := s.Repo.SearchDocumentsVisible(ownerID, q, facets, pageSize, (page-1)*pageSize)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -280,11 +280,17 @@ func (s *Resources) Upload(ownerID string, file *multipart.FileHeader, folderID,
|
||||
return FileDTO{}, ErrInvalidUpload
|
||||
}
|
||||
targetExisting := resourceID != ""
|
||||
var cur repository.FileRow
|
||||
if targetExisting {
|
||||
if !idPattern.MatchString(resourceID) {
|
||||
return FileDTO{}, repository.ErrNotFound
|
||||
}
|
||||
if _, err := s.Repo.GetFile(ownerID, resourceID); err != nil {
|
||||
var err error
|
||||
// GetFile ne voit que le live : un upload vers une ressource
|
||||
// trashée/absente/d'autrui → NOT_FOUND (jamais de résurrection
|
||||
// d'octets sur une ligne en corbeille).
|
||||
cur, err = s.Repo.GetFile(ownerID, resourceID)
|
||||
if err != nil {
|
||||
return FileDTO{}, err
|
||||
}
|
||||
} else {
|
||||
@@ -327,10 +333,45 @@ func (s *Resources) Upload(ownerID string, file *multipart.FileHeader, folderID,
|
||||
}
|
||||
}
|
||||
|
||||
// V3 versions : sur upload ciblé, figer l'ancienne génération AVANT
|
||||
// l'écriture (le rename atomique de copyMultipartAtomic détruirait
|
||||
// l'ancien contenu). Staging = copie, jamais move : l'échec reste safe.
|
||||
// Metadata-only (aucun octet live) → rien à versionner.
|
||||
var staged, stagedExt string
|
||||
var stagedSize int64
|
||||
var stagedMime string
|
||||
var hasStaged bool
|
||||
if targetExisting {
|
||||
var ferr error
|
||||
staged, stagedExt, stagedSize, stagedMime, hasStaged, ferr = s.freezeLive(ownerID, resourceID, cur, destDir)
|
||||
if ferr != nil {
|
||||
return FileDTO{}, ferr
|
||||
}
|
||||
} else {
|
||||
// V3 S6 A2 : garde-fou quota sur les créations (les overwrites
|
||||
// ciblées remplacent des octets existants — gain net ~nul, pas de
|
||||
// check). Vérifié au header (rejet rapide) PUIS aux octets réels
|
||||
// (vérité, le header peut mentir, cf. A2).
|
||||
if err := s.checkQuotaFor(ownerID, file.Size); err != nil {
|
||||
return FileDTO{}, err
|
||||
}
|
||||
}
|
||||
|
||||
written, err := copyMultipartAtomic(file, destDir, destPath, s.MaxFileSize)
|
||||
if err != nil {
|
||||
if hasStaged {
|
||||
_ = os.Remove(staged)
|
||||
}
|
||||
return FileDTO{}, err
|
||||
}
|
||||
if !targetExisting {
|
||||
// Vérité aux octets réels (le header `file.Size` peut mentir) :
|
||||
// dépassement → fichier supprimé + 413 (pas d'orphelin).
|
||||
if err := s.checkQuotaFor(ownerID, written); err != nil {
|
||||
_ = os.Remove(destPath)
|
||||
return FileDTO{}, err
|
||||
}
|
||||
}
|
||||
|
||||
mimeType := sanitizeMimeType(file.Header.Get("Content-Type"))
|
||||
var extPtr *string
|
||||
@@ -358,6 +399,9 @@ func (s *Resources) Upload(ownerID string, file *multipart.FileHeader, folderID,
|
||||
if !stillOld {
|
||||
_ = os.Remove(destPath)
|
||||
}
|
||||
if hasStaged {
|
||||
_ = os.Remove(staged)
|
||||
}
|
||||
return FileDTO{}, err
|
||||
}
|
||||
// Succès : supprime les anciens suffixes orphelins (stale bytes, cf. #6).
|
||||
@@ -366,6 +410,15 @@ func (s *Resources) Upload(ownerID string, file *multipart.FileHeader, folderID,
|
||||
_ = os.Remove(m)
|
||||
}
|
||||
}
|
||||
// Overwrite ciblée : l'ancien live devient une version figée (N=3).
|
||||
// Best-effort après succès des métadonnées : un échec ici ne fait pas
|
||||
// échouer l'upload (loggé, staging nettoyé).
|
||||
if hasStaged {
|
||||
if ferr := s.finalizeVersion(ownerID, resourceID, staged, stagedExt, stagedSize, stagedMime); ferr != nil {
|
||||
log.Printf("upload: finalize version(%s): %v", resourceID, ferr)
|
||||
_ = os.Remove(staged)
|
||||
}
|
||||
}
|
||||
|
||||
row, err := s.Repo.GetFile(ownerID, resourceID)
|
||||
if err != nil {
|
||||
@@ -379,6 +432,9 @@ func (s *Resources) Upload(ownerID string, file *multipart.FileHeader, folderID,
|
||||
}
|
||||
return FileDTO{}, err
|
||||
}
|
||||
// V3 S5 : octets frais → hash (doublons) + réconciliation des liens.
|
||||
// Best-effort (jamais d'échec sur du hashing).
|
||||
s.refreshContentHash(ownerID, resourceID)
|
||||
return toFileDTO(row), nil
|
||||
}
|
||||
|
||||
@@ -450,3 +506,48 @@ func toFileDTO(row repository.FileRow) FileDTO {
|
||||
UpdatedAt: row.UpdatedAt.UTC().Format(time.RFC3339),
|
||||
}
|
||||
}
|
||||
|
||||
// UsageDTO serializes GET /users/me/usage (V3 S6 A2) : quota + empreinte,
|
||||
// dont la corbeille (octets récupérables à la purge).
|
||||
type UsageDTO struct {
|
||||
UsedBytes int64 `json:"usedBytes"`
|
||||
QuotaBytes int64 `json:"quotaBytes"`
|
||||
FileCount int `json:"fileCount"`
|
||||
TrashCount int `json:"trashCount"`
|
||||
TrashedBytes int64 `json:"trashedBytes"`
|
||||
}
|
||||
|
||||
// Usage returns the account footprint (computed, never stored).
|
||||
func (s *Resources) Usage(ownerID string) (UsageDTO, error) {
|
||||
user, err := s.Repository.Users.GetByID(ownerID)
|
||||
if err != nil {
|
||||
return UsageDTO{}, err
|
||||
}
|
||||
usage, err := s.Repo.StorageUsage(ownerID)
|
||||
if err != nil {
|
||||
return UsageDTO{}, err
|
||||
}
|
||||
return UsageDTO{
|
||||
UsedBytes: usage.UsedBytes, QuotaBytes: user.QuotaBytes,
|
||||
FileCount: usage.FileCount, TrashCount: usage.TrashCount,
|
||||
TrashedBytes: usage.TrashedBytes,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// checkQuotaFor rejects a creation of incoming bytes that would overflow the
|
||||
// account quota (V3 S6 A2). Compte absent → l'erreur remonte (jamais de
|
||||
// défaut silencieux qui laisserait passer).
|
||||
func (s *Resources) checkQuotaFor(ownerID string, incoming int64) error {
|
||||
user, err := s.Repository.Users.GetByID(ownerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
usage, err := s.Repo.StorageUsage(ownerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if usage.UsedBytes+incoming > user.QuotaBytes {
|
||||
return ErrQuotaExceeded
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -291,12 +291,24 @@ func TestUploadReuploadNewExtensionRemovesStaleFile(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("readdir: %v", err)
|
||||
}
|
||||
if len(entries) != 1 || entries[0].Name() != resourceID+".jpg" {
|
||||
names := make([]string, 0, len(entries))
|
||||
// V3 versions : le re-upload fige l'ancien live (png) en version, le
|
||||
// cleanup des suffixes orphelins ne touche que le live. Attendu : le
|
||||
// live `<id>.jpg` + un dossier `.versions/` contenant le png figé.
|
||||
var live []string
|
||||
for _, e := range entries {
|
||||
names = append(names, e.Name())
|
||||
if e.Name() == ".versions" {
|
||||
continue
|
||||
}
|
||||
t.Errorf("un seul physique attendu (%s.jpg), got %v", resourceID, names)
|
||||
live = append(live, e.Name())
|
||||
}
|
||||
if len(live) != 1 || live[0] != resourceID+".jpg" {
|
||||
t.Errorf("un seul live attendu (%s.jpg), got %v", resourceID, live)
|
||||
}
|
||||
verFiles, _ := filepath.Glob(filepath.Join(s.UploadDir, userID, ".versions", resourceID+"_v*"))
|
||||
if len(verFiles) != 1 {
|
||||
t.Errorf("une version figée (png) attendue, got %v", verFiles)
|
||||
} else if content, err := os.ReadFile(verFiles[0]); err != nil || string(content) != "PNGDATA" {
|
||||
t.Errorf("version figée = %q, err=%v (attendu PNGDATA)", content, err)
|
||||
}
|
||||
content, err := os.ReadFile(filepath.Join(s.UploadDir, userID, resourceID+".jpg"))
|
||||
if err != nil || string(content) != "JPGDATA!" {
|
||||
|
||||
+179
-18
@@ -25,6 +25,14 @@ const (
|
||||
OpRevokeLink = "revoke_link"
|
||||
// V2 placements : un device signale la présence de ses octets.
|
||||
OpReportBytes = "report_bytes"
|
||||
// V3 corbeille (P1) : trash = soft (restaure via restore), purge = hard.
|
||||
// delete_resource (legacy) est mappé vers trash_resource (même sémantique
|
||||
// d'idempotence, cf. applyTrashOp).
|
||||
OpTrashResource = "trash_resource"
|
||||
OpRestoreResource = "restore_resource"
|
||||
OpPurgeResource = "purge_resource"
|
||||
// V3 corrections (S3) : remplace le jeu `user` des faits (editor+).
|
||||
OpCorrectFacts = "correct_facts"
|
||||
)
|
||||
|
||||
// SyncOperation is one outbox entry (shadow of mobile PendingOperationRow).
|
||||
@@ -45,10 +53,22 @@ type FailedOperation struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// SyncResult is the outbox response: applied = index of the next op to send.
|
||||
// ConflictInfo reports a last-writer-wins rename applied over a concurrent
|
||||
// change (V3 S6 A4). Non-fatal : le batch continue, le client affiche
|
||||
// « renommé ailleurs » avec le nom serveur gagnant.
|
||||
type ConflictInfo struct {
|
||||
OperationID string `json:"operation_id"`
|
||||
ResourceID string `json:"resource_id"`
|
||||
ServerName string `json:"serverName"`
|
||||
ServerUpdatedAt int64 `json:"serverUpdatedAt"`
|
||||
}
|
||||
|
||||
// SyncResult is the outbox response: applied = index of the next op to send,
|
||||
// conflicts = renames concurrents appliqués quand même (non-fataux, S6 A4).
|
||||
type SyncResult struct {
|
||||
Applied int `json:"applied"`
|
||||
Failed *FailedOperation `json:"failed,omitempty"`
|
||||
Conflicts []ConflictInfo `json:"conflicts,omitempty"`
|
||||
}
|
||||
|
||||
type createResourcePayload struct {
|
||||
@@ -56,10 +76,18 @@ type createResourcePayload struct {
|
||||
ParentResourceID string `json:"parentResourceId"` // dossier parent (racine si vide), cf. docs/api-v1.md §6.1
|
||||
MimeType string `json:"mimeType"`
|
||||
Extension string `json:"extension"`
|
||||
// V3 S5 : session de scan opaque (32-hex mobile) — le serveur en déduit
|
||||
// les arêtes same_scan. Absent = ressource isolée (jamais d'erreur).
|
||||
ScanSessionID string `json:"scanSessionId"`
|
||||
}
|
||||
|
||||
type renamePayload struct {
|
||||
Name string `json:"name"`
|
||||
// V3 S6 A4 : `updated_at` (ms epoch) vu par le client au moment du
|
||||
// rename. Si la ressource a bougé depuis (concurrent), le rename
|
||||
// s'applique quand même (last-writer-wins) mais est signalé en
|
||||
// `conflicts` (transparence, pas de CRDT).
|
||||
BaseUpdatedAt *int64 `json:"baseUpdatedAt"`
|
||||
}
|
||||
|
||||
type movePayload struct {
|
||||
@@ -92,6 +120,10 @@ type reportBytesPayload struct {
|
||||
HasBytes *bool `json:"hasBytes"`
|
||||
}
|
||||
|
||||
type correctFactsPayload struct {
|
||||
Facts []FactDTO `json:"facts"`
|
||||
}
|
||||
|
||||
func classifySyncError(err error) (string, string) {
|
||||
switch {
|
||||
case errors.Is(err, repository.ErrNameConflict):
|
||||
@@ -141,33 +173,39 @@ var errCycle = errors.New("cannot move a resource into its own subtree")
|
||||
// explicite avec code INTERNAL, jamais en erreur Go nue (500 nu) — le client
|
||||
// sait exactement où reprendre.
|
||||
func (s *Resources) ApplyBatch(userID, deviceID string, ops []SyncOperation) (SyncResult, error) {
|
||||
// Conflits de rename (S6 A4) : non-fataux, collectés tout au long du
|
||||
// batch et renvoyés avec le résultat (le batch continue).
|
||||
var conflicts []ConflictInfo
|
||||
fail := func(i int, op *SyncOperation, code, message string) (SyncResult, error) {
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: code, Message: message}, Conflicts: conflicts}, nil
|
||||
}
|
||||
for i := range ops {
|
||||
op := &ops[i]
|
||||
if err := validateSyncOp(op); err != nil {
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INVALID_REQUEST", Message: err.Error()}}, nil
|
||||
return fail(i, op, "INVALID_REQUEST", err.Error())
|
||||
}
|
||||
claimed, err := s.Repository.Operations.Claim(deviceID, op.OperationID, op.Operation, derefString(op.RefType), nullableInt64(derefInt64(op.RefID)), derefString(op.ResourceID), op.Payload)
|
||||
if err != nil {
|
||||
// Faute client (clé ou payload malformé) → INVALID_REQUEST halt,
|
||||
// message statique sûr ; panne persistance → INTERNAL explicite.
|
||||
if errors.Is(err, repository.ErrInvalidOperationID) {
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INVALID_REQUEST", Message: "operation_id must be 32 lowercase hex chars"}}, nil
|
||||
return fail(i, op, "INVALID_REQUEST", "operation_id must be 32 lowercase hex chars")
|
||||
}
|
||||
if errors.Is(err, repository.ErrInvalidPayload) {
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INVALID_REQUEST", Message: "invalid payload JSON"}}, nil
|
||||
return fail(i, op, "INVALID_REQUEST", "invalid payload JSON")
|
||||
}
|
||||
log.Printf("sync: claim device=%s op=%s: %v", deviceID, op.OperationID, err)
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INTERNAL", Message: "failed to persist operation state"}}, nil
|
||||
return fail(i, op, "INTERNAL", "failed to persist operation state")
|
||||
}
|
||||
if !claimed {
|
||||
// Rejeu : même (device, op) déjà tracé — comparer (A9).
|
||||
stored, err := s.Repository.Operations.Get(deviceID, op.OperationID)
|
||||
if err != nil {
|
||||
log.Printf("sync: replay read device=%s op=%s: %v", deviceID, op.OperationID, err)
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INTERNAL", Message: "failed to read operation state"}}, nil
|
||||
return fail(i, op, "INTERNAL", "failed to read operation state")
|
||||
}
|
||||
if err := stored.MatchesReplay(op.Operation, derefString(op.ResourceID), op.Payload); err != nil {
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INVALID_REQUEST", Message: "operation replayed with different payload"}}, nil
|
||||
return fail(i, op, "INVALID_REQUEST", "operation replayed with different payload")
|
||||
}
|
||||
if stored.Status == repository.OpStatusApplied {
|
||||
continue // déjà appliquée à l'identique → skip idempotent
|
||||
@@ -175,16 +213,18 @@ func (s *Resources) ApplyBatch(userID, deviceID string, ops []SyncOperation) (Sy
|
||||
// Trace 'failed' à payload identique : la première tentative est
|
||||
// morte avant MarkApplied (crash) → on ré-applique (recovery).
|
||||
}
|
||||
if err := s.applySyncOp(userID, deviceID, op); err != nil {
|
||||
if conflict, err := s.applySyncOpWithConflict(userID, deviceID, op); err != nil {
|
||||
code, message := classifySyncError(err)
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: code, Message: message}}, nil
|
||||
return fail(i, op, code, message)
|
||||
} else if conflict != nil {
|
||||
conflicts = append(conflicts, *conflict)
|
||||
}
|
||||
if err := s.Repository.Operations.MarkApplied(deviceID, op.OperationID); err != nil {
|
||||
log.Printf("sync: promote device=%s op=%s: %v", deviceID, op.OperationID, err)
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INTERNAL", Message: "operation applied but trace update failed; safe to retry"}}, nil
|
||||
return fail(i, op, "INTERNAL", "operation applied but trace update failed; safe to retry")
|
||||
}
|
||||
}
|
||||
return SyncResult{Applied: len(ops)}, nil
|
||||
return SyncResult{Applied: len(ops), Conflicts: conflicts}, nil
|
||||
}
|
||||
|
||||
func validateSyncOp(op *SyncOperation) error {
|
||||
@@ -200,7 +240,8 @@ func validateSyncOp(op *SyncOperation) error {
|
||||
switch op.Operation {
|
||||
case OpCreateResource, OpUpdateMetadata, OpMoveResource, OpDeleteResource,
|
||||
OpShare, OpRevokeShare, OpUpdateShare, OpCreateLink, OpRevokeLink,
|
||||
OpReportBytes:
|
||||
OpReportBytes, OpTrashResource, OpRestoreResource, OpPurgeResource,
|
||||
OpCorrectFacts:
|
||||
default:
|
||||
return errors.New("unknown operation " + op.Operation)
|
||||
}
|
||||
@@ -210,6 +251,48 @@ func validateSyncOp(op *SyncOperation) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// conflictToleranceMs absorbe le skew d'horloge client/serveur ET la
|
||||
// troncation à la seconde des `updatedAt` RFC3339 lus par le client : un
|
||||
// écart ≤ 2 s n'est pas un conflit (ex. create suivi d'un rename immédiat
|
||||
// par le même device). Au-delà, quelqu'un d'autre a écrit entre-temps.
|
||||
const conflictToleranceMs = 2000
|
||||
|
||||
// applySyncOpWithConflict applique une op et détecte les renames concurrents
|
||||
// (S6 A4) : si `baseUpdatedAt` est fourni et que la ressource a bougé depuis
|
||||
// (au-delà de la tolérance), le rename s'applique quand même (LWW) mais est
|
||||
// signalé. Sans base (vieux clients) : aucun signalement, comportement V1.
|
||||
func (s *Resources) applySyncOpWithConflict(ownerID, deviceID string, op *SyncOperation) (*ConflictInfo, error) {
|
||||
var base *int64
|
||||
resID := derefString(op.ResourceID)
|
||||
if op.Operation == OpUpdateMetadata {
|
||||
var p renamePayload
|
||||
if err := json.Unmarshal(op.Payload, &p); err == nil && p.BaseUpdatedAt != nil {
|
||||
base = p.BaseUpdatedAt
|
||||
}
|
||||
}
|
||||
var preMs int64
|
||||
havePre := false
|
||||
if base != nil {
|
||||
if _, ms, err := s.Repo.LiveNameAndUpdatedAt(resID); err == nil {
|
||||
preMs, havePre = ms, true
|
||||
}
|
||||
}
|
||||
if err := s.applySyncOp(ownerID, deviceID, op); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if base != nil && havePre && preMs > *base+conflictToleranceMs {
|
||||
name, ms, err := s.Repo.LiveNameAndUpdatedAt(resID)
|
||||
if err != nil {
|
||||
return nil, nil // renommé puis supprimé dans le même batch : rien à signaler.
|
||||
}
|
||||
return &ConflictInfo{
|
||||
OperationID: op.OperationID, ResourceID: resID,
|
||||
ServerName: name, ServerUpdatedAt: ms,
|
||||
}, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *Resources) applySyncOp(ownerID, deviceID string, op *SyncOperation) error {
|
||||
resourceID := derefString(op.ResourceID)
|
||||
switch op.Operation {
|
||||
@@ -228,6 +311,11 @@ func (s *Resources) applySyncOp(ownerID, deviceID string, op *SyncOperation) err
|
||||
if p.Name == "" {
|
||||
return errors.New("payload.name required")
|
||||
}
|
||||
// V3 S5 : session de scan opaque (dérive same_scan, jamais d'erreur
|
||||
// bloquante au-delà du format).
|
||||
if p.ScanSessionID != "" && !resourceIDPattern.MatchString(p.ScanSessionID) {
|
||||
return errors.New("payload.scanSessionId must be 32 lowercase hex chars")
|
||||
}
|
||||
// Le parent doit exister et appartenir à l'utilisateur (NOT_FOUND sinon,
|
||||
// cohérent avec move_resource). Ordre du SAF walk : parent avant enfant.
|
||||
if p.ParentResourceID != "" {
|
||||
@@ -257,6 +345,15 @@ func (s *Resources) applySyncOp(ownerID, deviceID string, op *SyncOperation) err
|
||||
}
|
||||
return insertErr
|
||||
}
|
||||
// V3 S5 : session de scan → arêtes same_scan (best-effort, jamais
|
||||
// bloquant : la ressource existe, seuls les liens manqueraient).
|
||||
if p.ScanSessionID != "" {
|
||||
if err := s.Repo.SetScanSession(ownerID, resourceID, p.ScanSessionID); err != nil {
|
||||
log.Printf("sync: scan session %s: %v", resourceID, err)
|
||||
} else if err := s.Repo.ReconcileLinks(ownerID, resourceID); err != nil {
|
||||
log.Printf("sync: reconcile %s: %v", resourceID, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
case OpUpdateMetadata:
|
||||
@@ -311,11 +408,13 @@ func (s *Resources) applySyncOp(ownerID, deviceID string, op *SyncOperation) err
|
||||
}
|
||||
return s.Repo.MoveResource(ownerID, resourceID, p.ToFolderResourceID)
|
||||
|
||||
case OpDeleteResource:
|
||||
// A8 : delete owner-only. Ligne vivante d'autrui → NOT_FOUND halt
|
||||
// (pas de applied silencieux). Aucune ligne vivante (jamais existé,
|
||||
// ou déjà supprimée par son owner) → no-op idempotent : l'état
|
||||
// terminal (absente) est déjà atteint, le rejeu reste sûr.
|
||||
case OpDeleteResource, OpTrashResource:
|
||||
// V3 : delete_resource (legacy) = trash_resource. A8 : owner-only.
|
||||
// Ligne vivante d'autrui → NOT_FOUND halt (pas de applied
|
||||
// silencieux). Aucune ligne (jamais existé, déjà trashée/purgée par
|
||||
// son owner) → no-op idempotent : l'état terminal est atteint.
|
||||
// Note : une ressource déjà trashée reste trashée (re-trash = no-op
|
||||
// via TrashResource qui ne touche que deleted_at IS NULL).
|
||||
exists, err := s.Repo.ExistsOwner(ownerID, resourceID)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -329,7 +428,53 @@ func (s *Resources) applySyncOp(ownerID, deviceID string, op *SyncOperation) err
|
||||
}
|
||||
return repository.ErrNotFound
|
||||
}
|
||||
return s.Repo.SyncDelete(ownerID, resourceID)
|
||||
return s.Repo.TrashResource(ownerID, resourceID)
|
||||
|
||||
case OpRestoreResource:
|
||||
// Owner-only (live ou trashé). Absente → no-op (rien à restaurer,
|
||||
// l'état terminal — visible — ne peut pas être atteint autrement,
|
||||
// mais halter casserait la convergence d'un restore rejoué après
|
||||
// purge... non : après purge la ligne n'existe plus, le restore est
|
||||
// sans objet → no-op documenté). Vivante d'autrui → NOT_FOUND halt.
|
||||
owned, err := s.Repo.ExistsOwnerAny(ownerID, resourceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !owned {
|
||||
if _, err := s.Repo.GetResourceOwner(resourceID); err != nil {
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
return repository.ErrNotFound
|
||||
}
|
||||
return s.Repo.RestoreResource(ownerID, resourceID)
|
||||
|
||||
case OpPurgeResource:
|
||||
// Owner-only (live ou trashé). Absente → no-op idempotent.
|
||||
// Vivante d'autrui → NOT_FOUND halt. Octets supprimés best-effort.
|
||||
owned, err := s.Repo.ExistsOwnerAny(ownerID, resourceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !owned {
|
||||
if _, err := s.Repo.GetResourceOwner(resourceID); err != nil {
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
return repository.ErrNotFound
|
||||
}
|
||||
purged, err := s.Repo.PurgeResource(ownerID, resourceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, pid := range purged {
|
||||
s.removePhysical(ownerID, pid)
|
||||
}
|
||||
return nil
|
||||
|
||||
case OpShare, OpUpdateShare:
|
||||
return s.applyShareOp(ownerID, resourceID, op)
|
||||
@@ -346,6 +491,22 @@ func (s *Resources) applySyncOp(ownerID, deviceID string, op *SyncOperation) err
|
||||
case OpReportBytes:
|
||||
return s.applyReportBytesOp(ownerID, deviceID, resourceID, op)
|
||||
|
||||
case OpCorrectFacts:
|
||||
// S3 : même garde que CorrectFacts (editor+, A8) — une ressource
|
||||
// visible sans droit de mutation n'est pas corrigeable (NOT_FOUND,
|
||||
// pas d'oracle). Le payload porte le jeu `user` complet
|
||||
// (full-replace, jamais de diff).
|
||||
var p correctFactsPayload
|
||||
if err := json.Unmarshal(op.Payload, &p); err != nil {
|
||||
return errors.New("invalid payload: " + err.Error())
|
||||
}
|
||||
for i, f := range p.Facts {
|
||||
if _, err := validateFactParam(i, f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return s.CorrectFacts(ownerID, resourceID, p.Facts)
|
||||
|
||||
default:
|
||||
return errors.New("unknown operation " + op.Operation)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// TestRenameConflictReporting : un rename avec baseUpdatedAt périmée
|
||||
// s'applique quand même (LWW) mais est signalé en conflicts[] (S6 A4) ;
|
||||
// avec une base fraîche, aucun signalement.
|
||||
func TestRenameConflictReporting(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "conflict-user")
|
||||
deviceA := mustRegisterDevice(t, s.Repository, "c0000000000000000000000000000001")
|
||||
fileID := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFile(userID, fileID, "origine.txt", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
|
||||
// Base fraîche : pas de conflit.
|
||||
name, ms, err := s.Repository.Resources.LiveNameAndUpdatedAt(fileID)
|
||||
if err != nil || name != "origine.txt" {
|
||||
t.Fatalf("live: %q %d %v", name, ms, err)
|
||||
}
|
||||
res, err := s.ApplyBatch(userID, deviceA, []SyncOperation{
|
||||
syncOp(repository.NewID(), fileID, OpUpdateMetadata, "file", map[string]any{
|
||||
"name": "v2.txt", "baseUpdatedAt": ms,
|
||||
}),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("apply: %v", err)
|
||||
}
|
||||
if res.Applied != 1 || res.Failed != nil || len(res.Conflicts) != 0 {
|
||||
t.Fatalf("rename frais doit être silencieux: %+v", res)
|
||||
}
|
||||
|
||||
// Base périmée (concurrent théorique entre-temps) : appliqué + signalé.
|
||||
stale := ms - 60_000
|
||||
res, err = s.ApplyBatch(userID, deviceA, []SyncOperation{
|
||||
syncOp(repository.NewID(), fileID, OpUpdateMetadata, "file", map[string]any{
|
||||
"name": "v3.txt", "baseUpdatedAt": stale,
|
||||
}),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("apply: %v", err)
|
||||
}
|
||||
if res.Applied != 1 || res.Failed != nil {
|
||||
t.Fatalf("rename concurrent doit s'appliquer (LWW): %+v", res)
|
||||
}
|
||||
if len(res.Conflicts) != 1 {
|
||||
t.Fatalf("conflit manquant: %+v", res)
|
||||
}
|
||||
c := res.Conflicts[0]
|
||||
if c.ResourceID != fileID || c.ServerName != "v3.txt" || c.ServerUpdatedAt <= 0 {
|
||||
t.Fatalf("conflit inattendu: %+v", c)
|
||||
}
|
||||
|
||||
// Sans base (vieux client) : appliqué, jamais signalé.
|
||||
res, err = s.ApplyBatch(userID, deviceA, []SyncOperation{
|
||||
syncOp(repository.NewID(), fileID, OpUpdateMetadata, "file", map[string]any{"name": "v4.txt"}),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("apply: %v", err)
|
||||
}
|
||||
if res.Applied != 1 || res.Failed != nil || len(res.Conflicts) != 0 {
|
||||
t.Fatalf("sans base : silencieux: %+v", res)
|
||||
}
|
||||
|
||||
// Tolérance 2 s : un écart d'1 s (skew/troncation) n'est pas un conflit.
|
||||
_, nowMs, _ := s.Repository.Resources.LiveNameAndUpdatedAt(fileID)
|
||||
res, err = s.ApplyBatch(userID, deviceA, []SyncOperation{
|
||||
syncOp(repository.NewID(), fileID, OpUpdateMetadata, "file", map[string]any{
|
||||
"name": "v5.txt", "baseUpdatedAt": nowMs - 1000,
|
||||
}),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("apply: %v", err)
|
||||
}
|
||||
if len(res.Conflicts) != 0 {
|
||||
t.Fatalf("écart 1 s ne doit pas signaler: %+v", res)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,420 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// TrashRetention is the corbeille lifetime (V3 P1 : 30j, cf. V3.md §2).
|
||||
// Après ce délai, la passe de purge détruit définitivement les lignes.
|
||||
const TrashRetention = 30 * 24 * time.Hour
|
||||
|
||||
// TrashDTO serializes one trashed resource for GET /trash.
|
||||
type TrashDTO struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name"`
|
||||
Size int64 `json:"size"`
|
||||
TrashedAt string `json:"trashedAt"`
|
||||
UpdatedAt string `json:"updatedAt,omitempty"`
|
||||
}
|
||||
|
||||
// VersionDTO serializes one frozen byte-generation for GET /files/:id/versions.
|
||||
type VersionDTO struct {
|
||||
VersionNo int `json:"versionNo"`
|
||||
Size int64 `json:"size"`
|
||||
MimeType string `json:"mimeType,omitempty"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
}
|
||||
|
||||
// Trash soft-deletes a resource and its subtree (corbeille). Owner-only :
|
||||
// foreign or absent → repository.ErrNotFound (même code, pas d'oracle).
|
||||
// delete_resource (legacy) est mappé vers Trash (cf. applySyncOp).
|
||||
func (s *Resources) Trash(ownerID, id string) (string, error) {
|
||||
owned, err := s.Repo.ExistsOwner(ownerID, id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !owned {
|
||||
return "", repository.ErrNotFound
|
||||
}
|
||||
if err := s.Repo.TrashResource(ownerID, id); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// DeleteFile is the legacy owner-only file removal — now a trash (V3 P1).
|
||||
// Purge définitive : Purge. Le handler choisit via ?mode=purge.
|
||||
func (s *Resources) DeleteFile(ownerID, id string) (string, error) {
|
||||
// Restreint aux files comme avant (les dossiers passent par l'outbox).
|
||||
if _, err := s.Repo.GetFile(ownerID, id); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return s.Trash(ownerID, id)
|
||||
}
|
||||
|
||||
// Restore clears the trash flag on a resource, its subtree and its ancestor
|
||||
// chain. Owner-only : absent/foreign → ErrNotFound ; live → no-op succès.
|
||||
func (s *Resources) Restore(ownerID, id string) (string, error) {
|
||||
owned, err := s.Repo.ExistsOwnerAny(ownerID, id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !owned {
|
||||
return "", repository.ErrNotFound
|
||||
}
|
||||
if err := s.Repo.RestoreResource(ownerID, id); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// Purge hard-deletes a resource and its subtree (lignes + versions + octets).
|
||||
// Owner-only (trashed ou live) : absent/foreign → ErrNotFound.
|
||||
// Les fichiers physiques sont supprimés best-effort (jamais d'échec sur FS).
|
||||
func (s *Resources) Purge(ownerID, id string) (string, error) {
|
||||
owned, err := s.Repo.ExistsOwnerAny(ownerID, id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !owned {
|
||||
return "", repository.ErrNotFound
|
||||
}
|
||||
purged, err := s.Repo.PurgeResource(ownerID, id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, pid := range purged {
|
||||
s.removePhysical(ownerID, pid)
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// ListTrashed returns the owner's corbeille, most recently trashed first.
|
||||
func (s *Resources) ListTrashed(ownerID string, page, pageSize int) ([]TrashDTO, int, error) {
|
||||
rows, total, err := s.Repo.ListTrashed(ownerID, pageSize, (page-1)*pageSize)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
out := make([]TrashDTO, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
out = append(out, TrashDTO{
|
||||
ID: row.ID,
|
||||
Type: row.Type,
|
||||
Name: row.Name,
|
||||
Size: row.Size,
|
||||
TrashedAt: row.TrashedAt.UTC().Format(time.RFC3339),
|
||||
UpdatedAt: row.UpdatedAt.UTC().Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
return out, total, nil
|
||||
}
|
||||
|
||||
// ListVersions returns the frozen byte-generations of a live owned resource,
|
||||
// newest first. Trashed/absent/foreign → ErrNotFound.
|
||||
func (s *Resources) ListVersions(ownerID, id string) ([]VersionDTO, error) {
|
||||
rows, err := s.Repo.ListVersions(ownerID, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]VersionDTO, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
out = append(out, VersionDTO{
|
||||
VersionNo: row.VersionNo,
|
||||
Size: row.Size,
|
||||
MimeType: row.MimeType,
|
||||
CreatedAt: row.CreatedAt.UTC().Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// RestoreVersion recopies la génération n vers l'emplacement live (l'actuel
|
||||
// est à son tour versionné, puis élagué à N=3). Owner-only, parent live.
|
||||
func (s *Resources) RestoreVersion(ownerID, id string, n int) (FileDTO, error) {
|
||||
ver, err := s.Repo.GetVersion(ownerID, id, n)
|
||||
if err != nil {
|
||||
return FileDTO{}, err
|
||||
}
|
||||
srcAbs := filepath.Join(s.UploadDir, ver.StoredPath)
|
||||
if fi, err := os.Stat(srcAbs); err != nil || !fi.Mode().IsRegular() {
|
||||
return FileDTO{}, repository.ErrNotFound
|
||||
}
|
||||
cur, err := s.Repo.GetFile(ownerID, id)
|
||||
if err != nil {
|
||||
return FileDTO{}, err
|
||||
}
|
||||
destDir := filepath.Join(s.UploadDir, ownerID)
|
||||
if err := os.MkdirAll(destDir, 0o700); err != nil {
|
||||
return FileDTO{}, fmt.Errorf("create upload dir: %w", err)
|
||||
}
|
||||
if err := os.Chmod(destDir, 0o700); err != nil {
|
||||
return FileDTO{}, fmt.Errorf("chmod upload dir: %w", err)
|
||||
}
|
||||
// 1. Geler l'actuel (staging copy, pas de move — l'échec reste safe).
|
||||
staged, stagedExt, stagedSize, stagedMime, hasLive, err := s.freezeLive(ownerID, id, cur, destDir)
|
||||
if err != nil {
|
||||
return FileDTO{}, err
|
||||
}
|
||||
if hasLive {
|
||||
defer func() {
|
||||
// Staging orphelin si on sort en erreur avant finalize.
|
||||
if err != nil {
|
||||
_ = os.Remove(staged)
|
||||
}
|
||||
}()
|
||||
}
|
||||
// 2. Recopier la version vers le live (même atomicité que l'upload).
|
||||
destName := id
|
||||
if ver.Extension != "" {
|
||||
destName = id + "." + ver.Extension
|
||||
}
|
||||
if err := copyFileAtomic(srcAbs, destDir, filepath.Join(destDir, destName)); err != nil {
|
||||
return FileDTO{}, err
|
||||
}
|
||||
mime := ver.MimeType
|
||||
if mime == "" {
|
||||
mime = "application/octet-stream"
|
||||
}
|
||||
var extPtr *string
|
||||
if ver.Extension != "" {
|
||||
extPtr = &ver.Extension
|
||||
}
|
||||
fi, err := os.Stat(filepath.Join(destDir, destName))
|
||||
if err != nil {
|
||||
return FileDTO{}, err
|
||||
}
|
||||
if err := s.Repo.UpdatePhysical(ownerID, id, fi.Size(), &mime, extPtr); err != nil {
|
||||
return FileDTO{}, err
|
||||
}
|
||||
if err := s.Repo.SetHasContent(ownerID, id, true); err != nil {
|
||||
log.Printf("versions: SetHasContent(%s): %v", id, err)
|
||||
}
|
||||
// 3. Nettoyer les suffixes orphelins + figer l'ancien actuel en version.
|
||||
s.removeStaleSuffixes(destDir, id, destName)
|
||||
if hasLive {
|
||||
if ferr := s.finalizeVersion(ownerID, id, staged, stagedExt, stagedSize, stagedMime); ferr != nil {
|
||||
log.Printf("versions: finalize displaced current(%s): %v", id, ferr)
|
||||
}
|
||||
}
|
||||
row, err := s.Repo.GetFile(ownerID, id)
|
||||
if err != nil {
|
||||
return FileDTO{}, err
|
||||
}
|
||||
return toFileDTO(row), nil
|
||||
}
|
||||
|
||||
// PurgeExpiredTrash détruit les lignes trashées depuis plus de retention
|
||||
// (corbeille 30j) + leurs octets (best-effort). Boucle par batch jusqu'à vide.
|
||||
// Retourne le nombre de ressources racines purgées. Appelé au boot puis par
|
||||
// ticker (cf. cmd/server/main.go).
|
||||
func (s *Resources) PurgeExpiredTrash(retention time.Duration) (int, error) {
|
||||
cutoff := time.Now().Add(-retention)
|
||||
total := 0
|
||||
for {
|
||||
purged, err := s.Repo.PurgeExpiredTrash(cutoff, 500)
|
||||
if err != nil {
|
||||
return total, err
|
||||
}
|
||||
if len(purged) == 0 {
|
||||
return total, nil
|
||||
}
|
||||
for _, p := range purged {
|
||||
s.removePhysical(p.UserID, p.ResourceID)
|
||||
total++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// removePhysical supprime tous les octets connus d'une ressource : live
|
||||
// (UPLOAD_DIR/<user>/<resource>.* + nu) et versions (.versions/). Best-effort.
|
||||
func (s *Resources) removePhysical(userID, resourceID string) {
|
||||
if !idPattern.MatchString(userID) || !idPattern.MatchString(resourceID) {
|
||||
return
|
||||
}
|
||||
dir := filepath.Join(s.UploadDir, userID)
|
||||
matches, _ := filepath.Glob(filepath.Join(dir, resourceID+".*"))
|
||||
for _, m := range matches {
|
||||
if strings.HasPrefix(filepath.Base(m), resourceID+".") {
|
||||
_ = os.Remove(m)
|
||||
}
|
||||
}
|
||||
_ = os.Remove(filepath.Join(dir, resourceID))
|
||||
verMatches, _ := filepath.Glob(filepath.Join(dir, ".versions", resourceID+"_v*"))
|
||||
for _, m := range verMatches {
|
||||
_ = os.Remove(m)
|
||||
}
|
||||
}
|
||||
|
||||
// removeStaleSuffixes supprime les anciens suffixes orphelins après écriture
|
||||
// du live (même règle que Upload : tout `<id>.*` sauf le live).
|
||||
func (s *Resources) removeStaleSuffixes(destDir, resourceID, liveName string) {
|
||||
matches, _ := filepath.Glob(filepath.Join(destDir, resourceID+".*"))
|
||||
for _, m := range matches {
|
||||
if filepath.Base(m) != liveName && strings.HasPrefix(filepath.Base(m), resourceID+".") {
|
||||
_ = os.Remove(m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// livePhysicalFile retourne le chemin live existant d'une ressource (suffixé
|
||||
// ou nu), "" si metadata-only.
|
||||
func livePhysicalFile(uploadDir, ownerID, resourceID string) string {
|
||||
dir := filepath.Join(uploadDir, ownerID)
|
||||
matches, _ := filepath.Glob(filepath.Join(dir, resourceID+".*"))
|
||||
for _, m := range matches {
|
||||
if strings.HasPrefix(filepath.Base(m), resourceID+".") {
|
||||
if fi, err := os.Lstat(m); err == nil && fi.Mode().IsRegular() {
|
||||
return m
|
||||
}
|
||||
}
|
||||
}
|
||||
if bare := filepath.Join(dir, resourceID); bare != "" {
|
||||
if fi, err := os.Lstat(bare); err == nil && fi.Mode().IsRegular() {
|
||||
return bare
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// freezeLive copie les octets live vers un staging temp (jamais de move :
|
||||
// l'original reste en place jusqu'au succès). Retourne le staging + métadonnées.
|
||||
// ok=false quand metadata-only (rien à versionner).
|
||||
func (s *Resources) freezeLive(ownerID, resourceID string, cur repository.FileRow, destDir string) (staging, ext string, size int64, mime string, ok bool, err error) {
|
||||
live := livePhysicalFile(s.UploadDir, ownerID, resourceID)
|
||||
if live == "" {
|
||||
return "", "", 0, "", false, nil
|
||||
}
|
||||
base := filepath.Base(live)
|
||||
ext = ""
|
||||
if strings.HasPrefix(base, resourceID+".") {
|
||||
ext = strings.TrimPrefix(base, resourceID+".")
|
||||
}
|
||||
tmp, err := os.CreateTemp(destDir, ".ver-"+resourceID+".*")
|
||||
if err != nil {
|
||||
return "", "", 0, "", false, fmt.Errorf("stage version: %w", err)
|
||||
}
|
||||
staging = tmp.Name()
|
||||
if err := copyInto(tmp, live); err != nil {
|
||||
tmp.Close()
|
||||
_ = os.Remove(staging)
|
||||
return "", "", 0, "", false, err
|
||||
}
|
||||
if err := tmp.Close(); err != nil {
|
||||
_ = os.Remove(staging)
|
||||
return "", "", 0, "", false, fmt.Errorf("stage version: %w", err)
|
||||
}
|
||||
fi, err := os.Stat(staging)
|
||||
if err != nil {
|
||||
_ = os.Remove(staging)
|
||||
return "", "", 0, "", false, err
|
||||
}
|
||||
mime = cur.MimeType
|
||||
if mime == "" {
|
||||
mime = "application/octet-stream"
|
||||
}
|
||||
return staging, ext, fi.Size(), mime, true, nil
|
||||
}
|
||||
|
||||
// finalizeVersion déplace le staging vers .versions/, insère la ligne et
|
||||
// élague au-delà de MaxVersions (fichiers élagués supprimés best-effort).
|
||||
func (s *Resources) finalizeVersion(ownerID, resourceID, staged, ext string, size int64, mime string) error {
|
||||
no, err := s.Repo.NextVersionNo(resourceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rel := filepath.Join(ownerID, ".versions", fmt.Sprintf("%s_v%d", resourceID, no))
|
||||
if ext != "" {
|
||||
rel += "." + ext
|
||||
}
|
||||
abs := filepath.Join(s.UploadDir, rel)
|
||||
if err := os.MkdirAll(filepath.Dir(abs), 0o700); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Rename(staged, abs); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Chmod(abs, 0o600); err != nil {
|
||||
log.Printf("versions: chmod(%s): %v", rel, err)
|
||||
}
|
||||
var extPtr *string
|
||||
if ext != "" {
|
||||
extPtr = &ext
|
||||
}
|
||||
vid, err := repository.NewIDErr()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.Repo.InsertVersion(vid, resourceID, no, size, &mime, extPtr, rel); err != nil {
|
||||
_ = os.Remove(abs)
|
||||
return err
|
||||
}
|
||||
pruned, err := s.Repo.PruneVersions(resourceID, repository.MaxVersions)
|
||||
if err != nil {
|
||||
log.Printf("versions: prune(%s): %v", resourceID, err)
|
||||
return nil
|
||||
}
|
||||
for _, p := range pruned {
|
||||
_ = os.Remove(filepath.Join(s.UploadDir, p))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// copyInto copie src vers le fichier ouvert dst.
|
||||
func copyInto(dst *os.File, src string) error {
|
||||
in, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer in.Close()
|
||||
if _, err := io.Copy(dst, in); err != nil {
|
||||
return fmt.Errorf("copy version bytes: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// copyFileAtomic recopie src vers destPath via temp+rename (0600).
|
||||
func copyFileAtomic(src, destDir, destPath string) error {
|
||||
tmp, err := os.CreateTemp(destDir, ".tmp-"+filepath.Base(destPath)+".*")
|
||||
if err != nil {
|
||||
return fmt.Errorf("create tmp file: %w", err)
|
||||
}
|
||||
tmpName := tmp.Name()
|
||||
success := false
|
||||
defer func() {
|
||||
if !success {
|
||||
_ = os.Remove(tmpName)
|
||||
}
|
||||
}()
|
||||
if err := tmp.Chmod(0o600); err != nil {
|
||||
tmp.Close()
|
||||
return fmt.Errorf("chmod tmp file: %w", err)
|
||||
}
|
||||
if err := copyInto(tmp, src); err != nil {
|
||||
tmp.Close()
|
||||
return err
|
||||
}
|
||||
if err := tmp.Sync(); err != nil {
|
||||
tmp.Close()
|
||||
return fmt.Errorf("fsync: %w", err)
|
||||
}
|
||||
if err := tmp.Close(); err != nil {
|
||||
return fmt.Errorf("close: %w", err)
|
||||
}
|
||||
if err := os.Rename(tmpName, destPath); err != nil {
|
||||
return fmt.Errorf("commit: %w", err)
|
||||
}
|
||||
if err := os.Chmod(destPath, 0o600); err != nil {
|
||||
return fmt.Errorf("chmod: %w", err)
|
||||
}
|
||||
success = true
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,364 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
func mustInsertFile(t *testing.T, s *Resources, userID, name string) string {
|
||||
t.Helper()
|
||||
id := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFile(userID, id, name, "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert file %q: %v", name, err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func TestTrashRestoreRoundtrip(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "trash-roundtrip")
|
||||
fileID := mustInsertFile(t, s, userID, "acte.pdf")
|
||||
|
||||
if _, err := s.Trash(userID, fileID); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
if _, err := s.Repo.GetFile(userID, fileID); !errors.Is(err, repository.ErrNotFound) {
|
||||
t.Fatalf("GetFile après trash : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
items, total, err := s.ListTrashed(userID, 1, 50)
|
||||
if err != nil {
|
||||
t.Fatalf("list trashed: %v", err)
|
||||
}
|
||||
if total != 1 || len(items) != 1 || items[0].ID != fileID {
|
||||
t.Fatalf("corbeille: total=%d items=%+v", total, items)
|
||||
}
|
||||
|
||||
if _, err := s.Restore(userID, fileID); err != nil {
|
||||
t.Fatalf("restore: %v", err)
|
||||
}
|
||||
if _, err := s.Repo.GetFile(userID, fileID); err != nil {
|
||||
t.Fatalf("GetFile après restore: %v", err)
|
||||
}
|
||||
if _, total, err := s.ListTrashed(userID, 1, 50); err != nil || total != 0 {
|
||||
t.Fatalf("corbeille après restore: total=%d err=%v", total, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrashSubtree(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "trash-subtree")
|
||||
folderID := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFolder(userID, folderID, "Dossier", ""); err != nil {
|
||||
t.Fatalf("insert folder: %v", err)
|
||||
}
|
||||
childID := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFile(userID, childID, "enfant.txt", folderID, 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert child: %v", err)
|
||||
}
|
||||
|
||||
if _, err := s.Trash(userID, folderID); err != nil {
|
||||
t.Fatalf("trash folder: %v", err)
|
||||
}
|
||||
if _, err := s.Repo.GetFile(userID, childID); !errors.Is(err, repository.ErrNotFound) {
|
||||
t.Fatalf("enfant après trash dossier : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
if _, total, _ := s.ListTrashed(userID, 1, 50); total != 2 {
|
||||
t.Fatalf("corbeille subtree: total=%d, attendu 2", total)
|
||||
}
|
||||
|
||||
if _, err := s.Restore(userID, folderID); err != nil {
|
||||
t.Fatalf("restore folder: %v", err)
|
||||
}
|
||||
if _, err := s.Repo.GetFile(userID, childID); err != nil {
|
||||
t.Fatalf("enfant après restore dossier: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrashForeignIsNotFound(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
owner := mustCreateUser(t, s.Repository, "trash-owner")
|
||||
other := mustCreateUser(t, s.Repository, "trash-other")
|
||||
fileID := mustInsertFile(t, s, owner, "secret.pdf")
|
||||
|
||||
if _, err := s.Trash(other, fileID); !errors.Is(err, repository.ErrNotFound) {
|
||||
t.Fatalf("trash autrui : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
if _, err := s.Restore(other, fileID); !errors.Is(err, repository.ErrNotFound) {
|
||||
t.Fatalf("restore autrui : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
if _, err := s.Purge(other, fileID); !errors.Is(err, repository.ErrNotFound) {
|
||||
t.Fatalf("purge autrui : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
if _, err := s.Repo.GetFile(owner, fileID); err != nil {
|
||||
t.Fatalf("fichier owner intact: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteLegacyMapsToTrash(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "trash-legacy")
|
||||
device := mustRegisterDevice(t, s.Repository, "b0000000000000000000000000000001")
|
||||
fileID := mustInsertFile(t, s, userID, "legacy.pdf")
|
||||
|
||||
// delete_resource → trash (restaurable, visible en corbeille).
|
||||
res, err := s.ApplyBatch(userID, device, []SyncOperation{
|
||||
syncOp(repository.NewID(), fileID, OpDeleteResource, "file", map[string]any{}),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("apply: %v", err)
|
||||
}
|
||||
if res.Applied != 1 || res.Failed != nil {
|
||||
t.Fatalf("résultat inattendu: %+v", res)
|
||||
}
|
||||
if _, err := s.Repo.GetFile(userID, fileID); !errors.Is(err, repository.ErrNotFound) {
|
||||
t.Fatalf("GetFile après delete legacy : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
if _, total, _ := s.ListTrashed(userID, 1, 50); total != 1 {
|
||||
t.Fatalf("delete legacy doit apparaître en corbeille")
|
||||
}
|
||||
|
||||
// Re-delete (déjà trashé) → no-op idempotent, pas NOT_FOUND.
|
||||
res, err = s.ApplyBatch(userID, device, []SyncOperation{
|
||||
syncOp(repository.NewID(), fileID, OpDeleteResource, "file", map[string]any{}),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("re-apply: %v", err)
|
||||
}
|
||||
if res.Applied != 1 || res.Failed != nil {
|
||||
t.Fatalf("re-delete trashé doit être no-op: %+v", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncOpsTrashRestorePurge(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "trash-ops")
|
||||
other := mustCreateUser(t, s.Repository, "trash-ops-other")
|
||||
device := mustRegisterDevice(t, s.Repository, "b0000000000000000000000000000002")
|
||||
fileID := mustInsertFile(t, s, userID, "ops.pdf")
|
||||
foreignID := mustInsertFile(t, s, other, "foreign.pdf")
|
||||
|
||||
apply := func(op, resID string) SyncResult {
|
||||
t.Helper()
|
||||
res, err := s.ApplyBatch(userID, device, []SyncOperation{
|
||||
syncOp(repository.NewID(), resID, op, "file", map[string]any{}),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("apply %s: %v", op, err)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
if res := apply(OpTrashResource, fileID); res.Applied != 1 || res.Failed != nil {
|
||||
t.Fatalf("trash op: %+v", res)
|
||||
}
|
||||
// Trash d'autrui → halt NOT_FOUND.
|
||||
if res := apply(OpTrashResource, foreignID); res.Failed == nil || res.Failed.Code != "NOT_FOUND" {
|
||||
t.Fatalf("trash autrui doit halter NOT_FOUND: %+v", res)
|
||||
}
|
||||
if res := apply(OpRestoreResource, fileID); res.Applied != 1 || res.Failed != nil {
|
||||
t.Fatalf("restore op: %+v", res)
|
||||
}
|
||||
if _, err := s.Repo.GetFile(userID, fileID); err != nil {
|
||||
t.Fatalf("fichier restauré via op: %v", err)
|
||||
}
|
||||
// Restore d'un live → no-op succès.
|
||||
if res := apply(OpRestoreResource, fileID); res.Applied != 1 || res.Failed != nil {
|
||||
t.Fatalf("restore live doit être no-op: %+v", res)
|
||||
}
|
||||
// Restore d'inexistant → no-op (convergence).
|
||||
if res := apply(OpRestoreResource, repository.NewID()); res.Applied != 1 || res.Failed != nil {
|
||||
t.Fatalf("restore inexistant doit être no-op: %+v", res)
|
||||
}
|
||||
// Purge d'un live → détruit.
|
||||
if res := apply(OpPurgeResource, fileID); res.Applied != 1 || res.Failed != nil {
|
||||
t.Fatalf("purge op: %+v", res)
|
||||
}
|
||||
if owned, _ := s.Repo.ExistsOwnerAny(userID, fileID); owned {
|
||||
t.Fatal("purge doit détruire la ligne")
|
||||
}
|
||||
// Purge d'inexistant → no-op.
|
||||
if res := apply(OpPurgeResource, fileID); res.Applied != 1 || res.Failed != nil {
|
||||
t.Fatalf("purge inexistant doit être no-op: %+v", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPurgeRemovesPhysicalFiles(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "trash-physical")
|
||||
|
||||
dto, err := s.Upload(userID, multipartFileHeader(t, "doc.txt", []byte("hello")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload: %v", err)
|
||||
}
|
||||
live := filepath.Join(s.UploadDir, userID, dto.ID+".txt")
|
||||
if _, err := os.Stat(live); err != nil {
|
||||
t.Fatalf("live absent: %v", err)
|
||||
}
|
||||
if _, err := s.Purge(userID, dto.ID); err != nil {
|
||||
t.Fatalf("purge: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(live); !os.IsNotExist(err) {
|
||||
t.Fatalf("octets live doivent être supprimés: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadOverwriteCreatesVersionsCappedAt3(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "trash-versions")
|
||||
|
||||
v1, err := s.Upload(userID, multipartFileHeader(t, "doc.txt", []byte("AAA")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload v1: %v", err)
|
||||
}
|
||||
// Le premier upload (création) ne crée pas de version.
|
||||
if vers, err := s.ListVersions(userID, v1.ID); err != nil || len(vers) != 0 {
|
||||
t.Fatalf("création ne doit pas versionner: %+v %v", vers, err)
|
||||
}
|
||||
|
||||
contents := []string{"BBBB", "CCCCC", "DDDDDD", "EEEEEEE"}
|
||||
for i, c := range contents {
|
||||
dto, err := s.Upload(userID, multipartFileHeader(t, "doc.txt", []byte(c)), "", v1.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("overwrite %d: %v", i, err)
|
||||
}
|
||||
if dto.Size != int64(len(c)) {
|
||||
t.Fatalf("overwrite %d: taille=%d", i, dto.Size)
|
||||
}
|
||||
}
|
||||
vers, err := s.ListVersions(userID, v1.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("list versions: %v", err)
|
||||
}
|
||||
if len(vers) != 3 {
|
||||
t.Fatalf("versions = %d, attendu 3 (plafond N=3)", len(vers))
|
||||
}
|
||||
// Les 3 conservées sont les plus récentes : AAA a été élaguée.
|
||||
// Ordre décroissant : version_no 4,3,2 → tailles 6,5,4.
|
||||
wantSizes := []int64{6, 5, 4}
|
||||
for i, v := range vers {
|
||||
if v.Size != wantSizes[i] {
|
||||
t.Errorf("version[%d].size = %d, attendu %d", i, v.Size, wantSizes[i])
|
||||
}
|
||||
}
|
||||
// 3 fichiers figés sur disque (AAA élaguée avec sa ligne).
|
||||
verFiles, _ := filepath.Glob(filepath.Join(s.UploadDir, userID, ".versions", v1.ID+"_v*"))
|
||||
if len(verFiles) != 3 {
|
||||
t.Errorf("fichiers versionnés = %d, attendu 3 (%v)", len(verFiles), verFiles)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoreVersion(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "trash-restore-ver")
|
||||
|
||||
dto, err := s.Upload(userID, multipartFileHeader(t, "doc.txt", []byte("AAA")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload v1: %v", err)
|
||||
}
|
||||
if _, err := s.Upload(userID, multipartFileHeader(t, "doc.txt", []byte("BBBB")), "", dto.ID); err != nil {
|
||||
t.Fatalf("overwrite: %v", err)
|
||||
}
|
||||
vers, err := s.ListVersions(userID, dto.ID)
|
||||
if err != nil || len(vers) != 1 {
|
||||
t.Fatalf("versions: %+v %v", vers, err)
|
||||
}
|
||||
|
||||
restored, err := s.RestoreVersion(userID, dto.ID, vers[0].VersionNo)
|
||||
if err != nil {
|
||||
t.Fatalf("restore version: %v", err)
|
||||
}
|
||||
if restored.Size != 3 {
|
||||
t.Fatalf("live restauré taille=%d, attendu 3", restored.Size)
|
||||
}
|
||||
raw, err := os.ReadFile(filepath.Join(s.UploadDir, userID, dto.ID+".txt"))
|
||||
if err != nil {
|
||||
t.Fatalf("read live: %v", err)
|
||||
}
|
||||
if string(raw) != "AAA" {
|
||||
t.Fatalf("contenu live = %q, attendu AAA", raw)
|
||||
}
|
||||
// L'ancien actuel (BBBB) a été à son tour versionné.
|
||||
vers2, err := s.ListVersions(userID, dto.ID)
|
||||
if err != nil || len(vers2) != 2 {
|
||||
t.Fatalf("versions après restore: %+v %v", vers2, err)
|
||||
}
|
||||
if _, err := s.RestoreVersion(userID, dto.ID, 999); !errors.Is(err, repository.ErrNotFound) {
|
||||
t.Fatalf("restore version inconnue : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadToTrashedIsNotFound(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "trash-upload-guard")
|
||||
|
||||
dto, err := s.Upload(userID, multipartFileHeader(t, "doc.txt", []byte("AAA")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload: %v", err)
|
||||
}
|
||||
if _, err := s.Trash(userID, dto.ID); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
if _, err := s.Upload(userID, multipartFileHeader(t, "doc.txt", []byte("BBB")), "", dto.ID); !errors.Is(err, repository.ErrNotFound) {
|
||||
t.Fatalf("upload vers trashé : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func backdateTrash(t *testing.T, s *Resources, resourceID string, ago time.Duration) {
|
||||
t.Helper()
|
||||
if _, err := s.Repository.Resources.DB.Exec(
|
||||
`UPDATE resources SET deleted_at = NOW() - ($1 || ' seconds')::interval WHERE resource_id = $2`,
|
||||
int(ago.Seconds()), resourceID,
|
||||
); err != nil {
|
||||
t.Fatalf("backdate: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPurgeExpiredTrash(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "trash-expire")
|
||||
|
||||
old, err := s.Upload(userID, multipartFileHeader(t, "vieux.txt", []byte("old")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload vieux: %v", err)
|
||||
}
|
||||
fresh, err := s.Upload(userID, multipartFileHeader(t, "frais.txt", []byte("new")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload frais: %v", err)
|
||||
}
|
||||
if _, err := s.Trash(userID, old.ID); err != nil {
|
||||
t.Fatalf("trash vieux: %v", err)
|
||||
}
|
||||
if _, err := s.Trash(userID, fresh.ID); err != nil {
|
||||
t.Fatalf("trash frais: %v", err)
|
||||
}
|
||||
backdateTrash(t, s, old.ID, 31*24*time.Hour)
|
||||
|
||||
n, err := s.PurgeExpiredTrash(TrashRetention)
|
||||
if err != nil {
|
||||
t.Fatalf("purge expired: %v", err)
|
||||
}
|
||||
if n != 1 {
|
||||
t.Fatalf("purgés = %d, attendu 1", n)
|
||||
}
|
||||
if owned, _ := s.Repo.ExistsOwnerAny(userID, old.ID); owned {
|
||||
t.Fatal("vieux doit être détruit")
|
||||
}
|
||||
if _, _, err := s.ListTrashed(userID, 1, 50); err != nil {
|
||||
t.Fatalf("list: %v", err)
|
||||
}
|
||||
if _, err := s.Repo.GetFile(userID, fresh.ID); err == nil {
|
||||
// Frais trashé mais pas expiré : invisible en lecture, présent en corbeille.
|
||||
t.Fatal("frais doit rester trashé (invisible en lecture)")
|
||||
}
|
||||
if _, total, _ := s.ListTrashed(userID, 1, 50); total != 1 {
|
||||
t.Fatalf("corbeille doit garder le frais: total=%d", total)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(s.UploadDir, userID, old.ID+".txt")); !os.IsNotExist(err) {
|
||||
t.Fatal("octets du purgé doivent être supprimés")
|
||||
}
|
||||
}
|
||||
+32
-13
@@ -18,8 +18,8 @@ Références : `V2.md` (modèle cible), `mobile/services/db/` (conventions sync)
|
||||
|
||||
- **User-first (V1 finale)** : le device s'enregistre d'abord (`POST /devices`) avec son identité **générée localement** (`device_user_id` 32-hex mobile) — **réponse `{ "deviceId" }` uniquement, sans token**. Puis le client appelle `POST /auth/login` (`username` + `password` + `device_id`) pour obtenir son token **paseto** v4-local. Requêtes suivantes : `Authorization: Bearer <token>` (toutes les routes **sauf `/health`, `/devices`, `/auth/login`**).
|
||||
- **Claims du token** : le **subject = `user_id`** (AUTORISE, clé de scoping de toutes les ressources) ; `device_id` est un **claim secondaire, porté mais NON autorisant seul** (idempotence outbox + jobs OCR). Résolu par middleware `RequireAuth` qui vérifie aussi que le compte existe toujours (`deleted_at IS NULL`).
|
||||
- **TTL : 7 jours, sans refresh.** À expiration, le client re-logine (`POST /auth/login`). Le changement de mot de passe (`PATCH /users/me/password`) **n'invalide pas** les tokens déjà émis — limite V1 assumée (pas de liste de révocation) jusqu'à l'expiration.
|
||||
- Au login, `devices.user_id` mémorise le **dernier user connecté** (INFORMATIF uniquement, jamais autorisant — recommande `POST /devices` → `POST /auth/login` pour un nouveau device, sans réutiliser le device d'un autre compte).
|
||||
- **TTL : 7 jours, sans refresh.** À expiration, le client re-logine (`POST /auth/login`). **Révocation V3 (S6)** : `PATCH /users/me/password` bumpe `users.token_version` — tous les tokens antérieurs deviennent 401 (`token revoked`), y compris non expirés ; le client doit se reconnecter (le claim `tv` porte la version, les tokens pré-V3 sans `tv` valent 0 et survivent jusqu'au premier changement de mot de passe).
|
||||
- Au login, `devices.user_id` mémorise le **dernier user connecté** (INFORMATIF uniquement, jamais autorisant — recommande `POST /devices` → `POST /auth/login` pour un nouveau device, sans réutiliser le device d'un autre compte). **Révocation V3 (S6 A1)** : `DELETE /devices/:id` supprime l'enregistrement (téléphone perdu) — avec `EnforceDeviceExists` (prod), les tokens du device deviennent 401 et son login est refusé ; seule une ré-inscription explicite (`POST /devices`) restaure l'accès. `GET /devices` liste les devices du compte (dernier login = ce compte).
|
||||
- **Identifiants** : `resource_id`, `device_user_id`, `user_id`, `is_admin`… = **TEXT opaque 32-hex minuscule**, `^[0-9a-f]{32}$`. Le mobile génère toujours `lower(hex(randomblob(16)))` ; le serveur stocke **tel quel**, sans conversion UUID (cf. note V2.md). Contrainte serveur : `CHECK (col ~ '^[0-9a-f]{32}$')` sur toutes les colonnes id + FK.
|
||||
- **Usernames** : `username` = forme affichée ; l'unicité et la résolution portent sur `username_normalized` (lowercase + trim). Résolution d'un destinataire : `GET /users/resolve?username=` (exact uniquement, jamais de listing ni de préfixe — pas d'énumération de comptes).
|
||||
- **Bootstrap** : au premier démarrage, si `users` est vide, `ADMIN_USERNAME`/`ADMIN_PASSWORD` (env) créent le **premier admin** ; absents → le serveur **refuse de démarrer**. L'env n'écrase jamais un compte existant.
|
||||
@@ -34,17 +34,31 @@ Références : `V2.md` (modèle cible), `mobile/services/db/` (conventions sync)
|
||||
| POST | `/auth/login` | `{ "username", "password", "device_id" }` | `{ "token", "expires_at" (ms), "user": { "id", "username", "is_admin" } }` | `UNAUTHORIZED` / `INVALID_DEVICE_ID` |
|
||||
| GET | `/shares/links/:token` | — (public, sans auth) | `{ "token", "resource_id", "resourceType", "name", "access", "expiresAt" }` | `NOT_FOUND` (inconnu/révoqué/expiré) |
|
||||
| GET | `/users/resolve` | query `username` (obligatoire) | `{ "id", "username" }` | `NOT_FOUND` |
|
||||
| PATCH | `/users/me/password` | `{ "current_password", "new_password" }` | `{ "id" }` | `INVALID_PASSWORD` (403) |
|
||||
| PATCH | `/users/me/password` | `{ "current_password", "new_password" }` | `{ "id" }` (+ révocation globale des tokens) | `INVALID_PASSWORD` (403) |
|
||||
| GET | `/users/me/usage` | — | `{ usedBytes, quotaBytes, fileCount, trashCount, trashedBytes }` (calculé, corbeille détaillée) | — |
|
||||
| GET | `/devices` | — | `[{ deviceId, lastSeenAt }]` (dernier login = ce compte) | — |
|
||||
| DELETE | `/devices/:id` | — (téléphone perdu, A1) | `{ "id" }` | `NOT_FOUND` (absent/autre compte — pas d'énumération) |
|
||||
| GET | `/files` | query `folderId?`, `page?`, `pageSize?`, `sort?` | `FileDto[]` (+ `meta`) | — |
|
||||
| GET | `/files/:id` | — | `FileDto` | `NOT_FOUND` |
|
||||
| GET | `/files/:id/content` | — (binaire brut, hors enveloppe `{data}` ; `Range` supporté) | octets physiques (`Content-Type` du fichier, `Content-Disposition: inline`) — streaming lecture seule, jamais persisté côté client | `NOT_FOUND` (invisible OU metadata-only jamais uploadé — indistinguables) |
|
||||
| DELETE | `/files/:id` | — | `{ "id": "…" }` | `NOT_FOUND` |
|
||||
| GET | `/files/search` | query `q` (obligatoire), `page?`, `pageSize?` | `FileDto[]` (+ `meta`) | — |
|
||||
| GET | `/files/:id/preview` | — (JPEG 512, `inline`, `Cache-Control: private, max-age=3600`, viewer+) | vignette (`image/jpeg`) — générée à la demande (images pur Go, PDF page 1 via `pdftoppm`), cachée jusqu'au prochain changement | `NOT_FOUND` (invisible/metadata-only), `NO_PREVIEW` (type non prévisualisable — icône côté client, sans réessai) |
|
||||
| POST | `/export` | `{ "password" }` (8+ caractères, en corps — jamais en query) | zip AES-256 streamé (`application/zip`, `attachment`) — fichiers vifs possédés + `manifest.json`, tout chiffré | `INVALID_REQUEST` (mot de passe court/malformé) |
|
||||
| DELETE | `/files/:id` | query `mode?` (`trash` défaut, `purge` = définitif) | `{ "id": "…" }` | `NOT_FOUND` |
|
||||
| POST | `/files/:id/restore` | — | `{ "id": "…" }` | `NOT_FOUND` (absente/étrangère/purgée ; vivante = succès idempotent) |
|
||||
| GET | `/trash` | query `page?`, `pageSize?` | `TrashDto[]` (`{ id, type, name, size, trashedAt, updatedAt }` + `meta`) | — |
|
||||
| GET | `/files/:id/versions` | — (parent live uniquement) | `VersionDto[]` (`{ versionNo, size, mimeType, createdAt }`, plus récent d'abord) | `NOT_FOUND` |
|
||||
| POST | `/files/:id/versions/:n/restore` | — (l'actuel est à son tour versionné) | `FileDto` | `NOT_FOUND` |
|
||||
| GET | `/files/search` | query `q` (obligatoire), `page?`, `pageSize?`, facettes V3 : `issuer?`, `from?`/`to?` (ISO `YYYY-MM-DD`, sur faits date/échéance + recouvrement périodes `YYYY-MM`), `min_amount?`/`max_amount?` (numériques), `doc_type?` (vocabulaire suggéré) | `FileDto[]` (+ `meta`, rang : nom exact > TS > fait sûr > fait douteux) | `INVALID_REQUEST` (facette malformée) |
|
||||
| GET | `/files/folders` | — | `FolderDto[]` (racines) | — |
|
||||
| POST | `/files/upload` | multipart : `file` (uri/name/type), `folderId?` | `FileDto` | `FILE_TOO_LARGE` |
|
||||
| POST | `/ocr/jobs` | `{ "fileId": "…" }` | `OcrJob` | — |
|
||||
| GET | `/ocr/jobs/:id` | — | `OcrJob` | `NOT_FOUND` |
|
||||
| GET | `/files/:id/ocr` | — | `FileOcr` (`{ text, updatedAt }`) | `NOT_FOUND` |
|
||||
| GET | `/files/:id/facts` | — (viewer+, même portée que l'OCR) | `Fact[]` (`{ factType: date|due_date|period|amount|issuer|reference|doc_type, valueText?, valueNum?, valueDate? (ISO), confidence (0..1), origin: auto|user }`, `[]` si rien d'extractible) | `NOT_FOUND` (trashé/absent/étranger) |
|
||||
| PATCH | `/files/:id/facts` | `{ facts: [{ factType, valueText?, valueNum?, valueDate?, confidence? }] }` (jeu `user` complet, full-replace, editor+) | `Fact[]` (jeu complet auto + user) | `NOT_FOUND` (viewer-/absent/trashé), `INVALID_REQUEST` (type inconnu, date non ISO, fait sans valeur, corps malformé) |
|
||||
| GET | `/files/:id/relations` | — (owner uniquement) | `Relation[]` (`{ id, otherId, type: same_scan|duplicate_of|same_issuer|same_reference, confidence, createdBy: auto|user, otherName, otherType, otherSize }`, réconcilié à la lecture, autre bout vivant) | `NOT_FOUND` (étranger/trashé/absent — pas d'énumération) |
|
||||
| POST | `/files/:id/relations/verdict` | `{ otherId, linkType, confirmed }` | `Relation[]` (jeu réconcilié) | `NOT_FOUND` (bouts non possédés), `INVALID_REQUEST` (type/paire invalide, corps malformé) |
|
||||
| GET | `/duplicates` | — (compte connecté) | `DuplicateGroup[]` (`{ contentHash, count, totalBytes, reclaimableBytes, files: [{ id, name, size, updatedAt }] }`, plus gros d'abord, `[]` si propre) | — |
|
||||
| GET | `/files/:id/shares` | — (owner uniquement) | `ShareGrant[]` (`{ granteeUserId, granteeKind: "user"|"group", username, access, inherit, expiresAt (ms|null), updatedAt (ms) }`) | `NOT_FOUND` (non-owner ou inconnue — pas d'énumération) |
|
||||
| GET | `/files/:id/links` | — (owner uniquement) | `ShareLink[]` (même forme que `GET /shares/links/:token`) | `NOT_FOUND` (non-owner ou inconnue) |
|
||||
| GET | `/files/:id/placements` | — (owner uniquement) | `Placement[]` (`{ locationType: "server"|"mobile", deviceId?, hasBytes, updatedAt }`, entrée serveur en premier) | `NOT_FOUND` (non-owner ou inconnue) |
|
||||
@@ -94,6 +108,7 @@ type FileOcr = { text: string; updatedAt?: string | null };
|
||||
- `fileId` inconnu/pas du user → `NOT_FOUND`. Fichier physique introuvable (ex. suppression manuelle sous `UPLOAD_DIR`) → job `failed` `"file not readable"`. Le job est créé par le device courant (`ocr_jobs.device_id`) mais la validation de la ressource est scopée par le **user**.
|
||||
- **Le texte atterrit sur la ressource** : à la complétion (`done` avec texte non vide), le worker écrit l'extrait trimé dans `resources.ocr_text` (et **bump `updated_at`**, d'où une injection dans le delta outbox/snapshot). `ocr_jobs.text` reste l'historique du job.
|
||||
- **`GET /files/:id/ocr`** → `FileOcr { text, updatedAt }` : l'extrait server-side du fichier, lisible par le **possédant ET les grantees** (`viewer+`, même portée que `GET /files/:id` ; `text` `""` si pas encore extrait). Un **second device** rapatrie ainsi le texte d'un OCR fait par un autre device (ou par la ressource partagée) **sans re-téléverser les octets ni re-soumettre de job** — le client Kotlin le fait en tête de `OcrRepository.syncFromServer`/`processAuto` et du viewer (`OcrViewModel`).
|
||||
- **Faits génériques V3 (`GET /files/:id/facts`)** : après chaque OCR `done`, le worker en dérive des faits vérifiables (règles FR, sans ML : dates ISO, échéances (`due_date` si mot-clé `échéance|à payer avant|date limite`), périodes (`2026-03`), montants € (confiance 0.9 si mot-clé `total|montant|à payer`), émetteurs (lignes capitales), références (`facture:FA-…`, `siret:…`, IBAN masqué `rib:••••1234`), `doc_type` suggéré (seuil de score, jamais gate). Chaque fait porte `confidence` (0..1) et `origin` (`auto` remplacé à chaque passe, `user` intouchable — corrections S3). Même portée que l'OCR (`viewer+`) ; `[]` si rien d'extractible. Backfill borné au boot (OCR d'avant la V3).
|
||||
|
||||
## 6. Contrat de sync (outbox + snapshot)
|
||||
|
||||
@@ -115,15 +130,19 @@ type FileOcr = { text: string; updatedAt?: string | null };
|
||||
}
|
||||
```
|
||||
|
||||
- `operation` ∈ `create_resource | update_metadata | delete_resource | move_resource | share | revoke_share | update_share | create_link | revoke_link | report_bytes` (cf. `PendingOperationType` mobile).
|
||||
- `operation` ∈ `create_resource | update_metadata | delete_resource | move_resource | share | revoke_share | update_share | create_link | revoke_link | report_bytes | trash_resource | restore_resource | purge_resource | correct_facts` (cf. `PendingOperationType` mobile).
|
||||
- **Identifiants** : `operation_id` est un **TEXT 32-hex** généré par le client (`^[0-9a-f]{32}$`, CHECK-enforced depuis la migration `000008`), distinct de `resource_id`. L'idempotence outbox reste **par device** : `UNIQUE(device_id, operation_id)` (la réinscription d'un device avec un login différent ne réutilise pas l'historique outbox d'un autre compte). Pour chaque op : si déjà traitée → **no-op** (comptée comme appliquée, les doublons arrivent à cause du backoff/retry). Sinon appliquée si valide.
|
||||
- **Ordre** : les opérations sont appliquées **séquentiellement**, dans l'ordre du batch. Le serveur **s'arrête à la première erreur non-idempotente** et renvoie l'index atteint — le client reprend à cet index.
|
||||
- Réponse : `2xx` avec `{ "applied": int, "failed": { "operation_id": "…32-hex", "code": string, "message": string } | null }` (`applied` = index de la prochaine op à envoyer).
|
||||
- Réponse : `2xx` avec `{ "applied": int, "failed": { "operation_id": "…32-hex", "code": string, "message": string } | null, "conflicts"?: [{ "operation_id", "resource_id", "serverName", "serverUpdatedAt" }] }` (`applied` = index de la prochaine op à envoyer ; `conflicts` = renames LWW appliqués malgré une `baseUpdatedAt` périmée — non-fataux, le batch continue).
|
||||
- Côté client, le `pushStatus` (pending/synced/failed) des shares/share_links est **dérivé** de l'état des opérations de l'outbox ; **dead-letter immédiat** sur erreur 4xx non-idempotente (`failed`, non resélectionné ; `attempts` reste un compteur diagnostic, pas un seuil) — seul le transitoire (`NETWORK_ERROR`/5xx) est rejoué avec backoff. **Côté serveur, les ops partage créent un état réel** (tables `shares`/`share_links`, migration `000009`) — l'outbox est le seul chemin d'écriture des droits.
|
||||
- Sémantique d'application (côté serveur) :
|
||||
- `create_resource` : crée la ressource ; **déjà présente → no-op** (rejeu idempotent). `payload.name` obligatoire ; `payload.parentResourceId` (32-hex, optionnel) = dossier parent — absent → racine. **Parent inexistant → `NOT_FOUND`** (cohérent avec `move_resource`). Ordre garanti par construction client : le walk SAF émet les `create` des dossiers (ordre préfixe) avant ceux des fichiers, dans la même transaction Room → `id ASC` = parent avant enfant.
|
||||
- `update_metadata` : ressource absente → **no-op** (état terminal atteint) ; **ressource partagée : nécessite `editor+`** (§6.2) — sinon `NOT_FOUND` (une ressource visible mais sans droit de mutation n'est pas renommée) ; une ressource sans relation applicable (ni owner, ni grant) reste un no-op (pas d'énumération). `move_resource` / `delete_resource` restent **owner-only** (scoping par `user_id`, les ops d'un non-owner sont des no-op).
|
||||
- `delete_resource` : **idempotent** — suppression d'une ressource absente = succès.
|
||||
- `create_resource` : crée la ressource ; **déjà présente → no-op** (rejeu idempotent). `payload.name` obligatoire ; `payload.parentResourceId` (32-hex, optionnel) = dossier parent — absent → racine. **Parent inexistant → `NOT_FOUND`** (cohérent avec `move_resource`). Ordre garanti par construction client : le walk SAF émet les `create` des dossiers (ordre préfixe) avant ceux des fichiers, dans la même transaction Room → `id ASC` = parent avant enfant. `payload.scanSessionId` (32-hex, optionnel, V3 S5) = session de scan opaque — le serveur en déduit les arêtes `same_scan` (même session, même compte) ; format invalide → `INVALID_REQUEST`.
|
||||
- `update_metadata` : ressource absente → **no-op** (état terminal atteint) ; **ressource partagée : nécessite `editor+`** (§6.2) — sinon `NOT_FOUND` (une ressource visible mais sans droit de mutation n'est pas renommée) ; une ressource sans relation applicable (ni owner, ni grant) reste un no-op (pas d'énumération). `payload = { "name", "baseUpdatedAt"? }` (V3 S6 A4 : `updated_at` ms vu par le client — périmé au-delà de 2 s de tolérance → rename appliqué quand même + signalé en `conflicts`, transparence sans CRDT). `move_resource` / `delete_resource` restent **owner-only** (scoping par `user_id`, les ops d'un non-owner sont des no-op).
|
||||
- `delete_resource` : **legacy → mappé vers `trash_resource`** (corbeille, restaurable, même idempotence : absent/déjà trashé = succès, vivant d'autrui = `NOT_FOUND` halt).
|
||||
- **Corbeille V3** (`trash_resource` / `restore_resource` / `purge_resource`, owner-only) : `trash` pose `deleted_at` sur la ressource **et son subtree** (dossier → contenu) ; `restore` lève le flag sur la ressource, son subtree **et sa chaîne d'ancêtres** (un fichier restauré dont le dossier est encore trashé resterait injoignable) ; `purge` détruit lignes + versions + octets. Sémantique : absent → no-op (`trash`/`restore`/`purge`), vivant d'autrui → `NOT_FOUND` halt, `restore` d'un live → no-op succès. Une ressource trashée reste `NOT_FOUND` en lecture normale, visible seulement via `GET /trash`. Passe serveur : destruction définitive après 30j (`TrashRetention`).
|
||||
- **Corrections V3** (`correct_facts`, editor+) : `payload = { "facts": [{ "factType", "valueText"?, "valueNum"?, "valueDate"? (ISO `YYYY-MM-DD`), "confidence"? }] }` — remplace le jeu `user` complet (full-replace, jamais de diff ; jeu vide = efface les corrections, l'auto survit). Mêmes gardes qu'`update_metadata` (viewer- → `NOT_FOUND` halt) ; fait invalide (type inconnu, date non ISO, sans valeur) → `INVALID_REQUEST` avec arrêt du batch. Le client corrige en un tap depuis la fiche document et pousse dans la même transaction Room que l'écriture optimiste.
|
||||
- **Graphe V3 (lecture)** : `GET /files/:id/relations` (owner-only) réconcilie puis renvoie les arêtes vivantes — `same_scan` (même `scan_session_id`, 1.0), `duplicate_of` (même SHA-256, 1.0 — hash posé à l'upload + backfill boot), `same_issuer`/`same_reference` (faits égaux, confiance = min, auto ignoré si correction `user` du même type — miroir de l'affichage). Stockage canonique (`from < to`), verdicts `user` préservés et bloquant toute re-création auto, élagage des auto périmées. `POST /files/:id/relations/verdict` (confirmé → 1.0, infirmé → 0.0 conservé). `version_of` non matérialisé : les versions vivent DANS la ressource (`resource_versions`). `GET /duplicates` (compte connecté) : groupes par hash avec gain récupérable (total − plus gros).
|
||||
- **Versions V3** : chaque overwrite d'octets (upload ciblé) fige l'ancienne génération (`resource_versions`, N=3, élagage auto) ; `POST /files/:id/versions/:n/restore` recopie vers le live (l'actuel est à son tour versionné). Upload ciblé vers une ressource trashée → `NOT_FOUND` (jamais de résurrection d'octets).
|
||||
- **Partage** (`share` / `update_share`, même sémantique d'upsert) : `payload = { "granteeUserId"?: "…32-hex", "granteeGroupId"?: "…32-hex", "access": "viewer"|"commenter"|"editor", "inherit"?: bool, "expiresAt"?: ms }` — **exactement un** principal (user XOR groupe, V2-6). La ressource doit **appartenir** au user appelant (sinon `NOT_FOUND`) ; le grantee user doit exister (sinon `GRANTEE_NOT_FOUND`), le groupe doit exister (sinon `GROUP_NOT_FOUND`) ; accès déjà partagé → mis à jour (idempotent). Privilégier `viewer` < `commenter` < `editor` (< `owner` réservé à l'ownership, non partageable → `INVALID_REQUEST`). Les membres d'un groupe partagé héritent du grant (un niveau, sans imbrication) dans le snapshot comme dans l'enforcement.
|
||||
- `revoke_share` : `payload = { "granteeUserId": "…32-hex" }`. **Idempotent** (déjà révoqué/absent → no-op).
|
||||
- `create_link` : `payload = { "token": "…32-hex", "access": "viewer"|"commenter"|"editor", "expiresAt"?: ms }`. Le token est **généré côté client** (`GenerateId`), c'est l'identifiant du lien (colonne `id` de `share_links`).
|
||||
@@ -169,8 +188,8 @@ type ResourcePermission = {
|
||||
|
||||
## 7. Codes d'erreur courants
|
||||
|
||||
- Authentification : `UNAUTHORIZED` (**401** — token manquant/invalide/expiré, compte supprimé, OU identifiants de login erronés : **indistinguables par design**, même code+message), `INVALID_DEVICE_ID` (**400** — device non enregistré au login).
|
||||
- Ressources : `NOT_FOUND` (404), `NAME_CONFLICT` (409 — même nom dans le même parent, cf. `UNIQUE(parent_id, name)`, **ou à la racine**, index partiel `(user_id, name) WHERE parent_id IS NULL`), `FILE_TOO_LARGE` (413), `INVALID_PASSWORD` (403 sur `PATCH /users/me/password`).
|
||||
- Authentification : `UNAUTHORIZED` (**401** — token manquant/invalide/expiré/**révoqué** (mot de passe changé, device révoqué), compte supprimé, OU identifiants de login erronés : **indistinguables par design**, même code+message), `INVALID_DEVICE_ID` (**400** — device non enregistré au login).
|
||||
- Ressources : `NOT_FOUND` (404), `NO_PREVIEW` (404 — type non prévisualisable : txt, binaire inconnu, PDF sans `pdftoppm`), `NAME_CONFLICT` (409 — même nom dans le même parent, cf. `UNIQUE(parent_id, name)`, **ou à la racine**, index partiel `(user_id, name) WHERE parent_id IS NULL`), `FILE_TOO_LARGE` (413), `QUOTA_EXCEEDED` (413 — créations au-delà de `storage_quota_bytes`, 10 Go par défaut ; overwrites ciblées exemptées, gain net ~nul), `INVALID_PASSWORD` (403 sur `PATCH /users/me/password`).
|
||||
- Partage : `GRANTEE_NOT_FOUND` (404 — grantee inexistant sur une op share), `GROUP_NOT_FOUND` (404 — groupe inexistant sur une op share ou un endpoint membres), `NOT_FOUND` (404 — ressource non possédée sur une op share : scoping, pas d'énumération).
|
||||
- Client-only : `NETWORK_ERROR`, `INVALID_RESPONSE` (2xx mais corps d'enveloppe invalide), `HTTP_<status>` (fallback). Statut `SERVICE_UNAVAILABLE` (503) si le backend n'est pas initialisé.
|
||||
- **V1 finale : toutes les routes sont réelles** (pas de 501 restant).
|
||||
@@ -179,4 +198,4 @@ type ResourcePermission = {
|
||||
|
||||
- `POST /auth/login` : le **device doit exister** (`POST /devices` d'abord, sinon 400 `INVALID_DEVICE_ID`). La vérification du mot de passe est **constant-time** (`argon2id`, comparaison `subtle`) et le délai est **égalisé** entre « username inconnu » et « mauvais mot de passe » (vérification contre un dummy-hash) — les deux produisent exactement la même réponse 401.
|
||||
- `GET /users/resolve` : résolution **exacte** du `username_normalized` uniquement ; ne renvoie **jamais** `email` ni `is_admin` (ni listing, ni préfixe → pas d'énumération de comptes).
|
||||
- `PATCH /users/me/password` : exige `current_password` (mauvais curl → 403). Minimum 8 caractères. **Limite V1** : tokens émis non révoqués (validité 7 j), et `is_admin`/`ADMIN_*` non modifiables par API.
|
||||
- `PATCH /users/me/password` : exige `current_password` (mauvais curl → 403). Minimum 8 caractères. **V3 S6** : bumpe `token_version` (tous les tokens antérieurs révoqués — le client se reconnecte) ; `is_admin`/`ADMIN_*` non modifiables par API.
|
||||
@@ -0,0 +1,766 @@
|
||||
{
|
||||
"formatVersion": 1,
|
||||
"database": {
|
||||
"version": 19,
|
||||
"identityHash": "db3263dbb69c07a043d41f1c83a892a5",
|
||||
"entities": [
|
||||
{
|
||||
"tableName": "folders",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `resource_id` TEXT NOT NULL, `uri` TEXT, `name` TEXT NOT NULL, `exists` INTEGER, `parent_resource_id` TEXT, `owner_id` TEXT, `sync_status` TEXT NOT NULL, `created_in_app` INTEGER NOT NULL DEFAULT 0, `trashed_at` INTEGER, `added_at` INTEGER NOT NULL, `updated_at` INTEGER NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "uri",
|
||||
"columnName": "uri",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "name",
|
||||
"columnName": "name",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "exists",
|
||||
"columnName": "exists",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "parentResourceId",
|
||||
"columnName": "parent_resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "ownerId",
|
||||
"columnName": "owner_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "syncStatus",
|
||||
"columnName": "sync_status",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "createdInApp",
|
||||
"columnName": "created_in_app",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true,
|
||||
"defaultValue": "0"
|
||||
},
|
||||
{
|
||||
"fieldPath": "trashedAt",
|
||||
"columnName": "trashed_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "addedAt",
|
||||
"columnName": "added_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_folders_resource_id",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_folders_resource_id` ON `${TABLE_NAME}` (`resource_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_folders_uri",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"uri"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_folders_uri` ON `${TABLE_NAME}` (`uri`)"
|
||||
},
|
||||
{
|
||||
"name": "index_folders_parent_resource_id",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"parent_resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_folders_parent_resource_id` ON `${TABLE_NAME}` (`parent_resource_id`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "user_preferences",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`key` TEXT NOT NULL, `value` TEXT NOT NULL, `updated_at` INTEGER NOT NULL, PRIMARY KEY(`key`))",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "key",
|
||||
"columnName": "key",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "value",
|
||||
"columnName": "value",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": false,
|
||||
"columnNames": [
|
||||
"key"
|
||||
]
|
||||
},
|
||||
"indices": [],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "files",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `resource_id` TEXT NOT NULL, `uri` TEXT, `name` TEXT NOT NULL, `folder_resource_id` TEXT NOT NULL, `extension` TEXT, `size` INTEGER NOT NULL, `mime_type` TEXT, `exists` INTEGER, `last_modified` INTEGER, `owner_id` TEXT, `category` TEXT, `sync_status` TEXT NOT NULL, `content` TEXT, `processed` INTEGER NOT NULL, `ocr_text` TEXT, `ocr_attempts` INTEGER NOT NULL, `ocr_queued_at` INTEGER, `bytes_uploaded` INTEGER NOT NULL, `bytes_upload_attempts` INTEGER NOT NULL, `has_content_server` INTEGER NOT NULL, `wanted_offline` INTEGER NOT NULL, `trashed_at` INTEGER, `added_at` INTEGER NOT NULL, `updated_at` INTEGER NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "uri",
|
||||
"columnName": "uri",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "name",
|
||||
"columnName": "name",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "folderResourceId",
|
||||
"columnName": "folder_resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "extension",
|
||||
"columnName": "extension",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "size",
|
||||
"columnName": "size",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "mimeType",
|
||||
"columnName": "mime_type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "exists",
|
||||
"columnName": "exists",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "lastModified",
|
||||
"columnName": "last_modified",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "ownerId",
|
||||
"columnName": "owner_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "category",
|
||||
"columnName": "category",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "syncStatus",
|
||||
"columnName": "sync_status",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "content",
|
||||
"columnName": "content",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "processed",
|
||||
"columnName": "processed",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "ocrText",
|
||||
"columnName": "ocr_text",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "ocrAttempts",
|
||||
"columnName": "ocr_attempts",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "ocrQueuedAt",
|
||||
"columnName": "ocr_queued_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "bytesUploaded",
|
||||
"columnName": "bytes_uploaded",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "bytesUploadAttempts",
|
||||
"columnName": "bytes_upload_attempts",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "hasContentServer",
|
||||
"columnName": "has_content_server",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "wantedOffline",
|
||||
"columnName": "wanted_offline",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "trashedAt",
|
||||
"columnName": "trashed_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "addedAt",
|
||||
"columnName": "added_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_files_resource_id",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_files_resource_id` ON `${TABLE_NAME}` (`resource_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_files_uri",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"uri"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_files_uri` ON `${TABLE_NAME}` (`uri`)"
|
||||
},
|
||||
{
|
||||
"name": "index_files_folder_resource_id",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"folder_resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_files_folder_resource_id` ON `${TABLE_NAME}` (`folder_resource_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_files_category",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"category"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_files_category` ON `${TABLE_NAME}` (`category`)"
|
||||
},
|
||||
{
|
||||
"name": "index_files_exists_processed_sync_status",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"exists",
|
||||
"processed",
|
||||
"sync_status"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_files_exists_processed_sync_status` ON `${TABLE_NAME}` (`exists`, `processed`, `sync_status`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "pending_operations",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `device_id` TEXT, `operation_id` TEXT NOT NULL, `ref_type` TEXT, `ref_id` INTEGER, `resource_id` TEXT, `resource_type` TEXT, `operation` TEXT NOT NULL, `payload` TEXT NOT NULL, `parent_resource_id` TEXT, `status` TEXT NOT NULL, `attempts` INTEGER NOT NULL, `created_at` INTEGER NOT NULL, `updated_at` INTEGER NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "deviceId",
|
||||
"columnName": "device_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "operationId",
|
||||
"columnName": "operation_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "refType",
|
||||
"columnName": "ref_type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "refId",
|
||||
"columnName": "ref_id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceType",
|
||||
"columnName": "resource_type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "operation",
|
||||
"columnName": "operation",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "payload",
|
||||
"columnName": "payload",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "parentResourceId",
|
||||
"columnName": "parent_resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "status",
|
||||
"columnName": "status",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "attempts",
|
||||
"columnName": "attempts",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "createdAt",
|
||||
"columnName": "created_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_pending_operations_device_id_operation_id",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"device_id",
|
||||
"operation_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_pending_operations_device_id_operation_id` ON `${TABLE_NAME}` (`device_id`, `operation_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_pending_operations_status",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"status"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_pending_operations_status` ON `${TABLE_NAME}` (`status`)"
|
||||
},
|
||||
{
|
||||
"name": "index_pending_operations_resource_id",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_pending_operations_resource_id` ON `${TABLE_NAME}` (`resource_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_pending_operations_resource_id_operation_status",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"resource_id",
|
||||
"operation",
|
||||
"status"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_pending_operations_resource_id_operation_status` ON `${TABLE_NAME}` (`resource_id`, `operation`, `status`)"
|
||||
},
|
||||
{
|
||||
"name": "index_pending_operations_parent_resource_id",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"parent_resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_pending_operations_parent_resource_id` ON `${TABLE_NAME}` (`parent_resource_id`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "scan_sessions",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `resource_id` TEXT NOT NULL, `root_folder_id` TEXT, `status` TEXT NOT NULL, `created_at` INTEGER NOT NULL, `updated_at` INTEGER NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "rootFolderId",
|
||||
"columnName": "root_folder_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "status",
|
||||
"columnName": "status",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "createdAt",
|
||||
"columnName": "created_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_scan_sessions_resource_id",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_scan_sessions_resource_id` ON `${TABLE_NAME}` (`resource_id`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "scan_pages",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `resource_id` TEXT NOT NULL, `session_id` INTEGER NOT NULL, `temp_uri` TEXT NOT NULL, `corners_json` TEXT NOT NULL, `width` INTEGER NOT NULL, `height` INTEGER NOT NULL, `sort_order` INTEGER NOT NULL, `status` TEXT NOT NULL, `created_at` INTEGER NOT NULL, FOREIGN KEY(`session_id`) REFERENCES `scan_sessions`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "sessionId",
|
||||
"columnName": "session_id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "tempUri",
|
||||
"columnName": "temp_uri",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "cornersJson",
|
||||
"columnName": "corners_json",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "width",
|
||||
"columnName": "width",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "height",
|
||||
"columnName": "height",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "sortOrder",
|
||||
"columnName": "sort_order",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "status",
|
||||
"columnName": "status",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "createdAt",
|
||||
"columnName": "created_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_scan_pages_resource_id",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_scan_pages_resource_id` ON `${TABLE_NAME}` (`resource_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_scan_pages_session_id",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"session_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_scan_pages_session_id` ON `${TABLE_NAME}` (`session_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_scan_pages_sort_order",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"sort_order"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_scan_pages_sort_order` ON `${TABLE_NAME}` (`sort_order`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": [
|
||||
{
|
||||
"table": "scan_sessions",
|
||||
"onDelete": "CASCADE",
|
||||
"onUpdate": "NO ACTION",
|
||||
"columns": [
|
||||
"session_id"
|
||||
],
|
||||
"referencedColumns": [
|
||||
"id"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"tableName": "resource_permissions",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`resource_id` TEXT NOT NULL, `resource_type` TEXT NOT NULL, `effective_access` TEXT NOT NULL, `owner_id` TEXT, `shared_by_id` TEXT, `expires_at` INTEGER, `cached_at` INTEGER NOT NULL, `updated_at` INTEGER NOT NULL, PRIMARY KEY(`resource_id`))",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceType",
|
||||
"columnName": "resource_type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "effectiveAccess",
|
||||
"columnName": "effective_access",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "ownerId",
|
||||
"columnName": "owner_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "sharedById",
|
||||
"columnName": "shared_by_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "expiresAt",
|
||||
"columnName": "expires_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "cachedAt",
|
||||
"columnName": "cached_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": false,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_resource_permissions_expires_at",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"expires_at"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_resource_permissions_expires_at` ON `${TABLE_NAME}` (`expires_at`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
}
|
||||
],
|
||||
"views": [],
|
||||
"setupQueries": [
|
||||
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
|
||||
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'db3263dbb69c07a043d41f1c83a892a5')"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,844 @@
|
||||
{
|
||||
"formatVersion": 1,
|
||||
"database": {
|
||||
"version": 20,
|
||||
"identityHash": "d29a3bf93b4ec7845c123b8b2e73ff7b",
|
||||
"entities": [
|
||||
{
|
||||
"tableName": "folders",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `resource_id` TEXT NOT NULL, `uri` TEXT, `name` TEXT NOT NULL, `exists` INTEGER, `parent_resource_id` TEXT, `owner_id` TEXT, `sync_status` TEXT NOT NULL, `created_in_app` INTEGER NOT NULL DEFAULT 0, `trashed_at` INTEGER, `added_at` INTEGER NOT NULL, `updated_at` INTEGER NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "uri",
|
||||
"columnName": "uri",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "name",
|
||||
"columnName": "name",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "exists",
|
||||
"columnName": "exists",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "parentResourceId",
|
||||
"columnName": "parent_resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "ownerId",
|
||||
"columnName": "owner_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "syncStatus",
|
||||
"columnName": "sync_status",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "createdInApp",
|
||||
"columnName": "created_in_app",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true,
|
||||
"defaultValue": "0"
|
||||
},
|
||||
{
|
||||
"fieldPath": "trashedAt",
|
||||
"columnName": "trashed_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "addedAt",
|
||||
"columnName": "added_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_folders_resource_id",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_folders_resource_id` ON `${TABLE_NAME}` (`resource_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_folders_uri",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"uri"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_folders_uri` ON `${TABLE_NAME}` (`uri`)"
|
||||
},
|
||||
{
|
||||
"name": "index_folders_parent_resource_id",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"parent_resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_folders_parent_resource_id` ON `${TABLE_NAME}` (`parent_resource_id`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "user_preferences",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`key` TEXT NOT NULL, `value` TEXT NOT NULL, `updated_at` INTEGER NOT NULL, PRIMARY KEY(`key`))",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "key",
|
||||
"columnName": "key",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "value",
|
||||
"columnName": "value",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": false,
|
||||
"columnNames": [
|
||||
"key"
|
||||
]
|
||||
},
|
||||
"indices": [],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "files",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `resource_id` TEXT NOT NULL, `uri` TEXT, `name` TEXT NOT NULL, `folder_resource_id` TEXT NOT NULL, `extension` TEXT, `size` INTEGER NOT NULL, `mime_type` TEXT, `exists` INTEGER, `last_modified` INTEGER, `owner_id` TEXT, `category` TEXT, `sync_status` TEXT NOT NULL, `content` TEXT, `processed` INTEGER NOT NULL, `ocr_text` TEXT, `ocr_attempts` INTEGER NOT NULL, `ocr_queued_at` INTEGER, `bytes_uploaded` INTEGER NOT NULL, `bytes_upload_attempts` INTEGER NOT NULL, `has_content_server` INTEGER NOT NULL, `wanted_offline` INTEGER NOT NULL, `trashed_at` INTEGER, `added_at` INTEGER NOT NULL, `updated_at` INTEGER NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "uri",
|
||||
"columnName": "uri",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "name",
|
||||
"columnName": "name",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "folderResourceId",
|
||||
"columnName": "folder_resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "extension",
|
||||
"columnName": "extension",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "size",
|
||||
"columnName": "size",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "mimeType",
|
||||
"columnName": "mime_type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "exists",
|
||||
"columnName": "exists",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "lastModified",
|
||||
"columnName": "last_modified",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "ownerId",
|
||||
"columnName": "owner_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "category",
|
||||
"columnName": "category",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "syncStatus",
|
||||
"columnName": "sync_status",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "content",
|
||||
"columnName": "content",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "processed",
|
||||
"columnName": "processed",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "ocrText",
|
||||
"columnName": "ocr_text",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "ocrAttempts",
|
||||
"columnName": "ocr_attempts",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "ocrQueuedAt",
|
||||
"columnName": "ocr_queued_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "bytesUploaded",
|
||||
"columnName": "bytes_uploaded",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "bytesUploadAttempts",
|
||||
"columnName": "bytes_upload_attempts",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "hasContentServer",
|
||||
"columnName": "has_content_server",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "wantedOffline",
|
||||
"columnName": "wanted_offline",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "trashedAt",
|
||||
"columnName": "trashed_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "addedAt",
|
||||
"columnName": "added_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_files_resource_id",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_files_resource_id` ON `${TABLE_NAME}` (`resource_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_files_uri",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"uri"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_files_uri` ON `${TABLE_NAME}` (`uri`)"
|
||||
},
|
||||
{
|
||||
"name": "index_files_folder_resource_id",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"folder_resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_files_folder_resource_id` ON `${TABLE_NAME}` (`folder_resource_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_files_category",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"category"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_files_category` ON `${TABLE_NAME}` (`category`)"
|
||||
},
|
||||
{
|
||||
"name": "index_files_exists_processed_sync_status",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"exists",
|
||||
"processed",
|
||||
"sync_status"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_files_exists_processed_sync_status` ON `${TABLE_NAME}` (`exists`, `processed`, `sync_status`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "pending_operations",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `device_id` TEXT, `operation_id` TEXT NOT NULL, `ref_type` TEXT, `ref_id` INTEGER, `resource_id` TEXT, `resource_type` TEXT, `operation` TEXT NOT NULL, `payload` TEXT NOT NULL, `parent_resource_id` TEXT, `status` TEXT NOT NULL, `attempts` INTEGER NOT NULL, `created_at` INTEGER NOT NULL, `updated_at` INTEGER NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "deviceId",
|
||||
"columnName": "device_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "operationId",
|
||||
"columnName": "operation_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "refType",
|
||||
"columnName": "ref_type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "refId",
|
||||
"columnName": "ref_id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceType",
|
||||
"columnName": "resource_type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "operation",
|
||||
"columnName": "operation",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "payload",
|
||||
"columnName": "payload",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "parentResourceId",
|
||||
"columnName": "parent_resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "status",
|
||||
"columnName": "status",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "attempts",
|
||||
"columnName": "attempts",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "createdAt",
|
||||
"columnName": "created_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_pending_operations_device_id_operation_id",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"device_id",
|
||||
"operation_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_pending_operations_device_id_operation_id` ON `${TABLE_NAME}` (`device_id`, `operation_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_pending_operations_status",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"status"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_pending_operations_status` ON `${TABLE_NAME}` (`status`)"
|
||||
},
|
||||
{
|
||||
"name": "index_pending_operations_resource_id",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_pending_operations_resource_id` ON `${TABLE_NAME}` (`resource_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_pending_operations_resource_id_operation_status",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"resource_id",
|
||||
"operation",
|
||||
"status"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_pending_operations_resource_id_operation_status` ON `${TABLE_NAME}` (`resource_id`, `operation`, `status`)"
|
||||
},
|
||||
{
|
||||
"name": "index_pending_operations_parent_resource_id",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"parent_resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_pending_operations_parent_resource_id` ON `${TABLE_NAME}` (`parent_resource_id`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "scan_sessions",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `resource_id` TEXT NOT NULL, `root_folder_id` TEXT, `status` TEXT NOT NULL, `created_at` INTEGER NOT NULL, `updated_at` INTEGER NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "rootFolderId",
|
||||
"columnName": "root_folder_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "status",
|
||||
"columnName": "status",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "createdAt",
|
||||
"columnName": "created_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_scan_sessions_resource_id",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_scan_sessions_resource_id` ON `${TABLE_NAME}` (`resource_id`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "scan_pages",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `resource_id` TEXT NOT NULL, `session_id` INTEGER NOT NULL, `temp_uri` TEXT NOT NULL, `corners_json` TEXT NOT NULL, `width` INTEGER NOT NULL, `height` INTEGER NOT NULL, `sort_order` INTEGER NOT NULL, `status` TEXT NOT NULL, `created_at` INTEGER NOT NULL, FOREIGN KEY(`session_id`) REFERENCES `scan_sessions`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "sessionId",
|
||||
"columnName": "session_id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "tempUri",
|
||||
"columnName": "temp_uri",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "cornersJson",
|
||||
"columnName": "corners_json",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "width",
|
||||
"columnName": "width",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "height",
|
||||
"columnName": "height",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "sortOrder",
|
||||
"columnName": "sort_order",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "status",
|
||||
"columnName": "status",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "createdAt",
|
||||
"columnName": "created_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_scan_pages_resource_id",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_scan_pages_resource_id` ON `${TABLE_NAME}` (`resource_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_scan_pages_session_id",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"session_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_scan_pages_session_id` ON `${TABLE_NAME}` (`session_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_scan_pages_sort_order",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"sort_order"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_scan_pages_sort_order` ON `${TABLE_NAME}` (`sort_order`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": [
|
||||
{
|
||||
"table": "scan_sessions",
|
||||
"onDelete": "CASCADE",
|
||||
"onUpdate": "NO ACTION",
|
||||
"columns": [
|
||||
"session_id"
|
||||
],
|
||||
"referencedColumns": [
|
||||
"id"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"tableName": "resource_permissions",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`resource_id` TEXT NOT NULL, `resource_type` TEXT NOT NULL, `effective_access` TEXT NOT NULL, `owner_id` TEXT, `shared_by_id` TEXT, `expires_at` INTEGER, `cached_at` INTEGER NOT NULL, `updated_at` INTEGER NOT NULL, PRIMARY KEY(`resource_id`))",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceType",
|
||||
"columnName": "resource_type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "effectiveAccess",
|
||||
"columnName": "effective_access",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "ownerId",
|
||||
"columnName": "owner_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "sharedById",
|
||||
"columnName": "shared_by_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "expiresAt",
|
||||
"columnName": "expires_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "cachedAt",
|
||||
"columnName": "cached_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": false,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_resource_permissions_expires_at",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"expires_at"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_resource_permissions_expires_at` ON `${TABLE_NAME}` (`expires_at`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "resource_facts",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `resource_id` TEXT NOT NULL, `fact_type` TEXT NOT NULL, `value_text` TEXT, `value_num` REAL, `value_date` TEXT, `confidence` REAL NOT NULL, `origin` TEXT NOT NULL, `updated_at` INTEGER NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "factType",
|
||||
"columnName": "fact_type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "valueText",
|
||||
"columnName": "value_text",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "valueNum",
|
||||
"columnName": "value_num",
|
||||
"affinity": "REAL",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "valueDate",
|
||||
"columnName": "value_date",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "confidence",
|
||||
"columnName": "confidence",
|
||||
"affinity": "REAL",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "origin",
|
||||
"columnName": "origin",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_resource_facts_resource_id",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_resource_facts_resource_id` ON `${TABLE_NAME}` (`resource_id`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
}
|
||||
],
|
||||
"views": [],
|
||||
"setupQueries": [
|
||||
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
|
||||
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'd29a3bf93b4ec7845c123b8b2e73ff7b')"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,882 @@
|
||||
{
|
||||
"formatVersion": 1,
|
||||
"database": {
|
||||
"version": 21,
|
||||
"identityHash": "ad6fcc232dbddec3aa912be2b3dfcb4c",
|
||||
"entities": [
|
||||
{
|
||||
"tableName": "folders",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `resource_id` TEXT NOT NULL, `uri` TEXT, `name` TEXT NOT NULL, `exists` INTEGER, `parent_resource_id` TEXT, `owner_id` TEXT, `sync_status` TEXT NOT NULL, `created_in_app` INTEGER NOT NULL DEFAULT 0, `trashed_at` INTEGER, `added_at` INTEGER NOT NULL, `updated_at` INTEGER NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "uri",
|
||||
"columnName": "uri",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "name",
|
||||
"columnName": "name",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "exists",
|
||||
"columnName": "exists",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "parentResourceId",
|
||||
"columnName": "parent_resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "ownerId",
|
||||
"columnName": "owner_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "syncStatus",
|
||||
"columnName": "sync_status",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "createdInApp",
|
||||
"columnName": "created_in_app",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true,
|
||||
"defaultValue": "0"
|
||||
},
|
||||
{
|
||||
"fieldPath": "trashedAt",
|
||||
"columnName": "trashed_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "addedAt",
|
||||
"columnName": "added_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_folders_resource_id",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_folders_resource_id` ON `${TABLE_NAME}` (`resource_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_folders_uri",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"uri"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_folders_uri` ON `${TABLE_NAME}` (`uri`)"
|
||||
},
|
||||
{
|
||||
"name": "index_folders_parent_resource_id",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"parent_resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_folders_parent_resource_id` ON `${TABLE_NAME}` (`parent_resource_id`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "user_preferences",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`key` TEXT NOT NULL, `value` TEXT NOT NULL, `updated_at` INTEGER NOT NULL, PRIMARY KEY(`key`))",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "key",
|
||||
"columnName": "key",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "value",
|
||||
"columnName": "value",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": false,
|
||||
"columnNames": [
|
||||
"key"
|
||||
]
|
||||
},
|
||||
"indices": [],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "files",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `resource_id` TEXT NOT NULL, `uri` TEXT, `name` TEXT NOT NULL, `folder_resource_id` TEXT NOT NULL, `extension` TEXT, `size` INTEGER NOT NULL, `mime_type` TEXT, `exists` INTEGER, `last_modified` INTEGER, `owner_id` TEXT, `category` TEXT, `sync_status` TEXT NOT NULL, `content` TEXT, `processed` INTEGER NOT NULL, `ocr_text` TEXT, `ocr_attempts` INTEGER NOT NULL, `ocr_queued_at` INTEGER, `bytes_uploaded` INTEGER NOT NULL, `bytes_upload_attempts` INTEGER NOT NULL, `has_content_server` INTEGER NOT NULL, `wanted_offline` INTEGER NOT NULL, `trashed_at` INTEGER, `added_at` INTEGER NOT NULL, `updated_at` INTEGER NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "uri",
|
||||
"columnName": "uri",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "name",
|
||||
"columnName": "name",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "folderResourceId",
|
||||
"columnName": "folder_resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "extension",
|
||||
"columnName": "extension",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "size",
|
||||
"columnName": "size",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "mimeType",
|
||||
"columnName": "mime_type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "exists",
|
||||
"columnName": "exists",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "lastModified",
|
||||
"columnName": "last_modified",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "ownerId",
|
||||
"columnName": "owner_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "category",
|
||||
"columnName": "category",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "syncStatus",
|
||||
"columnName": "sync_status",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "content",
|
||||
"columnName": "content",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "processed",
|
||||
"columnName": "processed",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "ocrText",
|
||||
"columnName": "ocr_text",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "ocrAttempts",
|
||||
"columnName": "ocr_attempts",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "ocrQueuedAt",
|
||||
"columnName": "ocr_queued_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "bytesUploaded",
|
||||
"columnName": "bytes_uploaded",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "bytesUploadAttempts",
|
||||
"columnName": "bytes_upload_attempts",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "hasContentServer",
|
||||
"columnName": "has_content_server",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "wantedOffline",
|
||||
"columnName": "wanted_offline",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "trashedAt",
|
||||
"columnName": "trashed_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "addedAt",
|
||||
"columnName": "added_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_files_resource_id",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_files_resource_id` ON `${TABLE_NAME}` (`resource_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_files_uri",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"uri"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_files_uri` ON `${TABLE_NAME}` (`uri`)"
|
||||
},
|
||||
{
|
||||
"name": "index_files_folder_resource_id",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"folder_resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_files_folder_resource_id` ON `${TABLE_NAME}` (`folder_resource_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_files_category",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"category"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_files_category` ON `${TABLE_NAME}` (`category`)"
|
||||
},
|
||||
{
|
||||
"name": "index_files_exists_processed_sync_status",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"exists",
|
||||
"processed",
|
||||
"sync_status"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_files_exists_processed_sync_status` ON `${TABLE_NAME}` (`exists`, `processed`, `sync_status`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "pending_operations",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `device_id` TEXT, `operation_id` TEXT NOT NULL, `ref_type` TEXT, `ref_id` INTEGER, `resource_id` TEXT, `resource_type` TEXT, `operation` TEXT NOT NULL, `payload` TEXT NOT NULL, `parent_resource_id` TEXT, `status` TEXT NOT NULL, `attempts` INTEGER NOT NULL, `created_at` INTEGER NOT NULL, `updated_at` INTEGER NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "deviceId",
|
||||
"columnName": "device_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "operationId",
|
||||
"columnName": "operation_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "refType",
|
||||
"columnName": "ref_type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "refId",
|
||||
"columnName": "ref_id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceType",
|
||||
"columnName": "resource_type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "operation",
|
||||
"columnName": "operation",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "payload",
|
||||
"columnName": "payload",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "parentResourceId",
|
||||
"columnName": "parent_resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "status",
|
||||
"columnName": "status",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "attempts",
|
||||
"columnName": "attempts",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "createdAt",
|
||||
"columnName": "created_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_pending_operations_device_id_operation_id",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"device_id",
|
||||
"operation_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_pending_operations_device_id_operation_id` ON `${TABLE_NAME}` (`device_id`, `operation_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_pending_operations_status",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"status"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_pending_operations_status` ON `${TABLE_NAME}` (`status`)"
|
||||
},
|
||||
{
|
||||
"name": "index_pending_operations_resource_id",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_pending_operations_resource_id` ON `${TABLE_NAME}` (`resource_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_pending_operations_resource_id_operation_status",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"resource_id",
|
||||
"operation",
|
||||
"status"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_pending_operations_resource_id_operation_status` ON `${TABLE_NAME}` (`resource_id`, `operation`, `status`)"
|
||||
},
|
||||
{
|
||||
"name": "index_pending_operations_parent_resource_id",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"parent_resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_pending_operations_parent_resource_id` ON `${TABLE_NAME}` (`parent_resource_id`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "scan_sessions",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `resource_id` TEXT NOT NULL, `root_folder_id` TEXT, `status` TEXT NOT NULL, `created_at` INTEGER NOT NULL, `updated_at` INTEGER NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "rootFolderId",
|
||||
"columnName": "root_folder_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "status",
|
||||
"columnName": "status",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "createdAt",
|
||||
"columnName": "created_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_scan_sessions_resource_id",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_scan_sessions_resource_id` ON `${TABLE_NAME}` (`resource_id`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "scan_pages",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `resource_id` TEXT NOT NULL, `session_id` INTEGER NOT NULL, `temp_uri` TEXT NOT NULL, `corners_json` TEXT NOT NULL, `width` INTEGER NOT NULL, `height` INTEGER NOT NULL, `sort_order` INTEGER NOT NULL, `status` TEXT NOT NULL, `created_at` INTEGER NOT NULL, FOREIGN KEY(`session_id`) REFERENCES `scan_sessions`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "sessionId",
|
||||
"columnName": "session_id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "tempUri",
|
||||
"columnName": "temp_uri",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "cornersJson",
|
||||
"columnName": "corners_json",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "width",
|
||||
"columnName": "width",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "height",
|
||||
"columnName": "height",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "sortOrder",
|
||||
"columnName": "sort_order",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "status",
|
||||
"columnName": "status",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "createdAt",
|
||||
"columnName": "created_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_scan_pages_resource_id",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_scan_pages_resource_id` ON `${TABLE_NAME}` (`resource_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_scan_pages_session_id",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"session_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_scan_pages_session_id` ON `${TABLE_NAME}` (`session_id`)"
|
||||
},
|
||||
{
|
||||
"name": "index_scan_pages_sort_order",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"sort_order"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_scan_pages_sort_order` ON `${TABLE_NAME}` (`sort_order`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": [
|
||||
{
|
||||
"table": "scan_sessions",
|
||||
"onDelete": "CASCADE",
|
||||
"onUpdate": "NO ACTION",
|
||||
"columns": [
|
||||
"session_id"
|
||||
],
|
||||
"referencedColumns": [
|
||||
"id"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"tableName": "resource_permissions",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`resource_id` TEXT NOT NULL, `resource_type` TEXT NOT NULL, `effective_access` TEXT NOT NULL, `owner_id` TEXT, `shared_by_id` TEXT, `expires_at` INTEGER, `cached_at` INTEGER NOT NULL, `updated_at` INTEGER NOT NULL, PRIMARY KEY(`resource_id`))",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceType",
|
||||
"columnName": "resource_type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "effectiveAccess",
|
||||
"columnName": "effective_access",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "ownerId",
|
||||
"columnName": "owner_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "sharedById",
|
||||
"columnName": "shared_by_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "expiresAt",
|
||||
"columnName": "expires_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "cachedAt",
|
||||
"columnName": "cached_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": false,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_resource_permissions_expires_at",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"expires_at"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_resource_permissions_expires_at` ON `${TABLE_NAME}` (`expires_at`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "resource_facts",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `resource_id` TEXT NOT NULL, `fact_type` TEXT NOT NULL, `value_text` TEXT, `value_num` REAL, `value_date` TEXT, `confidence` REAL NOT NULL, `origin` TEXT NOT NULL, `updated_at` INTEGER NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "resourceId",
|
||||
"columnName": "resource_id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "factType",
|
||||
"columnName": "fact_type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "valueText",
|
||||
"columnName": "value_text",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "valueNum",
|
||||
"columnName": "value_num",
|
||||
"affinity": "REAL",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "valueDate",
|
||||
"columnName": "value_date",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "confidence",
|
||||
"columnName": "confidence",
|
||||
"affinity": "REAL",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "origin",
|
||||
"columnName": "origin",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "updatedAt",
|
||||
"columnName": "updated_at",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_resource_facts_resource_id",
|
||||
"unique": false,
|
||||
"columnNames": [
|
||||
"resource_id"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE INDEX IF NOT EXISTS `index_resource_facts_resource_id` ON `${TABLE_NAME}` (`resource_id`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"ftsVersion": "FTS4",
|
||||
"ftsOptions": {
|
||||
"tokenizer": "simple",
|
||||
"tokenizerArgs": [],
|
||||
"contentTable": "",
|
||||
"languageIdColumnName": "",
|
||||
"matchInfo": "FTS4",
|
||||
"notIndexedColumns": [],
|
||||
"prefixSizes": [],
|
||||
"preferredOrder": "ASC"
|
||||
},
|
||||
"contentSyncTriggers": [],
|
||||
"tableName": "files_fts",
|
||||
"createSql": "CREATE VIRTUAL TABLE IF NOT EXISTS `${TABLE_NAME}` USING FTS4(`name` TEXT NOT NULL, `body` TEXT NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "name",
|
||||
"columnName": "name",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "body",
|
||||
"columnName": "body",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"rowid"
|
||||
]
|
||||
},
|
||||
"indices": [],
|
||||
"foreignKeys": []
|
||||
}
|
||||
],
|
||||
"views": [],
|
||||
"setupQueries": [
|
||||
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
|
||||
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'ad6fcc232dbddec3aa912be2b3dfcb4c')"
|
||||
]
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -33,6 +33,18 @@
|
||||
tools:node="remove" />
|
||||
</provider>
|
||||
|
||||
<!-- Partage de l'export chiffré (V3 S7) : FileProvider sur le cache
|
||||
(jamais d'URI file:// vers une autre app). -->
|
||||
<provider
|
||||
android:name="androidx.core.content.FileProvider"
|
||||
android:authorities="${applicationId}.fileprovider"
|
||||
android:exported="false"
|
||||
android:grantUriPermissions="true">
|
||||
<meta-data
|
||||
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||
android:resource="@xml/file_paths" />
|
||||
</provider>
|
||||
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true"
|
||||
|
||||
@@ -3,14 +3,21 @@ package com.kazier.mobile.data.local
|
||||
import androidx.room.Database
|
||||
import androidx.room.RoomDatabase
|
||||
import com.kazier.mobile.data.local.dao.FileDao
|
||||
import com.kazier.mobile.data.local.dao.FactsDao
|
||||
import com.kazier.mobile.data.local.dao.FolderDao
|
||||
import com.kazier.mobile.data.local.dao.LinksDao
|
||||
import com.kazier.mobile.data.local.dao.CollectionDao
|
||||
import com.kazier.mobile.data.local.dao.PendingOperationDao
|
||||
import com.kazier.mobile.data.local.dao.ResourcePermissionDao
|
||||
import com.kazier.mobile.data.local.dao.ScanDao
|
||||
import com.kazier.mobile.data.local.dao.UserPreferenceDao
|
||||
import com.kazier.mobile.data.local.entity.FileEntity
|
||||
import com.kazier.mobile.data.local.entity.FilesFts
|
||||
import com.kazier.mobile.data.local.entity.FolderEntity
|
||||
import com.kazier.mobile.data.local.entity.CollectionEntity
|
||||
import com.kazier.mobile.data.local.entity.PendingOperationEntity
|
||||
import com.kazier.mobile.data.local.entity.ResourceFact
|
||||
import com.kazier.mobile.data.local.entity.ResourceLink
|
||||
import com.kazier.mobile.data.local.entity.ResourcePermissionEntity
|
||||
import com.kazier.mobile.data.local.entity.ScanPageEntity
|
||||
import com.kazier.mobile.data.local.entity.ScanSessionEntity
|
||||
@@ -34,14 +41,24 @@ import com.kazier.mobile.data.local.entity.UserPreferenceEntity
|
||||
* v16: table resource_permissions (cache snapshot, M27).
|
||||
* v17: bytes_uploaded + bytes_upload_attempts sur files (upload V2).
|
||||
* v18: has_content_server + wanted_offline sur files (download V2).
|
||||
* v19: trashed_at sur files + folders (corbeille V3 — tombstone exists=0,
|
||||
* restore exists=1, purge locale après 30j+).
|
||||
* v20: table resource_facts (faits génériques V3 — miroir GET /facts,
|
||||
* remplacement en bloc au sync, corrections `user` poussées en outbox).
|
||||
* v21: table virtuelle FTS4 `files_fts` (nom + corps ocr+faits, entité
|
||||
* `FilesFts` déclarée pour KSP) + triggers de maintien (`FtsTriggers` :
|
||||
* migration + callback fresh-install) — recherche plein-texte offline S4.
|
||||
* v22: `resource_links` (miroir `GET /relations`, remplacement en bloc) +
|
||||
* `collections` (requêtes sauvegardées locales, jamais syncées).
|
||||
*/
|
||||
@Database(
|
||||
entities = [
|
||||
FolderEntity::class, UserPreferenceEntity::class, FileEntity::class,
|
||||
PendingOperationEntity::class, ScanSessionEntity::class, ScanPageEntity::class,
|
||||
ResourcePermissionEntity::class,
|
||||
ResourcePermissionEntity::class, ResourceFact::class, FilesFts::class,
|
||||
ResourceLink::class, CollectionEntity::class,
|
||||
],
|
||||
version = 18,
|
||||
version = 22,
|
||||
// m19 : schéma exporté (`app/schemas/`) — toute dérive non migrée casse
|
||||
// la compilation au lieu de briqueter les utilisateurs au démarrage.
|
||||
exportSchema = true,
|
||||
@@ -49,6 +66,9 @@ import com.kazier.mobile.data.local.entity.UserPreferenceEntity
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
|
||||
abstract fun folderDao(): FolderDao
|
||||
abstract fun factsDao(): FactsDao
|
||||
abstract fun linksDao(): LinksDao
|
||||
abstract fun collectionDao(): CollectionDao
|
||||
abstract fun userPreferenceDao(): UserPreferenceDao
|
||||
abstract fun fileDao(): FileDao
|
||||
abstract fun pendingOperationDao(): PendingOperationDao
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.kazier.mobile.data.local.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import com.kazier.mobile.data.local.entity.CollectionEntity
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface CollectionDao {
|
||||
|
||||
@Query("SELECT * FROM collections ORDER BY created_at DESC")
|
||||
fun observeAll(): Flow<List<CollectionEntity>>
|
||||
|
||||
@Query("SELECT * FROM collections WHERE id = :id LIMIT 1")
|
||||
suspend fun getById(id: Long): CollectionEntity?
|
||||
|
||||
@Insert
|
||||
suspend fun insert(collection: CollectionEntity): Long
|
||||
|
||||
@Query("DELETE FROM collections WHERE id = :id")
|
||||
suspend fun deleteById(id: Long)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.kazier.mobile.data.local.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import com.kazier.mobile.data.local.entity.ResourceFact
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface FactsDao {
|
||||
|
||||
@Query("SELECT * FROM resource_facts WHERE resource_id = :resourceId ORDER BY fact_type ASC, value_text ASC")
|
||||
fun observeForResource(resourceId: String): Flow<List<ResourceFact>>
|
||||
|
||||
@Query("SELECT * FROM resource_facts WHERE resource_id = :resourceId ORDER BY fact_type ASC, value_text ASC")
|
||||
suspend fun getForResource(resourceId: String): List<ResourceFact>
|
||||
|
||||
@Insert
|
||||
suspend fun insertAll(facts: List<ResourceFact>)
|
||||
|
||||
/** Remplacement en bloc (sync serveur, source de vérité) — voir repository. */
|
||||
@Query("DELETE FROM resource_facts WHERE resource_id = :resourceId")
|
||||
suspend fun deleteForResource(resourceId: String)
|
||||
|
||||
/** Efface les corrections locales (reset « rétablir auto », S3). */
|
||||
@Query("DELETE FROM resource_facts WHERE resource_id = :resourceId AND origin = 'user'")
|
||||
suspend fun deleteUserFacts(resourceId: String)
|
||||
}
|
||||
@@ -69,6 +69,108 @@ interface FileDao {
|
||||
""")
|
||||
fun searchWithFilters(query: String?, normQuery: String?, category: String?): Flow<List<FileEntity>>
|
||||
|
||||
/**
|
||||
* Recherche documentaire V3 (S4) : plein-texte FTS (`files_fts`, nom +
|
||||
* corps ocr+faits) OU nom (LIKE + accents, même branche que
|
||||
* [searchWithFilters]), restreint par catégorie + facettes faits
|
||||
* (émetteur, type suggéré, montant, période). Rang : nom exact > fait
|
||||
* sûr (confiance ≥ 0.5) > fait douteux (jamais exclu) > nom.
|
||||
*
|
||||
* `:match` = expression FTS (`"tok" OR …`, construite côté repository,
|
||||
* guillemets échappés) ; `:w1..:w5` = mots (≥ 3 lettres, échappés LIKE)
|
||||
* pour le rang et les facettes ; NULL = absent.
|
||||
*/
|
||||
@Query("""
|
||||
SELECT files.* FROM files
|
||||
WHERE files."exists" = 1
|
||||
AND (
|
||||
files.id IN (SELECT rowid FROM files_fts WHERE files_fts MATCH :match)
|
||||
OR files.name COLLATE NOCASE LIKE '%' || :query || '%' ESCAPE '\'
|
||||
OR REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
|
||||
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
|
||||
lower(files.name),
|
||||
'œ','oe'),'æ','ae'),'ç','c'),'à','a'),'â','a'),'ä','a'),
|
||||
'é','e'),'è','e'),'ê','e'),'ë','e'),'î','i'),'ï','i'),
|
||||
'ô','o'),'ö','o'),'ù','u'),'û','u'),'ü','u'),'ÿ','y')
|
||||
LIKE '%' || :normQuery || '%' ESCAPE '\'
|
||||
)
|
||||
AND (:category IS NULL OR files.category = :category)
|
||||
AND (:issuer IS NULL OR EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.fact_type = 'issuer' AND f.value_text LIKE '%' || :issuer || '%' ESCAPE '\'))
|
||||
AND (:docType IS NULL OR EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.fact_type = 'doc_type' AND f.value_text = :docType))
|
||||
AND (:minAmount IS NULL OR EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.fact_type = 'amount' AND f.value_num >= :minAmount))
|
||||
AND (:maxAmount IS NULL OR EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.fact_type = 'amount' AND f.value_num <= :maxAmount))
|
||||
AND ((:fromDate IS NULL AND :toDate IS NULL) OR EXISTS (
|
||||
SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND (
|
||||
(f.fact_type IN ('date', 'due_date') AND (:fromDate IS NULL OR f.value_date >= :fromDate) AND (:toDate IS NULL OR f.value_date <= :toDate))
|
||||
OR (f.fact_type = 'period' AND (:fromDate IS NULL OR f.value_text >= substr(:fromDate, 1, 7)) AND (:toDate IS NULL OR f.value_text <= substr(:toDate, 1, 7)))
|
||||
)))
|
||||
ORDER BY
|
||||
CASE WHEN files.name COLLATE NOCASE LIKE '%' || :query || '%' ESCAPE '\' THEN 0 ELSE 1 END,
|
||||
CASE WHEN (:w1 IS NOT NULL AND EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.confidence >= 0.5 AND f.value_text LIKE '%' || :w1 || '%' ESCAPE '\'))
|
||||
OR (:w2 IS NOT NULL AND EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.confidence >= 0.5 AND f.value_text LIKE '%' || :w2 || '%' ESCAPE '\'))
|
||||
OR (:w3 IS NOT NULL AND EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.confidence >= 0.5 AND f.value_text LIKE '%' || :w3 || '%' ESCAPE '\'))
|
||||
OR (:w4 IS NOT NULL AND EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.confidence >= 0.5 AND f.value_text LIKE '%' || :w4 || '%' ESCAPE '\'))
|
||||
OR (:w5 IS NOT NULL AND EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.confidence >= 0.5 AND f.value_text LIKE '%' || :w5 || '%' ESCAPE '\'))
|
||||
THEN 0
|
||||
WHEN (:w1 IS NOT NULL AND EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.value_text LIKE '%' || :w1 || '%' ESCAPE '\'))
|
||||
OR (:w2 IS NOT NULL AND EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.value_text LIKE '%' || :w2 || '%' ESCAPE '\'))
|
||||
OR (:w3 IS NOT NULL AND EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.value_text LIKE '%' || :w3 || '%' ESCAPE '\'))
|
||||
OR (:w4 IS NOT NULL AND EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.value_text LIKE '%' || :w4 || '%' ESCAPE '\'))
|
||||
OR (:w5 IS NOT NULL AND EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.value_text LIKE '%' || :w5 || '%' ESCAPE '\'))
|
||||
THEN 1 ELSE 2 END,
|
||||
files.name ASC
|
||||
""")
|
||||
fun searchDocuments(
|
||||
match: String,
|
||||
query: String,
|
||||
normQuery: String,
|
||||
category: String?,
|
||||
issuer: String?,
|
||||
docType: String?,
|
||||
minAmount: Double?,
|
||||
maxAmount: Double?,
|
||||
fromDate: String?,
|
||||
toDate: String?,
|
||||
w1: String?,
|
||||
w2: String?,
|
||||
w3: String?,
|
||||
w4: String?,
|
||||
w5: String?,
|
||||
): Flow<List<FileEntity>>
|
||||
|
||||
/**
|
||||
* Variante sans texte (facettes seules) : même contrat que
|
||||
* [searchDocuments] sans la branche FTS/nom.
|
||||
*/
|
||||
@Query("""
|
||||
SELECT files.* FROM files
|
||||
WHERE files."exists" = 1
|
||||
AND (:category IS NULL OR files.category = :category)
|
||||
AND (:issuer IS NULL OR EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.fact_type = 'issuer' AND f.value_text LIKE '%' || :issuer || '%' ESCAPE '\'))
|
||||
AND (:docType IS NULL OR EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.fact_type = 'doc_type' AND f.value_text = :docType))
|
||||
AND (:minAmount IS NULL OR EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.fact_type = 'amount' AND f.value_num >= :minAmount))
|
||||
AND (:maxAmount IS NULL OR EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND f.fact_type = 'amount' AND f.value_num <= :maxAmount))
|
||||
AND ((:fromDate IS NULL AND :toDate IS NULL) OR EXISTS (
|
||||
SELECT 1 FROM resource_facts f WHERE f.resource_id = files.resource_id AND (
|
||||
(f.fact_type IN ('date', 'due_date') AND (:fromDate IS NULL OR f.value_date >= :fromDate) AND (:toDate IS NULL OR f.value_date <= :toDate))
|
||||
OR (f.fact_type = 'period' AND (:fromDate IS NULL OR f.value_text >= substr(:fromDate, 1, 7)) AND (:toDate IS NULL OR f.value_text <= substr(:toDate, 1, 7)))
|
||||
)))
|
||||
ORDER BY files.name ASC
|
||||
""")
|
||||
fun searchFacetsOnly(
|
||||
category: String?,
|
||||
issuer: String?,
|
||||
docType: String?,
|
||||
minAmount: Double?,
|
||||
maxAmount: Double?,
|
||||
fromDate: String?,
|
||||
toDate: String?,
|
||||
): Flow<List<FileEntity>>
|
||||
|
||||
/** Types suggérés distincts (chips de filtre UI). */
|
||||
@Query("SELECT DISTINCT value_text FROM resource_facts WHERE fact_type = 'doc_type' AND value_text IS NOT NULL ORDER BY 1")
|
||||
fun observeDistinctDocTypes(): Flow<List<String>>
|
||||
|
||||
@Query("""
|
||||
SELECT * FROM files
|
||||
WHERE "exists" = 1
|
||||
@@ -97,6 +199,43 @@ interface FileDao {
|
||||
@Query("UPDATE files SET \"exists\" = 0, updated_at = :updatedAt WHERE resource_id = :resourceId")
|
||||
suspend fun markMissing(resourceId: String, updatedAt: Long)
|
||||
|
||||
/**
|
||||
* V3 corbeille : lignes trashées, les plus récentes d'abord (écran
|
||||
* Corbeille). Le masquage de l'UI passe par `exists = 0` (requêtes
|
||||
* `exists = 1` inchangées) ; `trashed_at` date la purge locale.
|
||||
*/
|
||||
@Query("SELECT * FROM files WHERE trashed_at IS NOT NULL ORDER BY trashed_at DESC")
|
||||
fun observeTrashed(): Flow<List<FileEntity>>
|
||||
|
||||
@Query("SELECT * FROM files WHERE trashed_at IS NOT NULL ORDER BY trashed_at DESC")
|
||||
suspend fun getTrashedOnce(): List<FileEntity>
|
||||
|
||||
@Query("SELECT COUNT(*) FROM files WHERE trashed_at IS NOT NULL")
|
||||
fun observeTrashedCount(): Flow<Int>
|
||||
|
||||
/** Descendants sans filtre `exists` — collecte trash/restore (Deleters). */
|
||||
@Query("SELECT * FROM files WHERE folder_resource_id = :folderResourceId ORDER BY name ASC")
|
||||
suspend fun getByFolderAll(folderResourceId: String): List<FileEntity>
|
||||
|
||||
/** Trash : tombstone `exists = 0` + timestamp (jamais de DELETE Room). */
|
||||
@Query("UPDATE files SET trashed_at = :now, \"exists\" = 0, updated_at = :now WHERE resource_id = :resourceId")
|
||||
suspend fun markTrashed(resourceId: String, now: Long)
|
||||
|
||||
/**
|
||||
* Restore : lève le flag, re-visible (`exists = 1` — cloud-only si `uri`
|
||||
* NULL, le walk re-marque si le physique a vraiment disparu).
|
||||
*/
|
||||
@Query("UPDATE files SET trashed_at = NULL, \"exists\" = 1, updated_at = :now WHERE resource_id = :resourceId")
|
||||
suspend fun clearTrashed(resourceId: String, now: Long)
|
||||
|
||||
/**
|
||||
* Purge locale miroir de la rétention serveur (corbeille 30j + marge) :
|
||||
* destruction Room des lignes trashées depuis plus de `cutoff`. Le
|
||||
* serveur purge de son côté ; sans ça les tombstones s'accumuleraient.
|
||||
*/
|
||||
@Query("DELETE FROM files WHERE trashed_at IS NOT NULL AND trashed_at < :cutoff")
|
||||
suspend fun deleteTrashedOlderThan(cutoff: Long): Int
|
||||
|
||||
/**
|
||||
* Promote le placement après confirmation serveur (`POST /sync/ops` appliqué) :
|
||||
* `local-cloud` si une copie physique existe, sinon `cloud`. Idempotent.
|
||||
|
||||
@@ -86,6 +86,29 @@ interface FolderDao {
|
||||
@Query("UPDATE folders SET \"exists\" = 0, updated_at = :updatedAt WHERE resource_id = :resourceId")
|
||||
suspend fun markMissing(resourceId: String, updatedAt: Long)
|
||||
|
||||
/** V3 corbeille : voir `FileDao.observeTrashed` — même sémantique. */
|
||||
@Query("SELECT * FROM folders WHERE trashed_at IS NOT NULL ORDER BY trashed_at DESC")
|
||||
fun observeTrashed(): Flow<List<FolderEntity>>
|
||||
|
||||
@Query("SELECT * FROM folders WHERE trashed_at IS NOT NULL ORDER BY trashed_at DESC")
|
||||
suspend fun getTrashedOnce(): List<FolderEntity>
|
||||
|
||||
/** Enfants sans filtre `exists` — collecte trash/restore (FolderDeleter). */
|
||||
@Query("SELECT * FROM folders WHERE parent_resource_id = :parentResourceId ORDER BY name ASC")
|
||||
suspend fun getChildrenAll(parentResourceId: String): List<FolderEntity>
|
||||
|
||||
/** V3 corbeille : voir `FileDao.markTrashed`. */
|
||||
@Query("UPDATE folders SET trashed_at = :now, \"exists\" = 0, updated_at = :now WHERE resource_id = :resourceId")
|
||||
suspend fun markTrashed(resourceId: String, now: Long)
|
||||
|
||||
/** V3 corbeille : voir `FileDao.clearTrashed`. */
|
||||
@Query("UPDATE folders SET trashed_at = NULL, \"exists\" = 1, updated_at = :now WHERE resource_id = :resourceId")
|
||||
suspend fun clearTrashed(resourceId: String, now: Long)
|
||||
|
||||
/** V3 corbeille : voir `FileDao.deleteTrashedOlderThan`. */
|
||||
@Query("DELETE FROM folders WHERE trashed_at IS NOT NULL AND trashed_at < :cutoff")
|
||||
suspend fun deleteTrashedOlderThan(cutoff: Long): Int
|
||||
|
||||
/** Re-parente des sous-dossiers vers un nouveau parent (fusion). */
|
||||
@Query("UPDATE folders SET parent_resource_id = :newParentId, updated_at = :updatedAt WHERE resource_id IN (:resourceIds)")
|
||||
suspend fun reparent(resourceIds: List<String>, newParentId: String, updatedAt: Long)
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.kazier.mobile.data.local.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import com.kazier.mobile.data.local.entity.ResourceLink
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface LinksDao {
|
||||
|
||||
/** Arêtes touchant la ressource (les deux sens, confiance décroissante). */
|
||||
@Query("""
|
||||
SELECT * FROM resource_links
|
||||
WHERE from_id = :resourceId OR to_id = :resourceId
|
||||
ORDER BY confidence DESC
|
||||
""")
|
||||
fun observeForResource(resourceId: String): Flow<List<ResourceLink>>
|
||||
|
||||
@Query("""
|
||||
SELECT * FROM resource_links
|
||||
WHERE from_id = :resourceId OR to_id = :resourceId
|
||||
ORDER BY confidence DESC
|
||||
""")
|
||||
suspend fun getForResource(resourceId: String): List<ResourceLink>
|
||||
|
||||
@Insert
|
||||
suspend fun insertAll(links: List<ResourceLink>)
|
||||
|
||||
/** Remplacement en bloc (sync serveur, source de vérité) — voir repository. */
|
||||
@Query("DELETE FROM resource_links WHERE from_id = :resourceId OR to_id = :resourceId")
|
||||
suspend fun deleteForResource(resourceId: String)
|
||||
}
|
||||
+20
-6
@@ -158,20 +158,34 @@ interface PendingOperationDao {
|
||||
suspend fun deleteUnpushed(): Int
|
||||
|
||||
/**
|
||||
* Un `delete_resource` est-il encore en attente de push pour cette
|
||||
* ressource ? Le walk SAF ne doit alors jamais la ressusciter (`exists =
|
||||
* 1`) : l'intention de suppression est déjà journalisée (ordre
|
||||
* journal-first, C4) et la repasse physique va suivre. Sans ce garde, le
|
||||
* walk réimporterait une ressource que le serveur s'apprête à supprimer.
|
||||
* Un `delete_resource` (legacy) ou `trash_resource` (V3) est-il encore en
|
||||
* attente de push pour cette ressource ? Le walk SAF ne doit alors jamais
|
||||
* la ressusciter (`exists = 1`) : l'intention de suppression est déjà
|
||||
* journalisée (ordre journal-first, C4) et la repasse physique va suivre.
|
||||
* Sans ce garde, le walk réimporterait une ressource que le serveur
|
||||
* s'apprête à trasher.
|
||||
*/
|
||||
@Query("""
|
||||
SELECT COUNT(*) FROM pending_operations
|
||||
WHERE resource_id = :resourceId
|
||||
AND operation = 'delete_resource'
|
||||
AND operation IN ('delete_resource', 'trash_resource')
|
||||
AND status = 'pending'
|
||||
""")
|
||||
suspend fun countPendingDeleteOperations(resourceId: String): Int
|
||||
|
||||
/**
|
||||
* Un `purge_resource` est-il encore en attente de push ? Le refresh
|
||||
* serveur (qui liste encore la ressource tant que l'op n'est pas synced)
|
||||
* ne doit pas la réimporter — destruction définitive en cours.
|
||||
*/
|
||||
@Query("""
|
||||
SELECT COUNT(*) FROM pending_operations
|
||||
WHERE resource_id = :resourceId
|
||||
AND operation = 'purge_resource'
|
||||
AND status = 'pending'
|
||||
""")
|
||||
suspend fun countPendingPurgeOperations(resourceId: String): Int
|
||||
|
||||
/**
|
||||
* Un `move_resource` est-il encore en attente de push ? Tant que l'op est
|
||||
* pendante, le `folder_resource_id` local reflète la cible future : ni le
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.kazier.mobile.data.local.entity
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
/**
|
||||
* Collection locale V3 (S5) : requête de recherche sauvegardée
|
||||
* (texte + facettes JSON) — pur local, jamais synchronisée (un device, pas
|
||||
* de conflit ; sync serveur = V4). Ex. « Impôts 2026 ».
|
||||
*/
|
||||
@Entity(
|
||||
tableName = "collections",
|
||||
indices = [
|
||||
Index(value = ["name"], unique = true),
|
||||
],
|
||||
)
|
||||
data class CollectionEntity(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
val id: Long = 0L,
|
||||
@ColumnInfo(name = "name")
|
||||
val name: String,
|
||||
/** Texte de recherche (peut être vide : facettes seules). */
|
||||
@ColumnInfo(name = "query")
|
||||
val query: String = "",
|
||||
/** Facettes sérialisées (`SearchFacets` JSON, Moshi Map). */
|
||||
@ColumnInfo(name = "facets_json")
|
||||
val facetsJson: String = "{}",
|
||||
@ColumnInfo(name = "category")
|
||||
val category: String? = null,
|
||||
@ColumnInfo(name = "created_at")
|
||||
val createdAt: Long,
|
||||
)
|
||||
@@ -87,6 +87,16 @@ data class FileEntity(
|
||||
*/
|
||||
@ColumnInfo(name = "wanted_offline")
|
||||
val wantedOffline: Boolean = false,
|
||||
/**
|
||||
* V3 corbeille : timestamp de trash (ms epoch), NULL = vivant. Posé avec
|
||||
* `exists = 0` (tombstone M5 : invisible des requêtes `exists = 1` sans
|
||||
* toucher à chaque requête, exclu de la rétention via `isReleasable`,
|
||||
* couvert par le sweep `getMissingWithUri` en cas de kill entre journal
|
||||
* et suppression SAF). `restore` remet `exists = 1` (cloud-only si `uri`
|
||||
* NULL — le walk re-marque si le physique a vraiment disparu).
|
||||
*/
|
||||
@ColumnInfo(name = "trashed_at")
|
||||
val trashedAt: Long? = null,
|
||||
@ColumnInfo(name = "added_at")
|
||||
val addedAt: Long,
|
||||
@ColumnInfo(name = "updated_at")
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.kazier.mobile.data.local.entity
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Fts4
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
/**
|
||||
* Index plein-texte V3 (S4) : `name` + `body` (`ocr_text` + faits concaténés).
|
||||
*
|
||||
* Table virtuelle maintenue EXCLUSIVEMENT par les triggers de la migration
|
||||
* 20→21 (insert/update/delete sur `files`, insert/update/delete sur
|
||||
* `resource_facts`) — jamais d'écriture Room directe. Les requêtes passent
|
||||
* par `FileDao.searchDocuments` (JOIN sur `rowid = files.id`).
|
||||
*/
|
||||
@Fts4
|
||||
@Entity(tableName = "files_fts")
|
||||
data class FilesFts(
|
||||
@PrimaryKey
|
||||
@ColumnInfo(name = "rowid")
|
||||
val rowId: Long,
|
||||
@ColumnInfo(name = "name")
|
||||
val name: String,
|
||||
@ColumnInfo(name = "body")
|
||||
val body: String,
|
||||
)
|
||||
@@ -45,6 +45,12 @@ data class FolderEntity(
|
||||
val syncStatus: String = "local",
|
||||
@ColumnInfo(name = "created_in_app", defaultValue = "0")
|
||||
val createdInApp: Boolean = false,
|
||||
/**
|
||||
* V3 corbeille : voir `FileEntity.trashedAt` — même sémantique (tombstone
|
||||
* `exists = 0`, trash/restore récursifs côté Deleters, jamais de delete).
|
||||
*/
|
||||
@ColumnInfo(name = "trashed_at")
|
||||
val trashedAt: Long? = null,
|
||||
@ColumnInfo(name = "added_at")
|
||||
val addedAt: Long,
|
||||
@ColumnInfo(name = "updated_at")
|
||||
|
||||
+14
@@ -90,4 +90,18 @@ object PendingOperationType {
|
||||
* `operation_id` (garde anti-rejeu serveur A9).
|
||||
*/
|
||||
const val REPORT_BYTES = "report_bytes"
|
||||
/**
|
||||
* V3 corbeille : `trash_resource` (soft, subtree), `restore_resource`
|
||||
* (lève le flag), `purge_resource` (destruction définitive). `payload`
|
||||
* vide — `delete_resource` (legacy, mappé serveur vers trash) conservé
|
||||
* pour compatibilité des lignes pré-V3 mais plus jamais émis.
|
||||
*/
|
||||
const val TRASH_RESOURCE = "trash_resource"
|
||||
const val RESTORE_RESOURCE = "restore_resource"
|
||||
const val PURGE_RESOURCE = "purge_resource"
|
||||
/**
|
||||
* V3 corrections : remplace le jeu `user` des faits (`{ facts: [...] }`,
|
||||
* full-replace, editor+). Même transaction que l'écriture Room optimiste.
|
||||
*/
|
||||
const val CORRECT_FACTS = "correct_facts"
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.kazier.mobile.data.local.entity
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
/**
|
||||
* Miroir local des faits génériques serveur (`GET /files/:id/facts`, V3 S2).
|
||||
*
|
||||
* Remplacés en bloc à chaque sync (`FactsRepository.syncFromServer`) —
|
||||
* jamais de merge : le serveur est la source de vérité. Les corrections
|
||||
* locales (`origin = 'user'`) sont écrites d'abord en Room (optimiste) puis
|
||||
* poussées via l'op outbox `correct_facts` (full-replace serveur).
|
||||
*/
|
||||
@Entity(
|
||||
tableName = "resource_facts",
|
||||
indices = [
|
||||
Index(value = ["resource_id"]),
|
||||
],
|
||||
)
|
||||
data class ResourceFact(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
val id: Long = 0L,
|
||||
@ColumnInfo(name = "resource_id")
|
||||
val resourceId: String,
|
||||
@ColumnInfo(name = "fact_type")
|
||||
val factType: String,
|
||||
@ColumnInfo(name = "value_text")
|
||||
val valueText: String? = null,
|
||||
@ColumnInfo(name = "value_num")
|
||||
val valueNum: Double? = null,
|
||||
@ColumnInfo(name = "value_date")
|
||||
val valueDate: String? = null,
|
||||
@ColumnInfo(name = "confidence")
|
||||
val confidence: Double = 0.0,
|
||||
@ColumnInfo(name = "origin")
|
||||
val origin: String = FactOrigin.AUTO,
|
||||
@ColumnInfo(name = "updated_at")
|
||||
val updatedAt: Long,
|
||||
)
|
||||
|
||||
/** Origines d'un fait — miroir du CHECK serveur (migration 000019). */
|
||||
object FactOrigin {
|
||||
const val AUTO = "auto"
|
||||
const val USER = "user"
|
||||
}
|
||||
|
||||
/** Types de faits — miroir du CHECK serveur (migration 000019). */
|
||||
object FactType {
|
||||
const val DATE = "date"
|
||||
const val DUE_DATE = "due_date"
|
||||
const val PERIOD = "period"
|
||||
const val AMOUNT = "amount"
|
||||
const val ISSUER = "issuer"
|
||||
const val REFERENCE = "reference"
|
||||
const val DOC_TYPE = "doc_type"
|
||||
}
|
||||
|
||||
/**
|
||||
* Faits affichables : pour chaque type, le `user` gagne sur l'`auto`
|
||||
* (correction en un tap, S3). Tri stable par type puis valeur.
|
||||
*/
|
||||
fun displayFacts(all: List<ResourceFact>): List<ResourceFact> {
|
||||
val userTypes = all.filter { it.origin == FactOrigin.USER }.map { it.factType }.toSet()
|
||||
return all
|
||||
.filter { it.origin == FactOrigin.USER || it.factType !in userTypes }
|
||||
.sortedWith(compareBy({ factTypeOrder(it.factType) }, { it.valueText ?: "" }))
|
||||
}
|
||||
|
||||
private fun factTypeOrder(type: String): Int = when (type) {
|
||||
FactType.ISSUER -> 0
|
||||
FactType.AMOUNT -> 1
|
||||
FactType.DUE_DATE -> 2
|
||||
FactType.DATE -> 3
|
||||
FactType.PERIOD -> 4
|
||||
FactType.REFERENCE -> 5
|
||||
FactType.DOC_TYPE -> 6
|
||||
else -> 7
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.kazier.mobile.data.local.entity
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
/**
|
||||
* Miroir local d'une arête `GET /files/:id/relations` (V3 S5).
|
||||
*
|
||||
* Stockée orientée (`fromId`/`toId` canoniques serveur) mais lue dans les
|
||||
* deux sens (`LinksRepository.observeRelations`). Remplacement en bloc au
|
||||
* sync par ressource (le serveur réconcilie à la lecture). Les verdicts
|
||||
* (`createdBy = user`) sont écrits d'abord en Room (optimiste) puis poussés
|
||||
* en REST direct (pas d'outbox — acte rare, en ligne uniquement, S5).
|
||||
*/
|
||||
@Entity(
|
||||
tableName = "resource_links",
|
||||
indices = [
|
||||
Index(value = ["from_id", "to_id", "link_type"], unique = true),
|
||||
Index(value = ["to_id"]),
|
||||
],
|
||||
)
|
||||
data class ResourceLink(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
val id: Long = 0L,
|
||||
@ColumnInfo(name = "from_id")
|
||||
val fromId: String,
|
||||
@ColumnInfo(name = "to_id")
|
||||
val toId: String,
|
||||
@ColumnInfo(name = "link_type")
|
||||
val linkType: String,
|
||||
@ColumnInfo(name = "confidence")
|
||||
val confidence: Double = 0.0,
|
||||
@ColumnInfo(name = "created_by")
|
||||
val createdBy: String = LinkOrigin.AUTO,
|
||||
@ColumnInfo(name = "updated_at")
|
||||
val updatedAt: Long,
|
||||
) {
|
||||
/** L'autre bout vu depuis [resourceId] (arête symétrique). */
|
||||
fun otherEnd(resourceId: String): String =
|
||||
if (fromId == resourceId) toId else fromId
|
||||
}
|
||||
|
||||
/** Origines d'une arête — miroir du CHECK serveur (migration 000021). */
|
||||
object LinkOrigin {
|
||||
const val AUTO = "auto"
|
||||
const val USER = "user"
|
||||
}
|
||||
|
||||
/** Types d'arêtes — miroir du CHECK serveur (pas de `version_of` : les
|
||||
* versions vivent DANS la ressource). */
|
||||
object LinkType {
|
||||
const val SAME_SCAN = "same_scan"
|
||||
const val DUPLICATE_OF = "duplicate_of"
|
||||
const val SAME_ISSUER = "same_issuer"
|
||||
const val SAME_REFERENCE = "same_reference"
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.kazier.mobile.data.local.fts
|
||||
|
||||
/**
|
||||
* Maintien de l'index plein-texte `files_fts` (V3 S4) — `name` + `body`
|
||||
* (`ocr_text` + faits concaténés).
|
||||
*
|
||||
* Utilisé à DEUX endroits (même SQL) :
|
||||
* - `Migrations.MIGRATION_20_21` (bases existantes + backfill) ;
|
||||
* - `RoomDatabase.Callback.onCreate` (fresh install — Room crée la table
|
||||
* virtuelle via l'entité `FilesFts`, mais jamais les triggers).
|
||||
*/
|
||||
object FtsTriggers {
|
||||
|
||||
/** CREATE de la table — identique au généré Room (`21.json`). */
|
||||
const val CREATE_TABLE =
|
||||
"CREATE VIRTUAL TABLE IF NOT EXISTS `files_fts` USING FTS4(`name` TEXT NOT NULL, `body` TEXT NOT NULL)"
|
||||
|
||||
val TRIGGERS: List<String> = listOf(
|
||||
"""
|
||||
CREATE TRIGGER IF NOT EXISTS `files_fts_ai` AFTER INSERT ON `files` BEGIN
|
||||
INSERT INTO `files_fts`(`rowid`, `name`, `body`) VALUES (
|
||||
NEW.`id`, NEW.`name`,
|
||||
COALESCE(NEW.`ocr_text`, '') || ' ' || COALESCE(
|
||||
(SELECT group_concat(`value_text`, ' ') FROM `resource_facts`
|
||||
WHERE `resource_facts`.`resource_id` = NEW.`resource_id`), ''));
|
||||
END
|
||||
""".trimIndent(),
|
||||
"""
|
||||
CREATE TRIGGER IF NOT EXISTS `files_fts_ad` AFTER DELETE ON `files` BEGIN
|
||||
DELETE FROM `files_fts` WHERE `rowid` = OLD.`id`;
|
||||
END
|
||||
""".trimIndent(),
|
||||
"""
|
||||
CREATE TRIGGER IF NOT EXISTS `files_fts_au` AFTER UPDATE OF `name`, `ocr_text` ON `files` BEGIN
|
||||
UPDATE `files_fts` SET `name` = NEW.`name`,
|
||||
`body` = COALESCE(NEW.`ocr_text`, '') || ' ' || COALESCE(
|
||||
(SELECT group_concat(`value_text`, ' ') FROM `resource_facts`
|
||||
WHERE `resource_facts`.`resource_id` = NEW.`resource_id`), '')
|
||||
WHERE `rowid` = NEW.`id`;
|
||||
END
|
||||
""".trimIndent(),
|
||||
// Les faits ne portent pas de FK vers `files.id` (clé = resource_id) :
|
||||
// chaque écriture reconstruit le `body` de la ligne parente.
|
||||
"""
|
||||
CREATE TRIGGER IF NOT EXISTS `facts_fts_ai` AFTER INSERT ON `resource_facts` BEGIN
|
||||
UPDATE `files_fts` SET `body` = COALESCE(
|
||||
(SELECT `ocr_text` FROM `files` WHERE `files`.`resource_id` = NEW.`resource_id`), '') || ' ' || COALESCE(
|
||||
(SELECT group_concat(`value_text`, ' ') FROM `resource_facts`
|
||||
WHERE `resource_facts`.`resource_id` = NEW.`resource_id`), '')
|
||||
WHERE `rowid` = (SELECT `id` FROM `files` WHERE `files`.`resource_id` = NEW.`resource_id`);
|
||||
END
|
||||
""".trimIndent(),
|
||||
"""
|
||||
CREATE TRIGGER IF NOT EXISTS `facts_fts_au` AFTER UPDATE OF `value_text` ON `resource_facts` BEGIN
|
||||
UPDATE `files_fts` SET `body` = COALESCE(
|
||||
(SELECT `ocr_text` FROM `files` WHERE `files`.`resource_id` = NEW.`resource_id`), '') || ' ' || COALESCE(
|
||||
(SELECT group_concat(`value_text`, ' ') FROM `resource_facts`
|
||||
WHERE `resource_facts`.`resource_id` = NEW.`resource_id`), '')
|
||||
WHERE `rowid` = (SELECT `id` FROM `files` WHERE `files`.`resource_id` = NEW.`resource_id`);
|
||||
END
|
||||
""".trimIndent(),
|
||||
"""
|
||||
CREATE TRIGGER IF NOT EXISTS `facts_fts_ad` AFTER DELETE ON `resource_facts` BEGIN
|
||||
UPDATE `files_fts` SET `body` = COALESCE(
|
||||
(SELECT `ocr_text` FROM `files` WHERE `files`.`resource_id` = OLD.`resource_id`), '') || ' ' || COALESCE(
|
||||
(SELECT group_concat(`value_text`, ' ') FROM `resource_facts`
|
||||
WHERE `resource_facts`.`resource_id` = OLD.`resource_id`), '')
|
||||
WHERE `rowid` = (SELECT `id` FROM `files` WHERE `files`.`resource_id` = OLD.`resource_id`);
|
||||
END
|
||||
""".trimIndent(),
|
||||
)
|
||||
}
|
||||
+123
-1
@@ -316,7 +316,129 @@ object Migrations {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* V3 corbeille : `trashed_at` (ms epoch, NULL = vivant) sur `files` +
|
||||
* `folders`. Zéro backfill (NULL = vivant = état courant). Le masquage
|
||||
* passe par `exists = 0` posé par l'action de trash (pas par cette
|
||||
* colonne seule) — les requêtes `exists = 1` restent inchangées.
|
||||
*/
|
||||
private val MIGRATION_18_19 = object : Migration(18, 19) {
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL("ALTER TABLE `files` ADD COLUMN `trashed_at` INTEGER")
|
||||
db.execSQL("ALTER TABLE `folders` ADD COLUMN `trashed_at` INTEGER")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* V3 faits génériques : table `resource_facts` (miroir `GET /facts`,
|
||||
* remplacement en bloc au sync, corrections `user` poussées en outbox).
|
||||
* Schéma aligné sur `ResourceFact` (export `app/schemas/20.json`).
|
||||
*/
|
||||
private val MIGRATION_19_20 = object : Migration(19, 20) {
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS `resource_facts` (
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`resource_id` TEXT NOT NULL,
|
||||
`fact_type` TEXT NOT NULL,
|
||||
`value_text` TEXT,
|
||||
`value_num` REAL,
|
||||
`value_date` TEXT,
|
||||
`confidence` REAL NOT NULL,
|
||||
`origin` TEXT NOT NULL,
|
||||
`updated_at` INTEGER NOT NULL
|
||||
)
|
||||
""".trimIndent(),
|
||||
)
|
||||
db.execSQL(
|
||||
"CREATE INDEX IF NOT EXISTS `index_resource_facts_resource_id` " +
|
||||
"ON `resource_facts` (`resource_id`)",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* V3 recherche (S4) : table virtuelle FTS4 `files_fts` (`name` + `body` =
|
||||
* `ocr_text` + faits concaténés) + triggers de maintien (`FtsTriggers`,
|
||||
* partagés avec le callback fresh-install).
|
||||
*
|
||||
* Backfill : reconstruit depuis `files` + `resource_facts` existants.
|
||||
* `body` évite les NULL (FTS `MATCH` + concat robustes).
|
||||
*
|
||||
* Déclarée AVANT `ALL` (ordre d'init Kotlin : `ALL` la référence).
|
||||
*/
|
||||
private val MIGRATION_20_21 = object : Migration(20, 21) {
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
// SQL table identique au généré Room (`21.json`) : la validation
|
||||
// d'ouverture compare la table migrée au schéma attendu.
|
||||
db.execSQL(com.kazier.mobile.data.local.fts.FtsTriggers.CREATE_TABLE)
|
||||
db.execSQL(
|
||||
"""
|
||||
INSERT OR IGNORE INTO `files_fts`(`rowid`, `name`, `body`)
|
||||
SELECT `id`, `name`,
|
||||
COALESCE(`ocr_text`, '') || ' ' || COALESCE(
|
||||
(SELECT group_concat(`value_text`, ' ') FROM `resource_facts`
|
||||
WHERE `resource_facts`.`resource_id` = `files`.`resource_id`), '')
|
||||
FROM `files`
|
||||
""".trimIndent(),
|
||||
)
|
||||
for (trigger in com.kazier.mobile.data.local.fts.FtsTriggers.TRIGGERS) {
|
||||
db.execSQL(trigger)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* V3 relations + collections (S5) : `resource_links` (miroir
|
||||
* `GET /relations`, remplacement en bloc) + `collections` (requêtes
|
||||
* sauvegardées locales). Schémas alignés sur les entités (exports
|
||||
* `app/schemas/22.json`).
|
||||
*/
|
||||
private val MIGRATION_21_22 = object : Migration(21, 22) {
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS `resource_links` (
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`from_id` TEXT NOT NULL,
|
||||
`to_id` TEXT NOT NULL,
|
||||
`link_type` TEXT NOT NULL,
|
||||
`confidence` REAL NOT NULL,
|
||||
`created_by` TEXT NOT NULL,
|
||||
`updated_at` INTEGER NOT NULL
|
||||
)
|
||||
""".trimIndent(),
|
||||
)
|
||||
db.execSQL(
|
||||
"CREATE UNIQUE INDEX IF NOT EXISTS `index_resource_links_from_id_to_id_link_type` " +
|
||||
"ON `resource_links` (`from_id`, `to_id`, `link_type`)",
|
||||
)
|
||||
db.execSQL(
|
||||
"CREATE INDEX IF NOT EXISTS `index_resource_links_to_id` " +
|
||||
"ON `resource_links` (`to_id`)",
|
||||
)
|
||||
db.execSQL(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS `collections` (
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`name` TEXT NOT NULL,
|
||||
`query` TEXT NOT NULL,
|
||||
`facets_json` TEXT NOT NULL,
|
||||
`category` TEXT,
|
||||
`created_at` INTEGER NOT NULL
|
||||
)
|
||||
""".trimIndent(),
|
||||
)
|
||||
db.execSQL(
|
||||
"CREATE UNIQUE INDEX IF NOT EXISTS `index_collections_name` " +
|
||||
"ON `collections` (`name`)",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** En dernier : l'ordre d'init Kotlin exige les migrations avant. */
|
||||
val ALL: Array<Migration> = arrayOf(
|
||||
MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5, MIGRATION_5_6, MIGRATION_6_7, MIGRATION_7_8, MIGRATION_8_9, MIGRATION_9_10, MIGRATION_10_11, MIGRATION_11_12, MIGRATION_12_13, MIGRATION_13_14, MIGRATION_14_15, MIGRATION_15_16, MIGRATION_16_17, MIGRATION_17_18,
|
||||
MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5, MIGRATION_5_6, MIGRATION_6_7, MIGRATION_7_8, MIGRATION_8_9, MIGRATION_9_10, MIGRATION_10_11, MIGRATION_11_12, MIGRATION_12_13, MIGRATION_13_14, MIGRATION_14_15, MIGRATION_15_16, MIGRATION_16_17, MIGRATION_17_18, MIGRATION_18_19, MIGRATION_19_20, MIGRATION_20_21, MIGRATION_21_22,
|
||||
)
|
||||
}
|
||||
@@ -29,11 +29,22 @@ import com.kazier.mobile.data.remote.dto.ShareLinkDto
|
||||
import com.kazier.mobile.data.remote.dto.SyncOpDto
|
||||
import com.kazier.mobile.data.remote.dto.SyncOpsRequest
|
||||
import com.kazier.mobile.data.remote.dto.SyncOpsResult
|
||||
import com.kazier.mobile.data.remote.dto.CorrectFactsDto
|
||||
import com.kazier.mobile.data.remote.dto.DeviceDto
|
||||
import com.kazier.mobile.data.remote.dto.DuplicateGroupDto
|
||||
import com.kazier.mobile.data.remote.dto.ExportRequestDto
|
||||
import com.kazier.mobile.data.remote.dto.FactDto
|
||||
import com.kazier.mobile.data.remote.dto.RelationDto
|
||||
import com.kazier.mobile.data.remote.dto.RelationVerdictDto
|
||||
import com.kazier.mobile.data.remote.dto.TrashDto
|
||||
import com.kazier.mobile.data.remote.dto.UsageDto
|
||||
import com.kazier.mobile.data.remote.dto.VersionDto
|
||||
import kotlin.coroutines.cancellation.CancellationException
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.RequestBody
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import okhttp3.ResponseBody
|
||||
import okio.IOException
|
||||
import retrofit2.Response
|
||||
import timber.log.Timber
|
||||
@@ -148,6 +159,107 @@ class ApiClient @Inject constructor(
|
||||
suspend fun deleteFile(fileId: String): String =
|
||||
unwrap({ apiService.deleteFile(fileId) }).id
|
||||
|
||||
/** Corbeille serveur V3 — `GET /trash` (+ `meta`). */
|
||||
suspend fun listTrashPaged(
|
||||
page: Int? = null,
|
||||
pageSize: Int? = null,
|
||||
): Pair<List<TrashDto>, ApiMeta?> =
|
||||
unwrapWithMeta({ apiService.listTrash(page, pageSize) })
|
||||
|
||||
/** Restaure une ressource trashée — `POST /files/:id/restore`. */
|
||||
suspend fun restoreFile(fileId: String): String =
|
||||
unwrap({ apiService.restoreFile(fileId) }).id
|
||||
|
||||
/** Historique des octets — `GET /files/:id/versions`. */
|
||||
suspend fun listVersions(fileId: String): List<VersionDto> =
|
||||
unwrap({ apiService.listVersions(fileId) })
|
||||
|
||||
/** Faits génériques V3 — `GET /files/:id/facts` (`[]` si vide). */
|
||||
suspend fun listFacts(fileId: String): List<FactDto> =
|
||||
unwrap({ apiService.listFacts(fileId) })
|
||||
|
||||
/** Remplace le jeu `user` — `PATCH /files/:id/facts` (full-replace). */
|
||||
suspend fun correctFacts(fileId: String, facts: List<FactDto>): List<FactDto> =
|
||||
unwrap({ apiService.correctFacts(fileId, CorrectFactsDto(facts)) })
|
||||
|
||||
/** Graphe documentaire V3 — `GET /files/:id/relations` (owner-only). */
|
||||
suspend fun listRelations(fileId: String): List<RelationDto> =
|
||||
unwrap({ apiService.listRelations(fileId) })
|
||||
|
||||
/** Verdict sur une arête — renvoie le jeu réconcilié. */
|
||||
suspend fun verdictRelation(fileId: String, otherId: String, linkType: String, confirmed: Boolean): List<RelationDto> =
|
||||
unwrap({ apiService.verdictRelation(fileId, RelationVerdictDto(otherId, linkType, confirmed)) })
|
||||
|
||||
/** Doublons d'octets — `GET /duplicates` (owner). */
|
||||
suspend fun listDuplicates(): List<DuplicateGroupDto> =
|
||||
unwrap({ apiService.listDuplicates() })
|
||||
|
||||
/** Devices du compte — `GET /devices`. */
|
||||
suspend fun listDevices(): List<DeviceDto> =
|
||||
unwrap({ apiService.listDevices() })
|
||||
|
||||
/** Révoque un device — `DELETE /devices/:id`. */
|
||||
suspend fun revokeDevice(deviceId: String): String =
|
||||
unwrap({ apiService.revokeDevice(deviceId) }).id
|
||||
|
||||
/** Empreinte du compte — `GET /users/me/usage`. */
|
||||
suspend fun getUsage(): UsageDto =
|
||||
unwrap({ apiService.getUsage() })
|
||||
|
||||
/**
|
||||
* Export chiffré vers un fichier (V3 S7) — `POST /export`, streamé
|
||||
* direct sur disque (jamais en RAM : un export peut peser des Go).
|
||||
* 400 si mot de passe trop court, 401 → re-login.
|
||||
*/
|
||||
suspend fun exportToFile(password: String, dest: java.io.File) {
|
||||
val response = try {
|
||||
apiService.exportZip(ExportRequestDto(password))
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: IOException) {
|
||||
Timber.d(e, "api: transport error (export)")
|
||||
throw ApiException("NETWORK_ERROR", "Serveur injoignable", 0)
|
||||
} catch (e: Exception) {
|
||||
if (e is CancellationException) throw e
|
||||
Timber.d(e, "api: unexpected error (export)")
|
||||
throw ApiException("NETWORK_ERROR", "Serveur injoignable", 0)
|
||||
}
|
||||
if (!response.isSuccessful) {
|
||||
val status = response.code()
|
||||
if (status == 401) {
|
||||
onUnauthorized?.invoke()
|
||||
}
|
||||
val error = response.errorBody()?.use { body ->
|
||||
body.string().let { raw ->
|
||||
runCatching { errorEnvelopeAdapter.fromJson(raw)?.error }.getOrNull()
|
||||
}
|
||||
}
|
||||
throw ApiException(
|
||||
code = error?.code ?: "HTTP_$status",
|
||||
message = error?.message ?: "HTTP $status",
|
||||
httpCode = status,
|
||||
)
|
||||
}
|
||||
val body = response.body()
|
||||
?: throw ApiException("INVALID_RESPONSE", "Réponse serveur invalide (corps vide)", response.code())
|
||||
try {
|
||||
dest.parentFile?.mkdirs()
|
||||
body.byteStream().use { input ->
|
||||
dest.outputStream().use { out ->
|
||||
input.copyTo(out)
|
||||
}
|
||||
}
|
||||
} catch (e: CancellationException) {
|
||||
runCatching { dest.delete() }
|
||||
throw e
|
||||
} catch (e: IOException) {
|
||||
runCatching { dest.delete() }
|
||||
throw ApiException("NETWORK_ERROR", "Serveur injoignable", 0)
|
||||
} finally {
|
||||
body.close()
|
||||
}
|
||||
}
|
||||
|
||||
/** Recherche serveur paginée — `GET /files/search?q=…` (+ `meta`). */
|
||||
suspend fun searchFilesPaged(
|
||||
query: String,
|
||||
@@ -221,17 +333,39 @@ class ApiClient @Inject constructor(
|
||||
suspend fun downloadContentBytes(
|
||||
fileId: String,
|
||||
maxBytes: Long = MAX_CONTENT_BYTES,
|
||||
): ByteArray = downloadRawBytes(tag = "content", maxBytes = maxBytes) {
|
||||
apiService.downloadContent(fileId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Vignette JPEG 512 d'un fichier visible — `GET /files/:id/preview`
|
||||
* (binaire brut, `inline`). 404 NOT_FOUND si invisible, 404 NO_PREVIEW
|
||||
* si le type ne se prévisualise pas (code propagé tel quel pour que
|
||||
* l'UI n'insiste pas).
|
||||
*/
|
||||
suspend fun downloadPreviewBytes(
|
||||
fileId: String,
|
||||
maxBytes: Long = MAX_PREVIEW_BYTES,
|
||||
): ByteArray = downloadRawBytes(tag = "preview", maxBytes = maxBytes) {
|
||||
apiService.downloadPreview(fileId)
|
||||
}
|
||||
|
||||
/** Cœur streaming borné partagé par `content` et `preview` (V3 S7). */
|
||||
private suspend fun downloadRawBytes(
|
||||
tag: String,
|
||||
maxBytes: Long,
|
||||
call: suspend () -> Response<ResponseBody>,
|
||||
): ByteArray {
|
||||
val response = try {
|
||||
apiService.downloadContent(fileId)
|
||||
call()
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: IOException) {
|
||||
Timber.d(e, "api: transport error (content)")
|
||||
Timber.d(e, "api: transport error ($tag)")
|
||||
throw ApiException("NETWORK_ERROR", "Serveur injoignable", 0)
|
||||
} catch (e: Exception) {
|
||||
if (e is CancellationException) throw e
|
||||
Timber.d(e, "api: unexpected error (content)")
|
||||
Timber.d(e, "api: unexpected error ($tag)")
|
||||
throw ApiException("NETWORK_ERROR", "Serveur injoignable", 0)
|
||||
}
|
||||
if (!response.isSuccessful) {
|
||||
@@ -286,6 +420,8 @@ class ApiClient @Inject constructor(
|
||||
companion object {
|
||||
/** Borne streaming sans cache : 12 Mo (jamais en RAM au-delà). */
|
||||
const val MAX_CONTENT_BYTES = 12 * 1024 * 1024L
|
||||
/** Borne vignette distante : 1 Mo (JPEG 512 ≪ ça, garde-fou). */
|
||||
const val MAX_PREVIEW_BYTES = 1024 * 1024L
|
||||
}
|
||||
|
||||
private suspend fun <T> unwrap(
|
||||
|
||||
@@ -22,6 +22,16 @@ import com.kazier.mobile.data.remote.dto.ShareGrantDto
|
||||
import com.kazier.mobile.data.remote.dto.ShareLinkDto
|
||||
import com.kazier.mobile.data.remote.dto.SyncOpsRequest
|
||||
import com.kazier.mobile.data.remote.dto.SyncOpsResult
|
||||
import com.kazier.mobile.data.remote.dto.CorrectFactsDto
|
||||
import com.kazier.mobile.data.remote.dto.DeviceDto
|
||||
import com.kazier.mobile.data.remote.dto.DuplicateGroupDto
|
||||
import com.kazier.mobile.data.remote.dto.ExportRequestDto
|
||||
import com.kazier.mobile.data.remote.dto.FactDto
|
||||
import com.kazier.mobile.data.remote.dto.RelationDto
|
||||
import com.kazier.mobile.data.remote.dto.RelationVerdictDto
|
||||
import com.kazier.mobile.data.remote.dto.TrashDto
|
||||
import com.kazier.mobile.data.remote.dto.UsageDto
|
||||
import com.kazier.mobile.data.remote.dto.VersionDto
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.RequestBody
|
||||
import okhttp3.ResponseBody
|
||||
@@ -139,12 +149,120 @@ interface ApiService {
|
||||
@Path("id") fileId: String,
|
||||
): Response<ResponseBody>
|
||||
|
||||
/**
|
||||
* Vignette JPEG 512 d'un fichier visible (V3 S7) —
|
||||
* `GET /files/:id/preview` (binaire brut, `inline`, cache 1 h).
|
||||
* 404 NOT_FOUND si invisible/metadata-only, 404 NO_PREVIEW si le type
|
||||
* ne se prévisualise pas (l'UI montre l'icône, sans réessai).
|
||||
*/
|
||||
@Streaming
|
||||
@GET("files/{id}/preview")
|
||||
suspend fun downloadPreview(
|
||||
@Path("id") fileId: String,
|
||||
): Response<ResponseBody>
|
||||
|
||||
/** Suppression serveur d'un fichier possédé — `DELETE /files/:id`. */
|
||||
@DELETE("files/{id}")
|
||||
suspend fun deleteFile(
|
||||
@Path("id") fileId: String,
|
||||
): Response<ApiEnvelope<IdResultDto>>
|
||||
|
||||
/**
|
||||
* Corbeille serveur V3 (`GET /trash`, paginé `meta`) — ressources
|
||||
* trashées du user, les plus récentes d'abord.
|
||||
*/
|
||||
@GET("trash")
|
||||
suspend fun listTrash(
|
||||
@Query("page") page: Int?,
|
||||
@Query("pageSize") pageSize: Int?,
|
||||
): Response<ApiEnvelope<List<TrashDto>>>
|
||||
|
||||
/** Restaure une ressource trashée — `POST /files/:id/restore`. */
|
||||
@POST("files/{id}/restore")
|
||||
suspend fun restoreFile(
|
||||
@Path("id") fileId: String,
|
||||
): Response<ApiEnvelope<IdResultDto>>
|
||||
|
||||
/** Historique des octets — `GET /files/:id/versions` (parent live). */
|
||||
@GET("files/{id}/versions")
|
||||
suspend fun listVersions(
|
||||
@Path("id") fileId: String,
|
||||
): Response<ApiEnvelope<List<VersionDto>>>
|
||||
|
||||
/** Recopie la génération `n` vers le live (V3 versions). */
|
||||
@POST("files/{id}/versions/{n}/restore")
|
||||
suspend fun restoreVersion(
|
||||
@Path("id") fileId: String,
|
||||
@Path("n") versionNo: Int,
|
||||
): Response<ApiEnvelope<FileDto>>
|
||||
|
||||
/**
|
||||
* Faits génériques V3 (`GET /files/:id/facts`, viewer+) — `[]` si rien
|
||||
* d'extractible, 404 si trashé/absent/étranger.
|
||||
*/
|
||||
@GET("files/{id}/facts")
|
||||
suspend fun listFacts(
|
||||
@Path("id") fileId: String,
|
||||
): Response<ApiEnvelope<List<FactDto>>>
|
||||
|
||||
/**
|
||||
* Remplace le jeu `user` des faits (`PATCH /files/:id/facts`,
|
||||
* full-replace, editor+) — renvoie le jeu complet (auto + user).
|
||||
*/
|
||||
@PATCH("files/{id}/facts")
|
||||
suspend fun correctFacts(
|
||||
@Path("id") fileId: String,
|
||||
@Body body: CorrectFactsDto,
|
||||
): Response<ApiEnvelope<List<FactDto>>>
|
||||
|
||||
/**
|
||||
* Graphe documentaire V3 (`GET /files/:id/relations`, owner-only) —
|
||||
* arêtes réconciliées à la lecture, autre bout vivant, confiance décroissante.
|
||||
*/
|
||||
@GET("files/{id}/relations")
|
||||
suspend fun listRelations(
|
||||
@Path("id") fileId: String,
|
||||
): Response<ApiEnvelope<List<RelationDto>>>
|
||||
|
||||
/** Verdict explicite sur une arête (confirmé → 1.0, infirmé → 0.0). */
|
||||
@POST("files/{id}/relations/verdict")
|
||||
suspend fun verdictRelation(
|
||||
@Path("id") fileId: String,
|
||||
@Body body: RelationVerdictDto,
|
||||
): Response<ApiEnvelope<List<RelationDto>>>
|
||||
|
||||
/**
|
||||
* Doublons d'octets du compte (`GET /duplicates`, owner) — groupes par
|
||||
* hash, plus gros d'abord, avec le gain récupérable (B2).
|
||||
*/
|
||||
@GET("duplicates")
|
||||
suspend fun listDuplicates(): Response<ApiEnvelope<List<DuplicateGroupDto>>>
|
||||
|
||||
/** Devices du compte (`GET /devices`, dernier login = ce compte). */
|
||||
@GET("devices")
|
||||
suspend fun listDevices(): Response<ApiEnvelope<List<DeviceDto>>>
|
||||
|
||||
/** Révoque un device (`DELETE /devices/:id`, A1 — téléphone perdu). */
|
||||
@DELETE("devices/{id}")
|
||||
suspend fun revokeDevice(
|
||||
@Path("id") deviceId: String,
|
||||
): Response<ApiEnvelope<IdResultDto>>
|
||||
|
||||
/** Empreinte du compte (`GET /users/me/usage`, calculée serveur). */
|
||||
@GET("users/me/usage")
|
||||
suspend fun getUsage(): Response<ApiEnvelope<UsageDto>>
|
||||
|
||||
/**
|
||||
* Export chiffré du compte (V3 S7) — `POST /export { password }`
|
||||
* (mot de passe en corps, jamais en query). Zip AES-256 streamé
|
||||
* (fichiers vifs possédés + manifest.json, tout chiffré).
|
||||
*/
|
||||
@Streaming
|
||||
@POST("export")
|
||||
suspend fun exportZip(
|
||||
@Body body: ExportRequestDto,
|
||||
): Response<ResponseBody>
|
||||
|
||||
/**
|
||||
* Recherche serveur (`GET /files/search?q=…`, paginé `meta`). Non consommée
|
||||
* en V1 (recherche 100 % locale via Room) — exposée pour parité contrat.
|
||||
|
||||
@@ -191,6 +191,19 @@ data class SyncOpsRequest(
|
||||
data class SyncOpsResult(
|
||||
@Json(name = "applied") val applied: Int,
|
||||
@Json(name = "failed") val failed: SyncFailedDto? = null,
|
||||
/**
|
||||
* Renames LWW appliqués malgré une base périmée (V3 S6 A4) — non-fataux,
|
||||
* le batch continue. Absent (vieux serveur) = liste vide.
|
||||
*/
|
||||
@Json(name = "conflicts") val conflicts: List<SyncConflictDto>? = null,
|
||||
)
|
||||
|
||||
/** Rename concurrent appliqué quand même (transparence, pas de CRDT). */
|
||||
data class SyncConflictDto(
|
||||
@Json(name = "operation_id") val operationId: String,
|
||||
@Json(name = "resource_id") val resourceId: String,
|
||||
@Json(name = "serverName") val serverName: String = "",
|
||||
@Json(name = "serverUpdatedAt") val serverUpdatedAt: Long = 0L,
|
||||
)
|
||||
|
||||
/** Première erreur non-idempotente du batch (arrêt du serveur). */
|
||||
@@ -251,3 +264,114 @@ data class FileOcrDto(
|
||||
@Json(name = "text") val text: String,
|
||||
@Json(name = "updatedAt") val updatedAt: String? = null,
|
||||
)
|
||||
|
||||
/**
|
||||
* Entrée `GET /trash` (V3 corbeille) — miroir de `TrashDTO` Go. `type` =
|
||||
* "file" | "folder", `trashedAt` en RFC3339 (affichage), tri serveur
|
||||
* (plus récent d'abord).
|
||||
*/
|
||||
data class TrashDto(
|
||||
@Json(name = "id") val id: String,
|
||||
@Json(name = "type") val type: String,
|
||||
@Json(name = "name") val name: String,
|
||||
@Json(name = "size") val size: Long = 0L,
|
||||
@Json(name = "trashedAt") val trashedAt: String? = null,
|
||||
@Json(name = "updatedAt") val updatedAt: String? = null,
|
||||
)
|
||||
|
||||
/**
|
||||
* Génération figée `GET /files/{id}/versions` (V3) — miroir de `VersionDTO`
|
||||
* Go. Le restore recopie vers le live (l'actuel est à son tour versionné).
|
||||
*/
|
||||
data class VersionDto(
|
||||
@Json(name = "versionNo") val versionNo: Int,
|
||||
@Json(name = "size") val size: Long = 0L,
|
||||
@Json(name = "mimeType") val mimeType: String? = null,
|
||||
@Json(name = "createdAt") val createdAt: String? = null,
|
||||
)
|
||||
|
||||
/**
|
||||
* Fait générique `GET /files/{id}/facts` (V3 S2) — miroir de `FactDTO` Go.
|
||||
* `valueNum` en Double (montants) ; `valueDate` ISO `YYYY-MM-DD` ;
|
||||
* `origin` = "auto" (remplacé à chaque passe) | "user" (correction S3).
|
||||
* Réutilisé tel quel dans le body `PATCH /files/{id}/facts`
|
||||
* (`{ facts: [...] }`, `origin` ignoré par le serveur).
|
||||
*/
|
||||
data class FactDto(
|
||||
@Json(name = "factType") val factType: String,
|
||||
@Json(name = "valueText") val valueText: String? = null,
|
||||
@Json(name = "valueNum") val valueNum: Double? = null,
|
||||
@Json(name = "valueDate") val valueDate: String? = null,
|
||||
@Json(name = "confidence") val confidence: Double = 0.0,
|
||||
@Json(name = "origin") val origin: String = "auto",
|
||||
)
|
||||
|
||||
/** Body `PATCH /files/{id}/facts` — jeu `user` complet (full-replace, S3). */
|
||||
data class CorrectFactsDto(
|
||||
@Json(name = "facts") val facts: List<FactDto>,
|
||||
)
|
||||
|
||||
/**
|
||||
* Arête `GET /files/{id}/relations` (V3 S5) — miroir de `RelationDTO` Go.
|
||||
* Graphe owner-only, réconcilié à la lecture côté serveur.
|
||||
*/
|
||||
data class RelationDto(
|
||||
@Json(name = "id") val id: String,
|
||||
@Json(name = "otherId") val otherId: String,
|
||||
@Json(name = "type") val type: String,
|
||||
@Json(name = "confidence") val confidence: Double = 0.0,
|
||||
@Json(name = "createdBy") val createdBy: String = "auto",
|
||||
@Json(name = "otherName") val otherName: String = "",
|
||||
@Json(name = "otherType") val otherType: String = "file",
|
||||
@Json(name = "otherSize") val otherSize: Long = 0L,
|
||||
)
|
||||
|
||||
/** Body `POST /files/{id}/relations/verdict` — verdict explicite (S5). */
|
||||
data class RelationVerdictDto(
|
||||
@Json(name = "otherId") val otherId: String,
|
||||
@Json(name = "linkType") val linkType: String,
|
||||
@Json(name = "confirmed") val confirmed: Boolean,
|
||||
)
|
||||
|
||||
/** Membre d'un groupe de doublons `GET /duplicates` (S5 B2). */
|
||||
data class DuplicateFileDto(
|
||||
@Json(name = "id") val id: String,
|
||||
@Json(name = "name") val name: String,
|
||||
@Json(name = "size") val size: Long = 0L,
|
||||
@Json(name = "updatedAt") val updatedAt: Long = 0L,
|
||||
)
|
||||
|
||||
/** Groupe de fichiers aux octets identiques (S5 B2). */
|
||||
data class DuplicateGroupDto(
|
||||
@Json(name = "contentHash") val contentHash: String,
|
||||
@Json(name = "count") val count: Int = 0,
|
||||
@Json(name = "totalBytes") val totalBytes: Long = 0L,
|
||||
@Json(name = "reclaimableBytes") val reclaimableBytes: Long = 0L,
|
||||
@Json(name = "files") val files: List<DuplicateFileDto> = emptyList(),
|
||||
)
|
||||
|
||||
/**
|
||||
* Device du compte `GET /devices` (V3 S6 A1) — dernier login = ce compte.
|
||||
* `lastSeenAt` ms epoch (null si jamais revu depuis l'enregistrement).
|
||||
*/
|
||||
data class DeviceDto(
|
||||
@Json(name = "deviceId") val deviceId: String,
|
||||
@Json(name = "lastSeenAt") val lastSeenAt: Long? = null,
|
||||
)
|
||||
|
||||
/**
|
||||
* Empreinte du compte `GET /users/me/usage` (V3 S6 A2) — calculée serveur,
|
||||
* corbeille détaillée (octets récupérables à la purge).
|
||||
*/
|
||||
data class UsageDto(
|
||||
@Json(name = "usedBytes") val usedBytes: Long = 0L,
|
||||
@Json(name = "quotaBytes") val quotaBytes: Long = 0L,
|
||||
@Json(name = "fileCount") val fileCount: Int = 0,
|
||||
@Json(name = "trashCount") val trashCount: Int = 0,
|
||||
@Json(name = "trashedBytes") val trashedBytes: Long = 0L,
|
||||
)
|
||||
|
||||
/** Body `POST /export` — mot de passe du zip AES (8+ caractères, V3 S7). */
|
||||
data class ExportRequestDto(
|
||||
@Json(name = "password") val password: String,
|
||||
)
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.kazier.mobile.data.repository
|
||||
|
||||
import com.squareup.moshi.JsonAdapter
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.Types
|
||||
import com.kazier.mobile.data.local.dao.CollectionDao
|
||||
import com.kazier.mobile.data.local.entity.CollectionEntity
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
/**
|
||||
* Collections locales V3 (S5) : recherches sauvegardées (texte + facettes),
|
||||
* purement locales — jamais synchronisées (un device, pas de conflit).
|
||||
*/
|
||||
@Singleton
|
||||
class CollectionsRepository @Inject constructor(
|
||||
private val collectionDao: CollectionDao,
|
||||
moshi: Moshi,
|
||||
) {
|
||||
|
||||
private val facetsAdapter: JsonAdapter<Map<String, Any?>> = moshi.adapter(
|
||||
Types.newParameterizedType(Map::class.java, String::class.java, Any::class.java),
|
||||
)
|
||||
|
||||
fun observeAll(): Flow<List<CollectionEntity>> = collectionDao.observeAll()
|
||||
|
||||
suspend fun save(name: String, query: String, category: String?, facets: SearchFacets): Long {
|
||||
val trimmed = name.trim()
|
||||
require(trimmed.isNotEmpty()) { "collection name required" }
|
||||
return collectionDao.insert(
|
||||
CollectionEntity(
|
||||
name = trimmed,
|
||||
query = query.trim(),
|
||||
facetsJson = facetsAdapter.toJson(facets.toMap()),
|
||||
category = category,
|
||||
createdAt = System.currentTimeMillis(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun delete(id: Long) = collectionDao.deleteById(id)
|
||||
|
||||
suspend fun get(id: Long): CollectionEntity? = collectionDao.getById(id)
|
||||
|
||||
/** Rejoue une collection : texte + facettes désérialisées (inconnues ignorées). */
|
||||
fun apply(collection: CollectionEntity): AppliedCollection {
|
||||
val map = runCatching {
|
||||
facetsAdapter.fromJson(collection.facetsJson)
|
||||
}.getOrNull() ?: emptyMap()
|
||||
return AppliedCollection(
|
||||
query = collection.query,
|
||||
category = collection.category,
|
||||
facets = SearchFacets(
|
||||
issuer = map["issuer"] as? String,
|
||||
docType = map["docType"] as? String,
|
||||
minAmount = (map["minAmount"] as? Number)?.toDouble(),
|
||||
maxAmount = (map["maxAmount"] as? Number)?.toDouble(),
|
||||
fromDate = map["fromDate"] as? String,
|
||||
toDate = map["toDate"] as? String,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
private fun SearchFacets.toMap(): Map<String, Any?> = buildMap {
|
||||
issuer?.let { put("issuer", it) }
|
||||
docType?.let { put("docType", it) }
|
||||
minAmount?.let { put("minAmount", it) }
|
||||
maxAmount?.let { put("maxAmount", it) }
|
||||
fromDate?.let { put("fromDate", it) }
|
||||
toDate?.let { put("toDate", it) }
|
||||
}
|
||||
}
|
||||
|
||||
/** Collection rejouée : à appliquer à l'état de recherche. */
|
||||
data class AppliedCollection(
|
||||
val query: String,
|
||||
val category: String?,
|
||||
val facets: SearchFacets,
|
||||
)
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.kazier.mobile.data.repository
|
||||
|
||||
import com.kazier.mobile.data.remote.ApiClient
|
||||
import com.kazier.mobile.data.remote.dto.DeviceDto
|
||||
import com.kazier.mobile.data.remote.dto.UsageDto
|
||||
import com.kazier.mobile.domain.DeviceIdentity
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* Appareils + empreinte du compte (V3 S6 A1/A2).
|
||||
*
|
||||
* - `listDevices` : devices dont le dernier login est ce compte ;
|
||||
* `isCurrent` repère ce téléphone (via `DeviceIdentity`) — le bouton
|
||||
* révoquer n'apparaît jamais dessus (pas de suicide) ;
|
||||
* - `revoke` : `DELETE /devices/:id` (téléphone perdu) — avec
|
||||
* `EnforceDeviceExists`, ses tokens deviennent 401 ;
|
||||
* - `usage` : `GET /users/me/usage` (quota + corbeille détaillée).
|
||||
*/
|
||||
@Singleton
|
||||
class DevicesRepository @Inject constructor(
|
||||
private val apiClient: ApiClient,
|
||||
private val deviceIdentity: DeviceIdentity,
|
||||
) {
|
||||
|
||||
data class DeviceUi(
|
||||
val deviceId: String,
|
||||
val lastSeenAt: Long?,
|
||||
val isCurrent: Boolean,
|
||||
)
|
||||
|
||||
suspend fun listDevices(): List<DeviceUi> {
|
||||
val current = runCatching { deviceIdentity.getOrCreate() }.getOrNull()
|
||||
return apiClient.listDevices().map { dto: DeviceDto ->
|
||||
DeviceUi(
|
||||
deviceId = dto.deviceId,
|
||||
lastSeenAt = dto.lastSeenAt,
|
||||
isCurrent = current != null && dto.deviceId == current,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun revoke(deviceId: String) {
|
||||
apiClient.revokeDevice(deviceId)
|
||||
}
|
||||
|
||||
suspend fun usage(): UsageDto = apiClient.getUsage()
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.kazier.mobile.data.repository
|
||||
|
||||
import androidx.room.withTransaction
|
||||
import com.kazier.mobile.data.local.AppDatabase
|
||||
import com.kazier.mobile.data.local.dao.FactsDao
|
||||
import com.kazier.mobile.data.local.entity.FactOrigin
|
||||
import com.kazier.mobile.data.local.entity.ResourceFact
|
||||
import com.kazier.mobile.data.remote.ApiClient
|
||||
import com.kazier.mobile.data.remote.dto.FactDto
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
/**
|
||||
* Faits génériques V3 (S3) côté client.
|
||||
*
|
||||
* - [syncFromServer] : `GET /files/:id/facts` → remplacement en bloc Room
|
||||
* (le serveur est la source de vérité, comme `ocr_text`).
|
||||
* - [saveCorrection] : écriture optimiste locale (jeu `user` remplacé) +
|
||||
* op outbox `correct_facts` **dans la même transaction** (contrat outbox
|
||||
* atomique). Ordre préservé : deux corrections successives s'appliquent
|
||||
* dans l'ordre (full-replace, la dernière gagne — convergence).
|
||||
* - [resetToAuto] : efface les corrections (jeu `user` vide poussé).
|
||||
*/
|
||||
@Singleton
|
||||
class FactsRepository @Inject constructor(
|
||||
private val appDatabase: AppDatabase,
|
||||
private val factsDao: FactsDao,
|
||||
private val outboxRepository: OutboxRepository,
|
||||
private val apiClient: ApiClient,
|
||||
) {
|
||||
|
||||
fun observeFacts(resourceId: String): Flow<List<ResourceFact>> =
|
||||
factsDao.observeForResource(resourceId)
|
||||
|
||||
suspend fun getFacts(resourceId: String): List<ResourceFact> =
|
||||
factsDao.getForResource(resourceId)
|
||||
|
||||
/** Rapatrie le jeu serveur (auto + user) et remplace le miroir local. */
|
||||
suspend fun syncFromServer(resourceId: String): List<ResourceFact> {
|
||||
val dtos = apiClient.listFacts(resourceId)
|
||||
val now = System.currentTimeMillis()
|
||||
val entities = dtos.map { it.toEntity(resourceId, now) }
|
||||
appDatabase.withTransaction {
|
||||
factsDao.deleteForResource(resourceId)
|
||||
if (entities.isNotEmpty()) factsDao.insertAll(entities)
|
||||
}
|
||||
return entities
|
||||
}
|
||||
|
||||
/**
|
||||
* Correction en un tap : remplace le jeu `user` local par [userFacts]
|
||||
* (faits édités, `origin = user`, confiance 1.0) et enqueue le jeu
|
||||
* complet vers le serveur. Si le serveur ne connaît pas encore la
|
||||
* ressource (jamais de create), pas d'op — pur local (le create puis les
|
||||
* faits suivront au sync... le jeu user local sera écrasé au prochain
|
||||
* syncFromServer, cas rare : faits édités avant tout push).
|
||||
*/
|
||||
suspend fun saveCorrection(resourceId: String, userFacts: List<ResourceFact>) {
|
||||
val now = System.currentTimeMillis()
|
||||
val stamped = userFacts.map {
|
||||
it.copy(resourceId = resourceId, origin = FactOrigin.USER, confidence = 1.0, updatedAt = now)
|
||||
}
|
||||
appDatabase.withTransaction {
|
||||
factsDao.deleteUserFacts(resourceId)
|
||||
if (stamped.isNotEmpty()) factsDao.insertAll(stamped)
|
||||
if (outboxRepository.hasCreateOperation(resourceId)) {
|
||||
outboxRepository.enqueueCorrectFacts(resourceId, "file", stamped.map { it.toPayload() })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** « Rétablir auto » : efface les corrections (jeu vide poussé si connu). */
|
||||
suspend fun resetToAuto(resourceId: String) {
|
||||
appDatabase.withTransaction {
|
||||
factsDao.deleteUserFacts(resourceId)
|
||||
if (outboxRepository.hasCreateOperation(resourceId)) {
|
||||
outboxRepository.enqueueCorrectFacts(resourceId, "file", emptyList())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun FactDto.toEntity(resourceId: String, now: Long) = ResourceFact(
|
||||
resourceId = resourceId,
|
||||
factType = factType,
|
||||
valueText = valueText,
|
||||
valueNum = valueNum,
|
||||
valueDate = valueDate,
|
||||
confidence = confidence,
|
||||
origin = origin.ifBlank { FactOrigin.AUTO },
|
||||
updatedAt = now,
|
||||
)
|
||||
|
||||
private fun ResourceFact.toPayload(): Map<String, Any?> = buildMap {
|
||||
put("factType", factType)
|
||||
valueText?.let { put("valueText", it) }
|
||||
valueNum?.let { put("valueNum", it) }
|
||||
valueDate?.let { put("valueDate", it) }
|
||||
put("confidence", confidence)
|
||||
}
|
||||
}
|
||||
@@ -53,6 +53,9 @@ class FileRepository @Inject constructor(
|
||||
* m7 : la saisie est échappée (`%`/`_` littéraux, pas des jokers) ;
|
||||
* suivi accents : la saisie normalisée (`normalizeSearch`) matche
|
||||
* « ÉTÉ » depuis « ete » (branche SQL dédiée, sans migration).
|
||||
*
|
||||
* Historique : remplacée par [searchDocuments] (FTS + facettes, S4) pour
|
||||
* l'écran de recherche — conservée pour compatibilité.
|
||||
*/
|
||||
fun searchFiles(query: String?, category: String?): Flow<List<FileEntity>> {
|
||||
val trimmed = query?.trim()?.takeIf { it.isNotEmpty() }
|
||||
@@ -63,6 +66,55 @@ class FileRepository @Inject constructor(
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Recherche documentaire V3 (S4) : plein-texte FTS (nom + corps ocr+faits,
|
||||
* offline) + facettes faits. `query` vide + facettes vides → récents
|
||||
* (même contrat que l'ancien `searchFiles`) ; `query` vide + facettes →
|
||||
* filtre seul (sans FTS).
|
||||
*/
|
||||
fun searchDocuments(
|
||||
query: String?,
|
||||
category: String?,
|
||||
facets: SearchFacets = SearchFacets(),
|
||||
): Flow<List<FileEntity>> {
|
||||
val trimmed = query?.trim().orEmpty()
|
||||
if (trimmed.isEmpty() && facets.isEmpty()) {
|
||||
return recentFiles(category)
|
||||
}
|
||||
if (trimmed.isEmpty()) {
|
||||
return fileDao.searchFacetsOnly(
|
||||
category = category,
|
||||
issuer = facets.issuer?.escapeLike(),
|
||||
docType = facets.docType,
|
||||
minAmount = facets.minAmount,
|
||||
maxAmount = facets.maxAmount,
|
||||
fromDate = facets.fromDate,
|
||||
toDate = facets.toDate,
|
||||
)
|
||||
}
|
||||
val words = ftsWords(trimmed)
|
||||
return fileDao.searchDocuments(
|
||||
match = buildFtsMatch(trimmed) ?: trimmed.escapeLike(),
|
||||
query = trimmed.escapeLike(),
|
||||
normQuery = trimmed.normalizeSearch().escapeLike(),
|
||||
category = category,
|
||||
issuer = facets.issuer?.escapeLike(),
|
||||
docType = facets.docType,
|
||||
minAmount = facets.minAmount,
|
||||
maxAmount = facets.maxAmount,
|
||||
fromDate = facets.fromDate,
|
||||
toDate = facets.toDate,
|
||||
w1 = words.getOrNull(0),
|
||||
w2 = words.getOrNull(1),
|
||||
w3 = words.getOrNull(2),
|
||||
w4 = words.getOrNull(3),
|
||||
w5 = words.getOrNull(4),
|
||||
)
|
||||
}
|
||||
|
||||
/** Types suggérés distincts (chips de filtre UI). */
|
||||
fun observeDistinctDocTypes(): Flow<List<String>> = fileDao.observeDistinctDocTypes()
|
||||
|
||||
/** Les `limit` fichiers les plus récents (date de référence), filtrés par catégorie. */
|
||||
fun recentFiles(category: String?, limit: Int = RECENT_LIMIT): Flow<List<FileEntity>> =
|
||||
fileDao.recentFiles(category = category, limit = limit)
|
||||
@@ -208,6 +260,13 @@ class FileRepository @Inject constructor(
|
||||
return existing.copy(exists = 0, updatedAt = now)
|
||||
}
|
||||
|
||||
// Garde corbeille V3 : une ligne trashée n'est JAMAIS ressuscitée par
|
||||
// le walk — ni par réimport SAF (IN_CLOUD garde le physique), ni par
|
||||
// réactivation. Seul `restore` (TrashRepository) lève le flag.
|
||||
if (existing != null && existing.trashedAt != null) {
|
||||
return existing
|
||||
}
|
||||
|
||||
// Un `move_resource` encore en attente fait de la cible outbox la
|
||||
// source de vérité du placement : un snapshot SAF périmé (walk lancé
|
||||
// avant le move) ne doit pas ré-attribuer le dossier à l'ancien parent.
|
||||
@@ -261,6 +320,7 @@ class FileRepository @Inject constructor(
|
||||
parentResourceId = entity.folderResourceId,
|
||||
mimeType = entity.mimeType,
|
||||
extension = entity.extension,
|
||||
scanSessionId = input.scanSessionId,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -271,6 +331,23 @@ class FileRepository @Inject constructor(
|
||||
suspend fun markMissing(resourceId: String, updatedAt: Long) =
|
||||
fileDao.markMissing(resourceId, updatedAt)
|
||||
|
||||
/** Lignes trashées (écran Corbeille) — les plus récentes d'abord. */
|
||||
fun observeTrashed(): Flow<List<FileEntity>> = fileDao.observeTrashed()
|
||||
|
||||
fun observeTrashedCount(): Flow<Int> = fileDao.observeTrashedCount()
|
||||
|
||||
/** Fichiers d'un dossier sans filtre `exists` — collecte trash/restore. */
|
||||
suspend fun getFilesInFolderAll(folderResourceId: String): List<FileEntity> =
|
||||
fileDao.getByFolderAll(folderResourceId)
|
||||
|
||||
/** Trash local (tombstone, jamais de DELETE) — dans la transaction de l'op outbox. */
|
||||
suspend fun markTrashed(resourceId: String, trashedAt: Long) =
|
||||
fileDao.markTrashed(resourceId, trashedAt)
|
||||
|
||||
/** Restore local — dans la transaction de l'op outbox `restore_resource`. */
|
||||
suspend fun clearTrashed(resourceId: String, now: Long) =
|
||||
fileDao.clearTrashed(resourceId, now)
|
||||
|
||||
/** Met à jour la cible dossier d'un fichier (déplacement local / cloud-only).
|
||||
*
|
||||
* Le `move_resource` n'est journalisé que si le serveur connaît déjà la
|
||||
@@ -320,6 +397,16 @@ class FileRepository @Inject constructor(
|
||||
// L'antichambre outbox fait foi tant que le move n'est pas synced.
|
||||
return@forEach
|
||||
}
|
||||
if (outboxRepository.hasPendingPurgeOperation(dto.id)) {
|
||||
// Destruction définitive en cours : le serveur renvoie
|
||||
// encore la ressource, ne jamais la réimporter (V3).
|
||||
return@forEach
|
||||
}
|
||||
// Ligne trashée localement : le serveur la renvoie encore
|
||||
// tant que l'op trash n'est pas synced — ne pas la ressusciter.
|
||||
if (fileDao.getByResourceId(dto.id)?.trashedAt != null) {
|
||||
return@forEach
|
||||
}
|
||||
upsertTolerant(toEntity(dto, folderResourceId, now))
|
||||
}
|
||||
if (files.isEmpty()) break
|
||||
@@ -420,4 +507,51 @@ data class SaveFileInput(
|
||||
val syncStatus: String? = null,
|
||||
/** Le fichier arrive déjà « traité » (ex. scan) ou à traiter dans la review. */
|
||||
val processed: Boolean = false,
|
||||
/**
|
||||
* V3 S5 : session de scan opaque (pages d'un même export) — propagée au
|
||||
* `create_resource` pour les arêtes `same_scan` serveur. NULL sinon.
|
||||
*/
|
||||
val scanSessionId: String? = null,
|
||||
)
|
||||
|
||||
/**
|
||||
* Facettes de recherche documentaire V3 (S4) — miroir de `SearchFacets` Go
|
||||
* (dates ISO `YYYY-MM-DD`, montants en Double). `isEmpty` = aucun filtre.
|
||||
*/
|
||||
data class SearchFacets(
|
||||
val issuer: String? = null,
|
||||
val docType: String? = null,
|
||||
val minAmount: Double? = null,
|
||||
val maxAmount: Double? = null,
|
||||
val fromDate: String? = null,
|
||||
val toDate: String? = null,
|
||||
) {
|
||||
fun isEmpty(): Boolean =
|
||||
issuer.isNullOrBlank() && docType.isNullOrBlank() &&
|
||||
minAmount == null && maxAmount == null &&
|
||||
fromDate.isNullOrBlank() && toDate.isNullOrBlank()
|
||||
}
|
||||
|
||||
/**
|
||||
* Expression FTS (`"tok" OR …`) depuis la saisie — guillemets doublés
|
||||
* (syntaxe FTS), tokens ≥ 2 lettres, max 5. NULL si aucun token (l'appelant
|
||||
* bascule sur les facettes seules).
|
||||
*/
|
||||
fun buildFtsMatch(query: String): String? {
|
||||
val toks = query.split(Regex("\\s+"))
|
||||
.map { it.replace("\"", "\"\"") }
|
||||
.filter { it.length >= 2 }
|
||||
.take(5)
|
||||
if (toks.isEmpty()) return null
|
||||
return toks.joinToString(" OR ") { "\"$it\"" }
|
||||
}
|
||||
|
||||
/** Mots du rang (≥ 3 lettres, échappés LIKE) — miroir de `longWords` Go. */
|
||||
fun ftsWords(query: String): List<String?> {
|
||||
val seen = LinkedHashSet<String>()
|
||||
for (w in query.split(Regex("\\s+"))) {
|
||||
if (w.length >= 3) seen += w
|
||||
if (seen.size >= 5) break
|
||||
}
|
||||
return seen.map { it.escapeLike() }
|
||||
}
|
||||
@@ -56,6 +56,21 @@ class FolderRepository @Inject constructor(
|
||||
suspend fun getChildren(parentResourceId: String): List<FolderEntity> =
|
||||
folderDao.getByParentOnce(parentResourceId)
|
||||
|
||||
/** Enfants sans filtre `exists` — collecte trash/restore (FolderDeleter). */
|
||||
suspend fun getChildrenAll(parentResourceId: String): List<FolderEntity> =
|
||||
folderDao.getChildrenAll(parentResourceId)
|
||||
|
||||
/** Lignes trashées (écran Corbeille). */
|
||||
fun observeTrashed(): Flow<List<FolderEntity>> = folderDao.observeTrashed()
|
||||
|
||||
/** Trash local (tombstone) — dans la transaction de l'op outbox. */
|
||||
suspend fun markTrashed(resourceId: String, trashedAt: Long) =
|
||||
folderDao.markTrashed(resourceId, trashedAt)
|
||||
|
||||
/** Restore local — dans la transaction de l'op outbox `restore_resource`. */
|
||||
suspend fun clearTrashed(resourceId: String, now: Long) =
|
||||
folderDao.clearTrashed(resourceId, now)
|
||||
|
||||
/** Dossiers créés dans l'app uniquement (cibles du picker de déplacement). */
|
||||
suspend fun getCreatedInApp(): List<FolderEntity> = folderDao.getCreatedInApp()
|
||||
|
||||
@@ -80,6 +95,15 @@ class FolderRepository @Inject constructor(
|
||||
// synced : le serveur renvoie encore l'ancien parent.
|
||||
outboxRepository.hasPendingMoveOperation(dto.id)
|
||||
}
|
||||
.filterNot { dto ->
|
||||
// Destruction définitive en cours (V3) : ne pas réimporter.
|
||||
outboxRepository.hasPendingPurgeOperation(dto.id)
|
||||
}
|
||||
.filterNot { dto ->
|
||||
// Ligne trashée localement : le serveur la renvoie encore
|
||||
// tant que l'op trash n'est pas synced — ne pas ressusciter.
|
||||
folderDao.getByResourceId(dto.id)?.trashedAt != null
|
||||
}
|
||||
.map { toEntity(it, now) }
|
||||
if (rows.isNotEmpty()) folderDao.upsertAll(rows)
|
||||
}
|
||||
@@ -129,6 +153,11 @@ class FolderRepository @Inject constructor(
|
||||
return existing.copy(exists = 0, updatedAt = now)
|
||||
}
|
||||
|
||||
// Garde corbeille V3 : voir `FileRepository.saveLocalFile`.
|
||||
if (existing != null && existing.trashedAt != null) {
|
||||
return existing
|
||||
}
|
||||
|
||||
// Garde pending-move (M1) : un snapshot SAF périmé ne revert jamais un
|
||||
// reparent/merge en cours de push.
|
||||
val movePending = existing != null && outboxRepository.hasPendingMoveOperation(existing.resourceId)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user