V2-1 : upload auto des octets (BytesUploadWorker, Room v17, SafMultipart)
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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<FileEntity>
|
||||
|
||||
/** 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)
|
||||
}
|
||||
@@ -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")
|
||||
|
||||
+14
-1
@@ -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<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_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,
|
||||
)
|
||||
}
|
||||
@@ -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),
|
||||
)
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
+60
@@ -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
|
||||
}
|
||||
|
||||
@@ -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<BytesUploadWorker>()
|
||||
.setConstraints(constraints)
|
||||
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 30, TimeUnit.SECONDS)
|
||||
.build()
|
||||
WorkManager.getInstance(context)
|
||||
.enqueueUniqueWork(NAME, ExistingWorkPolicy.KEEP, request)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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) }
|
||||
|
||||
+81
@@ -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<Context>()
|
||||
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<String>()
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -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<Context>()
|
||||
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<ApiEnvelope<List<FolderDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listFiles(folderId: String?, page: Int?, pageSize: Int?): Response<ApiEnvelope<List<FileDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun registerDevice(body: DeviceRegistrationDto): Response<ApiEnvelope<DeviceRegistrationDto>> =
|
||||
Response.success(ApiEnvelope(data = body))
|
||||
|
||||
override suspend fun login(body: LoginRequestDto): Response<ApiEnvelope<LoginResponseDto>> =
|
||||
Response.success(ApiEnvelope())
|
||||
|
||||
override suspend fun syncOps(body: SyncOpsRequest): Response<ApiEnvelope<SyncOpsResult>> =
|
||||
Response.success(ApiEnvelope(data = SyncOpsResult(applied = 0)))
|
||||
|
||||
override suspend fun resolveUser(username: String): Response<ApiEnvelope<ResolvedUserDto>> =
|
||||
Response.success(ApiEnvelope())
|
||||
|
||||
override suspend fun syncPermissions(after: Long?): Response<ApiEnvelope<List<ResourcePermissionDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun uploadFile(file: MultipartBody.Part, resourceId: RequestBody?, folderId: RequestBody?): Response<ApiEnvelope<FileDto>> {
|
||||
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<ApiEnvelope<OcrJobDto>> =
|
||||
Response.success(ApiEnvelope())
|
||||
|
||||
override suspend fun getOcrJob(jobId: String): Response<ApiEnvelope<OcrJobDto>> =
|
||||
Response.success(ApiEnvelope())
|
||||
|
||||
override suspend fun getFileOcr(fileId: String): Response<ApiEnvelope<FileOcrDto>> =
|
||||
Response.success(ApiEnvelope())
|
||||
|
||||
override suspend fun getFile(fileId: String): Response<ApiEnvelope<FileDto>> =
|
||||
Response.success(ApiEnvelope())
|
||||
|
||||
override suspend fun deleteFile(fileId: String): Response<ApiEnvelope<IdResultDto>> =
|
||||
Response.success(ApiEnvelope())
|
||||
|
||||
override suspend fun searchFiles(query: String, page: Int?, pageSize: Int?): Response<ApiEnvelope<List<FileDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun changePassword(body: ChangePasswordDto): Response<ApiEnvelope<IdResultDto>> =
|
||||
Response.success(ApiEnvelope())
|
||||
|
||||
override suspend fun getShareLink(token: String): Response<ApiEnvelope<ShareLinkDto>> =
|
||||
Response.success(ApiEnvelope())
|
||||
|
||||
override suspend fun downloadContent(fileId: String): Response<ResponseBody> =
|
||||
Response.success(ByteArray(0).toResponseBody(null))
|
||||
|
||||
override suspend fun listShares(fileId: String): Response<ApiEnvelope<List<ShareGrantDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listLinks(fileId: String): Response<ApiEnvelope<List<ShareLinkDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user