From c77af98a688b31dba41f4302b040d6721ee58aef Mon Sep 17 00:00:00 2001 From: venture Date: Fri, 25 Sep 2026 18:20:57 +0200 Subject: [PATCH] V2-1 : upload auto des octets (BytesUploadWorker, Room v17, SafMultipart) --- AGENTS.md | 8 +- CHANGELOG.md | 3 + .../17.json | 742 ++++++++++++++++++ .../kazier/mobile/data/local/AppDatabase.kt | 3 +- .../kazier/mobile/data/local/dao/FileDao.kt | 26 + .../mobile/data/local/entity/FileEntity.kt | 10 + .../mobile/data/local/migration/Migrations.kt | 15 +- .../kazier/mobile/data/remote/SafMultipart.kt | 45 ++ .../mobile/data/repository/OcrRepository.kt | 36 +- .../repository/RemoteContentRepository.kt | 60 ++ .../mobile/features/sync/BytesUploadWorker.kt | 97 +++ .../mobile/features/sync/OutboxSyncWorker.kt | 7 +- .../mobile/features/sync/SyncViewModel.kt | 2 + .../kazier/mobile/ui/auth/AuthViewModel.kt | 3 + .../local/migration/Migration16To17Test.kt | 81 ++ .../mobile/data/repository/BytesUploadTest.kt | 263 +++++++ 16 files changed, 1360 insertions(+), 41 deletions(-) create mode 100644 mobile-kotlin/app/schemas/com.kazier.mobile.data.local.AppDatabase/17.json create mode 100644 mobile-kotlin/app/src/main/java/com/kazier/mobile/data/remote/SafMultipart.kt create mode 100644 mobile-kotlin/app/src/main/java/com/kazier/mobile/features/sync/BytesUploadWorker.kt create mode 100644 mobile-kotlin/app/src/test/java/com/kazier/mobile/data/local/migration/Migration16To17Test.kt create mode 100644 mobile-kotlin/app/src/test/java/com/kazier/mobile/data/repository/BytesUploadTest.kt diff --git a/AGENTS.md b/AGENTS.md index da63dfe..555dd1d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -52,13 +52,13 @@ 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 16**, `Migrations.kt` : tables `folders`, `files` (+ `category`, `processed`, `content`, `ocr_text`, `ocr_attempts`, `ocr_queued_at`), `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/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`) - - `data/repository/` — `FolderRepository`, `FileRepository`, `AuthRepository`, `OutboxRepository` (enqueue `create_resource`/`move_resource`/`delete_resource` + ops partage `share`/`update_share`/`revoke_share`/`create_link`/`revoke_link`, é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é) + - `data/local/` — Room SQLite (DB `dot.db`, **version 17**, `Migrations.kt` : tables `folders`, `files` (+ `category`, `processed`, `content`, `ocr_text`, `ocr_attempts`, `ocr_queued_at`, `bytes_uploaded`, `bytes_upload_attempts`), `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/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`, é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`) - `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) - - `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) ; **schedulé** au login/restauration de session (`AuthViewModel`) et après chaque `syncAll()` (`SyncViewModel`) + - `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) ; **schedulé** au login/restauration de session (`AuthViewModel`) et après chaque `syncAll()` (`SyncViewModel`) ; enchaîne `OcrAutoWorker` + `BytesUploadWorker` quand un fichier est promu - `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), `pdfbuilder` (multi-select → génération PDF), `auth` (login, mode local, avis d'expiration), `share` (`ShareBottomSheet` : grants + liens + ouverture de lien ; `OpenLinkScreen` + deep-link `kazier://share/`), `components`, `theme` diff --git a/CHANGELOG.md b/CHANGELOG.md index 75d091a..201ce6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ Le format suit [Keep a Changelog](https://keepachangelog.com/fr/1.1.0/) et le ve ## [Unreleased] +### Ajouté +- Upload V2 des octets : worker `BytesUploadWorker` (fichier par fichier, overwrite ciblée idempotente, cap 3 échecs, backoff), marqueurs `bytes_uploaded`/`bytes_upload_attempts` (Room v17, migration 16→17), multipart SAF mutualisé (`SafMultipart.kt`). + ## [1.0.0] - 2026-09-25 ### Ajouté diff --git a/mobile-kotlin/app/schemas/com.kazier.mobile.data.local.AppDatabase/17.json b/mobile-kotlin/app/schemas/com.kazier.mobile.data.local.AppDatabase/17.json new file mode 100644 index 0000000..f740cbe --- /dev/null +++ b/mobile-kotlin/app/schemas/com.kazier.mobile.data.local.AppDatabase/17.json @@ -0,0 +1,742 @@ +{ + "formatVersion": 1, + "database": { + "version": 17, + "identityHash": "97790c7bc08828c46cc63c36d628a327", + "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, `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": "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, `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": "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, '97790c7bc08828c46cc63c36d628a327')" + ] + } +} \ No newline at end of file diff --git a/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/local/AppDatabase.kt b/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/local/AppDatabase.kt index 45a0490..efc4ef5 100644 --- a/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/local/AppDatabase.kt +++ b/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/local/AppDatabase.kt @@ -32,6 +32,7 @@ import com.kazier.mobile.data.local.entity.UserPreferenceEntity * v14: parent_resource_id sur pending_operations (cascade P8). * v15: backfill exists NULL→1 sur folders/files (M13). * v16: table resource_permissions (cache snapshot, M27). + * v17: bytes_uploaded + bytes_upload_attempts sur files (upload V2). */ @Database( entities = [ @@ -39,7 +40,7 @@ import com.kazier.mobile.data.local.entity.UserPreferenceEntity PendingOperationEntity::class, ScanSessionEntity::class, ScanPageEntity::class, ResourcePermissionEntity::class, ], - version = 16, + version = 17, // 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, diff --git a/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/local/dao/FileDao.kt b/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/local/dao/FileDao.kt index 3557305..8add2e3 100644 --- a/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/local/dao/FileDao.kt +++ b/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/local/dao/FileDao.kt @@ -215,4 +215,30 @@ interface FileDao { /** Échec terminal : +1 tentative, marqueur levé (ne rejoue pas à l'infini). */ @Query("UPDATE files SET ocr_attempts = ocr_attempts + 1, ocr_queued_at = NULL, updated_at = :now WHERE resource_id = :resourceId") suspend fun markOcrFailure(resourceId: String, now: Long) + + /** + * Candidats upload V2 : copie physique locale, métadonnées déjà connues + * du serveur (`local-cloud` = `create_resource` synced), octets jamais + * poussés, pas épuisés (< 3 échecs terminaux). Le serveur overwrite en + * aveugle sur re-upload ciblé : l'idempotence est au `resource_id`. + */ + @Query(""" + SELECT * FROM files + WHERE "exists" = 1 + AND processed = 1 + AND sync_status = 'local-cloud' + AND uri IS NOT NULL + AND bytes_uploaded = 0 + AND bytes_upload_attempts < 3 + ORDER BY COALESCE(last_modified, added_at) DESC, name ASC + """) + suspend fun getBytesUploadCandidates(): List + + /** Octets confirmés serveur (upload ciblé réussi). */ + @Query("UPDATE files SET bytes_uploaded = 1, updated_at = :now WHERE resource_id = :resourceId") + suspend fun markBytesUploaded(resourceId: String, now: Long) + + /** Échec terminal d'upload (4xx : trop gros, inconnu, rejeté). */ + @Query("UPDATE files SET bytes_upload_attempts = bytes_upload_attempts + 1, updated_at = :now WHERE resource_id = :resourceId") + suspend fun markBytesUploadFailure(resourceId: String, now: Long) } \ No newline at end of file diff --git a/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/local/entity/FileEntity.kt b/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/local/entity/FileEntity.kt index 2bdbb28..835e736 100644 --- a/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/local/entity/FileEntity.kt +++ b/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/local/entity/FileEntity.kt @@ -63,6 +63,16 @@ data class FileEntity( val ocrAttempts: Int = 0, @ColumnInfo(name = "ocr_queued_at") val ocrQueuedAt: Long? = null, + /** + * V2 : octets poussés vers le serveur (`POST /files/upload` ciblé) — + * 0 = en attente, 1 = présents serveur. Distinct de `sync_status` + * (métadonnées connues : `local-cloud`) qui ne dit rien des octets. + */ + @ColumnInfo(name = "bytes_uploaded") + val bytesUploaded: Int = 0, + /** Tentatives d'upload définitives (4xx) — cap 3, comme `ocr_attempts`. */ + @ColumnInfo(name = "bytes_upload_attempts") + val bytesUploadAttempts: Int = 0, @ColumnInfo(name = "added_at") val addedAt: Long, @ColumnInfo(name = "updated_at") diff --git a/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/local/migration/Migrations.kt b/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/local/migration/Migrations.kt index eecbfee..7206c85 100644 --- a/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/local/migration/Migrations.kt +++ b/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/local/migration/Migrations.kt @@ -291,7 +291,20 @@ object Migrations { } } + /** + * V2 : upload générique des octets — `bytes_uploaded` (0 = en attente, + * 1 = présents serveur) + `bytes_upload_attempts` (cap 3, comme + * `ocr_attempts`). Zéro backfill : tout fichier existant est + * ré-uploadé une fois (idempotent côté serveur, overwrite ciblé). + */ + private val MIGRATION_16_17 = object : Migration(16, 17) { + override fun migrate(db: SupportSQLiteDatabase) { + db.execSQL("ALTER TABLE `files` ADD COLUMN `bytes_uploaded` INTEGER NOT NULL DEFAULT 0") + db.execSQL("ALTER TABLE `files` ADD COLUMN `bytes_upload_attempts` INTEGER NOT NULL DEFAULT 0") + } + } + val ALL: Array = 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_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, ) } \ No newline at end of file diff --git a/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/remote/SafMultipart.kt b/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/remote/SafMultipart.kt new file mode 100644 index 0000000..3908dd5 --- /dev/null +++ b/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/remote/SafMultipart.kt @@ -0,0 +1,45 @@ +package com.kazier.mobile.data.remote + +import android.content.ContentResolver +import androidx.core.net.toUri +import okhttp3.MediaType +import okhttp3.MultipartBody +import okhttp3.RequestBody +import okio.BufferedSink +import okio.source + +/** + * Corps multipart paresseux lisant un flux SAF à l'écriture (jamais chargé + * en RAM) — partagé par l'upload OCR ciblé (`OcrRepository`) et l'upload + * générique V2 (`RemoteContentRepository`). + */ +internal class SafFileRequestBody( + private val contentResolver: ContentResolver, + private val uri: String, + private val length: Long, + private val mediaType: MediaType?, +) : RequestBody() { + + override fun contentType(): MediaType? = mediaType + + override fun contentLength(): Long = length + + override fun writeTo(sink: BufferedSink) { + contentResolver.openInputStream(uri.toUri())?.use { stream -> + sink.writeAll(stream.source()) + } + } +} + +/** Construit la partie `file` multipart depuis une URI SAF + métadonnées. */ +internal fun ContentResolver.safFilePart( + uri: String, + name: String, + size: Long, + mediaType: MediaType?, +): MultipartBody.Part = + MultipartBody.Part.createFormData( + "file", + name, + SafFileRequestBody(this, uri, size, mediaType), + ) diff --git a/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/repository/OcrRepository.kt b/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/repository/OcrRepository.kt index 2d0a9f1..2303d6b 100644 --- a/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/repository/OcrRepository.kt +++ b/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/repository/OcrRepository.kt @@ -1,23 +1,16 @@ package com.kazier.mobile.data.repository -import android.content.ContentResolver import android.content.Context -import android.net.Uri -import androidx.core.net.toUri import com.kazier.mobile.data.local.dao.FileDao import com.kazier.mobile.data.local.entity.FileEntity import com.kazier.mobile.data.remote.ApiClient import com.kazier.mobile.data.remote.ApiException import com.kazier.mobile.data.remote.dto.OcrJobDto import com.kazier.mobile.data.remote.dto.OcrJobStatus +import com.kazier.mobile.data.remote.safFilePart import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.delay -import okhttp3.MediaType import okhttp3.MediaType.Companion.toMediaTypeOrNull -import okhttp3.MultipartBody -import okhttp3.RequestBody -import okio.BufferedSink -import okio.source import javax.inject.Inject import javax.inject.Singleton @@ -48,9 +41,9 @@ class OcrRepository @Inject constructor( suspend fun ensurePhysical(file: FileEntity) { val uri = file.uri ?: return val mediaType = file.mimeType?.toMediaTypeOrNull() - val body = SafFileRequestBody(context.contentResolver, uri, file.size, mediaType) + val part = context.contentResolver.safFilePart(uri, file.name, file.size, mediaType) apiClient.uploadFile( - file = MultipartBody.Part.createFormData("file", file.name, body), + file = part, resourceId = file.resourceId, ) } @@ -178,27 +171,4 @@ sealed interface OcrAutoOutcome { /** Token expiré/révoqué : le re-login passera par l'UI. */ data object Unauthorized : OcrAutoOutcome -} - -/** - * `RequestBody` paresseux : le flux SAF est ouvert à l'écriture (multipart), - * jamais chargé en mémoire — les fichiers jusqu'à `MAX_FILE_SIZE_MB` restent - * gérables. - */ -private class SafFileRequestBody( - private val contentResolver: ContentResolver, - private val uri: String, - private val length: Long, - private val mediaType: MediaType?, -) : RequestBody() { - - override fun contentType(): MediaType? = mediaType - - override fun contentLength(): Long = length - - override fun writeTo(sink: BufferedSink) { - contentResolver.openInputStream(uri.toUri())?.use { stream -> - sink.writeAll(stream.source()) - } - } } \ No newline at end of file diff --git a/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/repository/RemoteContentRepository.kt b/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/repository/RemoteContentRepository.kt index 4f56cf8..3939d3f 100644 --- a/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/repository/RemoteContentRepository.kt +++ b/mobile-kotlin/app/src/main/java/com/kazier/mobile/data/repository/RemoteContentRepository.kt @@ -1,6 +1,13 @@ package com.kazier.mobile.data.repository +import android.content.Context +import com.kazier.mobile.data.local.dao.FileDao +import com.kazier.mobile.data.local.entity.FileEntity import com.kazier.mobile.data.remote.ApiClient +import com.kazier.mobile.data.remote.ApiException +import com.kazier.mobile.data.remote.safFilePart +import dagger.hilt.android.qualifiers.ApplicationContext +import okhttp3.MediaType.Companion.toMediaTypeOrNull import javax.inject.Inject import javax.inject.Singleton @@ -12,12 +19,65 @@ import javax.inject.Singleton * persister : ni Room, ni SAF, ni fichier durable. Seul le rendu PDF exige * un fichier temporaire du `cacheDir`, créé à la volée par le viewer et * supprimé à sa sortie (éphémère, pas un cache). + * + * Upload générique V2 ([upload]) : pousse les octets SAF vers le + * `resource_id` serveur déjà connu (métadonnées synced) via l'upload ciblé + * `POST /files/upload` — même pipeline que l'OCR, hors outbox. */ @Singleton class RemoteContentRepository @Inject constructor( private val apiClient: ApiClient, + private val fileDao: FileDao, + @ApplicationContext private val context: Context, ) { /** Octets du fichier, bornés ([ApiClient.MAX_CONTENT_BYTES]). */ suspend fun fetch(fileId: String): ByteArray = apiClient.downloadContentBytes(fileId) + + /** + * Pousse les octets locaux vers le serveur (overwrite ciblé idempotent). + * Succès → `bytes_uploaded = 1` ; 4xx (trop gros, rejeté) → +1 tentative + * (cap 3, le worker passe au suivant) ; réseau/5xx → `Retryable` sans + * toucher au compteur ; 401 → `Unauthorized` (re-login via l'UI). + */ + suspend fun upload(file: FileEntity): BytesUploadOutcome { + val uri = file.uri ?: return BytesUploadOutcome.Failed.also { + fileDao.markBytesUploadFailure(file.resourceId, System.currentTimeMillis()) + } + return try { + val part = context.contentResolver.safFilePart( + uri, file.name, file.size, file.mimeType?.toMediaTypeOrNull(), + ) + apiClient.uploadFile(file = part, resourceId = file.resourceId) + fileDao.markBytesUploaded(file.resourceId, System.currentTimeMillis()) + BytesUploadOutcome.Done + } catch (e: ApiException) { + when { + e.code == "NETWORK_ERROR" || e.httpCode >= 500 -> BytesUploadOutcome.Retryable + e.httpCode == 401 -> BytesUploadOutcome.Unauthorized + else -> { + fileDao.markBytesUploadFailure(file.resourceId, System.currentTimeMillis()) + BytesUploadOutcome.Failed + } + } + } catch (e: Exception) { + fileDao.markBytesUploadFailure(file.resourceId, System.currentTimeMillis()) + BytesUploadOutcome.Failed + } + } +} + +/** Résultat d'une passe d'upload V2 (un fichier max). */ +sealed interface BytesUploadOutcome { + /** Octets confirmés serveur : `bytes_uploaded = 1`. */ + data object Done : BytesUploadOutcome + + /** Erreur permanente : `bytes_upload_attempts` incrémenté. */ + data object Failed : BytesUploadOutcome + + /** Erreur transitoire : à retenter via backoff WorkManager. */ + data object Retryable : BytesUploadOutcome + + /** Token expiré/révoqué : le re-login passera par l'UI. */ + data object Unauthorized : BytesUploadOutcome } diff --git a/mobile-kotlin/app/src/main/java/com/kazier/mobile/features/sync/BytesUploadWorker.kt b/mobile-kotlin/app/src/main/java/com/kazier/mobile/features/sync/BytesUploadWorker.kt new file mode 100644 index 0000000..2e6c28f --- /dev/null +++ b/mobile-kotlin/app/src/main/java/com/kazier/mobile/features/sync/BytesUploadWorker.kt @@ -0,0 +1,97 @@ +package com.kazier.mobile.features.sync + +import android.content.Context +import androidx.hilt.work.HiltWorker +import androidx.work.BackoffPolicy +import androidx.work.Constraints +import androidx.work.CoroutineWorker +import androidx.work.ExistingWorkPolicy +import androidx.work.NetworkType +import androidx.work.OneTimeWorkRequestBuilder +import androidx.work.WorkManager +import androidx.work.WorkerParameters +import com.kazier.mobile.auth.TokenProvider +import com.kazier.mobile.data.local.dao.FileDao +import com.kazier.mobile.data.remote.ApiException +import com.kazier.mobile.data.repository.BytesUploadOutcome +import com.kazier.mobile.data.repository.RemoteContentRepository +import dagger.assisted.Assisted +import dagger.assisted.AssistedInject +import java.util.concurrent.TimeUnit +import timber.log.Timber + +/** + * Upload générique V2, fichier par fichier : à chaque exécution, un seul + * candidat (copie SAF locale, métadonnées synced `local-cloud`, octets jamais + * poussés, `< 3` échecs) est envoyé via l'upload ciblé `POST /files/upload` + * (`resourceId` = overwrite idempotent). Un seul run à la fois via + * `enqueueUniqueWork(KEEP)`, self-chain tant qu'il reste des candidats + * *distincts* (même discipline que `OcrAutoWorker`). + * + * Erreurs : + * - **réseau / 5xx** → `Result.retry()` (backoff 30 s), compteur intact ; + * - **4xx** (trop gros, rejeté) → +1 tentative, pas de re-enchaine ; + * - **token expiré (401)** → `Result.success()` (re-login passera par l'UI). + */ +@HiltWorker +class BytesUploadWorker @AssistedInject constructor( + @Assisted appContext: Context, + @Assisted workerParams: WorkerParameters, + private val remoteContentRepository: RemoteContentRepository, + private val fileDao: FileDao, + private val tokenProvider: TokenProvider, +) : CoroutineWorker(appContext, workerParams) { + + override suspend fun doWork(): Result { + if (tokenProvider.current == null) return Result.success() + + val file = fileDao.getBytesUploadCandidates().firstOrNull() + ?: return Result.success() + + Timber.d("bytes-upload: candidate %s (%s)", file.name, file.resourceId) + val outcome = try { + remoteContentRepository.upload(file) + } catch (e: ApiException) { + when { + e.code == "NETWORK_ERROR" || e.httpCode >= 500 -> BytesUploadOutcome.Retryable + e.httpCode == 401 -> BytesUploadOutcome.Unauthorized + else -> BytesUploadOutcome.Failed + } + } catch (e: Exception) { + Timber.w(e, "bytes-upload: unexpected failure for %s", file.resourceId) + BytesUploadOutcome.Failed + } + + when (outcome) { + is BytesUploadOutcome.Done -> + Timber.d("bytes-upload: done %s", file.resourceId) + is BytesUploadOutcome.Failed -> + Timber.w("bytes-upload: failed %s", file.resourceId) + is BytesUploadOutcome.Retryable -> return Result.retry() + is BytesUploadOutcome.Unauthorized -> return Result.success() + } + + // Self-chain : un autre candidat *distinct* du dernier attend ? + val more = fileDao.getBytesUploadCandidates().any { it.resourceId != file.resourceId } + if (more) { + enqueue(applicationContext) + } + return Result.success() + } + + companion object { + const val NAME = "bytes_upload" + + fun enqueue(context: Context) { + val constraints = Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build() + val request = OneTimeWorkRequestBuilder() + .setConstraints(constraints) + .setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 30, TimeUnit.SECONDS) + .build() + WorkManager.getInstance(context) + .enqueueUniqueWork(NAME, ExistingWorkPolicy.KEEP, request) + } + } +} diff --git a/mobile-kotlin/app/src/main/java/com/kazier/mobile/features/sync/OutboxSyncWorker.kt b/mobile-kotlin/app/src/main/java/com/kazier/mobile/features/sync/OutboxSyncWorker.kt index f21cf16..ce157a7 100644 --- a/mobile-kotlin/app/src/main/java/com/kazier/mobile/features/sync/OutboxSyncWorker.kt +++ b/mobile-kotlin/app/src/main/java/com/kazier/mobile/features/sync/OutboxSyncWorker.kt @@ -103,7 +103,7 @@ class OutboxSyncWorker @AssistedInject constructor( Types.newParameterizedType(Map::class.java, String::class.java, Any::class.java), ) - /** Set à true si un fichier a été promu pendant ce run → enchaîne OcrAutoWorker. */ + /** Set à true si un fichier a été promu pendant ce run → enchaîne OCR + upload octets. */ private var filePromoted = false override suspend fun doWork(): Result { @@ -121,7 +121,10 @@ class OutboxSyncWorker @AssistedInject constructor( val pending = pendingOperationDao.selectPending(BATCH_SIZE) if (pending.isEmpty()) { pendingOperationDao.purgeSynced(System.currentTimeMillis() - PURGE_AGE_MS) - if (filePromoted) OcrAutoWorker.enqueue(applicationContext) + if (filePromoted) { + OcrAutoWorker.enqueue(applicationContext) + BytesUploadWorker.enqueue(applicationContext) + } return Result.success() } diff --git a/mobile-kotlin/app/src/main/java/com/kazier/mobile/features/sync/SyncViewModel.kt b/mobile-kotlin/app/src/main/java/com/kazier/mobile/features/sync/SyncViewModel.kt index 179ccf4..f967f33 100644 --- a/mobile-kotlin/app/src/main/java/com/kazier/mobile/features/sync/SyncViewModel.kt +++ b/mobile-kotlin/app/src/main/java/com/kazier/mobile/features/sync/SyncViewModel.kt @@ -146,6 +146,7 @@ class SyncViewModel @Inject constructor( if (changed || pendingOperationDao.countPending() > 0) { OutboxSyncWorker.enqueue(appContext) OcrAutoWorker.enqueue(appContext) + BytesUploadWorker.enqueue(appContext) } } finally { // m1 : le badge « syncing » retombe même si le scope est @@ -182,6 +183,7 @@ class SyncViewModel @Inject constructor( // la racine ne doit pas dormir jusqu'au prochain tick. OutboxSyncWorker.enqueue(appContext) OcrAutoWorker.enqueue(appContext) + BytesUploadWorker.enqueue(appContext) runCatching { fileDeleter.sweepPendingPhysicalDeletes() + folderDeleter.sweepPendingPhysicalDeletes() diff --git a/mobile-kotlin/app/src/main/java/com/kazier/mobile/ui/auth/AuthViewModel.kt b/mobile-kotlin/app/src/main/java/com/kazier/mobile/ui/auth/AuthViewModel.kt index 52be511..c0ab711 100644 --- a/mobile-kotlin/app/src/main/java/com/kazier/mobile/ui/auth/AuthViewModel.kt +++ b/mobile-kotlin/app/src/main/java/com/kazier/mobile/ui/auth/AuthViewModel.kt @@ -9,6 +9,7 @@ import com.kazier.mobile.data.remote.ApiException import com.kazier.mobile.data.repository.AuthRepository import com.kazier.mobile.data.repository.ShareRepository import com.kazier.mobile.features.sync.OutboxSyncWorker +import com.kazier.mobile.features.sync.BytesUploadWorker import com.kazier.mobile.features.ocr.OcrAutoWorker import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.qualifiers.ApplicationContext @@ -69,6 +70,7 @@ class AuthViewModel @Inject constructor( // Session restaurée → drainer l'outbox laissée en attente. OutboxSyncWorker.enqueue(appContext) OcrAutoWorker.enqueue(appContext) + BytesUploadWorker.enqueue(appContext) if (session != null && _authState.value is AuthState.SignedIn) { // Snapshot complet des permissions partagées (convergence, M27). runCatching { shareRepository.syncSnapshot(forceFull = true) } @@ -106,6 +108,7 @@ class AuthViewModel @Inject constructor( if (reseed) { OutboxSyncWorker.enqueue(appContext) OcrAutoWorker.enqueue(appContext) + BytesUploadWorker.enqueue(appContext) viewModelScope.launch { runCatching { shareRepository.syncSnapshot(forceFull = true) } .onFailure { e -> Timber.d("syncSnapshot on login failed: %s", e.message) } diff --git a/mobile-kotlin/app/src/test/java/com/kazier/mobile/data/local/migration/Migration16To17Test.kt b/mobile-kotlin/app/src/test/java/com/kazier/mobile/data/local/migration/Migration16To17Test.kt new file mode 100644 index 0000000..abbdc5a --- /dev/null +++ b/mobile-kotlin/app/src/test/java/com/kazier/mobile/data/local/migration/Migration16To17Test.kt @@ -0,0 +1,81 @@ +package com.kazier.mobile.data.local.migration + +import android.content.Context +import androidx.sqlite.db.SupportSQLiteDatabase +import androidx.sqlite.db.SupportSQLiteOpenHelper +import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory +import androidx.test.core.app.ApplicationProvider +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** + * V2 : la migration 16→17 ajoute `bytes_uploaded` (0 = en attente, 1 = + * présents serveur) + `bytes_upload_attempts` (cap 3) sur `files`. Les lignes + * existantes repartent à 0/0 (ré-upload unique, overwrite ciblé idempotent). + * Une coquille SQL briquerait l'app au démarrage après update. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class Migration16To17Test { + + @Test + fun migration_ajoute_les_colonnes_avec_defaut_zero() { + val context = ApplicationProvider.getApplicationContext() + val helper = FrameworkSQLiteOpenHelperFactory().create( + SupportSQLiteOpenHelper.Configuration.builder(context) + .name(null) + .callback(V16Callback()) + .build(), + ) + val db = helper.writableDatabase + db.execSQL( + "INSERT INTO `files` (`resource_id`, `name`, `folder_resource_id`, `size`, `added_at`, `updated_at`) " + + "VALUES ('d-old', 'vieux.txt', 'f-one', 10, 1, 1)", + ) + + val migration = Migrations.ALL.first { it.startVersion == 16 && it.endVersion == 17 } + migration.migrate(db) + + val columns = mutableListOf() + db.query("PRAGMA table_info(`files`)").use { cursor -> + val nameIdx = cursor.getColumnIndexOrThrow("name") + while (cursor.moveToNext()) columns += cursor.getString(nameIdx) + } + assertTrue("colonne bytes_uploaded manquante", columns.contains("bytes_uploaded")) + assertTrue("colonne bytes_upload_attempts manquante", columns.contains("bytes_upload_attempts")) + + db.query("SELECT `bytes_uploaded`, `bytes_upload_attempts` FROM `files` WHERE `resource_id` = 'd-old'").use { cursor -> + assertTrue(cursor.moveToFirst()) + assertEquals(0, cursor.getInt(0)) + assertEquals(0, cursor.getInt(1)) + } + helper.close() + } + + /** Schéma v16 minimal de `files` (sans les colonnes V2). */ + private class V16Callback : SupportSQLiteOpenHelper.Callback(16) { + override fun onCreate(db: SupportSQLiteDatabase) { + db.execSQL( + "CREATE TABLE `files` (" + + "`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " + + "`resource_id` TEXT NOT NULL, " + + "`uri` TEXT, " + + "`name` TEXT NOT NULL, " + + "`folder_resource_id` TEXT NOT NULL, " + + "`size` INTEGER NOT NULL, " + + "`added_at` INTEGER NOT NULL, " + + "`updated_at` INTEGER NOT NULL)", + ) + } + + override fun onUpgrade( + db: SupportSQLiteDatabase, + oldVersion: Int, + newVersion: Int, + ) = Unit + } +} diff --git a/mobile-kotlin/app/src/test/java/com/kazier/mobile/data/repository/BytesUploadTest.kt b/mobile-kotlin/app/src/test/java/com/kazier/mobile/data/repository/BytesUploadTest.kt new file mode 100644 index 0000000..e0d4248 --- /dev/null +++ b/mobile-kotlin/app/src/test/java/com/kazier/mobile/data/repository/BytesUploadTest.kt @@ -0,0 +1,263 @@ +package com.kazier.mobile.data.repository + +import android.content.Context +import androidx.core.net.toUri +import androidx.room.Room +import androidx.test.core.app.ApplicationProvider +import com.squareup.moshi.Moshi +import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory +import com.kazier.mobile.data.local.AppDatabase +import com.kazier.mobile.data.local.dao.FileDao +import com.kazier.mobile.data.local.entity.FileEntity +import com.kazier.mobile.data.local.entity.FileStatus +import com.kazier.mobile.data.remote.ApiClient +import com.kazier.mobile.data.remote.ApiException +import com.kazier.mobile.data.remote.ApiService +import com.kazier.mobile.data.remote.dto.ApiEnvelope +import com.kazier.mobile.data.remote.dto.ChangePasswordDto +import com.kazier.mobile.data.remote.dto.DeviceRegistrationDto +import com.kazier.mobile.data.remote.dto.FileDto +import com.kazier.mobile.data.remote.dto.FileOcrDto +import com.kazier.mobile.data.remote.dto.FolderDto +import com.kazier.mobile.data.remote.dto.IdResultDto +import com.kazier.mobile.data.remote.dto.LoginRequestDto +import com.kazier.mobile.data.remote.dto.LoginResponseDto +import com.kazier.mobile.data.remote.dto.OcrJobDto +import com.kazier.mobile.data.remote.dto.OcrSubmitDto +import com.kazier.mobile.data.remote.dto.ResolvedUserDto +import com.kazier.mobile.data.remote.dto.ResourcePermissionDto +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 java.io.ByteArrayInputStream +import kotlinx.coroutines.test.runTest +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.MultipartBody +import okhttp3.RequestBody +import okhttp3.ResponseBody +import okhttp3.ResponseBody.Companion.toResponseBody +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import retrofit2.Response + +/** + * Upload générique V2 : `RemoteContentRepository.upload` pousse les octets + * SAF vers le `resource_id` connu (overwrite ciblé), marque + * `bytes_uploaded = 1`, borne les échecs terminaux à 3 tentatives. + * Les candidats exigent métadonnées synced (`local-cloud`) + copie locale. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class BytesUploadTest { + + private lateinit var context: Context + private lateinit var db: AppDatabase + private lateinit var fileDao: FileDao + private lateinit var apiService: UploadFake + private lateinit var repository: RemoteContentRepository + + @Before + fun setUp() { + context = ApplicationProvider.getApplicationContext() + db = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java) + .allowMainThreadQueries() + .build() + fileDao = db.fileDao() + apiService = UploadFake() + repository = RemoteContentRepository(ApiClient(apiService, moshi()), fileDao, context) + } + + @After + fun tearDown() { + db.close() + } + + @Test + fun `upload_succes_marque_bytes_uploaded`() = runTest { + val file = insertCandidate("content://up/ok.txt") + + val outcome = repository.upload(file) + + assertTrue(outcome is BytesUploadOutcome.Done) + assertEquals("ok-content", apiService.uploadedBytes) + assertEquals(file.resourceId, apiService.uploadedResourceId) + assertEquals(1, fileDao.getByResourceId(file.resourceId)?.bytesUploaded) + } + + @Test + fun `upload_413_incremente_tentatives_et_failed`() = runTest { + apiService.mode = UploadFake.Mode.TOO_LARGE + val file = insertCandidate("content://up/big.bin") + + val outcome = repository.upload(file) + + assertTrue(outcome is BytesUploadOutcome.Failed) + val stored = fileDao.getByResourceId(file.resourceId)!! + assertEquals(0, stored.bytesUploaded) + assertEquals(1, stored.bytesUploadAttempts) + } + + @Test + fun `upload_reseau_retryable_sans_toucher_au_compteur`() = runTest { + apiService.mode = UploadFake.Mode.NETWORK + val file = insertCandidate("content://up/flaky.txt") + + val outcome = repository.upload(file) + + assertTrue(outcome is BytesUploadOutcome.Retryable) + val stored = fileDao.getByResourceId(file.resourceId)!! + assertEquals(0, stored.bytesUploaded) + assertEquals(0, stored.bytesUploadAttempts) + } + + @Test + fun `candidats_exigent_local_cloud_propre_octets_et_tentatives`() = runTest { + val ok = insertCandidate("content://up/a.txt") + // Métadonnées pas encore synced → pas candidat. + insertCandidate("content://up/b.txt", syncStatus = FileStatus.LOCAL) + // Sans copie locale → pas candidat. + insertCandidate(uri = null) + // Déjà poussé → pas candidat. + insertCandidate("content://up/d.txt", bytesUploaded = 1) + // Épuisé (3 échecs) → pas candidat. + insertCandidate("content://up/e.txt", bytesUploadAttempts = 3) + // Pas encore traité (gate processed) → pas candidat. + insertCandidate("content://up/f.txt", processed = false) + + val candidates = fileDao.getBytesUploadCandidates() + + assertEquals(listOf(ok.resourceId), candidates.map { it.resourceId }) + } + + private suspend fun insertCandidate( + uri: String? = "content://up/x.txt", + syncStatus: String = FileStatus.LOCAL_CLOUD, + processed: Boolean = true, + bytesUploaded: Int = 0, + bytesUploadAttempts: Int = 0, + ): FileEntity { + uri?.let { + shadowOf(context.contentResolver).registerInputStream( + it.toUri(), ByteArrayInputStream("ok-content".toByteArray()), + ) + } + val file = FileEntity( + resourceId = generateResourceId(), + uri = uri, + name = uri?.substringAfterLast('/') ?: "cloud.txt", + folderResourceId = "f".repeat(32), + size = 10L, + mimeType = "text/plain", + exists = 1, + syncStatus = syncStatus, + processed = processed, + bytesUploaded = bytesUploaded, + bytesUploadAttempts = bytesUploadAttempts, + addedAt = 1_700_000_000_000L, + updatedAt = 1_700_000_000_000L, + ) + fileDao.upsert(file) + return file + } + + private var idSeq = 100L + private fun generateResourceId(): String { + val hex = (++idSeq).toString(16).padStart(32, '0') + return hex + } + + private fun moshi(): Moshi = + Moshi.Builder().add(KotlinJsonAdapterFactory()).build() + + /** Fake upload ciblé : succès, 413, ou panne réseau. */ + private class UploadFake : ApiService { + enum class Mode { SUCCESS, TOO_LARGE, NETWORK } + + var mode = Mode.SUCCESS + var uploadedBytes: String? = null + var uploadedResourceId: String? = null + + override suspend fun listFolders(): Response>> = + Response.success(ApiEnvelope(data = emptyList())) + + override suspend fun listFiles(folderId: String?, page: Int?, pageSize: Int?): Response>> = + Response.success(ApiEnvelope(data = emptyList())) + + override suspend fun registerDevice(body: DeviceRegistrationDto): Response> = + Response.success(ApiEnvelope(data = body)) + + override suspend fun login(body: LoginRequestDto): Response> = + Response.success(ApiEnvelope()) + + override suspend fun syncOps(body: SyncOpsRequest): Response> = + Response.success(ApiEnvelope(data = SyncOpsResult(applied = 0))) + + override suspend fun resolveUser(username: String): Response> = + Response.success(ApiEnvelope()) + + override suspend fun syncPermissions(after: Long?): Response>> = + Response.success(ApiEnvelope(data = emptyList())) + + override suspend fun uploadFile(file: MultipartBody.Part, resourceId: RequestBody?, folderId: RequestBody?): Response> { + if (mode == Mode.NETWORK) throw ApiException("NETWORK_ERROR", "down", 0) + if (mode == Mode.TOO_LARGE) { + return Response.error( + 413, + "{\"error\":{\"code\":\"FILE_TOO_LARGE\",\"message\":\"x\"}}".toResponseBody("application/json".toMediaType()), + ) + } + val buffer = okio.Buffer() + file.body.writeTo(buffer) + uploadedBytes = buffer.readUtf8() + uploadedResourceId = resourceId?.let { + val b = okio.Buffer() + it.writeTo(b) + b.readUtf8() + } + return Response.success( + ApiEnvelope(data = FileDto(id = uploadedResourceId ?: "?", name = "x", size = 10L)), + ) + } + + override suspend fun submitOcr(body: OcrSubmitDto): Response> = + Response.success(ApiEnvelope()) + + override suspend fun getOcrJob(jobId: String): Response> = + Response.success(ApiEnvelope()) + + override suspend fun getFileOcr(fileId: String): Response> = + Response.success(ApiEnvelope()) + + override suspend fun getFile(fileId: String): Response> = + Response.success(ApiEnvelope()) + + override suspend fun deleteFile(fileId: String): Response> = + Response.success(ApiEnvelope()) + + override suspend fun searchFiles(query: String, page: Int?, pageSize: Int?): Response>> = + Response.success(ApiEnvelope(data = emptyList())) + + override suspend fun changePassword(body: ChangePasswordDto): Response> = + Response.success(ApiEnvelope()) + + override suspend fun getShareLink(token: String): Response> = + Response.success(ApiEnvelope()) + + override suspend fun downloadContent(fileId: String): Response = + Response.success(ByteArray(0).toResponseBody(null)) + + override suspend fun listShares(fileId: String): Response>> = + Response.success(ApiEnvelope(data = emptyList())) + + override suspend fun listLinks(fileId: String): Response>> = + Response.success(ApiEnvelope(data = emptyList())) + } +}