Compare commits
19
Commits
318329a683
...
v0.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
225f6e4b93 | ||
|
|
f97e0aed45 | ||
|
|
9dbd9d7a11 | ||
|
|
33ff9a6789 | ||
|
|
22286aa2b1 | ||
|
|
d89c0d03c9 | ||
|
|
c642adb85b | ||
|
|
ed05093370 | ||
|
|
6b6f43bca9 | ||
|
|
a6b7f2002b | ||
|
|
df7dc76405 | ||
|
|
409842694d | ||
|
|
33579a50a5 | ||
|
|
71c002b8b6 | ||
|
|
5a452c46af | ||
|
|
d52460834a | ||
|
|
901df79d86 | ||
|
|
7d67ce3681 | ||
|
|
65242ac3c7 |
@@ -0,0 +1,28 @@
|
|||||||
|
# Production — copier vers .env.prod (jamais commité) et adapter.
|
||||||
|
# Utilisé avec : docker compose -f docker-compose.prod.yml --env-file .env.prod [pull|up -d]
|
||||||
|
|
||||||
|
# Image (registre Gitea). VERSION=latest par défaut ; pincer pour figer, ex. :
|
||||||
|
# VERSION=v0.1.0 (le tag release inclut le "v", cf. release.yml)
|
||||||
|
REGISTRY_IMAGE=gitea.thirdshop.fr/thirdshop/kazier
|
||||||
|
VERSION=latest
|
||||||
|
|
||||||
|
# Port d'écoute exposé sur l'hôte (derrière Caddy/Nginx/TLS en prod publique)
|
||||||
|
PORT=8080
|
||||||
|
|
||||||
|
# Postgres (mots de passe forts, générés, uniques à la prod)
|
||||||
|
POSTGRES_DB=kazier
|
||||||
|
POSTGRES_USER=kazier
|
||||||
|
POSTGRES_PASSWORD=CHANGE_ME_GENERER_UN_SECRET_LONG
|
||||||
|
|
||||||
|
# Backend
|
||||||
|
MAX_FILE_SIZE_MB=50
|
||||||
|
OCR_LANG=fra+eng
|
||||||
|
|
||||||
|
# Sécurité — générer un secret long et aléatoire, ex. :
|
||||||
|
# openssl rand -base64 48
|
||||||
|
AUTH_SECRET=CHANGE_ME_GENERER_UN_SECRET_LONG
|
||||||
|
|
||||||
|
# Premier admin (créé UNIQUEMENT si la table users est vide au premier boot).
|
||||||
|
# L'env n'écrase jamais un compte existant.
|
||||||
|
ADMIN_USERNAME=admin
|
||||||
|
ADMIN_PASSWORD=CHANGE_ME_MOT_DE_PASSE_INITIAL_FORT
|
||||||
@@ -0,0 +1,244 @@
|
|||||||
|
# Pipeline Gitea Actions pour Kazier — lint + test + build + image registre.
|
||||||
|
#
|
||||||
|
# Prérequis act_runner (une seule fois) :
|
||||||
|
# - Runner act_runner enregistré contre l'instance Gitea, labels `ubuntu-24.04` dispo.
|
||||||
|
# - Job `docker` : s'exécute directement sur l'hôte du runner et utilise le
|
||||||
|
# daemon Docker local (pas de conteneur `docker:27`, pas de DinD,
|
||||||
|
# pas de `--privileged`, pas de montage `-v /var/run/docker.sock` : ce
|
||||||
|
# dernier faisait échouer la création du conteneur sous act_runner avec
|
||||||
|
# `Duplicate mount point: /var/run/docker.sock`, le runner injectant déjà
|
||||||
|
# ce mount). L'hôte doit donc avoir un daemon Docker sain + le CLI docker.
|
||||||
|
# - Gitea app.ini : [actions] ENABLED=true, DEFAULT_ACTIONS_URL=https://github.com
|
||||||
|
# - Droits d'écriture du runner sur son répertoire de travail/cache.
|
||||||
|
#
|
||||||
|
# Secrets (Settings > Actions) :
|
||||||
|
# - CONTAINER_REGISTRY_TOKEN PAT scope write:package (registre conteneur)
|
||||||
|
# - CONTAINER_REGISTRY_USER utilisateur propriétaire du PAT ci-dessus
|
||||||
|
|
||||||
|
name: CI
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.ref }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
tags: ["v*"]
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
env:
|
||||||
|
GO_VERSION: "1.26"
|
||||||
|
ANDROID_IMAGE: cimg/android:2024.01-node
|
||||||
|
CGO_ENABLED: "0"
|
||||||
|
# Registre conteneur Gitea (ex: cname de l'instance)
|
||||||
|
REGISTRY_HOST: gitea.thirdshop.fr
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
lint-backend:
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
container: golang:${{ env.GO_VERSION }}-alpine
|
||||||
|
steps:
|
||||||
|
# golang:alpine n'a ni node ni git — requis par les actions JS
|
||||||
|
# (checkout/cache) exécutées dans le container par act_runner.
|
||||||
|
- name: Install node + git (actions JS)
|
||||||
|
run: apk add --no-cache nodejs git
|
||||||
|
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITEA_TOKEN }}
|
||||||
|
|
||||||
|
- uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
/go/pkg/mod
|
||||||
|
~/.cache/go-build
|
||||||
|
key: go-${{ runner.os }}-${{ hashFiles('backend/go.mod', 'backend/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
go-${{ runner.os }}-
|
||||||
|
|
||||||
|
- run: go mod download
|
||||||
|
working-directory: backend
|
||||||
|
- run: go vet ./...
|
||||||
|
working-directory: backend
|
||||||
|
- run: go install honnef.co/go/tools/cmd/staticcheck@2026.2.1 && staticcheck ./...
|
||||||
|
working-directory: backend
|
||||||
|
|
||||||
|
lint-mobile:
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
container: ${{ env.ANDROID_IMAGE }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITEA_TOKEN }}
|
||||||
|
- uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
~/.gradle/caches
|
||||||
|
~/.gradle/wrapper
|
||||||
|
key: gradle-${{ runner.os }}-${{ hashFiles('mobile-kotlin/gradle/libs.versions.toml', 'mobile-kotlin/gradle.properties', 'mobile-kotlin/settings.gradle.kts', 'mobile-kotlin/build.gradle.kts', 'mobile-kotlin/app/build.gradle.kts') }}
|
||||||
|
restore-keys: |
|
||||||
|
gradle-${{ runner.os }}-
|
||||||
|
- run: ./gradlew :app:lint -q
|
||||||
|
working-directory: mobile-kotlin
|
||||||
|
|
||||||
|
test-backend:
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
container: golang:${{ env.GO_VERSION }}-alpine
|
||||||
|
services:
|
||||||
|
postgres:
|
||||||
|
image: postgres:17-alpine
|
||||||
|
env:
|
||||||
|
POSTGRES_DB: kazier_test
|
||||||
|
POSTGRES_USER: kazier
|
||||||
|
POSTGRES_PASSWORD: kazier
|
||||||
|
options: >-
|
||||||
|
--health-cmd "pg_isready -U kazier"
|
||||||
|
--health-interval 5s
|
||||||
|
--health-timeout 3s
|
||||||
|
--health-retries 10
|
||||||
|
steps:
|
||||||
|
- name: Install node + git (actions JS)
|
||||||
|
run: apk add --no-cache nodejs git
|
||||||
|
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITEA_TOKEN }}
|
||||||
|
- uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
/go/pkg/mod
|
||||||
|
~/.cache/go-build
|
||||||
|
key: go-${{ runner.os }}-${{ hashFiles('backend/go.mod', 'backend/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
go-${{ runner.os }}-
|
||||||
|
- run: |
|
||||||
|
set -euo pipefail
|
||||||
|
# dbtest fait un skip silencieux si Postgres est injoignable : on
|
||||||
|
# attend explicitement que le service accepte les connexions pour
|
||||||
|
# que le job échoue (pas skip) si PG ne monte jamais.
|
||||||
|
apk add --no-cache postgresql-client >/dev/null
|
||||||
|
for i in $(seq 1 30); do
|
||||||
|
if pg_isready -h postgres -U kazier -d kazier_test; then
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
echo "Postgres jamais prêt après 30s" >&2
|
||||||
|
exit 1
|
||||||
|
- run: go test ./...
|
||||||
|
working-directory: backend
|
||||||
|
env:
|
||||||
|
TEST_DATABASE_URL: postgres://kazier:kazier@postgres:5432/kazier_test?sslmode=disable
|
||||||
|
|
||||||
|
test-mobile:
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
container: ${{ env.ANDROID_IMAGE }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITEA_TOKEN }}
|
||||||
|
- uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
~/.gradle/caches
|
||||||
|
~/.gradle/wrapper
|
||||||
|
key: gradle-${{ runner.os }}-${{ hashFiles('mobile-kotlin/gradle/libs.versions.toml', 'mobile-kotlin/gradle.properties', 'mobile-kotlin/settings.gradle.kts', 'mobile-kotlin/build.gradle.kts', 'mobile-kotlin/app/build.gradle.kts') }}
|
||||||
|
restore-keys: |
|
||||||
|
gradle-${{ runner.os }}-
|
||||||
|
- run: ./gradlew :app:testDebugUnitTest -q
|
||||||
|
working-directory: mobile-kotlin
|
||||||
|
|
||||||
|
build-backend:
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
container: golang:${{ env.GO_VERSION }}-alpine
|
||||||
|
needs: [lint-backend, test-backend]
|
||||||
|
steps:
|
||||||
|
- name: Install node + git (actions JS)
|
||||||
|
run: apk add --no-cache nodejs git
|
||||||
|
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITEA_TOKEN }}
|
||||||
|
- run: |
|
||||||
|
set -euo pipefail
|
||||||
|
export VERSION="$(cat VERSION)"
|
||||||
|
CGO_ENABLED=0 GOOS=linux go build \
|
||||||
|
-trimpath \
|
||||||
|
-ldflags "-s -w -X main.version=${VERSION}" \
|
||||||
|
-o bin/kazier-server \
|
||||||
|
cmd/server/main.go
|
||||||
|
./bin/kazier-server --version
|
||||||
|
working-directory: backend
|
||||||
|
# v4 demande le service d'artefacts v2 (@actions/artifact v2) non fourni
|
||||||
|
# par Gitea (détecté comme GHES → GHESNotSupportedError). v3 reste le
|
||||||
|
# protocole legacy supporté par l'API artefacts de Gitea.
|
||||||
|
- uses: actions/upload-artifact@v3
|
||||||
|
with:
|
||||||
|
name: kazier-server
|
||||||
|
path: backend/bin/kazier-server
|
||||||
|
if-no-files-found: error
|
||||||
|
|
||||||
|
docker:
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
# `build-backend` n'est qu'un garde-fou : le job docker ne télécharge pas
|
||||||
|
# son artifact, le Dockerfile recompile le binaire lui-même (build local
|
||||||
|
# autonome et reproductible). Le `needs` interdit juste de construire une
|
||||||
|
# image si lint + test + build échouent.
|
||||||
|
needs: [build-backend]
|
||||||
|
# Push branches uniquement : pas sur PR (pas de build d'image à la review),
|
||||||
|
# pas sur tag (c'est release.yml qui construit, évite une course dans le
|
||||||
|
# registre entre le job `docker` et `release-docker`).
|
||||||
|
if: github.event_name == 'push' && github.ref_type == 'branch'
|
||||||
|
# S'exécute directement sur l'hôte du runner (pas de `container: docker:27`) :
|
||||||
|
# le CLI docker de l'hôte parle au daemon local sans montage de socket.
|
||||||
|
# L'ancien montage explicite `-v /var/run/docker.sock:/var/run/docker.sock`
|
||||||
|
# via `container.options` faisait échouer la création du conteneur sous
|
||||||
|
# act_runner (`Duplicate mount point: /var/run/docker.sock` — le runner
|
||||||
|
# injecte déjà ce mount).
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITEA_TOKEN }}
|
||||||
|
|
||||||
|
- name: Sanity daemon Docker hôte
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
docker info >/dev/null
|
||||||
|
echo "daemon Docker hôte joignable"
|
||||||
|
|
||||||
|
- name: Login registre Gitea
|
||||||
|
env:
|
||||||
|
CONTAINER_REGISTRY_TOKEN: ${{ secrets.CONTAINER_REGISTRY_TOKEN }}
|
||||||
|
CONTAINER_REGISTRY_USER: ${{ secrets.CONTAINER_REGISTRY_USER }}
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
: "${CONTAINER_REGISTRY_TOKEN:?CONTAINER_REGISTRY_TOKEN manquant (PAT scope write:package)}"
|
||||||
|
: "${CONTAINER_REGISTRY_USER:?CONTAINER_REGISTRY_USER manquant (propriétaire du PAT)}"
|
||||||
|
echo "$CONTAINER_REGISTRY_TOKEN" | docker login "$REGISTRY_HOST" -u "$CONTAINER_REGISTRY_USER" --password-stdin
|
||||||
|
# Le registre impose un namespace minuscule (github.repository_owner
|
||||||
|
# peut être `Thirdshop` avec une majuscule → tag Docker invalide).
|
||||||
|
OWNER_LC="$(printf '%s' "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')"
|
||||||
|
echo "REGISTRY_IMAGE=${REGISTRY_HOST}/${OWNER_LC}/kazier" >> "$GITHUB_ENV"
|
||||||
|
echo "SHA_TAG=sha-$(printf '%s' "$GITHUB_SHA" | cut -c1-7)" >> "$GITHUB_ENV"
|
||||||
|
|
||||||
|
- name: Build + push (registre Gitea)
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
# --pull : base alpine/golang fraîche ; --cache-from : réutilise les
|
||||||
|
# layers du dernier build `main` si dispo (build plus rapide).
|
||||||
|
docker pull "$REGISTRY_IMAGE:main" 2>/dev/null || true
|
||||||
|
docker build \
|
||||||
|
--pull \
|
||||||
|
--cache-from "$REGISTRY_IMAGE:main" \
|
||||||
|
--build-arg VERSION="$(cat VERSION)" \
|
||||||
|
--build-arg BUILD_DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
|
||||||
|
-t "$REGISTRY_IMAGE:main" \
|
||||||
|
-t "$REGISTRY_IMAGE:$SHA_TAG" \
|
||||||
|
./backend
|
||||||
|
docker run --rm "$REGISTRY_IMAGE:$SHA_TAG" --version
|
||||||
|
docker push "$REGISTRY_IMAGE:main"
|
||||||
|
docker push "$REGISTRY_IMAGE:$SHA_TAG"
|
||||||
@@ -0,0 +1,165 @@
|
|||||||
|
# Pipeline Gitea Actions pour Kazier — Release (tags vX.Y.Z uniquement).
|
||||||
|
#
|
||||||
|
# Construit l'APK signé (assembleRelease), crée la Release Gitea avec l'APK en
|
||||||
|
# asset (API, équivalent de `glab release create`), puis pousse l'image
|
||||||
|
# kazier-server sur le registre conteneur Gitea (tag + latest).
|
||||||
|
# Déploiement perso uniquement : pas de push Docker Hub.
|
||||||
|
#
|
||||||
|
# Prérequis act_runner : job `release-docker` s'exécute directement sur
|
||||||
|
# l'hôte et utilise le daemon Docker local (pas de conteneur `docker:27`,
|
||||||
|
# pas de DinD, pas de montage `-v /var/run/docker.sock` — ce dernier faisait
|
||||||
|
# échouer la création du conteneur sous act_runner avec
|
||||||
|
# `Duplicate mount point: /var/run/docker.sock`, le runner injectant déjà ce
|
||||||
|
# mount). L'hôte doit avoir un daemon Docker sain + le CLI docker.
|
||||||
|
#
|
||||||
|
# Secrets (Settings > Actions) :
|
||||||
|
# - RELEASE_KEYSTORE_B64 base64 du keystore release Android (jamais en clair)
|
||||||
|
# - SIGNING_STORE_PASSWORD SIGNING_KEY_ALIAS SIGNING_KEY_PASSWORD
|
||||||
|
# - RELEASE_API_TOKEN PAT scope write:repository (API : création Release)
|
||||||
|
# - CONTAINER_REGISTRY_TOKEN PAT scope write:package (registre conteneur Gitea)
|
||||||
|
# - CONTAINER_REGISTRY_USER utilisateur propriétaire du PAT ci-dessus
|
||||||
|
|
||||||
|
name: Release
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags: ["v*"]
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
env:
|
||||||
|
ANDROID_IMAGE: cimg/android:2024.01-node
|
||||||
|
# API Gitea (host de l'instance)
|
||||||
|
API_HOST: gitea.thirdshop.fr
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
release-apk:
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
container: ${{ env.ANDROID_IMAGE }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITEA_TOKEN }}
|
||||||
|
- uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
~/.gradle/caches
|
||||||
|
~/.gradle/wrapper
|
||||||
|
key: gradle-${{ runner.os }}-${{ hashFiles('mobile-kotlin/gradle/libs.versions.toml', 'mobile-kotlin/gradle.properties', 'mobile-kotlin/settings.gradle.kts', 'mobile-kotlin/build.gradle.kts', 'mobile-kotlin/app/build.gradle.kts') }}
|
||||||
|
restore-keys: |
|
||||||
|
gradle-${{ runner.os }}-
|
||||||
|
|
||||||
|
- name: Signer + assembler l'APK release
|
||||||
|
working-directory: mobile-kotlin
|
||||||
|
shell: bash
|
||||||
|
env:
|
||||||
|
RELEASE_KEYSTORE_B64: ${{ secrets.RELEASE_KEYSTORE_B64 }}
|
||||||
|
SIGNING_STORE_PASSWORD: ${{ secrets.SIGNING_STORE_PASSWORD }}
|
||||||
|
SIGNING_KEY_ALIAS: ${{ secrets.SIGNING_KEY_ALIAS }}
|
||||||
|
SIGNING_KEY_PASSWORD: ${{ secrets.SIGNING_KEY_PASSWORD }}
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
# Nettoie keystore + mots de passe même en cas d'échec du build.
|
||||||
|
cleanup() { rm -f app/release.keystore app/signing.properties; }
|
||||||
|
trap cleanup EXIT
|
||||||
|
if [ -z "${RELEASE_KEYSTORE_B64:-}" ]; then
|
||||||
|
echo "RELEASE_KEYSTORE_B64 manquant — release non signée." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
: "${SIGNING_STORE_PASSWORD:?SIGNING_STORE_PASSWORD manquant}"
|
||||||
|
: "${SIGNING_KEY_ALIAS:?SIGNING_KEY_ALIAS manquant}"
|
||||||
|
: "${SIGNING_KEY_PASSWORD:?SIGNING_KEY_PASSWORD manquant}"
|
||||||
|
echo "$RELEASE_KEYSTORE_B64" | base64 -d > app/release.keystore
|
||||||
|
cat > app/signing.properties <<EOF
|
||||||
|
storeFile=release.keystore
|
||||||
|
storePassword=${SIGNING_STORE_PASSWORD}
|
||||||
|
keyAlias=${SIGNING_KEY_ALIAS}
|
||||||
|
keyPassword=${SIGNING_KEY_PASSWORD}
|
||||||
|
EOF
|
||||||
|
./gradlew :app:assembleRelease -q
|
||||||
|
|
||||||
|
- name: Créer la Release Gitea + upload APK
|
||||||
|
shell: bash
|
||||||
|
env:
|
||||||
|
RELEASE_API_TOKEN: ${{ secrets.RELEASE_API_TOKEN }}
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
VERSION="$(cat VERSION)"
|
||||||
|
TAG="v${VERSION}"
|
||||||
|
API="https://${API_HOST}/api/v1"
|
||||||
|
AUTH="Authorization: token ${RELEASE_API_TOKEN:?RELEASE_API_TOKEN manquant (PAT scope write:repository)}"
|
||||||
|
NOTES_JSON="$(python3 -c 'import json;print(json.dumps(open("CHANGELOG.md", encoding="utf-8").read()))')"
|
||||||
|
# Idempotent : si la release du tag existe déjà (re-tag, retry du job),
|
||||||
|
# la réutiliser au lieu d'échouer en 409.
|
||||||
|
if RELEASE_JSON="$(curl -sSf -H "${AUTH}" "${API}/repos/${GITHUB_REPOSITORY}/releases/tags/${TAG}" 2>/dev/null)"; then
|
||||||
|
echo "Release ${TAG} existante — réutilisation."
|
||||||
|
else
|
||||||
|
PAYLOAD_JSON="$(TAG="${TAG}" BODY="$NOTES_JSON" python3 -c 'import json,os;print(json.dumps({"tag_name":os.environ["TAG"],"name":"Kazier "+os.environ["TAG"],"body":json.loads(os.environ["BODY"])}))')"
|
||||||
|
RELEASE_JSON="$(curl -sSf -X POST "${API}/repos/${GITHUB_REPOSITORY}/releases" \
|
||||||
|
-H "${AUTH}" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d "${PAYLOAD_JSON}")"
|
||||||
|
fi
|
||||||
|
RELEASE_ID="$(printf '%s' "${RELEASE_JSON}" | python3 -c 'import json,sys;print(json.load(sys.stdin)["id"])')"
|
||||||
|
: "${RELEASE_ID:?id de release introuvable dans la réponse API}"
|
||||||
|
# Asset idempotent : remplace l'APK existant de même nom s'il y en a un.
|
||||||
|
APK_PATH="mobile-kotlin/app/build/outputs/apk/release/app-release.apk"
|
||||||
|
EXISTING_ASSET_ID="$(curl -sSf -H "${AUTH}" "${API}/repos/${GITHUB_REPOSITORY}/releases/${RELEASE_ID}/assets" \
|
||||||
|
| python3 -c 'import json,sys;assets=json.load(sys.stdin);print(next((str(a["id"]) for a in assets if a.get("name")=="app-release.apk"),""))')"
|
||||||
|
if [ -n "${EXISTING_ASSET_ID}" ]; then
|
||||||
|
echo "Asset existant ${EXISTING_ASSET_ID} — suppression avant re-upload."
|
||||||
|
curl -sSf -X DELETE -H "${AUTH}" "${API}/repos/${GITHUB_REPOSITORY}/releases/${RELEASE_ID}/assets/${EXISTING_ASSET_ID}" > /dev/null
|
||||||
|
fi
|
||||||
|
curl -sSf -X POST \
|
||||||
|
"${API}/repos/${GITHUB_REPOSITORY}/releases/${RELEASE_ID}/assets?name=app-release.apk" \
|
||||||
|
-H "${AUTH}" \
|
||||||
|
-F "attachment=@${APK_PATH}"
|
||||||
|
|
||||||
|
release-docker:
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
# Directement sur l'hôte du runner (même raison que CI/docker : le montage
|
||||||
|
# explicite du socket via `container.options` doublonne celui injecté par
|
||||||
|
# act_runner → `Duplicate mount point`).
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITEA_TOKEN }}
|
||||||
|
|
||||||
|
- name: Sanity daemon Docker hôte
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
docker info >/dev/null
|
||||||
|
echo "daemon Docker hôte joignable"
|
||||||
|
|
||||||
|
- name: Login registre Gitea
|
||||||
|
shell: bash
|
||||||
|
env:
|
||||||
|
CONTAINER_REGISTRY_TOKEN: ${{ secrets.CONTAINER_REGISTRY_TOKEN }}
|
||||||
|
CONTAINER_REGISTRY_USER: ${{ secrets.CONTAINER_REGISTRY_USER }}
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
: "${CONTAINER_REGISTRY_TOKEN:?CONTAINER_REGISTRY_TOKEN manquant (PAT scope write:package)}"
|
||||||
|
: "${CONTAINER_REGISTRY_USER:?CONTAINER_REGISTRY_USER manquant (propriétaire du PAT)}"
|
||||||
|
echo "$CONTAINER_REGISTRY_TOKEN" | docker login "$API_HOST" -u "$CONTAINER_REGISTRY_USER" --password-stdin
|
||||||
|
OWNER_LC="$(printf '%s' "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')"
|
||||||
|
echo "REGISTRY_IMAGE=${API_HOST}/${OWNER_LC}/kazier" >> "$GITHUB_ENV"
|
||||||
|
|
||||||
|
- name: "Build + push (registre Gitea : vX.Y.Z + latest)"
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
VERSION="$(cat VERSION)"
|
||||||
|
docker pull "$REGISTRY_IMAGE:latest" 2>/dev/null || true
|
||||||
|
docker build \
|
||||||
|
--pull \
|
||||||
|
--cache-from "$REGISTRY_IMAGE:latest" \
|
||||||
|
--build-arg VERSION="${VERSION}" \
|
||||||
|
--build-arg BUILD_DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
|
||||||
|
-t "$REGISTRY_IMAGE:v${VERSION}" \
|
||||||
|
-t "$REGISTRY_IMAGE:latest" \
|
||||||
|
./backend
|
||||||
|
docker run --rm "$REGISTRY_IMAGE:v${VERSION}" --version
|
||||||
|
docker push "$REGISTRY_IMAGE:v${VERSION}"
|
||||||
|
docker push "$REGISTRY_IMAGE:latest"
|
||||||
-196
@@ -1,196 +0,0 @@
|
|||||||
# Pipeline GitLab pour Kazier — CI + Release.
|
|
||||||
#
|
|
||||||
# Prérequis runner :
|
|
||||||
# - Un runner avec executor Docker pour les jobs d'images (postgres service,
|
|
||||||
# docker:dind). Les jobs Android utilisent $ANDROID_IMAGE (défaut
|
|
||||||
# cimg/android) ; remplacez par l'image installée sur vos workers.
|
|
||||||
#
|
|
||||||
# Variables CI/CD à définir (Settings > CI/CD > Variables) :
|
|
||||||
# - RELEASE_KEYSTORE_B64 base64 du keystore release Android (jamais en clair)
|
|
||||||
# - SIGNING_STORE_PASSWORD SIGNING_KEY_ALIAS SIGNING_KEY_PASSWORD
|
|
||||||
# - GITLAB_API_TOKEN token (scope api, write_repository) pour créer la GitLab Release
|
|
||||||
# - DOCKERHUB_USER DOCKERHUB_TOKEN PAT Docker Hub (push public)
|
|
||||||
#
|
|
||||||
# Publication : à chaque tag vX.Y.Z → GitLab Release (APK attaché, latest +
|
|
||||||
# historique) + image $CI_REGISTRY + image Docker Hub (docker.io/thirdshop/kasier).
|
|
||||||
|
|
||||||
stages:
|
|
||||||
- lint
|
|
||||||
- test
|
|
||||||
- build
|
|
||||||
- docker
|
|
||||||
- release
|
|
||||||
|
|
||||||
variables:
|
|
||||||
GO_IMAGE: "golang:1.26-alpine"
|
|
||||||
POSTGRES_IMAGE: "postgres:17-alpine"
|
|
||||||
ANDROID_IMAGE: "cimg/android:2024.01"
|
|
||||||
GRADLE_USER_HOME: "$CI_PROJECT_DIR/.gradle-home"
|
|
||||||
CGO_ENABLED: "0"
|
|
||||||
DOCKERHUB_IMAGE: "docker.io/thirdshop/kasier"
|
|
||||||
GLAB_VERSION: "1.118.0"
|
|
||||||
|
|
||||||
default:
|
|
||||||
interruptible: true
|
|
||||||
retry:
|
|
||||||
max: 1
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------- lint ----
|
|
||||||
lint-backend:
|
|
||||||
stage: lint
|
|
||||||
image: $GO_IMAGE
|
|
||||||
script:
|
|
||||||
- go mod download
|
|
||||||
- go vet ./...
|
|
||||||
- go install honnef.co/go/tools/cmd/staticcheck@latest && staticcheck ./...
|
|
||||||
|
|
||||||
lint-mobile:
|
|
||||||
stage: lint
|
|
||||||
image: $ANDROID_IMAGE
|
|
||||||
cache:
|
|
||||||
key: gradle-cache
|
|
||||||
paths:
|
|
||||||
- .gradle-home/caches
|
|
||||||
- .gradle-home/wrapper
|
|
||||||
script:
|
|
||||||
- cd mobile-kotlin && ./gradlew :app:lint -q
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------- test ----
|
|
||||||
test-backend:
|
|
||||||
stage: test
|
|
||||||
image: $GO_IMAGE
|
|
||||||
services:
|
|
||||||
- name: $POSTGRES_IMAGE
|
|
||||||
alias: postgres
|
|
||||||
variables:
|
|
||||||
POSTGRES_DB: kazier_test
|
|
||||||
POSTGRES_USER: kazier
|
|
||||||
POSTGRES_PASSWORD: kazier
|
|
||||||
TEST_DATABASE_URL: postgres://kazier:kazier@postgres:5432/kazier_test?sslmode=disable
|
|
||||||
script:
|
|
||||||
- mkdir -p .go-cache && export GOPATH="$CI_PROJECT_DIR/.go-cache"
|
|
||||||
- go mod download
|
|
||||||
- go test ./...
|
|
||||||
cache:
|
|
||||||
key: go-cache
|
|
||||||
paths:
|
|
||||||
- .go-cache
|
|
||||||
|
|
||||||
test-mobile:
|
|
||||||
stage: test
|
|
||||||
image: $ANDROID_IMAGE
|
|
||||||
cache:
|
|
||||||
key: gradle-cache
|
|
||||||
paths:
|
|
||||||
- .gradle-home/caches
|
|
||||||
- .gradle-home/wrapper
|
|
||||||
script:
|
|
||||||
- cd mobile-kotlin && ./gradlew :app:testDebugUnitTest -q
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------- build ----
|
|
||||||
build-backend:
|
|
||||||
stage: build
|
|
||||||
image: $GO_IMAGE
|
|
||||||
script:
|
|
||||||
- export VERSION=$(cat VERSION)
|
|
||||||
- mkdir -p backend/bin
|
|
||||||
- cd backend && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags "-s -w -X main.version=$VERSION" -o bin/kazier-server cmd/server/main.go
|
|
||||||
- ./bin/kazier-server --version
|
|
||||||
artifacts:
|
|
||||||
paths:
|
|
||||||
- backend/bin/kazier-server
|
|
||||||
expire_in: 1 week
|
|
||||||
|
|
||||||
# --------------------------------------------------------------- docker ----
|
|
||||||
docker:
|
|
||||||
stage: docker
|
|
||||||
image: docker:27
|
|
||||||
services:
|
|
||||||
- name: docker:27-dind
|
|
||||||
alias: docker
|
|
||||||
variables:
|
|
||||||
DOCKER_HOST: tcp://docker:2375
|
|
||||||
DOCKER_TLS_CERTDIR: ""
|
|
||||||
IMAGE_TAG: "$CI_COMMIT_TAG"
|
|
||||||
script:
|
|
||||||
- export IMAGE_TAG="${IMAGE_TAG:-$CI_COMMIT_REF_SLUG}"
|
|
||||||
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
|
|
||||||
- docker build
|
|
||||||
--build-arg VERSION="$(cat VERSION)"
|
|
||||||
--build-arg BUILD_DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
|
||||||
-t "$CI_REGISTRY_IMAGE:$IMAGE_TAG"
|
|
||||||
./backend
|
|
||||||
- docker push "$CI_REGISTRY_IMAGE:$IMAGE_TAG"
|
|
||||||
- docker run --rm "$CI_REGISTRY_IMAGE:$IMAGE_TAG" --version
|
|
||||||
needs: ["build-backend"]
|
|
||||||
rules:
|
|
||||||
- if: $CI_COMMIT_TAG
|
|
||||||
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
|
|
||||||
|
|
||||||
# -------------------------------------------------------------- release ----
|
|
||||||
release-apk:
|
|
||||||
stage: release
|
|
||||||
image: $ANDROID_IMAGE
|
|
||||||
cache:
|
|
||||||
key: gradle-cache
|
|
||||||
paths:
|
|
||||||
- .gradle-home/caches
|
|
||||||
- .gradle-home/wrapper
|
|
||||||
script:
|
|
||||||
- export VERSION=$(cat VERSION)
|
|
||||||
- cd mobile-kotlin
|
|
||||||
- |
|
|
||||||
if [ -n "${RELEASE_KEYSTORE_B64:-}" ]; then
|
|
||||||
echo "$RELEASE_KEYSTORE_B64" | base64 -d > app/release.keystore
|
|
||||||
cat > app/signing.properties <<EOF
|
|
||||||
storeFile=release.keystore
|
|
||||||
storePassword=${SIGNING_STORE_PASSWORD:?}
|
|
||||||
keyAlias=${SIGNING_KEY_ALIAS:?}
|
|
||||||
keyPassword=${SIGNING_KEY_PASSWORD:?}
|
|
||||||
EOF
|
|
||||||
else
|
|
||||||
echo "RELEASE_KEYSTORE_B64 manquant — release non signée." >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
- ./gradlew :app:assembleRelease -q
|
|
||||||
- cd "$CI_PROJECT_DIR"
|
|
||||||
- |
|
|
||||||
# glab : création de la GitLab Release + upload de l'APK en asset.
|
|
||||||
curl -sSLf \
|
|
||||||
"https://gitlab.com/gitlab-org/cli/-/releases/v${GLAB_VERSION}/downloads/glab_${GLAB_VERSION}_linux_amd64.tar.gz" \
|
|
||||||
| tar xz --strip-components=1 -C /usr/local/bin bin/glab
|
|
||||||
- glab --version
|
|
||||||
- export GITLAB_TOKEN="$GITLAB_API_TOKEN"
|
|
||||||
- glab release create "v$VERSION"
|
|
||||||
--name "Kazier v$VERSION"
|
|
||||||
--notes-file CHANGELOG.md
|
|
||||||
"mobile-kotlin/app/build/outputs/apk/release/app-release.apk"
|
|
||||||
needs: ["docker"]
|
|
||||||
rules:
|
|
||||||
- if: $CI_COMMIT_TAG
|
|
||||||
|
|
||||||
release-docker:
|
|
||||||
stage: release
|
|
||||||
image: docker:27
|
|
||||||
services:
|
|
||||||
- name: docker:27-dind
|
|
||||||
alias: docker
|
|
||||||
variables:
|
|
||||||
DOCKER_HOST: tcp://docker:2375
|
|
||||||
DOCKER_TLS_CERTDIR: ""
|
|
||||||
script:
|
|
||||||
- export VERSION=$(cat VERSION)
|
|
||||||
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
|
|
||||||
- docker pull "$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG"
|
|
||||||
- |
|
|
||||||
: "${DOCKERHUB_USER:?DOCKERHUB_USER manquant}"
|
|
||||||
: "${DOCKERHUB_TOKEN:?DOCKERHUB_TOKEN manquant}"
|
|
||||||
echo "$DOCKERHUB_TOKEN" | docker login -u "$DOCKERHUB_USER" --password-stdin docker.io
|
|
||||||
IMAGE="$DOCKERHUB_IMAGE"
|
|
||||||
docker tag "$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG" "$IMAGE:$CI_COMMIT_TAG"
|
|
||||||
docker tag "$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG" "$IMAGE:latest"
|
|
||||||
docker push "$IMAGE:$CI_COMMIT_TAG"
|
|
||||||
docker push "$IMAGE:latest"
|
|
||||||
needs: ["docker"]
|
|
||||||
rules:
|
|
||||||
- if: $CI_COMMIT_TAG
|
|
||||||
+202
@@ -0,0 +1,202 @@
|
|||||||
|
# FEEDBACK — Audit bugs mobile-kotlin (local-first)
|
||||||
|
|
||||||
|
> Généré le 2026-09-22 par 5 agents d'exploration parallèles (scopes : Room/DB, sync/outbox/SAF, réseau/auth/repos, UI/navigation/VM, scan/pdf/review/thumbnails).
|
||||||
|
> Chaque finding est vérifié avec preuve `file:line`. Méthode : lecture intégrale des fichiers + croisement `docs/api-v1.md`, `AGENTS.md`, handlers Go.
|
||||||
|
> Scope : `mobile-kotlin/` uniquement. Pas de fix appliqué — diagnostic seul.
|
||||||
|
> Légende : 🔴 Critique (data loss / crash / sync bloquée) · 🟠 Majeur (divergence silencieuse, contrat violé) · 🟡 Mineur (UX, perf, dette).
|
||||||
|
|
||||||
|
Base paths : `mobile-kotlin/app/src/main/java/com/kazier/mobile/…`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🔴 Critiques (à corriger en priorité)
|
||||||
|
|
||||||
|
### C1 — `domain/GenerateId.kt:19` — IDs 32-hex non conformes (sign-extension `Byte` → `%02x`)
|
||||||
|
`bytes.joinToString("") { "%02x".format(it) }` promeut chaque `Byte` signé en `int` : tout octet négatif produit `ffffffxx` (8 chars). Longueur 32→128, P(32 exact) ≈ 1/65536.
|
||||||
|
Impact : `resource_id` / `operation_id` / `device_user_id` violent `^[0-9a-f]{32}$` (CHECK serveur) → `POST /sync/ops` rejetés, outbox bloquée, identités invalides.
|
||||||
|
Repro : boucler `newResourceId()` et tester `length == 32 && matches([0-9a-f]{32})` → échec quasi systématique.
|
||||||
|
|
||||||
|
### C2 — `features/sync/OutboxSyncWorker.kt:101-106` — dead-letter abusive sur erreurs non-attribuables
|
||||||
|
Le `else` dead-letter `pending.first()` pour toute `ApiException` non-`NETWORK_ERROR`/5xx/401. `INVALID_RESPONSE` (200 sans enveloppe `{data}`) et `HTTP_429`/`408` (transitoires) tombent dedans. La faute est imputée à la 1re op du batch quelle que soit l'op rejetée.
|
||||||
|
Impact : op saine → `failed` à vie sur erreur batch-level ou transitoire ; inclut le cas `429 RATE_LIMITED` (login/devices/sync) qui devrait suivre le chemin `retry()`.
|
||||||
|
Repro : serveur 200 sans `data` ou 429 → 1re op dead-lettrée.
|
||||||
|
|
||||||
|
### C3 — `features/sync/OutboxSyncWorker.kt:83` + `data/local/dao/PendingOperationDao.kt:42-43` — purge >7j qui casse le gate `processed`/`hasCreateOperation`
|
||||||
|
`purgeSynced` efface les lignes `synced`, or `countCreateOperations` (`IN ('pending','synced')`) fonde les gates delete/create dessus.
|
||||||
|
Impact : (a) fichier poussé puis purgé → `SafFileDeleter`/`FileDeleter`/`treeMarkMissing` omettent le `delete_resource` → fantôme serveur indélébile ; (b) `markProcessed` ré-enqueue un `create_resource` dupliqué.
|
||||||
|
Repro : pousser un fichier, attendre 7j, le supprimer → aucun `delete_resource`, serveur garde la ressource.
|
||||||
|
|
||||||
|
### C4 — `features/saf/FolderDeleter.kt:106-124` + `features/saf/FileDeleter.kt:109-119` — suppression physique avant transaction, delete perdu sur crash
|
||||||
|
`deleteTreePhysically` (SAF, irréversible) avant `withTransaction { treeMarkMissing }`.
|
||||||
|
Impact : kill/crash entre les deux → octets supprimés, aucun `delete_resource` journalisé, walk suivant marque `exists=0` sans outbox (aucun chemin walk→enqueue) → fantôme serveur + divergence définitive.
|
||||||
|
Repro : `deleteFull` puis kill avant commit Room → serveur jamais notifié.
|
||||||
|
|
||||||
|
### C5 — `ui/auth/AuthViewModel.kt:112-121` — pas de vidage outbox au sign-out, replay cross-account
|
||||||
|
`signOut`/`signOutToLocal` purgent session et mode local mais aucune suppression de `pending_operations`. Les ops du compte A survivent au login du compte B (même `device_id`, `UNIQUE(device_id, operation_id)` sans historique partagé).
|
||||||
|
Impact : ops de A appliquées dans le scope de B (`NOT_FOUND`/`NAME_CONFLICT`, voire créations croisées).
|
||||||
|
Repro : enqueue offline sous A → sign-out → login B → drain pousse les ops de A sous token B.
|
||||||
|
|
||||||
|
### C6 — `features/sync/OutboxSyncWorker.kt:118-133` — drain poursuivi après échec + cascade limitée au même `resource_id`
|
||||||
|
Après `markFailed`, `continue` envoie le batch suivant, et `failDescendants` (`PendingOperationDao.kt:38`) ne cible que `resource_id = :resourceId` alors que les enfants portent un autre `resource_id` (lien dans le payload).
|
||||||
|
Impact : viole « serveur stop à la 1re erreur, client reprend à cet index » — creates enfants poussés et rejetés `NOT_FOUND` un par un, head-of-line blocking + dead-letters en cascade.
|
||||||
|
Repro : `create` dossier-parent en échec → le run pousse quand même les creates enfants au tour suivant.
|
||||||
|
|
||||||
|
### C7 — `features/sync/OutboxSyncWorker.kt:112-131` — gardes absents sur réponse serveur (boucle infinie / crash)
|
||||||
|
(a) `result.failed.operationId` absent du batch (`indexOfFirst < 0`) → silence + re-sélection du même batch → boucle serrée sans backoff ; (b) `result.applied > pending.size` → `pending[i]` lève `IndexOutOfBoundsException` → crash worker.
|
||||||
|
Impact : (a) drain CPU/batterie ; (b) batch jamais acquitté. Complété par `data/remote` : `while(true)` sans garde `applied==0 && failed==null` → busy-loop si serveur renvoie `{applied:0, failed:null}`.
|
||||||
|
Repro : mocker `syncOps → applied=0, failed=null` ou `operation_id` inconnu → boucle infinie ; `applied` incohérent → crash.
|
||||||
|
|
||||||
|
### C8 — Scan : perte de données à l'export
|
||||||
|
- `ui/scan/ScanViewModel.kt:387-389` (`exportPdf`) — pages en échec seulement loggées (`failedIds`), puis **toutes** marquées `EXPORTED` + session `DONE` → PDF incomplet, sources abandonnées.
|
||||||
|
- `ui/scan/ScanViewModel.kt:259-298` (`export`) — retry non idempotent : `getPages` retourne aussi les `EXPORTED`, chaque retry crée un nouvel URI SAF → documents + lignes Room + ops outbox dupliqués.
|
||||||
|
- `ui/scan/ScanViewModel.kt:263` — fichier temp manquant → page sautée sans marquage mais `finishSession` ferme la session → page `PENDING` dans session `DONE`, jamais reprise ni nettoyée.
|
||||||
|
- `ui/scan/ScanViewModel.kt:186-201` — nom page `page_$order.jpg` avec `order = pageCount+1` → collision après suppression (A,B → del A → C écrase B) → deux entités même `tempUri`, `deletePage` orpheline B.
|
||||||
|
Repro : cf. chaque item (export partiel, retry après perte permission SAF, temp supprimé, capture-suppression-capture).
|
||||||
|
|
||||||
|
### C9 — `features/scan/ScanImageProcessor.kt:87-88` + `ui/scan/ScanViewModel.kt:192` — `recycle()` du bitmap encore affiché quand OpenCV indisponible
|
||||||
|
`process()` retourne l'instance d'entrée si `!isAvailable()` ; l'appelant `recycle()` le `processed` qui alias le draft dessiné par `CropStage` → crash `recycled bitmap`.
|
||||||
|
Repro : device sans lib OpenCV → valider un crop → crash au retour caméra.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🟠 Majeurs
|
||||||
|
|
||||||
|
### Sync / Outbox / SAF
|
||||||
|
- **M1 — `features/sync/DeviceSync.kt:85-91` + `data/repository/FolderRepository.kt:99-134`** : le walk écrase le parent des dossiers sans garde pending-move (`saveFolder` écrase toujours, `reparentSubFolders` métadonnée-seule sans déplacement SAF). Les fichiers ont `hasPendingMoveOperation` (`FileRepository.kt:190`), les dossiers rien → tout reparent/merge revert au tick 30s suivant, divergence disque/serveur permanente.
|
||||||
|
- **M2 — `data/repository/FileRepository.kt:238-250`** : `applyMove` enqueue `move_resource` sans gate `create` (contrairement aux deletes) → move d'un fichier jamais poussé = `NOT_FOUND`/no-op serveur + ligne figée par `hasPendingMoveOperation`. Même cause `reparentSubFolders` (`FolderRepository.kt:144-157`) pour dossier au `create` purgé (>7j).
|
||||||
|
- **M3 — `features/saf/FileMover.kt:47-53`** : repli métadonnée-seule incohérent (nouveau parent + ancienne uri quand `moveDocument` refusé) → Room ment durablement, walk bloqué par le garde pending-move, divergence serveur/disque.
|
||||||
|
- **M4 — `features/sync/DeviceSync.kt:122-134`** : réconciliation `exists=0` asymétrique — fichiers exclus via `hasMoveOperation` (pending **ou** synced → à vie après un move syncé, fichier supprimé ensuite = fantôme `exists=1` éternel) ; dossiers sans exclusion (move en transition marqué `missing` à tort).
|
||||||
|
- **M5 — `features/saf/FileDeleter.kt:88-100` + `features/saf/FolderDeleter.kt:91-103`** : `deleteInCloud` sans tombstone locale (`exists` reste 1) → ressource toujours affichée, walk la revoit, `refreshFromServer` ne supprime jamais de lignes → fantôme local vs serveur supprimé.
|
||||||
|
- **M6 — `features/sync/DeviceSync.kt:84`** : `?: root.resourceId` masque toute violation parent-avant-enfant (parent absent de `resourceIdByUri` → enfant rattaché racine) → arbre serveur corrompu silencieusement.
|
||||||
|
- **M7 — `features/sync/SyncViewModel.kt:135-156`** : `importRoot` ne schedule jamais le drain (contraire `syncAll:110`, login `AuthViewModel:54,77`) → `create_resource` racine dort jusqu'au prochain tick, badge faussement à jour.
|
||||||
|
- **M8 — `features/sync/SafScanner.kt:146`** : `if (size == 0L) continue` → fichiers vides légitimes exclus du miroir, jamais reviewés ni poussés (perte silencieuse).
|
||||||
|
- **M9 — `features/saf/FolderMerger.kt:45-85`** : merge multi-transactions non-atomique, non-idempotent (`createMergedFolder` recrée un dossier à chaque appel) → crash à mi-fusion = état partiel poussé, relance = dossier dupliqué côté serveur.
|
||||||
|
|
||||||
|
### Room / DB / Domain
|
||||||
|
- **M10 — `domain/DeviceIdentity.kt:18-26`** : `getOrCreate()` non atomique (read/insert/re-read hors transaction) + fallback `newResourceId()` jamais persisté → deux identités concurrentes, `owner_id` incohérents, outbox sous identité fantôme (aggravé par C1).
|
||||||
|
- **M11 — `data/local/migration/Migrations.kt:90-96`** : `MIGRATION_5_6` fait `UPDATE files SET processed = 1` sans `create_resource` rétroactif → backlog jamais poussé, invisible de la review (`getUnprocessed`) et du serveur, à vie.
|
||||||
|
- **M12 — `data/local/dao/FolderDao.kt:50-54` + `data/local/dao/FileDao.kt:48-49`** : `@Upsert` résout sur PK (`id`) pas sur uniques `resource_id`/`uri` ; pattern read-then-upsert (`id=0`) → deux inserts concurrents même `uri` = `SQLiteConstraintException` (crash walk SAF / refresh serveur).
|
||||||
|
- **M13 — entités `exists: Int? = null` + filtres `= 1`** (`FolderEntity.kt:36`, `FileEntity.kt:45`, `FolderDao.kt:34,41`, `FileDao.kt:15,18,21,32,42,95,98,108,135`, `Migrations.kt:51-72` sans DEFAULT) : toute ligne sans `exists` explicite disparaît UI/recherche/review — perte fonctionnelle silencieuse.
|
||||||
|
- **M14 — `data/local/entity/PendingOperationEntity.kt:28-53` + `Migrations.kt:101-123`** : pas de colonne `device_id`, unicité locale sur `operation_id` seul vs contrat `UNIQUE(device_id, operation_id)` → après régénération `device_user_id`, rejeu = `create_resource` dupliqués serveur.
|
||||||
|
- **M15 — `data/local/dao/PendingOperationDao.kt:38`** : `failDescendants` ne cascade que sur le même `resource_id`, pas les enfants (cf. C6).
|
||||||
|
- **M16 — `data/local/dao/FileDao.kt:133-145` + `Migrations.kt:75-82`** : `category` ajoutée sans backfill, `getOcrAutoCandidates()` exige `category IN (…)` → tout PDF/image/texte pré-v4 exclu de l'OCR auto à vie.
|
||||||
|
- **M17 — `data/local/dao/FileDao.kt:151-152`** : `clearOcrQueued` reset `ocr_attempts = 0` inconditionnellement (contredit commentaire `:130-132`) → échec permanent rejoué indéfiniment.
|
||||||
|
- **M18 — pas de FK `files → folders`** (`FileEntity.kt:18-26`, `FolderEntity.kt:18-25`, `FolderDao.kt:95-96`, `FileDao.kt:92-93` deletes physiques) → `remove(dossier)` laisse orphelins, `moveToFolder` vers parent inexistant accepté, cycles possibles.
|
||||||
|
- **M19 — `FolderDao.kt:56-57` + `FileDao.kt:51-52`** : `markMissing` = `SET exists = 0` seul (ni `sync_status` ni annulation `create` pending) → ressource disparue quand même poussée (fantôme serveur), placement `local-cloud` mensonger.
|
||||||
|
- **M20 — `data/local/dao/ScanDao.kt:12-64` + `entity/ScanSessionEntity.kt:15-20`** : sessions `active` multiples possibles (aucun index unique/partiel), aucun `deleteSession` → doubles inserts concurrents = session orpheline + pages fuyantes.
|
||||||
|
|
||||||
|
### Réseau / Auth / Repos
|
||||||
|
- **M21 — `data/repository/FileRepository.kt:261-272` + `data/remote/ApiClient.kt:64-65`** : pagination jetée (`pageSize=50` sans `page`, `meta` ignoré) alors que le serveur pagine → dossiers > 50 fichiers ne convergent jamais, silencieusement.
|
||||||
|
- **M22 — `data/remote/ApiClient.kt:119-125`** : `catch (Exception) → NETWORK_ERROR` attrape aussi `JsonDataException` (Moshi) et `InterruptedIOException` (annulation) → parsing cassé = `retry()` infini, coroutine annulée = `ApiException` au lieu de `CancellationException` (scope/WorkManager corrompus).
|
||||||
|
- **M23 — `data/repository/ShareRepository.kt:45,99-104` + `FolderRepository.kt:115`** : confusion `device_id` vs `user_id` — `syncSnapshot` compare `deviceIdentity.getOrCreate()` (device) à `perm.ownerId` (user serveur), `saveFolder` stocke le device dans `ownerId`, `ActiveUserStore` ignoré. Le snapshot serveur **exclut les ressources possédées** → toute ligne cloud-only au vrai `ownerId=user` est `markMissing` à chaque login/restore (ex. note `createNote(ownerId=userId)` disparaît).
|
||||||
|
- **M24 — `features/sync/OutboxSyncWorker.kt:159-170` + `data/repository/OutboxRepository.kt:29-31`** : round-trip `expiresAt` Long→Double (Moshi `Map<String,Any?>`) → `enqueueShare(expiresAt: Long)` réémis `1.7E12` → serveur `*int64` refuse → `400 INVALID_REQUEST` → **tout partage avec expiration dead-lettered**.
|
||||||
|
- **M25 — `ui/auth/AuthViewModel.kt:43,123-125`** : callback 401 singleton possédé par un ViewModel (`apiClient.onUnauthorized`, `ApiClient` `@Singleton` survit au VM, annulé dans `onCleared`) → après destruction, 401 ne purge plus la session → token expiré réutilisé, worker `success()` sur 401 fige l'outbox, utilisateur jamais déconnecté.
|
||||||
|
- **M26 — `domain/ServerConfigStore.kt:32-43` + `data/remote/ServerUrlInterceptor.kt:25-38`** : race `load()` — interceptor lit `current` (défaut `10.0.2.2`) sur thread OkHttp pendant que `load()` restaure l'URL ; `registerDevice()` part avant `load()` → cold start avec URL custom = premières requêtes (y compris `Authorization: Bearer` restauré) vers l'hôte par défaut (**fuite du bearer**).
|
||||||
|
- **M27 — `data/repository/ShareRepository.kt:44-93` + `data/remote/dto/Dtos.kt:120-132`** : snapshot permissions sans delta (`after` jamais passé), `sharedById`/`expiresAt` en `Any?` jamais lus, `cachedAt` jamais persisté, aucun downgrade 24h ni `canAccess` → grant révoqué/expiré reste visible et ouvrable (`404`) jusqu'au prochain login.
|
||||||
|
- **M28 — `domain/ServerConfigStore.kt:46-59`** : `set()` persiste URL non validée (`"http://"` → `"http:"`, `"foo bar"` → `"http://foo bar"`) → app semi-brickée jusqu'à correction manuelle (recoupé par 2 agents).
|
||||||
|
|
||||||
|
### UI / Navigation / VM
|
||||||
|
- **M29 — `ui/folderlist/FolderListScreen.kt:202+220`** : onboarding racine bloqué après rotation — `pendingDefaultPick` en `remember` (non-saveable), `LaunchedEffect` polling annulé à la rotation, import SAF continu en fond mais `setDefaultRoot()` jamais rappelé → écran figé sur `DefaultRootOnboarding`, re-picker = doublon d'import.
|
||||||
|
- **M30 — `ui/document/DocumentViewerScreen.kt:130-137`** : position pager perdue à la rotation (`jumpPending = remember`), re-scroll vers `initialResourceId` → page N → retour page initiale. Fix : `rememberSaveable` / mémoriser index courant.
|
||||||
|
- **M31 — `ui/search/SearchScreen.kt:276-284` + `SearchViewModel.kt:55-72`** : `TextField` branché sur la valeur **debouncée** (300 ms) → frappe affichée par à-coups, IME/autocorrect écrasés. Correct : champ sur requête brute, debounce pour Room seule.
|
||||||
|
- **M32 — `ui/folderdetail/FolderDetailScreen.kt:196-202`** : Partager sans gate `isShareable` (contraire FolderList `canShare`, `:154-159`) → partage d'un dossier local-only/reçu = op `share` vouée au dead-letter `NOT_FOUND`.
|
||||||
|
- **M33 — `ui/folderlist/FolderListViewModel.kt:165-173`** : `canShare` stale au login/logout (combine `_defaultRootId`/`_browseFolderId` sans token/auth) → « Partager » reste désactivé jusqu'à navigation après connexion depuis Réglages.
|
||||||
|
- **M34 — `ui/folderlist/FolderListScreen.kt:333-375 vs 326-330`** : suppression multi-dossiers filtre les items **visibles** alors que `selection.ids` survit à `openBrowseFolder` (jamais clear) → sélection hors-vue abandonnée silencieusement avec snackbar succès. Même classe `ui/search/SearchScreen.kt:290-305` (chips actifs en mode sélection, delete par snapshot).
|
||||||
|
- **M35 — `ui/document/DocumentViewerScreen.kt:131-144`** : pas de clamp après suppression — supprimer le dernier doc (page 2/2) laisse `pagerState` hors bornes → viewer vide au lieu de retomber sur 1er ou `onBack`.
|
||||||
|
- **M36 — `ui/settings/SettingsScreen.kt:120-128`** : préférence `SYSTEM` inatteignable (Switch DARK/LIGHT depuis `darkNow`, store + thème supportent SYSTEM) → impossible de revenir à « suivre le système ».
|
||||||
|
- **M37 — `ui/settings/SettingsViewModel.kt:30-36`** : `load()` écrase une saisie serveur en cours (`_serverUrl` init défaut puis réassigné après `load()` en `init`).
|
||||||
|
|
||||||
|
### Scan / PDF / Review / Thumbnails
|
||||||
|
- **M38 — `data/repository/ScanRepository.kt:63-78`** : `addPage` avec `sortOrder = pageCount` → doublons après suppression (`ORDER BY sort_order` instable, `swapOrder` ambigu). Cause : compteur au lieu de `MAX+1`.
|
||||||
|
- **M39 — `ui/scan/ScanViewModel.kt:180/199`** : dimensions stockées (pré-scale) ≠ réelles (downscale 2400px dans `process()`) → fiche `W×H` fausse pour grands quads.
|
||||||
|
- **M40 — `ui/scan/ScanViewModel.kt:177-203`** : écriture JPEG hors transaction Room (orphelins si insert échoue) ; `deletePage` supprime le fichier **avant** la ligne DB (crash = ligne vers fichier absent → rejoint C8).
|
||||||
|
- **M41 — `data/repository/FileRepository.kt:206`** : `saveLocalFile` ne remonte jamais `cloud` → `local-cloud` (`existing?.syncStatus ?: …`) → fichier cloud-only réimporté en SAF reste badgé « cloud », ignoré vignette/OCR (`sync_status='local-cloud'` requis, `FileDao.kt:137`).
|
||||||
|
- **M42 — `ui/pdfbuilder/PdfBuilderViewModel.kt:227-237`** : PDF assemblé importé `processed=false` (défaut omis, contraire export scan `true`) → atterrit en review alors que l'utilisateur vient de le construire.
|
||||||
|
- **M43 — `ui/review/SwipeReviewViewModel.kt:68-74,94-100`** : retrait optimiste sans réinsertion sur échec (`keep`/`markAllProcessed`, contraire `delete` qui réinsère) → carte perdue jusqu'au prochain `load()` (init seule).
|
||||||
|
- **M44 — `ui/pdfbuilder/PdfBuilderScreen.kt:248`** : `remember(files)` réinitialise la sélection à chaque update Room → cases cochées effacées pendant un refresh serveur.
|
||||||
|
- **M45 — `ui/pdfbuilder/PdfBuilderEngine.kt:104-107`** : pages PDF sources plafonnées à 1200pt malgré commentaire « format conservé » (`min(1, 1200/max, 3000/max)`, le 1200 gagne toujours) → pixellisation A3/300dpi.
|
||||||
|
- **M46 — `features/scan/CornerGeometry.kt:19-26`** : `orderPoints` sans validation dégénérescence (doublons/quad aplati) → `outputSize = 0` → bitmap 1×1 validé (page blanche) au lieu d'erreur.
|
||||||
|
- **M47 — `ui/scan/CameraStage.kt:162-181`** : overlay calibré sur flux `ImageAnalysis` (hypothèse `FILL_CENTER`) alors que `PreviewView` affiche le flux `Preview` (ratio/crop différents) → quad décalé systématiquement quand les ratios diffèrent.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🟡 Mineurs (UX, perf, dette)
|
||||||
|
|
||||||
|
### Sync / Worker / SAF
|
||||||
|
- **m1 — `features/sync/SyncViewModel.kt:103-121`** : `_walkInProgress` sans `finally` (reste `true` si scope annulé) → badge « syncing » collé ; la fenêtre inclut l'appel réseau partage.
|
||||||
|
- **m2 — `features/sync/SyncViewModel.kt:106-112`** : `OutboxSyncWorker.enqueue` + `OcrAutoWorker.enqueue` à chaque tick même sans changement → réveils WorkManager / 30s pour rien.
|
||||||
|
- **m3 — `features/sync/OutboxSyncWorker.kt:97-100`** : 401 → `success()` sans re-signal, ops pendantes figées jusqu'au prochain tick/login (bénin, `AuthViewModel` ré-enqueue au re-login).
|
||||||
|
- **m4 — `features/sync/SafScanner.kt:132-139`** : `cursor.getString(iDoc)` sans garde `iDoc == -1`, `name` plateforme-nullable déréférencé → provider dégénéré = crash de tout `scanTree` à chaque tick.
|
||||||
|
- **m5 — `features/sync/DeviceSync.kt:75-137`** : « transaction courte » en réalité géante (N upserts + `getAll()` dossiers **et** fichiers par racine, O(racines×DB) sous verrou écriture) → lecteurs Room bloqués, jank/ANR sur grosses arborescences.
|
||||||
|
- **m6 — `features/saf/FolderDeleter.kt:74-88`** : `deleteLocally` multi-writes hors transaction (contraire `treeMarkMissing`) → kill à mi-chemin = `exists` partiel.
|
||||||
|
|
||||||
|
### Room / Domain / DI
|
||||||
|
- **m7 — `data/local/dao/FileDao.kt:30-37`** : `searchWithFilters` `LIKE` brut sans `ESCAPE`, casse ASCII-only → `%`/`_` = jokers (match-all), « ete » ne trouve pas « ÉTÉ ».
|
||||||
|
- **m8 — `data/local/FileOrdering.kt:5-11`** : tri mémoire (`referenceDate` seule) divergent du SQL (`name ASC` tiebreak, `FileDao.kt:21,43`) ; `lastModified = 0` SAF trié comme plus vieux → ordre incohérent + fichiers `0` enfouis.
|
||||||
|
- **m9 — `data/local/dao/PendingOperationDao.kt:30-32`** : commentaire `MAX_PENDING_ATTEMPTS` contredit le code (`markFailed()` = dead-letter immédiate, `attempts` diagnostique) — trompeur pour futurs appelants (cohérent avec worker, seul le commentaire est faux).
|
||||||
|
- **m10 — `data/local/dao/FileDao.kt:89`** : `updateUri` ne touche pas `updated_at` → ordre de convergence faussé après move.
|
||||||
|
- **m11 — `domain/LocalModeStore.kt:24`** : `get()` teste `!= null` au lieu de `== "1"` → toute valeur parasite (`"0"`, `""`) = mode local actif à tort.
|
||||||
|
- **m12 — `domain/ServerConfigStore.kt:40-44` + `domain/ThemePreferenceStore.kt:28-32`** : `load()` once-only (`AtomicBoolean`, sans réarmement) → après wipe/logout, URL/thème périmés en mémoire.
|
||||||
|
- **m13 — `domain/ThemePreferenceStore.kt:34-39`** : `set()` publie le `StateFlow` avant succès DB → divergence UI/DB si upsert échoue.
|
||||||
|
- **m14 — `domain/PrefKeys.kt:8`** : clé `SYNC_MODE` morte (0 usage, grep vérifié) → dérive spec/impl.
|
||||||
|
- **m15 — `di/DatabaseModule.kt:25-28`** : aucune stratégie de downgrade → APK plus ancien sur DB v11 = crash `IllegalStateException` au lieu de dégradation contrôlée.
|
||||||
|
- **m16 — index composites manquants** (`PendingOperationEntity.kt:20-27`, `FileEntity.kt:18-26`, `PendingOperationDao.kt:13`, `FileDao.kt:133-145`) : `selectPending` et `getOcrAutoCandidates` = full-table scan → jank/batterie à 10k lignes.
|
||||||
|
- **m17 — `data/local/entity/FileEntity.kt:56-57` vs `Migrations.kt:92`** : `processed` sans `defaultValue` Room alors que la migration met `DEFAULT 0` (contraire `createdInApp`, `FolderEntity.kt:43`) → fresh-install ≠ migré.
|
||||||
|
- **m18 — `data/local/dao/ScanDao.kt:48-57`** : `swapOrder` sans contrôle d'appartenance ni transaction → swap inter-sessions corrompt `sort_order`, concurrence = doublons.
|
||||||
|
- **m19 — `data/local/AppDatabase.kt:27-34`** : `exportSchema = false`, migrations non snapshotées → dérives indétectables en CI.
|
||||||
|
- **m20 — `data/repository/OcrRepository.kt:66-73`** : `poll()` infini (`while(true)` + 3s, sans timeout) alors qu'un job peut rester `processing` 15 min (stale claim serveur) → coroutine tenue indéfiniment.
|
||||||
|
- **m21 — `data/repository/OutboxRepository.kt`** : ops contrat non émissibles (`update_metadata`, `update_share`, `create_link`, `revoke_link` — §6.1 = 9 ops, 5 helpers) → rename partagé, update share, liens publics inopérants.
|
||||||
|
- **m22 — `data/remote/ApiService.kt`** : endpoints contrat absents (`PATCH /users/me/password`, `GET/DELETE /files/:id`, `GET /files/search`, `GET /shares/links/:token`) → changement mot de passe impossible avec tokens 7j non révoqués.
|
||||||
|
- **m23 — `data/repository/AuthRepository.kt:21-35`** : `registerDevice` best-effort avale tout (y compris 400/429) → `INVALID_DEVICE_ID`/`RATE_LIMITED` invisibles, 401 trompeur plus tard.
|
||||||
|
- **m24 — `ui/auth/AuthViewModel.kt:52-55`** : `registerDevice()` émis même en mode local (pas de garde contrairement à `syncSnapshot`).
|
||||||
|
- **m25 — `di/NetworkModule.kt:37-43`** : logging `BASIC` en DEBUG loggue les query PII (`GET /users/resolve?username=…`) dans logcat.
|
||||||
|
- **m26 — `data/remote/AuthInterceptor.kt:20-26`** : `Bearer` joint aux routes publiques (`POST /devices`, `POST /auth/login`) — serveur l'ignore mais hygiène + amplification de la fuite M26.
|
||||||
|
- **m27 — `auth/SessionManager.kt:33-38`** : `expiresAt` du login jeté (paseto TTL 7j) → expiration découverte par 401 brutal, impossible de pré-relancer.
|
||||||
|
- **m28 — `auth/SecureTokenStore.kt:27-36`** : init Keystore/`EncryptedSharedPreferences` au construct Hilt + I/O synchrone possiblement Main (`MasterKeys` déprécié) → ANR au restore, crash Direct Boot avant déverrouillage.
|
||||||
|
|
||||||
|
### UI
|
||||||
|
- **m29 — `ui/navigation/NavGraph.kt:50`** : `pdfBuilder(listOf())` → `"pdfbuilder/"` ne matche pas `"pdfbuilder/{ids}"` (protégé aujourd'hui par `enabled = ids.isNotEmpty()` — contrat implicite fragile).
|
||||||
|
- **m30 — `ui/navigation/NavGraph.kt:53-59,165`** : `folder/…` sans `launchSingleTop` → double-tap = doublon de pile.
|
||||||
|
- **m31 — `ui/navigation/KazierApp.kt:54-63`** : `rememberNavController()` par branche SignedIn↔Local → `signOutToLocal()` perd la position d'exploration (hoister le controller).
|
||||||
|
- **m32 — `ui/folderdetail/FolderDetailScreen.kt:395-429`** : dossiers non sélectionnables dans le détail (vs FolderList) — incohérence partage/multi-suppression inter-écrans.
|
||||||
|
- **m33 — `ui/pdfbuilder/PdfBuilderScreen.kt:104-114`** : `LaunchedEffect(buildPhase)` rejoue side-effects à la recomposition racine (rotation → `Failed` re-snackbar, `Saved` re-`onOpenDocument` = double navigation possible).
|
||||||
|
- **m34 — dialogs `remember` perdus à la rotation** (`SyncStatusBadge.kt:129`, `showDeleteWarning/Confirm`, `showMoveDialog`, `showShareSheet`, `showNameDialog`) — sûr mais UX ; `showOcrDialog` en `rememberSaveable` = bon exemple.
|
||||||
|
- **m35 — `ui/components/WatchedFoldersBadge.kt:36-40` + `WatchedFoldersViewModel.kt:25-27`** : double instance VM + `SharingStarted.Eagerly` collecte Room en continu même sur autres onglets.
|
||||||
|
- **m36 — `ui/settings/SettingsViewModel.kt:43-48`** : `onServerUrlChange` écrit en prefs à chaque frappe (churn IO) ; commentaire `testConnection` obsolète.
|
||||||
|
- **m37 — `MainActivity.kt:27-33`** : `themePreferenceStore.load()` fire-and-forget → flash de thème aux premières frames.
|
||||||
|
- **m38 — `ui/navigation/NavGraph.kt:213-219`** : bandeau import global chevauchant la FloatingNavBar, doublon des indicateurs par-écran.
|
||||||
|
- **m39 — `ui/pdfbuilder/PdfBuilderViewModel.kt:236`** : `syncStatus="local"` en littéral au lieu de `FileStatus.LOCAL` — fonctionne mais casse silencieusement à toute renommée.
|
||||||
|
|
||||||
|
### Scan / PDF / Thumbnails
|
||||||
|
- **m40 — `ui/scan/CameraStage.kt:86-91`** : `ImageCapture` forcé 1920×1080 paysage → sur portrait, bitmap pleine-rés + copie tournée (×2 mémoire ~16 Mo) → OOM petits devices.
|
||||||
|
- **m41 — `ui/scan/CameraStage.kt:165-181`** : `toBitmap()` pleine-rés à chaque frame d'analyse (~30fps) puis downscale interne → churn GC + batterie.
|
||||||
|
- **m42 — `ui/scan/CameraStage.kt:99-101`** : `addOnLayoutChangeListener` jamais retiré → fuite mineure du listener.
|
||||||
|
- **m43 — `ui/scan/CameraStage.kt:200-211`** : `when` rotation sans `else` → angle inattendu = copie silencieuse au lieu d'erreur.
|
||||||
|
- **m44 — `ui/scan/ScanGeometry.kt:42-69`** : division par zéro si `sourceRect` dégénérée (`Infinity/NaN` dans le quad ; inatteignable via `initialViewport` mais API publique exposée).
|
||||||
|
- **m45 — `ui/scan/CropStage.kt:188-193`** : arrondi `roundToInt` viewport indépendant de l'offset → dépassement 1px en bordure (frange noire sur images impaires).
|
||||||
|
- **m46 — `ui/scan/ScanViewModel.kt:111-122`** : `combine(pages, pageCount)` avec count ignoré → réassignations `_pages` redondantes.
|
||||||
|
- **m47 — `ui/scan/ScanViewModel.kt:409-418`** : `abandon()` laisse sessions vides `ABANDONED` jamais purgées + `sessionDir()` fait `mkdirs()` en getter (recrée ce qu'on supprime).
|
||||||
|
- **m48 — `data/repository/ScanRepository.kt:27-40`** : race `getOrCreateActiveSession` (double `active` possible, aucune unicité partielle ; fenêtre étroite).
|
||||||
|
- **m49 — `domain/FileCategory.kt:19-27`** : CSV classé `TEXT` (par MIME) ou `OFFICE` (par extension) selon le chemin → icône/filtres/OCR divergents pour le même fichier.
|
||||||
|
- **m50 — `features/thumbnails/ThumbnailStore.kt:51`** : map de `Mutex` jamais purgée → fuite lente par `resourceId`.
|
||||||
|
- **m51 — `features/thumbnails/ThumbnailStore.kt:104-112`** : `.tmp` orphelin si `renameTo` échoue → résidus `*.webp.tmp`.
|
||||||
|
- **m52 — `features/thumbnails/ThumbnailStore.kt:83-87`** : `isUpToDate` sur `mtime` seule (granularité seconde SAF) → vignette périmée après édition rapide (pas de taille/hash).
|
||||||
|
- **m53 — `ui/pdfbuilder/PdfBuilderEngine.kt:121-124`** : `renderer?.close()` non protégé (si `close()` lève, `pfd.close()` sautée → fd leak ; `ThumbnailStore.renderPdfPage` fait `runCatching`).
|
||||||
|
- **m54 — `features/scan/CornerGeometry.kt:40-45`** : commentaire « moyenne » mais code `max` → warp légèrement étiré vs réel.
|
||||||
|
- **m55 — `features/scan/ScanImageProcessor.kt:126-181`** : mats de contours non libérés sur exception (`c.release()` chemin nominal seul) → fuite native OpenCV.
|
||||||
|
- **m56 — `ui/review/SwipeReviewViewModel.kt:52-64`** : échec de reload écrase le deck (`update{cards=files}` même sur `emptyList` d'erreur) → deck vidé à tort.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## ✅ Vérifié infondé (suspects écartés)
|
||||||
|
- Single-flight `Mutex` présent (`DeviceSync.kt:49`), `enqueueUniqueWork(KEEP)` + batch 20 + `ORDER BY id` conformes — non-bugs.
|
||||||
|
- `checkHealth` construit bien `{base}/health` avec base incluant `/api/v1` = route backend `GET /api/v1/health` — OK.
|
||||||
|
- Token **non** stocké en clair (`EncryptedSharedPreferences` AES256-SIV/GCM + Keystore) — seul `TokenProvider.current` en mémoire, normal.
|
||||||
|
- Ordre interceptors `serverUrl → auth → logging`, `BASIC` ne log ni headers ni bodies — OK.
|
||||||
|
- Contrat HTTP conforme sur endpoints déclarés (query `folderId/page/pageSize`, `deviceId` camel vs `device_id` login, multipart `file`/`resourceId`/`folderId`, enveloppes `{data,meta}`/`{error:{code,message}}`, codes) — OK.
|
||||||
|
- `device_id` claim jamais utilisé comme preuve, seul le `Bearer` parle — conforme middleware.
|
||||||
|
- Worker no-op en mode local (`OutboxSyncWorker.kt:71`), batch 20 ≤ 100 serveur, statuts OCR, `resolveUser` trim compatible, password jamais trimé — OK.
|
||||||
|
- Deep-links : aucun défini (absence, pas de bug) ; args nav `folderResourceId`/`documentResourceId`/`ids` cohérents, 32-hex sans `/`.
|
||||||
|
- Hilt : `MainActivity @AndroidEntryPoint`, tous `hiltViewModel()` ↔ `@HiltViewModel`, scopes corrects — aucune injection manquante.
|
||||||
|
- Pas de recomposition infinie trouvée ; `collectAsStateWithLifecycle` partout ; placeholder cloud-only correct (`DocumentContentViewer.kt:39-47`) ; recherche 100 % locale conforme V1 ; back stack onglets correct (`saveState`/`restoreState`) ; `SelectionState` volatile documenté volontaire.
|
||||||
|
- Scan : pas de double-`recycle` draft, pas de double rotation EXIF (comportement correct, commentaire trompeur), `swapOrder` args corrects, FK `scan_pages→sessions CASCADE` actif.
|
||||||
+1
-1
@@ -34,7 +34,7 @@ LABEL org.opencontainers.image.title="kazier-server" \
|
|||||||
org.opencontainers.image.description="Kazier backend API (Go/Gin + PostgreSQL) — local-first document vault" \
|
org.opencontainers.image.description="Kazier backend API (Go/Gin + PostgreSQL) — local-first document vault" \
|
||||||
org.opencontainers.image.version="${VERSION}" \
|
org.opencontainers.image.version="${VERSION}" \
|
||||||
org.opencontainers.image.licenses="AGPL-3.0-only" \
|
org.opencontainers.image.licenses="AGPL-3.0-only" \
|
||||||
org.opencontainers.image.source="https://gitlab.thirdshop.fr/thirdshop/kasier" \
|
org.opencontainers.image.source="https://gitea.thirdshop.fr/thirdshop/kazier" \
|
||||||
org.opencontainers.image.url="https://thirdshop.fr/en/applications/kazier/" \
|
org.opencontainers.image.url="https://thirdshop.fr/en/applications/kazier/" \
|
||||||
org.opencontainers.image.created="${BUILD_DATE}" \
|
org.opencontainers.image.created="${BUILD_DATE}" \
|
||||||
org.opencontainers.image.revision=""
|
org.opencontainers.image.revision=""
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -41,14 +42,19 @@ func main() {
|
|||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Migrations AVANT l'ouverture du pool applicatif : un seul *sql.DB vit à
|
||||||
|
// la fois (MigrateDatabase ouvre/ferme sa propre connexion éphémère).
|
||||||
|
// Inter-lots : pour partager strictement le même pool, db doit exposer
|
||||||
|
// Migrate(conn *sql.DB) — fichier LOT 4 (db/db.go), non édité ici.
|
||||||
|
if err := db.MigrateDatabase(cfg.DatabaseURL); err != nil {
|
||||||
|
log.Fatalf("migrations postgres: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
conn, err := db.Open(cfg.DatabaseURL)
|
conn, err := db.Open(cfg.DatabaseURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("connexion postgres: %v", err)
|
log.Fatalf("connexion postgres: %v", err)
|
||||||
}
|
}
|
||||||
|
defer conn.Close()
|
||||||
if err := db.MigrateDatabase(cfg.DatabaseURL); err != nil {
|
|
||||||
log.Fatalf("migrations postgres: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
authManager, err := auth.NewManager(cfg.AuthSecret)
|
authManager, err := auth.NewManager(cfg.AuthSecret)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -59,8 +65,19 @@ func main() {
|
|||||||
repo := repository.NewRepository(conn)
|
repo := repository.NewRepository(conn)
|
||||||
|
|
||||||
// Premier admin (users vide) — refus de démarrer si identifiants absents.
|
// Premier admin (users vide) — refus de démarrer si identifiants absents.
|
||||||
|
// Concurrence au boot (2 instances, users vide) : le perdant de l'INSERT
|
||||||
|
// reçoit ErrNameConflict (UNIQUE username_normalized) → recompte avant de
|
||||||
|
// refuser de démarrer (retry logique, sans toucher bootstrap.go — LOT 4).
|
||||||
if err := service.EnsureAdmin(repo, cfg.AdminUsername, cfg.AdminPassword); err != nil {
|
if err := service.EnsureAdmin(repo, cfg.AdminUsername, cfg.AdminPassword); err != nil {
|
||||||
log.Fatalln(err)
|
if errors.Is(err, repository.ErrNameConflict) {
|
||||||
|
if n, cerr := repo.Users.Count(); cerr == nil && n > 0 {
|
||||||
|
log.Println("bootstrap admin: compte créé par une instance concurrente, démarrage poursuivi")
|
||||||
|
} else {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handlers.Store = service.NewResources(
|
handlers.Store = service.NewResources(
|
||||||
@@ -74,7 +91,15 @@ func main() {
|
|||||||
defer stop()
|
defer stop()
|
||||||
|
|
||||||
// Worker OCR borné (OCR_WORKERS) : file `ocr_jobs`, hors request handlers.
|
// Worker OCR borné (OCR_WORKERS) : file `ocr_jobs`, hors request handlers.
|
||||||
go handlers.Ocr.Run(ctx, cfg.OcrWorkers)
|
// WaitGroup + budget d'arrêt partagé (5s, voir shutdown plus bas) : le
|
||||||
|
// worker finit le job en cours ; passé le délai, le process sort et le job
|
||||||
|
// resté `processing` sera requeue au prochain boot (ResetStaleProcessing).
|
||||||
|
var ocrWg sync.WaitGroup
|
||||||
|
ocrWg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer ocrWg.Done()
|
||||||
|
handlers.Ocr.Run(ctx, cfg.OcrWorkers)
|
||||||
|
}()
|
||||||
log.Printf("ocr worker démarré (%d workers)", cfg.OcrWorkers)
|
log.Printf("ocr worker démarré (%d workers)", cfg.OcrWorkers)
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
@@ -95,5 +120,19 @@ func main() {
|
|||||||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||||
log.Printf("shutdown: %v", err)
|
log.Printf("shutdown: %v", err)
|
||||||
}
|
}
|
||||||
|
// Le worker OCR dispose du même budget de 5s : on attend la fin du job en
|
||||||
|
// cours, passé ce délai le process sort (job resté `processing` → requeue
|
||||||
|
// au prochain boot via ResetStaleProcessing, jamais de job perdu).
|
||||||
|
ocrDone := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
ocrWg.Wait()
|
||||||
|
close(ocrDone)
|
||||||
|
}()
|
||||||
|
select {
|
||||||
|
case <-ocrDone:
|
||||||
|
log.Println("worker OCR arrêté")
|
||||||
|
case <-shutdownCtx.Done():
|
||||||
|
log.Println("worker OCR: budget 5s écoulé, arrêt forcé (jobs `processing` requeues au prochain boot)")
|
||||||
|
}
|
||||||
log.Println("arrêt propre")
|
log.Println("arrêt propre")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,11 +13,11 @@ var expectedRoutes = []string{
|
|||||||
"GET /api/v1/shares/links/:token",
|
"GET /api/v1/shares/links/:token",
|
||||||
|
|
||||||
"GET /api/v1/files",
|
"GET /api/v1/files",
|
||||||
|
"GET /api/v1/files/search",
|
||||||
|
"GET /api/v1/files/folders",
|
||||||
"GET /api/v1/files/:id",
|
"GET /api/v1/files/:id",
|
||||||
"GET /api/v1/files/:id/ocr",
|
"GET /api/v1/files/:id/ocr",
|
||||||
"DELETE /api/v1/files/:id",
|
"DELETE /api/v1/files/:id",
|
||||||
"GET /api/v1/files/search",
|
|
||||||
"GET /api/v1/files/folders",
|
|
||||||
"POST /api/v1/files/upload",
|
"POST /api/v1/files/upload",
|
||||||
|
|
||||||
"GET /api/v1/users/resolve",
|
"GET /api/v1/users/resolve",
|
||||||
@@ -44,3 +44,21 @@ func TestRoutesMatchClientContract(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestStaticFileRoutesListedBeforeParam (LOT 6 §1) : verrouille que les routes
|
||||||
|
// statiques /files/search et /files/folders restent déclarées avant
|
||||||
|
// /files/:id dans le contrat. Gin priorise déjà le statique sur le paramétré
|
||||||
|
// à l'exécution, mais l'ordre source documente l'intention ("/files/folders
|
||||||
|
// ne doit jamais être avalé par FilesGet avec id="folders"). Le comportement
|
||||||
|
// réel est couvert côté handlers (folders→200, :id="folders"→404).
|
||||||
|
func TestStaticFileRoutesListedBeforeParam(t *testing.T) {
|
||||||
|
index := map[string]int{}
|
||||||
|
for i, r := range expectedRoutes {
|
||||||
|
index[r] = i
|
||||||
|
}
|
||||||
|
for _, static := range []string{"GET /api/v1/files/search", "GET /api/v1/files/folders"} {
|
||||||
|
if index[static] > index["GET /api/v1/files/:id"] {
|
||||||
|
t.Errorf("contrat: %q doit rester listé avant %q (ordre §1)", static, "GET /api/v1/files/:id")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
@@ -19,6 +21,21 @@ type ApplicationConfig struct {
|
|||||||
AdminPassword string
|
AdminPassword string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sécurité AUTH_SECRET (FEEDBACK #4) :
|
||||||
|
// - valeur par défaut DEV UNIQUEMENT (jamais en production : refusée au boot
|
||||||
|
// si APP_ENV/ENV vaut "prod"/"production") ;
|
||||||
|
// - longueur minimale 32 caractères partout (dev comme prod) : le secret ne
|
||||||
|
// sert qu'à dériver la clé paseto v4-local (SHA256), un secret court ou
|
||||||
|
// deviné = tokens forgeables.
|
||||||
|
//
|
||||||
|
// NOTE inter-lots (LOT 6 « secret-doc ») : aligner backend/.env.example —
|
||||||
|
// documenter « ≥ 32 caractères, ex. `openssl rand -hex 32` » et ne plus
|
||||||
|
// suggérer une valeur copiable telle quelle.
|
||||||
|
const (
|
||||||
|
defaultAuthSecret = "dev-secret-change-me-please-set-in-prod"
|
||||||
|
minAuthSecretLen = 32
|
||||||
|
)
|
||||||
|
|
||||||
func LoadApplicationConfig() (*ApplicationConfig, error) {
|
func LoadApplicationConfig() (*ApplicationConfig, error) {
|
||||||
|
|
||||||
// .env optionnel — les défauts suffisent pour le dev local.
|
// .env optionnel — les défauts suffisent pour le dev local.
|
||||||
@@ -28,16 +45,33 @@ func LoadApplicationConfig() (*ApplicationConfig, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if port < 1 || port > 65535 {
|
||||||
|
return nil, fmt.Errorf("PORT hors plage 1-65535: %d", port)
|
||||||
|
}
|
||||||
|
|
||||||
maxSize, err := getInt("MAX_FILE_SIZE_MB", 50)
|
maxSize, err := getInt("MAX_FILE_SIZE_MB", 50)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if maxSize < 1 || maxSize > 1024 {
|
||||||
|
return nil, fmt.Errorf("MAX_FILE_SIZE_MB hors plage 1-1024: %d", maxSize)
|
||||||
|
}
|
||||||
|
|
||||||
ocrWorkers, err := getInt("OCR_WORKERS", 2)
|
ocrWorkers, err := getInt("OCR_WORKERS", 2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if ocrWorkers < 1 || ocrWorkers > 16 {
|
||||||
|
return nil, fmt.Errorf("OCR_WORKERS hors plage 1-16: %d", ocrWorkers)
|
||||||
|
}
|
||||||
|
|
||||||
|
authSecret := get("AUTH_SECRET", defaultAuthSecret)
|
||||||
|
if isProduction() && (authSecret == "" || authSecret == defaultAuthSecret) {
|
||||||
|
return nil, fmt.Errorf("AUTH_SECRET doit être défini en production (pas de défaut)")
|
||||||
|
}
|
||||||
|
if len(authSecret) < minAuthSecretLen {
|
||||||
|
return nil, fmt.Errorf("AUTH_SECRET trop court (min %d caractères)", minAuthSecretLen)
|
||||||
|
}
|
||||||
|
|
||||||
return &ApplicationConfig{
|
return &ApplicationConfig{
|
||||||
Port: port,
|
Port: port,
|
||||||
@@ -46,13 +80,21 @@ func LoadApplicationConfig() (*ApplicationConfig, error) {
|
|||||||
MaxFileSizeMB: int64(maxSize),
|
MaxFileSizeMB: int64(maxSize),
|
||||||
OcrLang: get("OCR_LANG", "fra+eng"),
|
OcrLang: get("OCR_LANG", "fra+eng"),
|
||||||
OcrWorkers: ocrWorkers,
|
OcrWorkers: ocrWorkers,
|
||||||
AuthSecret: get("AUTH_SECRET", "dev-secret-change-me"),
|
AuthSecret: authSecret,
|
||||||
AdminUsername: get("ADMIN_USERNAME", ""),
|
AdminUsername: get("ADMIN_USERNAME", ""),
|
||||||
AdminPassword: get("ADMIN_PASSWORD", ""),
|
AdminPassword: get("ADMIN_PASSWORD", ""),
|
||||||
}, nil
|
}, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isProduction() bool {
|
||||||
|
env := strings.ToLower(strings.TrimSpace(os.Getenv("APP_ENV")))
|
||||||
|
if env == "" {
|
||||||
|
env = strings.ToLower(strings.TrimSpace(os.Getenv("ENV")))
|
||||||
|
}
|
||||||
|
return env == "production" || env == "prod"
|
||||||
|
}
|
||||||
|
|
||||||
func get(name string, defaultValue string) string {
|
func get(name string, defaultValue string) string {
|
||||||
value := os.Getenv(name)
|
value := os.Getenv(name)
|
||||||
if value == "" {
|
if value == "" {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ func clearEnv(t *testing.T) {
|
|||||||
for _, name := range []string{
|
for _, name := range []string{
|
||||||
"PORT", "DATABASE_URL", "UPLOAD_DIR", "MAX_FILE_SIZE_MB",
|
"PORT", "DATABASE_URL", "UPLOAD_DIR", "MAX_FILE_SIZE_MB",
|
||||||
"OCR_LANG", "OCR_WORKERS", "AUTH_SECRET", "ADMIN_USERNAME", "ADMIN_PASSWORD",
|
"OCR_LANG", "OCR_WORKERS", "AUTH_SECRET", "ADMIN_USERNAME", "ADMIN_PASSWORD",
|
||||||
|
"APP_ENV", "ENV",
|
||||||
} {
|
} {
|
||||||
t.Setenv(name, "")
|
t.Setenv(name, "")
|
||||||
}
|
}
|
||||||
@@ -40,9 +41,12 @@ func TestLoadApplicationDefaults(t *testing.T) {
|
|||||||
if cfg.OcrWorkers != 2 {
|
if cfg.OcrWorkers != 2 {
|
||||||
t.Errorf("OcrWorkers = %d, attendu 2", cfg.OcrWorkers)
|
t.Errorf("OcrWorkers = %d, attendu 2", cfg.OcrWorkers)
|
||||||
}
|
}
|
||||||
if cfg.AuthSecret != "dev-secret-change-me" {
|
if cfg.AuthSecret != defaultAuthSecret {
|
||||||
t.Errorf("AuthSecret défaut inattendu: %q", cfg.AuthSecret)
|
t.Errorf("AuthSecret défaut inattendu: %q", cfg.AuthSecret)
|
||||||
}
|
}
|
||||||
|
if len(cfg.AuthSecret) < minAuthSecretLen {
|
||||||
|
t.Errorf("AuthSecret défaut trop court pour le minimum %d: %q", minAuthSecretLen, cfg.AuthSecret)
|
||||||
|
}
|
||||||
if cfg.AdminUsername != "" || cfg.AdminPassword != "" {
|
if cfg.AdminUsername != "" || cfg.AdminPassword != "" {
|
||||||
t.Errorf("identifiants admin doivent être vides par défaut: %+v", cfg)
|
t.Errorf("identifiants admin doivent être vides par défaut: %+v", cfg)
|
||||||
}
|
}
|
||||||
@@ -56,7 +60,7 @@ func TestLoadApplicationEnvOverrides(t *testing.T) {
|
|||||||
t.Setenv("UPLOAD_DIR", "/tmp/up")
|
t.Setenv("UPLOAD_DIR", "/tmp/up")
|
||||||
t.Setenv("OCR_LANG", "eng")
|
t.Setenv("OCR_LANG", "eng")
|
||||||
t.Setenv("OCR_WORKERS", "4")
|
t.Setenv("OCR_WORKERS", "4")
|
||||||
t.Setenv("AUTH_SECRET", "super-secret")
|
t.Setenv("AUTH_SECRET", "super-secret-0123456789abcdef01234567")
|
||||||
t.Setenv("ADMIN_USERNAME", "root")
|
t.Setenv("ADMIN_USERNAME", "root")
|
||||||
t.Setenv("ADMIN_PASSWORD", "toor")
|
t.Setenv("ADMIN_PASSWORD", "toor")
|
||||||
|
|
||||||
@@ -65,12 +69,46 @@ func TestLoadApplicationEnvOverrides(t *testing.T) {
|
|||||||
t.Fatalf("LoadApplicationConfig: %v", err)
|
t.Fatalf("LoadApplicationConfig: %v", err)
|
||||||
}
|
}
|
||||||
if cfg.Port != 9090 || cfg.MaxFileSizeMB != 120 || cfg.DatabaseURL != "postgres://u:p@host:5433/db?sslmode=disable" ||
|
if cfg.Port != 9090 || cfg.MaxFileSizeMB != 120 || cfg.DatabaseURL != "postgres://u:p@host:5433/db?sslmode=disable" ||
|
||||||
cfg.UploadDir != "/tmp/up" || cfg.OcrLang != "eng" || cfg.OcrWorkers != 4 || cfg.AuthSecret != "super-secret" ||
|
cfg.UploadDir != "/tmp/up" || cfg.OcrLang != "eng" || cfg.OcrWorkers != 4 || cfg.AuthSecret != "super-secret-0123456789abcdef01234567" ||
|
||||||
cfg.AdminUsername != "root" || cfg.AdminPassword != "toor" {
|
cfg.AdminUsername != "root" || cfg.AdminPassword != "toor" {
|
||||||
t.Errorf("overrides non appliqués: %+v", cfg)
|
t.Errorf("overrides non appliqués: %+v", cfg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadApplicationRejectsShortSecret(t *testing.T) {
|
||||||
|
clearEnv(t)
|
||||||
|
t.Setenv("AUTH_SECRET", "too-short")
|
||||||
|
if _, err := LoadApplicationConfig(); err == nil {
|
||||||
|
t.Error("AUTH_SECRET court : attendu une erreur (min 32)")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Le défaut dev passe en dev mais est refusé en production.
|
||||||
|
clearEnv(t)
|
||||||
|
if _, err := LoadApplicationConfig(); err != nil {
|
||||||
|
t.Errorf("défaut dev en dev : inattendu %v", err)
|
||||||
|
}
|
||||||
|
for _, env := range []string{"prod", "production", "PROD"} {
|
||||||
|
clearEnv(t)
|
||||||
|
t.Setenv("APP_ENV", env)
|
||||||
|
if _, err := LoadApplicationConfig(); err == nil {
|
||||||
|
t.Errorf("défaut dev avec APP_ENV=%q : attendu une erreur", env)
|
||||||
|
}
|
||||||
|
clearEnv(t)
|
||||||
|
t.Setenv("ENV", env)
|
||||||
|
if _, err := LoadApplicationConfig(); err == nil {
|
||||||
|
t.Errorf("défaut dev avec ENV=%q : attendu une erreur", env)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Un secret long et explicite passe en production.
|
||||||
|
clearEnv(t)
|
||||||
|
t.Setenv("APP_ENV", "prod")
|
||||||
|
t.Setenv("AUTH_SECRET", "prod-secret-0123456789abcdef01234567")
|
||||||
|
if _, err := LoadApplicationConfig(); err != nil {
|
||||||
|
t.Errorf("secret prod valide : inattendu %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestLoadApplicationRejectsInvalidInt(t *testing.T) {
|
func TestLoadApplicationRejectsInvalidInt(t *testing.T) {
|
||||||
clearEnv(t)
|
clearEnv(t)
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,21 @@
|
|||||||
-- Reverse de 000007. Réversible SEULEMENT sur base vide (les ressources sont
|
-- Reverse de 000007. Chemin TESTS uniquement : ne jamais down en production
|
||||||
-- détruites dans les deux sens : sans device associé, il n'y a rien à
|
-- avec des données.
|
||||||
-- préserver).
|
-- Les ressources retrouvent leur device d'origine via devices.user_id
|
||||||
DELETE FROM resources;
|
-- (premier device trouvé en cas de multi-device — mapping non bijectif
|
||||||
|
-- assumé) ; les orphelines (user_id NULL, ou user sans device, ou valeur ne
|
||||||
|
-- correspondant à aucun device) sont détruites car owner_id est NOT NULL
|
||||||
|
-- avec FK vers devices.
|
||||||
|
UPDATE resources r
|
||||||
|
SET user_id = d.device_id
|
||||||
|
FROM devices d
|
||||||
|
WHERE d.user_id = r.user_id;
|
||||||
|
|
||||||
|
DELETE FROM resources
|
||||||
|
WHERE user_id IS NULL
|
||||||
|
OR user_id NOT IN (SELECT device_id FROM devices);
|
||||||
|
|
||||||
ALTER TABLE resources DROP CONSTRAINT resources_user_id_fkey;
|
ALTER TABLE resources DROP CONSTRAINT resources_user_id_fkey;
|
||||||
ALTER TABLE resources RENAME COLUMN user_id TO owner_id;
|
ALTER TABLE resources RENAME COLUMN user_id TO owner_id;
|
||||||
|
ALTER TABLE resources ALTER COLUMN owner_id SET NOT NULL;
|
||||||
ALTER TABLE resources ADD CONSTRAINT resources_owner_id_fkey
|
ALTER TABLE resources ADD CONSTRAINT resources_owner_id_fkey
|
||||||
FOREIGN KEY (owner_id) REFERENCES devices(device_id) ON DELETE CASCADE;
|
FOREIGN KEY (owner_id) REFERENCES devices(device_id) ON DELETE CASCADE;
|
||||||
|
|||||||
@@ -1,15 +1,35 @@
|
|||||||
-- Bascule ownership device → user (étapes 6-10 de la tranche identité).
|
-- Bascule ownership device → user (étapes 6-10 de la tranche identité).
|
||||||
-- Destructif ASSUMÉ : à ce stade aucune release V1, la base de dev est RESET
|
-- Migration DOUCE et non destructive (cf. #10) : AUCUNE suppression de
|
||||||
-- (DELETE trivial sur une table vide ; PAS de user marqueur). Le modele
|
-- ressources (l'ancien DELETE FROM resources est supprimé).
|
||||||
-- resources reste parfaitement identique par ailleurs.
|
-- Le device d'origine est résolu vers son dernier utilisateur connu
|
||||||
DELETE FROM resources;
|
-- (devices.user_id) ; les ressources orphelines (device sans user connu ou
|
||||||
|
-- device inconnu) gardent user_id NULL — visibles par personne mais
|
||||||
|
-- conservées — et user_id reste donc NULLABLE après cette migration.
|
||||||
|
|
||||||
ALTER TABLE resources DROP CONSTRAINT resources_owner_id_fkey;
|
ALTER TABLE resources DROP CONSTRAINT resources_owner_id_fkey;
|
||||||
ALTER TABLE resources RENAME COLUMN owner_id TO user_id;
|
ALTER TABLE resources RENAME COLUMN owner_id TO user_id;
|
||||||
|
|
||||||
|
-- La colonne hérite du NOT NULL de owner_id : on l'assouplit AVANT le
|
||||||
|
-- backfill pour accueillir les orphelines.
|
||||||
|
ALTER TABLE resources ALTER COLUMN user_id DROP NOT NULL;
|
||||||
|
|
||||||
|
-- Backfill : device → dernier utilisateur connu.
|
||||||
|
UPDATE resources r
|
||||||
|
SET user_id = d.user_id
|
||||||
|
FROM devices d
|
||||||
|
WHERE r.user_id = d.device_id
|
||||||
|
AND d.user_id IS NOT NULL;
|
||||||
|
|
||||||
|
-- Orphelines : tout ce qui ne référence toujours pas un user existant → NULL.
|
||||||
|
UPDATE resources
|
||||||
|
SET user_id = NULL
|
||||||
|
WHERE user_id IS NOT NULL
|
||||||
|
AND user_id NOT IN (SELECT id FROM users);
|
||||||
|
|
||||||
ALTER TABLE resources ADD CONSTRAINT resources_user_id_fkey
|
ALTER TABLE resources ADD CONSTRAINT resources_user_id_fkey
|
||||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
|
||||||
|
|
||||||
-- Les index créés en 000003 (idx_resources_owner, idx_resources_root_name
|
-- Les index créés en 000003 (idx_resources_owner, idx_resources_root_name
|
||||||
-- sur owner_id) suivent le renommage Intrinsèquement : ils portent désormais
|
-- sur owner_id) suivent le renommage intrinsèquement : ils portent désormais
|
||||||
-- sur user_id. L'unicité racine devient donc « 1 nom par USER » (multi-device
|
-- sur user_id. L'unicité racine devient donc « 1 nom par USER » (multi-device
|
||||||
-- possible pour le même compte).
|
-- possible pour le même compte).
|
||||||
|
|||||||
@@ -1,4 +1,23 @@
|
|||||||
|
-- Down de 000008 : retour operation_id TEXT 32-hex → BIGINT.
|
||||||
|
-- PERTES ASSUMÉES, chemin TESTS uniquement (migrate down complet) : seules
|
||||||
|
-- les valeurs exclusivement numériques sur ≤ 18 chiffres significatifs
|
||||||
|
-- (traces héritées paddées par le up, p.ex. '0000…0042' → 42) survivent ;
|
||||||
|
-- les vrais 32-hex client-generated (contenant a-f, ou > 18 chiffres pour
|
||||||
|
-- éviter tout overflow bigint) ne sont pas convertibles et leurs lignes
|
||||||
|
-- sont supprimées. L'ancien `USING operation_id::bigint` échouait sur ces
|
||||||
|
-- valeurs. Ne jamais down en production avec des données V1 : recréer la
|
||||||
|
-- base plutôt que `migrate down`.
|
||||||
ALTER TABLE operations DROP CONSTRAINT operations_operation_id_hex_check;
|
ALTER TABLE operations DROP CONSTRAINT operations_operation_id_hex_check;
|
||||||
|
|
||||||
ALTER TABLE operations
|
ALTER TABLE operations ADD COLUMN operation_id_bigint BIGINT;
|
||||||
ALTER COLUMN operation_id TYPE BIGINT USING operation_id::bigint;
|
|
||||||
|
UPDATE operations
|
||||||
|
SET operation_id_bigint = operation_id::bigint
|
||||||
|
WHERE operation_id ~ '^0*[0-9]{1,18}$';
|
||||||
|
|
||||||
|
DELETE FROM operations WHERE operation_id_bigint IS NULL;
|
||||||
|
|
||||||
|
ALTER TABLE operations DROP COLUMN operation_id;
|
||||||
|
ALTER TABLE operations RENAME COLUMN operation_id_bigint TO operation_id;
|
||||||
|
ALTER TABLE operations ALTER COLUMN operation_id SET NOT NULL;
|
||||||
|
ALTER TABLE operations ADD CONSTRAINT operations_unique_per_device UNIQUE (device_id, operation_id);
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
DROP INDEX IF EXISTS idx_resources_owner_updated_active;
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
-- LOT 4 (DATA ownership) : index snapshot delta — ListOwned filtre
|
||||||
|
-- (user_id, updated_at) WHERE deleted_at IS NULL. Numéro réservé au LOT 4
|
||||||
|
-- par le plan TASKS_PARALLELES.md (LOT 5 = 000012, LOT 3 = 000013) : cette
|
||||||
|
-- migration bouche aussi le trou de version (golang-migrate refuse les gaps
|
||||||
|
-- au `Up` une fois 000012/000013 présentes).
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_resources_owner_updated_active
|
||||||
|
ON resources(user_id, updated_at) WHERE deleted_at IS NULL;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
DROP INDEX IF EXISTS idx_ocr_jobs_status_created;
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
-- Index composite pour la file OCR : ClaimNext filtre `status = 'queued'` et
|
||||||
|
-- trie par `created_at, job_id` (FIFO). L'index simple (status) de 000005
|
||||||
|
-- est conservé (LOT 4 reste propriétaire du schéma historique).
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_ocr_jobs_status_created ON ocr_jobs(status, created_at);
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
ALTER TABLE operations DROP CONSTRAINT IF EXISTS operations_payload_hash_hex_check;
|
||||||
|
ALTER TABLE operations DROP COLUMN IF EXISTS payload_hash;
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
-- Trace outbox : hash du payload (A9, LOT 3).
|
||||||
|
-- Au rejeu du même (device_id, operation_id) avec un payload différent, le
|
||||||
|
-- serveur rejette en INVALID_REQUEST au lieu d'un skip silencieux.
|
||||||
|
-- Colonne NULLABLE : les traces héritées (pré-000013) restent valides ; pour
|
||||||
|
-- elles, le hash est recalculé en Go depuis le payload stocké.
|
||||||
|
ALTER TABLE operations ADD COLUMN payload_hash TEXT;
|
||||||
|
|
||||||
|
ALTER TABLE operations
|
||||||
|
ADD CONSTRAINT operations_payload_hash_hex_check
|
||||||
|
CHECK (payload_hash IS NULL OR payload_hash ~ '^[0-9a-f]{64}$');
|
||||||
@@ -2,20 +2,21 @@ package db
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"os"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
_ "github.com/lib/pq"
|
"github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultTestDatabaseURL = "postgres://kazier:kazier@localhost:5432/kazier_migrations_test?sslmode=disable"
|
const defaultTestDatabaseURL = "postgres://kazier:kazier@localhost:5432/kazier_migrations_test?sslmode=disable"
|
||||||
|
|
||||||
func testDatabaseURL(t *testing.T) string {
|
func testDatabaseURL(t *testing.T) string {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
url := os.Getenv("TEST_DATABASE_URL")
|
url := TestDatabaseURL(defaultTestDatabaseURL)
|
||||||
if url == "" {
|
if err := ensureTestDatabaseExists(url); err != nil {
|
||||||
url = defaultTestDatabaseURL
|
t.Fatalf("ensure database: %v", err)
|
||||||
}
|
}
|
||||||
conn, err := sql.Open("postgres", url)
|
conn, err := sql.Open("postgres", url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -28,6 +29,41 @@ func testDatabaseURL(t *testing.T) string {
|
|||||||
return url
|
return url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ensureTestDatabaseExists crée la base de tests du package quand Postgres est
|
||||||
|
// joignable mais que la base n'est pas encore provisionnée (service CI frais,
|
||||||
|
// volume local vierge). Le probing de disponibilité et le skip restent le rôle
|
||||||
|
// du Ping de testDatabaseURL.
|
||||||
|
func ensureTestDatabaseExists(databaseURL string) error {
|
||||||
|
parsed, err := url.Parse(databaseURL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
dbName := strings.TrimPrefix(parsed.Path, "/")
|
||||||
|
if dbName == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
maintenance := *parsed
|
||||||
|
maintenance.Path = "/postgres"
|
||||||
|
conn, err := sql.Open("postgres", maintenance.String())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
if err := conn.Ping(); err != nil {
|
||||||
|
return nil // Postgres down : le Ping du test skippera
|
||||||
|
}
|
||||||
|
var exists bool
|
||||||
|
if err := conn.QueryRow(`SELECT EXISTS(SELECT 1 FROM pg_database WHERE datname = $1)`, dbName).Scan(&exists); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
if _, err := conn.Exec(fmt.Sprintf(`CREATE DATABASE %s`, pq.QuoteIdentifier(dbName))); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func resetSchema(t *testing.T, url string) {
|
func resetSchema(t *testing.T, url string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
conn, err := sql.Open("postgres", url)
|
conn, err := sql.Open("postgres", url)
|
||||||
@@ -179,6 +215,33 @@ func TestMigrationsUpDown(t *testing.T) {
|
|||||||
t.Error("FK resources.user_id → users(id) manquante après 000007")
|
t.Error("FK resources.user_id → users(id) manquante après 000007")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- 000007 : migration douce (#10) ----------------------------------
|
||||||
|
// user_id NULLABLE (orphelines conservées, pas de DELETE FROM resources).
|
||||||
|
var userIDNullable string
|
||||||
|
err = conn.QueryRow(`
|
||||||
|
SELECT is_nullable FROM information_schema.columns
|
||||||
|
WHERE table_schema = 'public' AND table_name = 'resources' AND column_name = 'user_id'`).Scan(&userIDNullable)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("resources.user_id nullabilité: %v", err)
|
||||||
|
}
|
||||||
|
if userIDNullable != "YES" {
|
||||||
|
t.Error("resources.user_id doit être NULLABLE après 000007 (migration douce, orphelines)")
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- 000011 : index snapshot delta (LOT 4) -----------------------------
|
||||||
|
var ownerUpdatedIdx int
|
||||||
|
err = conn.QueryRow(`
|
||||||
|
SELECT COUNT(*) FROM pg_index i
|
||||||
|
JOIN pg_class t ON t.oid = i.indrelid
|
||||||
|
WHERE t.relname = 'resources' AND i.indisvalid
|
||||||
|
AND pg_get_indexdef(i.indexrelid) LIKE '%idx_resources_owner_updated_active%'`).Scan(&ownerUpdatedIdx)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("000011 index: %v", err)
|
||||||
|
}
|
||||||
|
if ownerUpdatedIdx == 0 {
|
||||||
|
t.Error("index idx_resources_owner_updated_active manquant après 000011")
|
||||||
|
}
|
||||||
|
|
||||||
// operation_id outbox = UUID 32-hex client-generated — cf. docs/api-v1.md §6.1
|
// operation_id outbox = UUID 32-hex client-generated — cf. docs/api-v1.md §6.1
|
||||||
var opType string
|
var opType string
|
||||||
err = conn.QueryRow(`
|
err = conn.QueryRow(`
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestDatabaseURL resolve l'URL que chaque package de test doit utiliser pour
|
||||||
|
// joindre SA base dédiée.
|
||||||
|
//
|
||||||
|
// Chaque package possède une URL par défaut portant un nom de base distinct
|
||||||
|
// (kazier_repository_test, kazier_handlers_test, kazier_migrations_test…), de
|
||||||
|
// sorte que `go test ./...` puisse lancer les binaires de test en parallèle
|
||||||
|
// sans qu'ils ne s'écrasent le schéma. Quand TEST_DATABASE_URL est posée (CI),
|
||||||
|
// elle joue le rôle d'override d'endpoint : scheme, hôte, identifiants et
|
||||||
|
// paramètres de connexion gagnent, mais le nom de base du package est
|
||||||
|
// conservé. Sans variable d'env, l'URL par défaut du package est retournée
|
||||||
|
// telle quelle.
|
||||||
|
func TestDatabaseURL(packageURL string) string {
|
||||||
|
env := os.Getenv("TEST_DATABASE_URL")
|
||||||
|
if env == "" {
|
||||||
|
return packageURL
|
||||||
|
}
|
||||||
|
base, err := url.Parse(env)
|
||||||
|
if err != nil || base.Scheme == "" || base.Host == "" {
|
||||||
|
return packageURL
|
||||||
|
}
|
||||||
|
pkg, err := url.Parse(packageURL)
|
||||||
|
if err != nil || pkg.Path == "" || pkg.Path == "/" {
|
||||||
|
return env
|
||||||
|
}
|
||||||
|
merged := *base
|
||||||
|
merged.Path = pkg.Path
|
||||||
|
return merged.String()
|
||||||
|
}
|
||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -16,19 +15,16 @@ import (
|
|||||||
// OpenTestDatabase ensures the test database exists, resets its schema, runs
|
// OpenTestDatabase ensures the test database exists, resets its schema, runs
|
||||||
// all migrations, and returns a live connection (closed via t.Cleanup).
|
// all migrations, and returns a live connection (closed via t.Cleanup).
|
||||||
// Tests are skipped only when Postgres itself is unreachable. `databaseURL`
|
// Tests are skipped only when Postgres itself is unreachable. `databaseURL`
|
||||||
// empty falls back to TEST_DATABASE_URL, then to the global default.
|
// empty falls back to the global default. TEST_DATABASE_URL (CI) n'override
|
||||||
|
// que l'endpoint (hôte/creds) : le nom de base propre au package est
|
||||||
|
// préservé, pour que les packages parallèles ne partagent jamais un schéma.
|
||||||
func OpenTestDatabase(t *testing.T, databaseURL string) *sql.DB {
|
func OpenTestDatabase(t *testing.T, databaseURL string) *sql.DB {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
switch {
|
|
||||||
case databaseURL == "":
|
|
||||||
databaseURL = os.Getenv("TEST_DATABASE_URL")
|
|
||||||
case os.Getenv("TEST_DATABASE_URL") != "":
|
|
||||||
databaseURL = os.Getenv("TEST_DATABASE_URL")
|
|
||||||
}
|
|
||||||
if databaseURL == "" {
|
if databaseURL == "" {
|
||||||
databaseURL = "postgres://kazier:kazier@localhost:5432/kazier_test?sslmode=disable"
|
databaseURL = "postgres://kazier:kazier@localhost:5432/kazier_test?sslmode=disable"
|
||||||
}
|
}
|
||||||
|
databaseURL = db.TestDatabaseURL(databaseURL)
|
||||||
|
|
||||||
// Postgres joignable ? (sinon on skippe, sans être gênés par l'existence
|
// Postgres joignable ? (sinon on skippe, sans être gênés par l'existence
|
||||||
// ou non de la base cible)
|
// ou non de la base cible)
|
||||||
|
|||||||
@@ -4,12 +4,12 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
"github.com/kazier/backend/pkg/api"
|
"github.com/kazier/backend/pkg/api"
|
||||||
"github.com/kazier/backend/pkg/auth"
|
|
||||||
"github.com/kazier/backend/pkg/passwd"
|
"github.com/kazier/backend/pkg/passwd"
|
||||||
"github.com/kazier/backend/repository"
|
"github.com/kazier/backend/repository"
|
||||||
"github.com/kazier/backend/service"
|
"github.com/kazier/backend/service"
|
||||||
@@ -33,9 +33,55 @@ type LoginResponse struct {
|
|||||||
User LoginUserDTO `json:"user"`
|
User LoginUserDTO `json:"user"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Anti-bruteforce du login (FEEDBACK #12) : limiteur basique en mémoire,
|
||||||
|
// N tentatives / minute / IP sur POST /auth/login, placé AVANT la vérification
|
||||||
|
// argon2 (coûteuse) pour protéger le CPU. Au-delà → 429 RATE_LIMITED.
|
||||||
|
// TODO(suivi) : rate-limit distribué (ex. Redis) + métriques pour les déploiements
|
||||||
|
// multi-réplicas, et budget global coordonné avec le rate-limit POST /devices
|
||||||
|
// (LOT 6) contre le remplissage de table.
|
||||||
|
var (
|
||||||
|
loginRateLimitPerMinute = 60
|
||||||
|
loginRateWindow = time.Minute
|
||||||
|
loginAttemptsMu sync.Mutex
|
||||||
|
loginAttempts = map[string][]time.Time{}
|
||||||
|
)
|
||||||
|
|
||||||
|
// OnLoginRateLimited est un hook d'observabilité appelé quand le limiteur
|
||||||
|
// rejette une tentative (nil par défaut = no-op). Les tests peuvent l'utiliser
|
||||||
|
// pour asserter le déclenchement sans dépendre du timing.
|
||||||
|
var OnLoginRateLimited func(ip string)
|
||||||
|
|
||||||
|
// ResetLoginRateLimit vide le compteur (tests uniquement).
|
||||||
|
func ResetLoginRateLimit() {
|
||||||
|
loginAttemptsMu.Lock()
|
||||||
|
defer loginAttemptsMu.Unlock()
|
||||||
|
loginAttempts = map[string][]time.Time{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// loginRateLimited enregistre une tentative et retourne true si le quota
|
||||||
|
// IP est dépassé (fenêtre glissante).
|
||||||
|
func loginRateLimited(ip string, now time.Time) bool {
|
||||||
|
loginAttemptsMu.Lock()
|
||||||
|
defer loginAttemptsMu.Unlock()
|
||||||
|
cutoff := now.Add(-loginRateWindow)
|
||||||
|
kept := make([]time.Time, 0, len(loginAttempts[ip])+1)
|
||||||
|
for _, t := range loginAttempts[ip] {
|
||||||
|
if t.After(cutoff) {
|
||||||
|
kept = append(kept, t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(kept) >= loginRateLimitPerMinute {
|
||||||
|
loginAttempts[ip] = kept
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
loginAttempts[ip] = append(kept, now)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// AuthLogin est la SEULE porte d'émission de token (V1). L'erreur est
|
// AuthLogin est la SEULE porte d'émission de token (V1). L'erreur est
|
||||||
// volontairement indistinguable entre « username inconnu » et « mauvais mot de
|
// volontairement indistinguable entre « username inconnu », « mauvais mot de
|
||||||
// passe » — même code, même message, délai égalisé via passwd.VerifyTimedEqual.
|
// passe » et « device non enregistré » — même code 401, même message, délai
|
||||||
|
// égalisé via passwd.VerifyTimedEqual (anti-oracle, FEEDBACK #12).
|
||||||
func AuthLogin(c *gin.Context) {
|
func AuthLogin(c *gin.Context) {
|
||||||
if Store == nil || Store.Repository == nil || Auth == nil {
|
if Store == nil || Store.Repository == nil || Auth == nil {
|
||||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||||
@@ -51,12 +97,24 @@ func AuthLogin(c *gin.Context) {
|
|||||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "username, password and device_id are required")
|
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "username, password and device_id are required")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if !deviceIDPattern.MatchString(req.DeviceID) {
|
||||||
|
api.Error(c, http.StatusBadRequest, "INVALID_DEVICE_ID", "device_id must be 32 lowercase hex chars")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if loginRateLimited(c.ClientIP(), time.Now()) {
|
||||||
|
if OnLoginRateLimited != nil {
|
||||||
|
OnLoginRateLimited(c.ClientIP())
|
||||||
|
}
|
||||||
|
api.Error(c, http.StatusTooManyRequests, "RATE_LIMITED", "too many login attempts, retry later")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
normalized := service.NormalizeUsername(req.Username)
|
normalized := service.NormalizeUsername(req.Username)
|
||||||
user, userErr := Store.Repository.Users.GetByUsernameNormalized(normalized)
|
user, userErr := Store.Repository.Users.GetByUsernameNormalized(normalized)
|
||||||
|
|
||||||
// Même durée de traitement que le compte existe ou non (username inconnu =>
|
// Même durée de traitement que le compte existe ou non (username inconnu =>
|
||||||
// stored vide → vérification contre dummyHash).
|
// stored vide → vérification contre dummyHash aléatoire par boot).
|
||||||
stored := ""
|
stored := ""
|
||||||
if userErr == nil && user != nil {
|
if userErr == nil && user != nil {
|
||||||
stored = user.PasswordHash
|
stored = user.PasswordHash
|
||||||
@@ -69,6 +127,12 @@ func AuthLogin(c *gin.Context) {
|
|||||||
api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not verify credentials")
|
api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not verify credentials")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// A1 : ne jamais déréférencer user si inconnu — même si Verify (par
|
||||||
|
// extraordinaire) réussissait, on reste en 401.
|
||||||
|
if userErr != nil || user == nil {
|
||||||
|
api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "invalid credentials")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
exists, err := Store.Repository.Devices.Exists(req.DeviceID)
|
exists, err := Store.Repository.Devices.Exists(req.DeviceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -76,24 +140,31 @@ func AuthLogin(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !exists {
|
if !exists {
|
||||||
api.Error(c, http.StatusBadRequest, "INVALID_DEVICE_ID", "device not registered")
|
// Volontairement indistinguable d'un mauvais password (anti-oracle).
|
||||||
|
api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "invalid credentials")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := Store.Repository.Devices.MarkUser(req.DeviceID, user.ID); err != nil {
|
if err := Store.Repository.Devices.MarkUser(req.DeviceID, user.ID); err != nil {
|
||||||
|
if errors.Is(err, repository.ErrNotFound) {
|
||||||
|
// Course : device supprimé entre Exists et MarkUser → même 401
|
||||||
|
// indistinguable, jamais de token pour un device fantôme.
|
||||||
|
api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "invalid credentials")
|
||||||
|
return
|
||||||
|
}
|
||||||
api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not update device")
|
api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not update device")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := Auth.Issue(user.ID, req.DeviceID)
|
token, expiresAt, err := Auth.IssueWithExpiry(user.ID, req.DeviceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.Error(c, http.StatusInternalServerError, "TOKEN_ERROR", "could not issue token")
|
api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not issue token")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
api.OK(c, LoginResponse{
|
api.OK(c, LoginResponse{
|
||||||
Token: token,
|
Token: token,
|
||||||
ExpiresAt: time.Now().Add(auth.TokenTTL).UnixMilli(),
|
ExpiresAt: expiresAt.UnixMilli(),
|
||||||
User: LoginUserDTO{
|
User: LoginUserDTO{
|
||||||
ID: user.ID,
|
ID: user.ID,
|
||||||
Username: user.Username,
|
Username: user.Username,
|
||||||
|
|||||||
@@ -42,6 +42,8 @@ func setTestStore(t *testing.T) *repository.Repository {
|
|||||||
func setTestAuth(t *testing.T) {
|
func setTestAuth(t *testing.T) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
Auth, _ = auth.NewManager("test-secret")
|
Auth, _ = auth.NewManager("test-secret")
|
||||||
|
ResetLoginRateLimit()
|
||||||
|
ResetDevicesRateLimit()
|
||||||
t.Cleanup(func() { Auth = nil })
|
t.Cleanup(func() { Auth = nil })
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,6 +204,71 @@ func TestAuthLoginWrongCredentials(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestAuthLoginUnknownUserRegisteredDevice — régression A1 : un username
|
||||||
|
// inconnu ne doit JAMAIS déréférencer user (panic → 500 via Gin recovery).
|
||||||
|
// Le device est enregistré pour isoler la branche « user inconnu » : on attend
|
||||||
|
// 401 UNAUTHORIZED, corps identique au cas « mauvais mot de passe ».
|
||||||
|
func TestAuthLoginUnknownUserRegisteredDevice(t *testing.T) {
|
||||||
|
setTestAuth(t)
|
||||||
|
repo := setTestStore(t)
|
||||||
|
createAdmin(t, repo, "admin", authTestPassword)
|
||||||
|
r := newTestRouterForAuth()
|
||||||
|
|
||||||
|
doAuth(t, r, http.MethodPost, "/api/v1/devices", "", `{"deviceId":"`+authTestDevice+`"}`)
|
||||||
|
|
||||||
|
badPassword := doAuth(t, r, http.MethodPost, "/api/v1/auth/login", "", `{"username":"admin","password":"wrong-password","device_id":"`+authTestDevice+`"}`)
|
||||||
|
if badPassword.Code != 401 {
|
||||||
|
t.Fatalf("mauvais mot de passe: status %d body %s", badPassword.Code, badPassword.Body.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
ghost := doAuth(t, r, http.MethodPost, "/api/v1/auth/login", "", `{"username":"ghost","password":"wrong-password","device_id":"`+authTestDevice+`"}`)
|
||||||
|
if ghost.Code != 401 {
|
||||||
|
t.Fatalf("username inconnu: status %d body %s (attendu 401, jamais 500)", ghost.Code, ghost.Body.String())
|
||||||
|
}
|
||||||
|
if !strings.Contains(ghost.Body.String(), "UNAUTHORIZED") {
|
||||||
|
t.Errorf("code attendu UNAUTHORIZED, got %s", ghost.Body.String())
|
||||||
|
}
|
||||||
|
if ghost.Body.String() != badPassword.Body.String() {
|
||||||
|
t.Errorf("réponses distinguables (sécurité):\n%q\n%q", ghost.Body.String(), badPassword.Body.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAuthLoginRateLimited(t *testing.T) {
|
||||||
|
setTestAuth(t)
|
||||||
|
repo := setTestStore(t)
|
||||||
|
createAdmin(t, repo, "admin", authTestPassword)
|
||||||
|
r := newTestRouterForAuth()
|
||||||
|
doAuth(t, r, http.MethodPost, "/api/v1/devices", "", `{"deviceId":"`+authTestDevice+`"}`)
|
||||||
|
|
||||||
|
oldLimit := loginRateLimitPerMinute
|
||||||
|
loginRateLimitPerMinute = 2
|
||||||
|
hookCalls := 0
|
||||||
|
oldHook := OnLoginRateLimited
|
||||||
|
OnLoginRateLimited = func(ip string) { hookCalls++ }
|
||||||
|
t.Cleanup(func() {
|
||||||
|
loginRateLimitPerMinute = oldLimit
|
||||||
|
OnLoginRateLimited = oldHook
|
||||||
|
ResetLoginRateLimit()
|
||||||
|
})
|
||||||
|
|
||||||
|
badBody := `{"username":"admin","password":"wrong-password","device_id":"` + authTestDevice + `"}`
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
if rec := doAuth(t, r, http.MethodPost, "/api/v1/auth/login", "", badBody); rec.Code != 401 {
|
||||||
|
t.Fatalf("tentative %d: status %d, attendu 401", i+1, rec.Code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rec := doAuth(t, r, http.MethodPost, "/api/v1/auth/login", "", badBody)
|
||||||
|
if rec.Code != 429 {
|
||||||
|
t.Fatalf("quota dépassé: status %d body %s, attendu 429", rec.Code, rec.Body.String())
|
||||||
|
}
|
||||||
|
if !strings.Contains(rec.Body.String(), "RATE_LIMITED") {
|
||||||
|
t.Errorf("code attendu RATE_LIMITED, got %s", rec.Body.String())
|
||||||
|
}
|
||||||
|
if hookCalls != 1 {
|
||||||
|
t.Errorf("OnLoginRateLimited appelé %d fois, attendu 1", hookCalls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestAuthLoginUnregisteredDevice(t *testing.T) {
|
func TestAuthLoginUnregisteredDevice(t *testing.T) {
|
||||||
setTestAuth(t)
|
setTestAuth(t)
|
||||||
repo := setTestStore(t)
|
repo := setTestStore(t)
|
||||||
@@ -209,11 +276,11 @@ func TestAuthLoginUnregisteredDevice(t *testing.T) {
|
|||||||
r := newTestRouterForAuth()
|
r := newTestRouterForAuth()
|
||||||
|
|
||||||
rec := doAuth(t, r, http.MethodPost, "/api/v1/auth/login", "", `{"username":"admin","password":"`+authTestPassword+`","device_id":"`+authTestDevice+`"}`)
|
rec := doAuth(t, r, http.MethodPost, "/api/v1/auth/login", "", `{"username":"admin","password":"`+authTestPassword+`","device_id":"`+authTestDevice+`"}`)
|
||||||
if rec.Code != 400 {
|
if rec.Code != 401 {
|
||||||
t.Fatalf("device non enregistré: status %d body %s", rec.Code, rec.Body.String())
|
t.Fatalf("device non enregistré: status %d body %s", rec.Code, rec.Body.String())
|
||||||
}
|
}
|
||||||
if !strings.Contains(rec.Body.String(), "INVALID_DEVICE_ID") {
|
if !strings.Contains(rec.Body.String(), "UNAUTHORIZED") {
|
||||||
t.Errorf("code attendu INVALID_DEVICE_ID, got %s", rec.Body.String())
|
t.Errorf("code attendu UNAUTHORIZED (anti-oracle), got %s", rec.Body.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/kazier/backend/pkg/api"
|
"github.com/kazier/backend/pkg/api"
|
||||||
@@ -9,12 +12,65 @@ import (
|
|||||||
|
|
||||||
var deviceIDPattern = regexp.MustCompile(`^[0-9a-f]{32}$`)
|
var deviceIDPattern = regexp.MustCompile(`^[0-9a-f]{32}$`)
|
||||||
|
|
||||||
|
// Anti-remplissage de POST /devices (FEEDBACK §moyenne) : l'endpoint est
|
||||||
|
// public et bon marché (un upsert), donc anonyme et floodable. Limiteur
|
||||||
|
// basique en mémoire, N enregistrements / minute / IP → 429 RATE_LIMITED.
|
||||||
|
// Budget large (300) : les usages légitimes (1 device = 1 appel) sont très
|
||||||
|
// loin du seuil ; la protection vise le flood massif, un WAF frontal fera
|
||||||
|
// le reste en prod multi-réplicas.
|
||||||
|
// TODO(suivi) : rate-limit distribué + budget global coordonné avec le
|
||||||
|
// rate-limit login (cf. handlers/auth.go).
|
||||||
|
var (
|
||||||
|
devicesRateLimitPerMinute = 300
|
||||||
|
devicesRateWindow = time.Minute
|
||||||
|
devicesAttemptsMu sync.Mutex
|
||||||
|
devicesAttempts = map[string][]time.Time{}
|
||||||
|
)
|
||||||
|
|
||||||
|
// OnDevicesRateLimited est un hook d'observabilité (nil = no-op).
|
||||||
|
var OnDevicesRateLimited func(ip string)
|
||||||
|
|
||||||
|
// ResetDevicesRateLimit vide le compteur (tests uniquement).
|
||||||
|
func ResetDevicesRateLimit() {
|
||||||
|
devicesAttemptsMu.Lock()
|
||||||
|
defer devicesAttemptsMu.Unlock()
|
||||||
|
devicesAttempts = map[string][]time.Time{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func devicesRateLimited(ip string, now time.Time) bool {
|
||||||
|
devicesAttemptsMu.Lock()
|
||||||
|
defer devicesAttemptsMu.Unlock()
|
||||||
|
cutoff := now.Add(-devicesRateWindow)
|
||||||
|
kept := make([]time.Time, 0, len(devicesAttempts[ip])+1)
|
||||||
|
for _, t := range devicesAttempts[ip] {
|
||||||
|
if t.After(cutoff) {
|
||||||
|
kept = append(kept, t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(kept) >= devicesRateLimitPerMinute {
|
||||||
|
devicesAttempts[ip] = kept
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
devicesAttempts[ip] = append(kept, now)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
type DeviceRegisterRequest struct {
|
type DeviceRegisterRequest struct {
|
||||||
DeviceID string `json:"deviceId"`
|
DeviceID string `json:"deviceId"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func DevicesRegister(c *gin.Context) {
|
func DevicesRegister(c *gin.Context) {
|
||||||
|
// POST /devices est PUBLIC (sans token) : le device s'enregistre pour
|
||||||
|
// exister, puis le client appelle POST /auth/login pour obtenir son
|
||||||
|
// token user-first (cf. docs/api-v1.md §2).
|
||||||
|
//
|
||||||
|
// NOTE rate-limit : limiteur en mémoire ci-dessus (300/min/IP, 429
|
||||||
|
// au-delà). Si le budget doit changer, ajuster
|
||||||
|
// devicesRateLimitPerMinute + documenter dans docs/api-v1.md.
|
||||||
|
//
|
||||||
|
// NOTE MarkUser : repository.Devices.MarkUser retourne ErrNotFound quand
|
||||||
|
// RowsAffected == 0 (device supprimé entre Exists et UPDATE) ; le login
|
||||||
|
// mappe ce cas en 401 indistinguable (cf. handlers/auth.go).
|
||||||
var req DeviceRegisterRequest
|
var req DeviceRegisterRequest
|
||||||
if err := c.ShouldBindJSON(&req); err != nil || req.DeviceID == "" {
|
if err := c.ShouldBindJSON(&req); err != nil || req.DeviceID == "" {
|
||||||
api.Error(c, 400, "INVALID_REQUEST", "missing deviceId")
|
api.Error(c, 400, "INVALID_REQUEST", "missing deviceId")
|
||||||
@@ -25,6 +81,14 @@ func DevicesRegister(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if devicesRateLimited(c.ClientIP(), time.Now()) {
|
||||||
|
if OnDevicesRateLimited != nil {
|
||||||
|
OnDevicesRateLimited(c.ClientIP())
|
||||||
|
}
|
||||||
|
api.Error(c, http.StatusTooManyRequests, "RATE_LIMITED", "too many device registrations, retry later")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if Store == nil || Store.Repository == nil {
|
if Store == nil || Store.Repository == nil {
|
||||||
api.Error(c, 503, "SERVICE_UNAVAILABLE", "backend not initialized")
|
api.Error(c, 503, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||||
return
|
return
|
||||||
|
|||||||
+54
-16
@@ -8,17 +8,38 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/kazier/backend/pkg/api"
|
"github.com/kazier/backend/pkg/api"
|
||||||
|
"github.com/kazier/backend/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
func intParam(value string, fallback int) int {
|
// parsePageParams valide page/pageSize (400 si garbage, cf. FEEDBACK A7/A16)
|
||||||
if value == "" {
|
// au lieu du fallback silencieux. Cap pageSize 200 et offset 100000.
|
||||||
return fallback
|
func parsePageParams(c *gin.Context) (page, pageSize int, ok bool) {
|
||||||
|
page = 1
|
||||||
|
pageSize = 50
|
||||||
|
if v := c.Query("page"); v != "" {
|
||||||
|
p, err := strconv.Atoi(v)
|
||||||
|
if err != nil || p < 1 {
|
||||||
|
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid query param `page`")
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
page = p
|
||||||
}
|
}
|
||||||
parsed, err := strconv.Atoi(value)
|
if v := c.Query("pageSize"); v != "" {
|
||||||
if err != nil || parsed < 1 {
|
ps, err := strconv.Atoi(v)
|
||||||
return fallback
|
if err != nil || ps < 1 {
|
||||||
|
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid query param `pageSize`")
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
pageSize = ps
|
||||||
}
|
}
|
||||||
return parsed
|
if pageSize > 200 {
|
||||||
|
pageSize = 200
|
||||||
|
}
|
||||||
|
if (page-1)*pageSize > 100000 {
|
||||||
|
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "page offset too large")
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
return page, pageSize, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func FilesList(c *gin.Context) {
|
func FilesList(c *gin.Context) {
|
||||||
@@ -27,12 +48,16 @@ func FilesList(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
userID := c.GetString(UserIDKey)
|
userID := c.GetString(UserIDKey)
|
||||||
page := intParam(c.Query("page"), 1)
|
page, pageSize, ok := parsePageParams(c)
|
||||||
pageSize := intParam(c.Query("pageSize"), 50)
|
if !ok {
|
||||||
if pageSize > 200 {
|
return
|
||||||
pageSize = 200
|
|
||||||
}
|
}
|
||||||
files, total, err := Store.ListFiles(userID, c.Query("folderId"), page, pageSize, c.Query("sort"), c.Query("order"))
|
folderID := c.Query("folderId")
|
||||||
|
if folderID != "" && !deviceIDPattern.MatchString(folderID) {
|
||||||
|
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid query param `folderId`")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
files, total, err := Store.ListFiles(userID, folderID, page, pageSize, c.Query("sort"), c.Query("order"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(c, err)
|
writeError(c, err)
|
||||||
return
|
return
|
||||||
@@ -108,6 +133,10 @@ func FilesUpload(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
userID := c.GetString(UserIDKey)
|
userID := c.GetString(UserIDKey)
|
||||||
folderID := c.PostForm("folderId")
|
folderID := c.PostForm("folderId")
|
||||||
|
if folderID != "" && !deviceIDPattern.MatchString(folderID) {
|
||||||
|
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid `folderId`")
|
||||||
|
return
|
||||||
|
}
|
||||||
// resourceId optionnel : cible un fichier déjà connu (metadata-only de
|
// resourceId optionnel : cible un fichier déjà connu (metadata-only de
|
||||||
// l'outbox) pour lui attacher ses octets physiques (ex. OCR serveur).
|
// l'outbox) pour lui attacher ses octets physiques (ex. OCR serveur).
|
||||||
resourceID := c.PostForm("resourceId")
|
resourceID := c.PostForm("resourceId")
|
||||||
@@ -134,6 +163,12 @@ func FilesUpload(c *gin.Context) {
|
|||||||
|
|
||||||
dto, err := Store.Upload(userID, file, folderID, resourceID)
|
dto, err := Store.Upload(userID, file, folderID, resourceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// Nom/extension rejetés côté service (TrimSpace, cap 255,
|
||||||
|
// anti-slash/retour-ligne, cf. FEEDBACK A16) → 400, jamais 500.
|
||||||
|
if errors.Is(err, service.ErrInvalidUpload) {
|
||||||
|
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid file name")
|
||||||
|
return
|
||||||
|
}
|
||||||
writeError(c, err)
|
writeError(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -165,10 +200,13 @@ func FilesSearch(c *gin.Context) {
|
|||||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "missing required query param `q`")
|
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "missing required query param `q`")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
page := intParam(c.Query("page"), 1)
|
if len(q) > 200 {
|
||||||
pageSize := intParam(c.Query("pageSize"), 50)
|
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "query param `q` too long (max 200)")
|
||||||
if pageSize > 200 {
|
return
|
||||||
pageSize = 200
|
}
|
||||||
|
page, pageSize, ok := parsePageParams(c)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
files, total, err := Store.SearchFiles(userID, q, page, pageSize)
|
files, total, err := Store.SearchFiles(userID, q, page, pageSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -73,6 +73,7 @@ func setup(t *testing.T) (*gin.Engine, *service.Resources, *repository.Repositor
|
|||||||
}
|
}
|
||||||
handlers.Auth = manager
|
handlers.Auth = manager
|
||||||
handlers.Store = store
|
handlers.Store = store
|
||||||
|
handlers.ResetDevicesRateLimit()
|
||||||
|
|
||||||
gin.SetMode(gin.TestMode)
|
gin.SetMode(gin.TestMode)
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
@@ -517,12 +518,12 @@ func TestFilesPaginationDefaultsAndClamp(t *testing.T) {
|
|||||||
t.Errorf("clamp pageSize: %+v", env.Meta)
|
t.Errorf("clamp pageSize: %+v", env.Meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Paramètres invalides → défauts (page=1, pageSize=50)
|
// Paramètres invalides → 400 (plus de fallback silencieux, cf. A16).
|
||||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files?page=0&pageSize=-5", token, nil, "")
|
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files?page=0&pageSize=-5", token, nil, "")
|
||||||
env = expectOK(t, rec, "invalid-params")
|
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "negative-params")
|
||||||
if env.Meta == nil || env.Meta.Page != 1 || env.Meta.PageSize != 50 {
|
|
||||||
t.Errorf("defauts page/pageSize: %+v", env.Meta)
|
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files?page=abc&pageSize=zz", token, nil, "")
|
||||||
}
|
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "garbage-params")
|
||||||
|
|
||||||
// sort/order inconnus → pas d'erreur (défaut created_at desc)
|
// sort/order inconnus → pas d'erreur (défaut created_at desc)
|
||||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files?sort=zzz&order=up&pageSize=10", token, nil, "")
|
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files?sort=zzz&order=up&pageSize=10", token, nil, "")
|
||||||
@@ -633,3 +634,55 @@ func TestFilesGetOcrOwnerShareAndScoping(t *testing.T) {
|
|||||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/not-hex/ocr", ownerToken, nil, "")
|
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/not-hex/ocr", ownerToken, nil, "")
|
||||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "ocr-invalid-id")
|
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "ocr-invalid-id")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFilesUploadRejectsBadFilename(t *testing.T) {
|
||||||
|
r, _, repo := setup(t)
|
||||||
|
device := repository.NewID()
|
||||||
|
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "bn"), "badname-test-password", device)
|
||||||
|
|
||||||
|
// Body multipart construit à la main : filename brut, NON neutralisé par
|
||||||
|
// le writer stdlib (qui basename/encode de son côté). Le serveur doit
|
||||||
|
// rejeter en 400, jamais 500 (cf. A16).
|
||||||
|
rawUpload := func(filename string) *httptest.ResponseRecorder {
|
||||||
|
t.Helper()
|
||||||
|
boundary := "testboundary123"
|
||||||
|
body := "--" + boundary + "\r\n" +
|
||||||
|
"Content-Disposition: form-data; name=\"file\"; filename=\"" + filename + "\"\r\n" +
|
||||||
|
"Content-Type: application/octet-stream\r\n\r\n" +
|
||||||
|
"hi\r\n" +
|
||||||
|
"--" + boundary + "--\r\n"
|
||||||
|
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/files/upload", token, []byte(body), "multipart/form-data; boundary="+boundary)
|
||||||
|
return rec
|
||||||
|
}
|
||||||
|
|
||||||
|
rec := rawUpload("../../evil.txt")
|
||||||
|
// La stdlib Go basename déjà côté lecture (défense en profondeur côté
|
||||||
|
// service via sanitizeFileName, testé en unitaire + backslash) : le nom
|
||||||
|
// stocké est "evil.txt" et aucun chemin ne s'échappe (destPath dérivé
|
||||||
|
// uniquement de IDs 32-hex + extension sanitizée).
|
||||||
|
env := expectOK(t, rec, "upload-traversal-name")
|
||||||
|
var uploaded fileDTO
|
||||||
|
if err := json.Unmarshal(env.Data, &uploaded); err != nil {
|
||||||
|
t.Fatalf("upload-traversal-name: unmarshal: %v", err)
|
||||||
|
}
|
||||||
|
if uploaded.Name != "evil.txt" {
|
||||||
|
t.Errorf("nom traversal neutralisé attendu \"evil.txt\", got %q", uploaded.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Backslash : préservé par la stdlib → rejet service 400, jamais 500.
|
||||||
|
rec = rawUpload(`bad\name.txt`)
|
||||||
|
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "upload-backslash-name")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSearchQTooLong(t *testing.T) {
|
||||||
|
r, _, repo := setup(t)
|
||||||
|
device := repository.NewID()
|
||||||
|
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "ql"), "search-test-password", device)
|
||||||
|
|
||||||
|
long := make([]byte, 201)
|
||||||
|
for i := range long {
|
||||||
|
long[i] = 'a'
|
||||||
|
}
|
||||||
|
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/search?q="+string(long), token, nil, "")
|
||||||
|
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "search-q-too-long")
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,126 @@
|
|||||||
|
package handlers_test
|
||||||
|
|
||||||
|
// Tests contrat LOT 6 : ordre statique/param (§1), validation 32-hex et
|
||||||
|
// curseur `after` stricts (A7), validation POST /devices.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
"github.com/kazier/backend/handlers"
|
||||||
|
"github.com/kazier/backend/repository"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestFoldersRouteServesListNotParam (§1) : GET /files/folders via le routeur
|
||||||
|
// atteint FoldersList (200), jamais FilesGet avec id="folders".
|
||||||
|
func TestFoldersRouteServesListNotParam(t *testing.T) {
|
||||||
|
r, _, repo := setup(t)
|
||||||
|
device := repository.NewID()
|
||||||
|
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "l6f"), "lot6-test-password", device)
|
||||||
|
|
||||||
|
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/folders", token, nil, "")
|
||||||
|
expectOK(t, rec, "folders-static-wins")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestFilesGetRejectsFoldersLiteral (§1) : si le routeur tombait un jour sur
|
||||||
|
// FilesGet avec id="folders", le garde 32-hex répond 404 (jamais 200, jamais
|
||||||
|
// 500). Appel direct du handler pour forcer le cas paramétré.
|
||||||
|
func TestFilesGetRejectsFoldersLiteral(t *testing.T) {
|
||||||
|
setup(t)
|
||||||
|
|
||||||
|
gin.SetMode(gin.TestMode)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := gin.CreateTestContext(w)
|
||||||
|
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/files/folders", nil)
|
||||||
|
c.Params = gin.Params{{Key: "id", Value: "folders"}}
|
||||||
|
c.Set(handlers.UserIDKey, "00000000000000000000000000000001")
|
||||||
|
|
||||||
|
handlers.FilesGet(c)
|
||||||
|
|
||||||
|
expectError(t, w, http.StatusNotFound, "NOT_FOUND", "files-get-folders-literal")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestShareLinkMalformedToken400 (A7) : token non 32-hex → 400
|
||||||
|
// INVALID_REQUEST (pas 404).
|
||||||
|
func TestShareLinkMalformedToken400(t *testing.T) {
|
||||||
|
r, _, _ := setup(t)
|
||||||
|
for _, path := range []string{
|
||||||
|
"/api/v1/shares/links/NOT-HEX",
|
||||||
|
"/api/v1/shares/links/xyz",
|
||||||
|
"/api/v1/shares/links/ABCDEF0123456789ABCDEF0123456789", // majuscules rejetées
|
||||||
|
} {
|
||||||
|
rec, _ := doRequest(t, r, http.MethodGet, path, "", nil, "")
|
||||||
|
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "share-malformed-token")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSyncPermissionsAfterValidation (A7) : garbage/négatif → 400, epoch-ms
|
||||||
|
// 64-bit accepté.
|
||||||
|
func TestSyncPermissionsAfterValidation(t *testing.T) {
|
||||||
|
r, _, repo := setup(t)
|
||||||
|
device := repository.NewID()
|
||||||
|
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "l6a"), "lot6-test-password", device)
|
||||||
|
|
||||||
|
for _, path := range []string{
|
||||||
|
"/api/v1/sync/permissions?after=garbage",
|
||||||
|
"/api/v1/sync/permissions?after=-5",
|
||||||
|
"/api/v1/sync/permissions?after=12.5",
|
||||||
|
"/api/v1/sync/permissions?after=",
|
||||||
|
} {
|
||||||
|
// "?after=" vide = curseur absent → 200 (seul le dernier cas).
|
||||||
|
rec, _ := doRequest(t, r, http.MethodGet, path, token, nil, "")
|
||||||
|
if path == "/api/v1/sync/permissions?after=" {
|
||||||
|
expectOK(t, rec, "after-empty-ok")
|
||||||
|
} else {
|
||||||
|
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "after-invalid:"+path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Epoch-ms au-delà de 2^31 (64-bit) → 200, pas de troncation/rejet.
|
||||||
|
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/sync/permissions?after=9999999999999", token, nil, "")
|
||||||
|
expectOK(t, rec, "after-64bit-ok")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestDevicesRegisterValidation : 32-hex malformé/absent → 400, valide → 200.
|
||||||
|
func TestDevicesRegisterValidation(t *testing.T) {
|
||||||
|
r, _, _ := setup(t)
|
||||||
|
|
||||||
|
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{"deviceId":"NOT-HEX"}`), "application/json")
|
||||||
|
expectError(t, rec, http.StatusBadRequest, "INVALID_DEVICE_ID", "devices-malformed")
|
||||||
|
|
||||||
|
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{}`), "application/json")
|
||||||
|
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "devices-missing")
|
||||||
|
|
||||||
|
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{"deviceId":`+`"`+repository.NewID()+`"}`), "application/json")
|
||||||
|
expectOK(t, rec, "devices-valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestDevicesRegisterRateLimited : flood → 429 RATE_LIMITED (budget 300/min/IP,
|
||||||
|
// cf. handlers/devices.go). Les requêtes invalides ne consomment pas le budget.
|
||||||
|
func TestDevicesRegisterRateLimited(t *testing.T) {
|
||||||
|
r, _, _ := setup(t)
|
||||||
|
|
||||||
|
hookCalls := 0
|
||||||
|
handlers.OnDevicesRateLimited = func(ip string) { hookCalls++ }
|
||||||
|
t.Cleanup(func() { handlers.OnDevicesRateLimited = nil })
|
||||||
|
|
||||||
|
// Les requêtes invalides (avant le compteur) ne doivent pas déclencher.
|
||||||
|
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{"deviceId":"BAD"}`), "application/json")
|
||||||
|
expectError(t, rec, http.StatusBadRequest, "INVALID_DEVICE_ID", "devices-ratelimit-invalid")
|
||||||
|
|
||||||
|
for i := 0; i < 300; i++ {
|
||||||
|
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{"deviceId":`+`"`+repository.NewID()+`"}`), "application/json")
|
||||||
|
if rec.Code != http.StatusOK {
|
||||||
|
t.Fatalf("enregistrement %d: attendu 200, got %d body=%s", i, rec.Code, rec.Body.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{"deviceId":`+`"`+repository.NewID()+`"}`), "application/json")
|
||||||
|
expectError(t, rec, http.StatusTooManyRequests, "RATE_LIMITED", "devices-ratelimited")
|
||||||
|
if hookCalls != 1 {
|
||||||
|
t.Errorf("OnDevicesRateLimited appelé %d fois, attendu 1", hookCalls)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -19,6 +20,15 @@ const (
|
|||||||
// Auth issues/verifies device bearer tokens; set once at startup (cmd/server).
|
// Auth issues/verifies device bearer tokens; set once at startup (cmd/server).
|
||||||
var Auth *auth.Manager
|
var Auth *auth.Manager
|
||||||
|
|
||||||
|
// EnforceDeviceExists durcit RequireAuth (FEEDBACK A15) : quand true, le
|
||||||
|
// device_id porté par le token doit exister en base (POST /devices), sinon la
|
||||||
|
// requête est rejetée en 401 et le rejet est loggé côté serveur.
|
||||||
|
// Défaut false (V1) : le device reste un claim porté NON autorisant —
|
||||||
|
// l'autorisation vient du subject user_id + existence du compte. Passer à true
|
||||||
|
// coûte une lecture DB par requête authentifiée ; ne pas activer sans mesurer
|
||||||
|
// sur les routes à fort trafic (snapshot/sync).
|
||||||
|
var EnforceDeviceExists = false
|
||||||
|
|
||||||
// RequireAuth authenticates the bearer paseto token. Le subject (user_id)
|
// RequireAuth authenticates the bearer paseto token. Le subject (user_id)
|
||||||
// autorise — c'est la clé de scoping de toutes les ressources ; device_id est
|
// autorise — c'est la clé de scoping de toutes les ressources ; device_id est
|
||||||
// porté (non autorisant) pour l'idempotence outbox et les jobs OCR. Le compte
|
// porté (non autorisant) pour l'idempotence outbox et les jobs OCR. Le compte
|
||||||
@@ -26,7 +36,13 @@ var Auth *auth.Manager
|
|||||||
func RequireAuth(c *gin.Context) {
|
func RequireAuth(c *gin.Context) {
|
||||||
|
|
||||||
header := c.GetHeader("Authorization")
|
header := c.GetHeader("Authorization")
|
||||||
token, found := strings.CutPrefix(header, "Bearer ")
|
token := ""
|
||||||
|
found := false
|
||||||
|
// Préfixe Bearer insensible à la casse (cf. FEEDBACK A7).
|
||||||
|
if len(header) > 7 && strings.EqualFold(header[:7], "Bearer ") {
|
||||||
|
token = strings.TrimSpace(header[7:])
|
||||||
|
found = token != ""
|
||||||
|
}
|
||||||
|
|
||||||
if Auth == nil || !found || token == "" {
|
if Auth == nil || !found || token == "" {
|
||||||
api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "missing bearer token")
|
api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "missing bearer token")
|
||||||
@@ -57,6 +73,22 @@ func RequireAuth(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if EnforceDeviceExists && Store != nil && Store.Repository != nil {
|
||||||
|
// Verify garantit déjà le format 32-hex : Exists reçoit une entrée saine.
|
||||||
|
exists, err := Store.Repository.Devices.Exists(identity.DeviceID)
|
||||||
|
if err != nil {
|
||||||
|
api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not check device")
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
log.Printf("auth: token d'un device inconnu rejeté (user=%s device=%s)", identity.UserID, identity.DeviceID)
|
||||||
|
api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "unknown device")
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
c.Set(UserIDKey, identity.UserID)
|
c.Set(UserIDKey, identity.UserID)
|
||||||
c.Set(DeviceIDKey, identity.DeviceID)
|
c.Set(DeviceIDKey, identity.DeviceID)
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
const middlewareUnknownDevice = "ffffffffffffffffffffffffffffffff"
|
||||||
|
|
||||||
|
// Par défaut (EnforceDeviceExists=false), un token portant un device inconnu
|
||||||
|
// passe le middleware : le device est un claim non autorisant (V1).
|
||||||
|
func TestRequireAuthUnknownDevicePassesByDefault(t *testing.T) {
|
||||||
|
setTestAuth(t)
|
||||||
|
repo := setTestStore(t)
|
||||||
|
userID := createAdmin(t, repo, "admin", authTestPassword)
|
||||||
|
r := newTestRouterForAuth()
|
||||||
|
|
||||||
|
if EnforceDeviceExists {
|
||||||
|
t.Fatal("EnforceDeviceExists devrait être false par défaut")
|
||||||
|
}
|
||||||
|
|
||||||
|
signed, err := Auth.Issue(userID, middlewareUnknownDevice)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("issue: %v", err)
|
||||||
|
}
|
||||||
|
rec := doAuth(t, r, http.MethodGet, "/api/v1/files", signed, "")
|
||||||
|
if rec.Code == 401 {
|
||||||
|
t.Fatalf("device inconnu rejeté alors que EnforceDeviceExists=false: %s", rec.Body.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Avec EnforceDeviceExists=true, le device inconnu est rejeté en 401 (et
|
||||||
|
// loggé), le device enregistré passe.
|
||||||
|
func TestRequireAuthEnforceDeviceExists(t *testing.T) {
|
||||||
|
setTestAuth(t)
|
||||||
|
repo := setTestStore(t)
|
||||||
|
userID := createAdmin(t, repo, "admin", authTestPassword)
|
||||||
|
_ = repo.Devices.Upsert(authTestDevice)
|
||||||
|
r := newTestRouterForAuth()
|
||||||
|
|
||||||
|
old := EnforceDeviceExists
|
||||||
|
EnforceDeviceExists = true
|
||||||
|
t.Cleanup(func() { EnforceDeviceExists = old })
|
||||||
|
|
||||||
|
unknown, _ := Auth.Issue(userID, middlewareUnknownDevice)
|
||||||
|
rec := doAuth(t, r, http.MethodGet, "/api/v1/files", unknown, "")
|
||||||
|
if rec.Code != 401 {
|
||||||
|
t.Fatalf("device inconnu: status %d body %s, attendu 401", rec.Code, rec.Body.String())
|
||||||
|
}
|
||||||
|
if !strings.Contains(rec.Body.String(), "UNAUTHORIZED") {
|
||||||
|
t.Errorf("code attendu UNAUTHORIZED, got %s", rec.Body.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
known, _ := Auth.Issue(userID, authTestDevice)
|
||||||
|
rec = doAuth(t, r, http.MethodGet, "/api/v1/files", known, "")
|
||||||
|
if rec.Code == 401 {
|
||||||
|
t.Fatalf("device enregistré rejeté avec EnforceDeviceExists=true: %s", rec.Body.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -40,6 +40,7 @@ func setupOcr(t *testing.T) (*gin.Engine, string, *repository.Repository) {
|
|||||||
manager, _ := auth.NewManager("test-secret")
|
manager, _ := auth.NewManager("test-secret")
|
||||||
handlers.Auth = manager
|
handlers.Auth = manager
|
||||||
handlers.Store = store
|
handlers.Store = store
|
||||||
|
handlers.ResetDevicesRateLimit()
|
||||||
handlers.Ocr = service.NewOcr(repo, uploadDir, "fra+eng", stubEngine{text: "HELLO OCR"})
|
handlers.Ocr = service.NewOcr(repo, uploadDir, "fra+eng", stubEngine{text: "HELLO OCR"})
|
||||||
|
|
||||||
// Worker OCR borné — le handler n'enfile plus que le job, le traitement
|
// Worker OCR borné — le handler n'enfile plus que le job, le traitement
|
||||||
|
|||||||
@@ -25,11 +25,15 @@ func RegisterRoutes(r *gin.Engine) {
|
|||||||
protected.Use(RequireAuth)
|
protected.Use(RequireAuth)
|
||||||
{
|
{
|
||||||
protected.GET("/files", FilesList)
|
protected.GET("/files", FilesList)
|
||||||
|
// Verrou LOT 6 §1 : les routes statiques restent déclarées AVANT
|
||||||
|
// /files/:id (Gin priorise déjà le statique sur le paramétré, mais
|
||||||
|
// l'ordre source verrouille l'intention : /files/folders ne doit
|
||||||
|
// jamais être avalé par FilesGet avec id="folders").
|
||||||
protected.GET("/files/search", FilesSearch)
|
protected.GET("/files/search", FilesSearch)
|
||||||
|
protected.GET("/files/folders", FoldersList)
|
||||||
protected.GET("/files/:id", FilesGet)
|
protected.GET("/files/:id", FilesGet)
|
||||||
protected.GET("/files/:id/ocr", FilesGetOcr)
|
protected.GET("/files/:id/ocr", FilesGetOcr)
|
||||||
protected.DELETE("/files/:id", FilesDelete)
|
protected.DELETE("/files/:id", FilesDelete)
|
||||||
protected.GET("/files/folders", FoldersList)
|
|
||||||
protected.POST("/files/upload", FilesUpload)
|
protected.POST("/files/upload", FilesUpload)
|
||||||
|
|
||||||
protected.GET("/users/resolve", ResolveUser)
|
protected.GET("/users/resolve", ResolveUser)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -18,9 +19,18 @@ func ShareLinkGet(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
token := c.Param("token")
|
token := c.Param("token")
|
||||||
|
// Contrat V1 : les tokens de lien sont du 32-hex (cf. docs/api-v1.md).
|
||||||
|
// Malformé → 400 INVALID_REQUEST (pas 404 : ce n'est pas « introuvable »,
|
||||||
|
// c'est « requête invalide »). deviceIDPattern (devices.go, même package)
|
||||||
|
// exprime exactement ce format.
|
||||||
|
if !deviceIDPattern.MatchString(token) {
|
||||||
|
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "malformed link token")
|
||||||
|
return
|
||||||
|
}
|
||||||
link, err := Store.Repository.Shares.GetPublicLink(token)
|
link, err := Store.Repository.Shares.GetPublicLink(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == repository.ErrNotFound {
|
// A6 : errors.Is (pas ==) — le repo peut envelopper ErrNotFound.
|
||||||
|
if errors.Is(err, repository.ErrNotFound) {
|
||||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "link not found or revoked")
|
api.Error(c, http.StatusNotFound, "NOT_FOUND", "link not found or revoked")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/kazier/backend/pkg/api"
|
"github.com/kazier/backend/pkg/api"
|
||||||
@@ -28,6 +29,13 @@ func writeError(c *gin.Context, err error) {
|
|||||||
case errors.Is(err, service.ErrFileTooLarge):
|
case errors.Is(err, service.ErrFileTooLarge):
|
||||||
api.Error(c, 413, "FILE_TOO_LARGE", "file exceeds the maximum allowed size")
|
api.Error(c, 413, "FILE_TOO_LARGE", "file exceeds the maximum allowed size")
|
||||||
default:
|
default:
|
||||||
api.Error(c, 500, "INTERNAL", err.Error())
|
// Jamais de err.Error() brut au client (fuite SQL/FS/paths, cf.
|
||||||
|
// FEEDBACK #8) : message générique, détail au log serveur.
|
||||||
|
path := ""
|
||||||
|
if c.Request != nil && c.Request.URL != nil {
|
||||||
|
path = c.Request.URL.Path
|
||||||
|
}
|
||||||
|
log.Printf("internal error: path=%s err=%v", path, err)
|
||||||
|
api.Error(c, 500, "INTERNAL", "internal error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
@@ -9,10 +10,37 @@ import (
|
|||||||
"github.com/kazier/backend/service"
|
"github.com/kazier/backend/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// parseAfterMs parses the `after` delta cursor of GET /sync/permissions :
|
||||||
|
// epoch-millis, 64-bit (les curseurs dépassent 2^31). Chaîne vide → 0 (sync
|
||||||
|
// complet). Garbage ou négatif → ok=false : le handler répond 400
|
||||||
|
// INVALID_REQUEST, jamais de repli silencieux vers 0 (un curseur malformé
|
||||||
|
// pris pour « sync complet » masquerait une désynchro client).
|
||||||
|
// NOTE : la pagination (files.go) répond 400 sur page/pageSize malformés,
|
||||||
|
// jamais de repli silencieux vers un défaut (un curseur malformé pris pour
|
||||||
|
// « sync complet » masquerait une désynchro client).
|
||||||
|
func parseAfterMs(raw string) (int64, bool) {
|
||||||
|
if raw == "" {
|
||||||
|
return 0, true
|
||||||
|
}
|
||||||
|
ms, err := strconv.ParseInt(raw, 10, 64)
|
||||||
|
if err != nil || ms < 0 {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
return ms, true
|
||||||
|
}
|
||||||
|
|
||||||
type syncOpsRequest struct {
|
type syncOpsRequest struct {
|
||||||
Operations []service.SyncOperation `json:"operations"`
|
Operations []service.SyncOperation `json:"operations"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bornes anti-DoS de POST /sync/ops (FEEDBACK §moyenne) : le client envoie des
|
||||||
|
// batchs de 20 ; au-delà de 100 ops ou 1 MiB de body → rejet explicite, jamais
|
||||||
|
// d'OOM sur batch géant.
|
||||||
|
const (
|
||||||
|
maxSyncOpsBodyBytes = 1 << 20
|
||||||
|
maxSyncOpsPerBatch = 100
|
||||||
|
)
|
||||||
|
|
||||||
func SyncOpsPush(c *gin.Context) {
|
func SyncOpsPush(c *gin.Context) {
|
||||||
if Store == nil {
|
if Store == nil {
|
||||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||||
@@ -20,11 +48,16 @@ func SyncOpsPush(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
userID := c.GetString(UserIDKey)
|
userID := c.GetString(UserIDKey)
|
||||||
|
|
||||||
|
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxSyncOpsBodyBytes)
|
||||||
var req syncOpsRequest
|
var req syncOpsRequest
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid JSON body")
|
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid JSON body")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if len(req.Operations) > maxSyncOpsPerBatch {
|
||||||
|
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "too many operations in batch (max 100)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
result, err := Store.ApplyBatch(userID, c.GetString(DeviceIDKey), req.Operations)
|
result, err := Store.ApplyBatch(userID, c.GetString(DeviceIDKey), req.Operations)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -40,10 +73,10 @@ func SyncPermissionsGet(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
userID := c.GetString(UserIDKey)
|
userID := c.GetString(UserIDKey)
|
||||||
after := c.Query("after")
|
afterMs, ok := parseAfterMs(c.Query("after"))
|
||||||
var afterMs int64
|
if !ok {
|
||||||
if after != "" {
|
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid `after` cursor, must be non-negative epoch-millis")
|
||||||
afterMs = int64(intParam(after, 0))
|
return
|
||||||
}
|
}
|
||||||
perms, err := Store.Snapshot(userID, afterMs)
|
perms, err := Store.Snapshot(userID, afterMs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -254,6 +254,20 @@ func TestSyncOpsLargeBatchAccepted(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSyncOpsBatchTooLargeRejected(t *testing.T) {
|
||||||
|
r, _, repo := setup(t)
|
||||||
|
device := repository.NewID()
|
||||||
|
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "sx"), "sync-test-password", device)
|
||||||
|
|
||||||
|
ops := make([]map[string]any, 0, 101)
|
||||||
|
for i := 0; i < 101; i++ {
|
||||||
|
ops = append(ops, op(repository.NewID(), repository.NewID(), "create_resource", "file", map[string]any{"name": fmt.Sprintf("g%03d.txt", i)}))
|
||||||
|
}
|
||||||
|
|
||||||
|
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/sync/ops", token, syncOpsBody(ops), "application/json")
|
||||||
|
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "sync-batch-too-large")
|
||||||
|
}
|
||||||
|
|
||||||
func TestSyncOpsStopsAtFirstNonIdempotentFailure(t *testing.T) {
|
func TestSyncOpsStopsAtFirstNonIdempotentFailure(t *testing.T) {
|
||||||
r, _, repo := setup(t)
|
r, _, repo := setup(t)
|
||||||
device := repository.NewID()
|
device := repository.NewID()
|
||||||
|
|||||||
@@ -4,13 +4,42 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/ledongthuc/pdf"
|
"github.com/ledongthuc/pdf"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// tesseractTimeout : budget max par job (A13) — Tesseract ne doit jamais
|
||||||
|
// bloquer un worker indéfiniment (image pathologique).
|
||||||
|
tesseractTimeout = 120 * time.Second
|
||||||
|
// outputCap : plafond stdout/stderr capturés (A13, 2 Mo) — un document
|
||||||
|
// hostile ne doit pas faire exploser la RAM du worker.
|
||||||
|
outputCap = 2 << 20
|
||||||
|
// maxPDFPages : plafond de pages lues (A13) — au-delà, erreur explicite
|
||||||
|
// (pas de troncature silencieuse).
|
||||||
|
maxPDFPages = 50
|
||||||
|
)
|
||||||
|
|
||||||
|
// validTesseractLang : whitelist `^[a-z+]{2,20}$` (A12, défense en profondeur
|
||||||
|
// — le point d'entrée normal est service.normalizeOcrLang). Évite toute
|
||||||
|
// injection via `tesseract -l` (espaces, `;`, options `--...`).
|
||||||
|
func validTesseractLang(lang string) bool {
|
||||||
|
if len(lang) < 2 || len(lang) > 20 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, r := range lang {
|
||||||
|
if (r < 'a' || r > 'z') && r != '+' {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// TesseractEngine runs the `tesseract` binary in a subprocess.
|
// TesseractEngine runs the `tesseract` binary in a subprocess.
|
||||||
// Images are OCR'd directly; PDFs have their text layer extracted first
|
// Images are OCR'd directly; PDFs have their text layer extracted first
|
||||||
// (scanned PDFs → empty text, no rendering pipeline in V1).
|
// (scanned PDFs → empty text, no rendering pipeline in V1).
|
||||||
@@ -18,20 +47,55 @@ type TesseractEngine struct{}
|
|||||||
|
|
||||||
func NewTesseract() *TesseractEngine { return &TesseractEngine{} }
|
func NewTesseract() *TesseractEngine { return &TesseractEngine{} }
|
||||||
|
|
||||||
// ExtractText implements Engine.
|
// ExtractText implements Engine. Chaque job est borné par tesseractTimeout
|
||||||
|
// (contexte dérivé, annulé en fin d'appel).
|
||||||
func (t *TesseractEngine) ExtractText(ctx context.Context, filePath, lang string) (string, error) {
|
func (t *TesseractEngine) ExtractText(ctx context.Context, filePath, lang string) (string, error) {
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, tesseractTimeout)
|
||||||
|
defer cancel()
|
||||||
if strings.ToLower(filepath.Ext(filePath)) == ".pdf" {
|
if strings.ToLower(filepath.Ext(filePath)) == ".pdf" {
|
||||||
return extractPDFText(filePath)
|
return extractPDFText(ctx, filePath)
|
||||||
}
|
}
|
||||||
return runTesseract(ctx, filePath, lang)
|
return runTesseract(ctx, filePath, lang)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cappedBuffer borne un flux capturé à `limit` octets et signale la
|
||||||
|
// troncature (erreur explicite côté appelant, pas de coupe silencieuse).
|
||||||
|
type cappedBuffer struct {
|
||||||
|
buf bytes.Buffer
|
||||||
|
limit int
|
||||||
|
truncated bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cappedBuffer) Write(p []byte) (int, error) {
|
||||||
|
remaining := c.limit - c.buf.Len()
|
||||||
|
if remaining <= 0 {
|
||||||
|
c.truncated = true
|
||||||
|
return len(p), nil // déverse : le process ne bloque pas sur un pipe plein
|
||||||
|
}
|
||||||
|
if len(p) > remaining {
|
||||||
|
c.truncated = true
|
||||||
|
p = p[:remaining]
|
||||||
|
}
|
||||||
|
return c.buf.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cappedBuffer) String() string { return c.buf.String() }
|
||||||
|
|
||||||
func runTesseract(ctx context.Context, filePath, lang string) (string, error) {
|
func runTesseract(ctx context.Context, filePath, lang string) (string, error) {
|
||||||
|
// Défense en profondeur (A12) : même si l'appelant ne passe pas par
|
||||||
|
// service.normalizeOcrLang, `-l` ne reçoit jamais de métacaractères.
|
||||||
|
if !validTesseractLang(lang) {
|
||||||
|
return "", fmt.Errorf("tesseract: langue %q invalide", lang)
|
||||||
|
}
|
||||||
cmd := exec.CommandContext(ctx, "tesseract", filePath, "stdout", "-l", lang)
|
cmd := exec.CommandContext(ctx, "tesseract", filePath, "stdout", "-l", lang)
|
||||||
var stdout, stderr bytes.Buffer
|
stdout := &cappedBuffer{limit: outputCap}
|
||||||
cmd.Stdout = &stdout
|
stderr := &cappedBuffer{limit: outputCap}
|
||||||
cmd.Stderr = &stderr
|
cmd.Stdout = stdout
|
||||||
|
cmd.Stderr = stderr
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
|
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
||||||
|
return "", fmt.Errorf("tesseract: délai dépassé (%s): %w", tesseractTimeout, ctx.Err())
|
||||||
|
}
|
||||||
if errors.Is(ctx.Err(), context.Canceled) {
|
if errors.Is(ctx.Err(), context.Canceled) {
|
||||||
return "", ctx.Err()
|
return "", ctx.Err()
|
||||||
}
|
}
|
||||||
@@ -41,24 +105,37 @@ func runTesseract(ctx context.Context, filePath, lang string) (string, error) {
|
|||||||
}
|
}
|
||||||
return "", errors.New("tesseract: " + msg)
|
return "", errors.New("tesseract: " + msg)
|
||||||
}
|
}
|
||||||
|
if stdout.truncated {
|
||||||
|
return "", fmt.Errorf("tesseract: sortie tronquée (> %d octets)", outputCap)
|
||||||
|
}
|
||||||
|
// stderr tronqué avec un run OK : le texte est complet, mais le diagnostic
|
||||||
|
// est perdu — on l'accepte sans échouer le job.
|
||||||
return strings.TrimSpace(stdout.String()), nil
|
return strings.TrimSpace(stdout.String()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractPDFText(filePath string) (string, error) {
|
func extractPDFText(ctx context.Context, filePath string) (string, error) {
|
||||||
f, r, err := pdf.Open(filePath)
|
f, r, err := pdf.Open(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.New("pdf: " + err.Error())
|
return "", errors.New("pdf: " + err.Error())
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
if n := r.NumPage(); n > maxPDFPages {
|
||||||
|
return "", fmt.Errorf("pdf: %d pages > plafond %d (tronquer = perdre du texte)", n, maxPDFPages)
|
||||||
|
}
|
||||||
var builder strings.Builder
|
var builder strings.Builder
|
||||||
for i := 1; i <= r.NumPage(); i++ {
|
for i := 1; i <= r.NumPage(); i++ {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return "", fmt.Errorf("pdf: annulé page %d: %w", i, ctx.Err())
|
||||||
|
default:
|
||||||
|
}
|
||||||
p := r.Page(i)
|
p := r.Page(i)
|
||||||
if p.V.IsNull() {
|
if p.V.IsNull() {
|
||||||
continue
|
return "", fmt.Errorf("pdf: page %d nulle", i)
|
||||||
}
|
}
|
||||||
plain, err := p.GetPlainText(nil)
|
plain, err := p.GetPlainText(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
return "", fmt.Errorf("pdf: page %d illisible: %w", i, err)
|
||||||
}
|
}
|
||||||
builder.WriteString(plain)
|
builder.WriteString(plain)
|
||||||
builder.WriteString("\n")
|
builder.WriteString("\n")
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package ocr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestValidTesseractLang(t *testing.T) {
|
||||||
|
valid := []string{"fra", "eng", "fra+eng", "de", "abcdefghijklmnopqrst"} // 20 chars
|
||||||
|
for _, lang := range valid {
|
||||||
|
if !validTesseractLang(lang) {
|
||||||
|
t.Errorf("langue %q : attendue valide", lang)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
invalid := []string{
|
||||||
|
"", "e", "fra;rm -rf", "fra eng", "fra+eng --psm",
|
||||||
|
"FRA", "fra-eng", "fra/eng", "abcdefghijklmnopqrstu", // 21 chars
|
||||||
|
"fra\neng", "-l fra",
|
||||||
|
}
|
||||||
|
for _, lang := range invalid {
|
||||||
|
if validTesseractLang(lang) {
|
||||||
|
t.Errorf("langue %q : attendue invalide (injection -l)", lang)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCappedBufferTruncation(t *testing.T) {
|
||||||
|
c := &cappedBuffer{limit: 8}
|
||||||
|
if _, err := c.Write([]byte("12345678")); err != nil {
|
||||||
|
t.Fatalf("write: %v", err)
|
||||||
|
}
|
||||||
|
if c.truncated {
|
||||||
|
t.Error("8 octets pour limite 8 : ne doit pas être tronqué")
|
||||||
|
}
|
||||||
|
if _, err := c.Write([]byte("9")); err != nil {
|
||||||
|
t.Fatalf("write: %v", err)
|
||||||
|
}
|
||||||
|
if !c.truncated {
|
||||||
|
t.Error("9 octets pour limite 8 : doit être marqué tronqué")
|
||||||
|
}
|
||||||
|
if c.String() != "12345678" {
|
||||||
|
t.Errorf("contenu = %q, attendu %q", c.String(), "12345678")
|
||||||
|
}
|
||||||
|
// Écriture massive d'un coup : bornée, jamais plus que `limit`.
|
||||||
|
big := &cappedBuffer{limit: 4}
|
||||||
|
if _, err := big.Write([]byte(strings.Repeat("x", 1<<20))); err != nil {
|
||||||
|
t.Fatalf("write: %v", err)
|
||||||
|
}
|
||||||
|
if !big.truncated || len(big.String()) != 4 {
|
||||||
|
t.Errorf("écriture 1 Mo limite 4 : tronqué=%v len=%d", big.truncated, len(big.String()))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,10 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import "github.com/gin-gonic/gin"
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
const NotImplementedCode = "NOT_IMPLEMENTED"
|
const NotImplementedCode = "NOT_IMPLEMENTED"
|
||||||
|
|
||||||
@@ -22,6 +26,14 @@ func OKList(c *gin.Context, data any, page, pageSize, total int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Error(c *gin.Context, status int, code, message string) {
|
func Error(c *gin.Context, status int, code, message string) {
|
||||||
|
// LOT 6 §5 : le client ne reçoit que l'enveloppe générique (code +
|
||||||
|
// message sans détail interne — jamais de err.Error() brut). Le détail
|
||||||
|
// va au log serveur pour le diagnostic.
|
||||||
|
path := ""
|
||||||
|
if c.Request != nil && c.Request.URL != nil {
|
||||||
|
path = c.Request.URL.Path
|
||||||
|
}
|
||||||
|
log.Printf("api error: status=%d code=%s message=%s path=%s", status, code, message, path)
|
||||||
c.JSON(status, gin.H{
|
c.JSON(status, gin.H{
|
||||||
"error": gin.H{"code": code, "message": message},
|
"error": gin.H{"code": code, "message": message},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package auth
|
|||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"errors"
|
"errors"
|
||||||
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"aidanwoods.dev/go-paseto"
|
"aidanwoods.dev/go-paseto"
|
||||||
@@ -14,6 +15,8 @@ const TokenTTL = 7 * 24 * time.Hour
|
|||||||
|
|
||||||
var ErrInvalidToken = errors.New("invalid token")
|
var ErrInvalidToken = errors.New("invalid token")
|
||||||
|
|
||||||
|
var hex32Pattern = regexp.MustCompile(`^[0-9a-f]{32}$`)
|
||||||
|
|
||||||
// Identity est le résultat de Verify : l'utilisateur (sujet du token, autorisant)
|
// Identity est le résultat de Verify : l'utilisateur (sujet du token, autorisant)
|
||||||
// et le device (claim secondaire, porté mais NON autorisant seul).
|
// et le device (claim secondaire, porté mais NON autorisant seul).
|
||||||
type Identity struct {
|
type Identity struct {
|
||||||
@@ -39,29 +42,40 @@ func NewManager(secret string) (*Manager, error) {
|
|||||||
|
|
||||||
// Issue émet un token lié à un utilisateur ET à un device.
|
// Issue émet un token lié à un utilisateur ET à un device.
|
||||||
func (m *Manager) Issue(userID, deviceID string) (string, error) {
|
func (m *Manager) Issue(userID, deviceID string) (string, error) {
|
||||||
|
token, _, err := m.IssueWithExpiry(userID, deviceID)
|
||||||
|
return token, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// IssueWithExpiry émet un token et retourne son expiration réelle (le `exp`
|
||||||
|
// embarqué). Les appelants doivent exposer expiresAt, jamais le recalculer
|
||||||
|
// via Now()+TTL (dérive d'horloge, cf. FEEDBACK A7).
|
||||||
|
func (m *Manager) IssueWithExpiry(userID, deviceID string) (string, time.Time, error) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
expiresAt := now.Add(TokenTTL)
|
||||||
token := paseto.NewToken()
|
token := paseto.NewToken()
|
||||||
token.SetIssuedAt(now)
|
token.SetIssuedAt(now)
|
||||||
token.SetNotBefore(now)
|
token.SetNotBefore(now)
|
||||||
token.SetExpiration(now.Add(TokenTTL))
|
token.SetExpiration(expiresAt)
|
||||||
token.SetSubject(userID)
|
token.SetSubject(userID)
|
||||||
// v1.6.0 : SetString n'expose pas d'erreur (panic si non sérialisable).
|
// v1.6.0 : SetString n'expose pas d'erreur (panic si non sérialisable).
|
||||||
token.SetString("device_id", deviceID)
|
token.SetString("device_id", deviceID)
|
||||||
return token.V4Encrypt(m.key, nil), nil
|
return token.V4Encrypt(m.key, nil), expiresAt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify décode et contrôle le token, retourne l'identité (user + device).
|
// Verify décode et contrôle le token, retourne l'identité (user + device).
|
||||||
|
// Les deux identifiants doivent être 32-hex (cf. docs/api-v1.md) — rejet
|
||||||
|
// sinon pour préserver les invariants UNIQUE(device_id,operation_id)/CHECK.
|
||||||
func (m *Manager) Verify(signed string) (Identity, error) {
|
func (m *Manager) Verify(signed string) (Identity, error) {
|
||||||
parsed, err := paseto.NewParserForValidNow().ParseV4Local(m.key, signed, nil)
|
parsed, err := paseto.NewParserForValidNow().ParseV4Local(m.key, signed, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Identity{}, ErrInvalidToken
|
return Identity{}, ErrInvalidToken
|
||||||
}
|
}
|
||||||
userID, err := parsed.GetSubject()
|
userID, err := parsed.GetSubject()
|
||||||
if err != nil || userID == "" {
|
if err != nil || !hex32Pattern.MatchString(userID) {
|
||||||
return Identity{}, ErrInvalidToken
|
return Identity{}, ErrInvalidToken
|
||||||
}
|
}
|
||||||
deviceID, err := parsed.GetString("device_id")
|
deviceID, err := parsed.GetString("device_id")
|
||||||
if err != nil || deviceID == "" {
|
if err != nil || !hex32Pattern.MatchString(deviceID) {
|
||||||
return Identity{}, ErrInvalidToken
|
return Identity{}, ErrInvalidToken
|
||||||
}
|
}
|
||||||
return Identity{UserID: userID, DeviceID: deviceID}, nil
|
return Identity{UserID: userID, DeviceID: deviceID}, nil
|
||||||
|
|||||||
@@ -96,6 +96,44 @@ func TestVerifyRequiresDeviceClaim(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestVerifyRejectsNonHex32IDs(t *testing.T) {
|
||||||
|
m, _ := NewManager("test-secret")
|
||||||
|
|
||||||
|
issueWith := func(userID, deviceID string) string {
|
||||||
|
token := paseto.NewToken()
|
||||||
|
now := time.Now()
|
||||||
|
token.SetIssuedAt(now)
|
||||||
|
token.SetNotBefore(now)
|
||||||
|
token.SetExpiration(now.Add(TokenTTL))
|
||||||
|
token.SetSubject(userID)
|
||||||
|
token.SetString("device_id", deviceID)
|
||||||
|
return token.V4Encrypt(m.key, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := map[string][2]string{
|
||||||
|
"subject vide": {"", testDeviceID},
|
||||||
|
"subject non-hex": {"not-hex-at-all-!!!!!!!!!!!!!!!!", testDeviceID},
|
||||||
|
"subject majuscules": {"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", testDeviceID},
|
||||||
|
"subject trop court": {"abc123", testDeviceID},
|
||||||
|
"device vide": {testUserID, ""},
|
||||||
|
"device non-hex": {testUserID, "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"},
|
||||||
|
"device majuscules": {testUserID, "0123456789ABCDEF0123456789ABCDEF"},
|
||||||
|
"device trop long": {testUserID, testDeviceID + "00"},
|
||||||
|
"device avec tirets": {testUserID, "01234567-89ab-cdef-0123-456789abcdef"},
|
||||||
|
}
|
||||||
|
for name, ids := range cases {
|
||||||
|
signed := issueWith(ids[0], ids[1])
|
||||||
|
if _, err := m.Verify(signed); err == nil {
|
||||||
|
t.Errorf("%s: token accepté (user=%q device=%q), attendu rejet", name, ids[0], ids[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sanity : les IDs 32-hex valides passent toujours.
|
||||||
|
if _, err := m.Verify(issueWith(testUserID, testDeviceID)); err != nil {
|
||||||
|
t.Errorf("IDs valides rejetés: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestIssueSetsExpiration(t *testing.T) {
|
func TestIssueSetsExpiration(t *testing.T) {
|
||||||
m, _ := NewManager("test-secret")
|
m, _ := NewManager("test-secret")
|
||||||
before := time.Now()
|
before := time.Now()
|
||||||
@@ -118,3 +156,25 @@ func TestIssueSetsExpiration(t *testing.T) {
|
|||||||
t.Errorf("expiration = %v, attendu ≈ now+%v (fenêtre [%v, %v])", exp, TokenTTL, lower, upper)
|
t.Errorf("expiration = %v, attendu ≈ now+%v (fenêtre [%v, %v])", exp, TokenTTL, lower, upper)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIssueWithExpiryMatchesTokenExp(t *testing.T) {
|
||||||
|
m, _ := NewManager("test-secret")
|
||||||
|
signed, expiresAt, err := m.IssueWithExpiry(testUserID, testDeviceID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("IssueWithExpiry: %v", err)
|
||||||
|
}
|
||||||
|
parsed, err := paseto.NewParserForValidNow().ParseV4Local(m.key, signed, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("parse: %v", err)
|
||||||
|
}
|
||||||
|
exp, err := parsed.GetExpiration()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetExpiration: %v", err)
|
||||||
|
}
|
||||||
|
// L'expiresAt retourné doit être le exp embarqué (pas un Now()+TTL
|
||||||
|
// recalculé — cf. FEEDBACK A7). Comparaison à la seconde : le token
|
||||||
|
// sérialise sans lecture monotonique ni sous-seconde.
|
||||||
|
if exp.Unix() != expiresAt.Unix() {
|
||||||
|
t.Errorf("expiresAt = %v, exp token = %v", expiresAt, exp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -30,12 +30,17 @@ var (
|
|||||||
ErrMalformed = errors.New("malformed password hash")
|
ErrMalformed = errors.New("malformed password hash")
|
||||||
)
|
)
|
||||||
|
|
||||||
// dummyHash est un hash argon2id valide d'un mot de passe aléatoire fixe.
|
// dummyHash est un hash argon2id valide d'un mot de passe aléatoire généré
|
||||||
// Il sert d'égalisation de timing : quand le username est inconnu, on vérifie
|
// au boot. Il sert d'égalisation de timing : quand le username est inconnu,
|
||||||
// quand même contre ce hash pour que le délai de réponse soit identique à un
|
// on vérifie quand même contre ce hash pour que le délai de réponse soit
|
||||||
// « mauvais mot de passe ».
|
// identique à un « mauvais mot de passe ». Aléatoire par boot pour éviter
|
||||||
|
// toute préimage hardcodée (cf. FEEDBACK A1).
|
||||||
var dummyHash = func() string {
|
var dummyHash = func() string {
|
||||||
h, err := Hash("kazier-constantime-dummy")
|
raw := make([]byte, 32)
|
||||||
|
if _, err := rand.Read(raw); err != nil {
|
||||||
|
panic(fmt.Sprintf("passwd: entropie dummy indisponible: %v", err))
|
||||||
|
}
|
||||||
|
h, err := Hash(string(raw))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("passwd: impossible de pré-hacher le dummy: %v", err))
|
panic(fmt.Sprintf("passwd: impossible de pré-hacher le dummy: %v", err))
|
||||||
}
|
}
|
||||||
@@ -75,6 +80,18 @@ func Verify(password, stored string) error {
|
|||||||
if _, err := fmt.Sscanf(fields[3], "m=%d,t=%d,p=%d", &memory, &timeCost, &threads); err != nil {
|
if _, err := fmt.Sscanf(fields[3], "m=%d,t=%d,p=%d", &memory, &timeCost, &threads); err != nil {
|
||||||
return ErrMalformed
|
return ErrMalformed
|
||||||
}
|
}
|
||||||
|
// Caps anti-DoS : un hash stocké compromis ne doit pas provoquer un
|
||||||
|
// OOM/CPU arbitraire au prochain login (cf. FEEDBACK §moyenne).
|
||||||
|
const (
|
||||||
|
maxMemory = 256 * 1024 // 256 MiB
|
||||||
|
maxTime = 10
|
||||||
|
maxThreads = 8
|
||||||
|
maxKeyLen = 128
|
||||||
|
maxSaltLen = 128
|
||||||
|
)
|
||||||
|
if memory <= 0 || memory > maxMemory || timeCost <= 0 || timeCost > maxTime || threads <= 0 || threads > maxThreads {
|
||||||
|
return ErrMalformed
|
||||||
|
}
|
||||||
|
|
||||||
salt, err := base64.RawStdEncoding.DecodeString(fields[4])
|
salt, err := base64.RawStdEncoding.DecodeString(fields[4])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -84,6 +101,9 @@ func Verify(password, stored string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrMalformed
|
return ErrMalformed
|
||||||
}
|
}
|
||||||
|
if len(salt) == 0 || len(salt) > maxSaltLen || len(want) == 0 || len(want) > maxKeyLen {
|
||||||
|
return ErrMalformed
|
||||||
|
}
|
||||||
|
|
||||||
got := argon2.IDKey([]byte(password), salt, uint32(timeCost), uint32(memory), uint8(threads), uint32(len(want)))
|
got := argon2.IDKey([]byte(password), salt, uint32(timeCost), uint32(memory), uint8(threads), uint32(len(want)))
|
||||||
if subtle.ConstantTimeCompare(got, want) != 1 {
|
if subtle.ConstantTimeCompare(got, want) != 1 {
|
||||||
|
|||||||
@@ -33,10 +33,22 @@ func (d *Devices) Exists(deviceID string) (bool, error) {
|
|||||||
|
|
||||||
// MarkUser mémorise le dernier utilisateur connecté sur ce device (INFORMATIF,
|
// MarkUser mémorise le dernier utilisateur connecté sur ce device (INFORMATIF,
|
||||||
// jamais autorisant) et rafraîchit last_seen_at. Appelé au login.
|
// jamais autorisant) et rafraîchit last_seen_at. Appelé au login.
|
||||||
|
// Device absent → ErrNotFound (le handler distingue la course
|
||||||
|
// Exists→MarkUser au lieu d'émettre un token pour un device fantôme).
|
||||||
func (d *Devices) MarkUser(deviceID, userID string) error {
|
func (d *Devices) MarkUser(deviceID, userID string) error {
|
||||||
_, err := d.DB.Exec(
|
result, err := d.DB.Exec(
|
||||||
`UPDATE devices SET user_id = $2, last_seen_at = NOW() WHERE device_id = $1`,
|
`UPDATE devices SET user_id = $2, last_seen_at = NOW() WHERE device_id = $1`,
|
||||||
deviceID, userID,
|
deviceID, userID,
|
||||||
)
|
)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
affected, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if affected == 0 {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package repository
|
package repository
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/kazier/backend/dbtest"
|
"github.com/kazier/backend/dbtest"
|
||||||
@@ -71,3 +72,12 @@ func TestDevicesMarkUser(t *testing.T) {
|
|||||||
t.Errorf("user_id mémorisé = %q, attendu %q", storedUser, userID)
|
t.Errorf("user_id mémorisé = %q, attendu %q", storedUser, userID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDevicesMarkUserUnknownDevice(t *testing.T) {
|
||||||
|
d := newTestDevices(t)
|
||||||
|
// Device jamais enregistré → ErrNotFound (pas de UPDATE silencieux à
|
||||||
|
// 0 ligne) : le login mappe ce cas en 401 au lieu d'émettre un token.
|
||||||
|
if err := d.MarkUser(NewID(), NewID()); !errors.Is(err, ErrNotFound) {
|
||||||
|
t.Errorf("MarkUser device inconnu: attendu ErrNotFound, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -22,6 +22,12 @@ var ErrJobNotFound = errors.New("ocr job not found")
|
|||||||
// ErrNoQueuedJobs marks an OCR queue drained (no job to claim).
|
// ErrNoQueuedJobs marks an OCR queue drained (no job to claim).
|
||||||
var ErrNoQueuedJobs = errors.New("no queued ocr jobs")
|
var ErrNoQueuedJobs = errors.New("no queued ocr jobs")
|
||||||
|
|
||||||
|
// ErrJobStateConflict marks a guarded transition attempted from an
|
||||||
|
// unexpected state (double Complete, Fail of a non-processing job,
|
||||||
|
// TouchProcessing of a non-queued job). Terminal states are sticky:
|
||||||
|
// a second transition never overwrites the first.
|
||||||
|
var ErrJobStateConflict = errors.New("ocr job state conflict")
|
||||||
|
|
||||||
type OcrJobs struct{ DB *sql.DB }
|
type OcrJobs struct{ DB *sql.DB }
|
||||||
|
|
||||||
func (o *OcrJobs) Create(jobID, deviceID, fileID string) error {
|
func (o *OcrJobs) Create(jobID, deviceID, fileID string) error {
|
||||||
@@ -58,12 +64,22 @@ func (o *OcrJobs) Get(deviceID, jobID string) (OcrJobRow, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *OcrJobs) TouchProcessing(deviceID, jobID string) error {
|
func (o *OcrJobs) TouchProcessing(deviceID, jobID string) error {
|
||||||
_, err := o.DB.Exec(
|
res, err := o.DB.Exec(
|
||||||
`UPDATE ocr_jobs SET status = 'processing', started_at = NOW()
|
`UPDATE ocr_jobs SET status = 'processing', started_at = NOW()
|
||||||
WHERE job_id = $1 AND device_id = $2 AND status = 'queued'`,
|
WHERE job_id = $1 AND device_id = $2 AND status = 'queued'`,
|
||||||
jobID, deviceID,
|
jobID, deviceID,
|
||||||
)
|
)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
affected, err := res.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if affected == 0 {
|
||||||
|
return ErrJobStateConflict
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClaimNext atomically picks the oldest queued job (FIFO) and moves it to
|
// ClaimNext atomically picks the oldest queued job (FIFO) and moves it to
|
||||||
@@ -96,12 +112,15 @@ func (o *OcrJobs) ClaimNext() (OcrJobRow, error) {
|
|||||||
|
|
||||||
// ResetStaleProcessing requeues jobs stuck in `processing` (worker crash /
|
// ResetStaleProcessing requeues jobs stuck in `processing` (worker crash /
|
||||||
// processus redémarré) et plus vieux que `olderThan`. Retourne le nb de jobs
|
// processus redémarré) et plus vieux que `olderThan`. Retourne le nb de jobs
|
||||||
// requeued.
|
// requeued. La comparaison utilise l'horloge DB (NOW(), pas time.Now() du
|
||||||
|
// process) pour rester correcte même si les horloges divergent. Les jobs sans
|
||||||
|
// `started_at` (claim sans timestamp, anomalie) sont aussi requeued.
|
||||||
func (o *OcrJobs) ResetStaleProcessing(olderThan time.Duration) (int64, error) {
|
func (o *OcrJobs) ResetStaleProcessing(olderThan time.Duration) (int64, error) {
|
||||||
res, err := o.DB.Exec(
|
res, err := o.DB.Exec(
|
||||||
`UPDATE ocr_jobs SET status = 'queued', started_at = NULL
|
`UPDATE ocr_jobs SET status = 'queued', started_at = NULL
|
||||||
WHERE status = 'processing' AND started_at < $1`,
|
WHERE status = 'processing'
|
||||||
time.Now().Add(-olderThan),
|
AND (started_at IS NULL OR started_at < NOW() - ($1::bigint * INTERVAL '1 millisecond'))`,
|
||||||
|
olderThan.Milliseconds(),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -110,19 +129,39 @@ func (o *OcrJobs) ResetStaleProcessing(olderThan time.Duration) (int64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *OcrJobs) Complete(deviceID, jobID, text string) error {
|
func (o *OcrJobs) Complete(deviceID, jobID, text string) error {
|
||||||
_, err := o.DB.Exec(
|
res, err := o.DB.Exec(
|
||||||
`UPDATE ocr_jobs SET status = 'done', text = NULLIF($3, ''), started_at = COALESCE(started_at, NOW()), completed_at = NOW()
|
`UPDATE ocr_jobs SET status = 'done', text = NULLIF($3, ''), started_at = COALESCE(started_at, NOW()), completed_at = NOW()
|
||||||
WHERE job_id = $1 AND device_id = $2`,
|
WHERE job_id = $1 AND device_id = $2 AND status = 'processing'`,
|
||||||
jobID, deviceID, text,
|
jobID, deviceID, text,
|
||||||
)
|
)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
affected, err := res.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if affected == 0 {
|
||||||
|
return ErrJobStateConflict
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *OcrJobs) Fail(deviceID, jobID, message string) error {
|
func (o *OcrJobs) Fail(deviceID, jobID, message string) error {
|
||||||
_, err := o.DB.Exec(
|
res, err := o.DB.Exec(
|
||||||
`UPDATE ocr_jobs SET status = 'failed', error = NULLIF($3, ''), started_at = COALESCE(started_at, NOW()), completed_at = NOW()
|
`UPDATE ocr_jobs SET status = 'failed', error = NULLIF($3, ''), started_at = COALESCE(started_at, NOW()), completed_at = NOW()
|
||||||
WHERE job_id = $1 AND device_id = $2`,
|
WHERE job_id = $1 AND device_id = $2 AND status = 'processing'`,
|
||||||
jobID, deviceID, message,
|
jobID, deviceID, message,
|
||||||
)
|
)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
affected, err := res.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if affected == 0 {
|
||||||
|
return ErrJobStateConflict
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,6 +67,10 @@ func TestOcrJobsFail(t *testing.T) {
|
|||||||
if err := repo.OcrJobs.Create(jobID, deviceID, fileID); err != nil {
|
if err := repo.OcrJobs.Create(jobID, deviceID, fileID); err != nil {
|
||||||
t.Fatalf("create: %v", err)
|
t.Fatalf("create: %v", err)
|
||||||
}
|
}
|
||||||
|
// Fail n'est valide que depuis `processing` (transition gardée).
|
||||||
|
if err := repo.OcrJobs.TouchProcessing(deviceID, jobID); err != nil {
|
||||||
|
t.Fatalf("touch processing: %v", err)
|
||||||
|
}
|
||||||
if err := repo.OcrJobs.Fail(deviceID, jobID, "tesseract exploded"); err != nil {
|
if err := repo.OcrJobs.Fail(deviceID, jobID, "tesseract exploded"); err != nil {
|
||||||
t.Fatalf("fail: %v", err)
|
t.Fatalf("fail: %v", err)
|
||||||
}
|
}
|
||||||
@@ -76,6 +80,71 @@ func TestOcrJobsFail(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOcrJobsGuardedTransitions(t *testing.T) {
|
||||||
|
repo, _, deviceID, fileID := seedOcrJobFixture(t)
|
||||||
|
newJob := func(t *testing.T) string {
|
||||||
|
t.Helper()
|
||||||
|
jobID := NewID()
|
||||||
|
if err := repo.OcrJobs.Create(jobID, deviceID, fileID); err != nil {
|
||||||
|
t.Fatalf("create: %v", err)
|
||||||
|
}
|
||||||
|
return jobID
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fail depuis `queued` → conflit, le job reste queued.
|
||||||
|
queued := newJob(t)
|
||||||
|
if err := repo.OcrJobs.Fail(deviceID, queued, "boom"); !errors.Is(err, ErrJobStateConflict) {
|
||||||
|
t.Errorf("fail depuis queued : attendu ErrJobStateConflict, got %v", err)
|
||||||
|
}
|
||||||
|
if row, _ := repo.OcrJobs.Get(deviceID, queued); row.Status != "queued" {
|
||||||
|
t.Errorf("fail depuis queued : status = %q, attendu queued", row.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Complete depuis `queued` → conflit.
|
||||||
|
if err := repo.OcrJobs.Complete(deviceID, queued, "x"); !errors.Is(err, ErrJobStateConflict) {
|
||||||
|
t.Errorf("complete depuis queued : attendu ErrJobStateConflict, got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Double TouchProcessing → conflit.
|
||||||
|
if err := repo.OcrJobs.TouchProcessing(deviceID, queued); err != nil {
|
||||||
|
t.Fatalf("touch processing: %v", err)
|
||||||
|
}
|
||||||
|
if err := repo.OcrJobs.TouchProcessing(deviceID, queued); !errors.Is(err, ErrJobStateConflict) {
|
||||||
|
t.Errorf("double touch : attendu ErrJobStateConflict, got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Double Complete → le premier gagne, le second est un conflit.
|
||||||
|
if err := repo.OcrJobs.Complete(deviceID, queued, "FIRST"); err != nil {
|
||||||
|
t.Fatalf("complete: %v", err)
|
||||||
|
}
|
||||||
|
if err := repo.OcrJobs.Complete(deviceID, queued, "SECOND"); !errors.Is(err, ErrJobStateConflict) {
|
||||||
|
t.Errorf("double complete : attendu ErrJobStateConflict, got %v", err)
|
||||||
|
}
|
||||||
|
if row, _ := repo.OcrJobs.Get(deviceID, queued); row.Text == nil || *row.Text != "FIRST" {
|
||||||
|
t.Errorf("double complete : texte = %+v, attendu FIRST", row.Text)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fail après Complete → conflit, le statut `done` est collant.
|
||||||
|
if err := repo.OcrJobs.Fail(deviceID, queued, "late"); !errors.Is(err, ErrJobStateConflict) {
|
||||||
|
t.Errorf("fail après done : attendu ErrJobStateConflict, got %v", err)
|
||||||
|
}
|
||||||
|
if row, _ := repo.OcrJobs.Get(deviceID, queued); row.Status != "done" {
|
||||||
|
t.Errorf("fail après done : status = %q, attendu done", row.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Complete après Fail → conflit.
|
||||||
|
failed := newJob(t)
|
||||||
|
if err := repo.OcrJobs.TouchProcessing(deviceID, failed); err != nil {
|
||||||
|
t.Fatalf("touch processing: %v", err)
|
||||||
|
}
|
||||||
|
if err := repo.OcrJobs.Fail(deviceID, failed, "boom"); err != nil {
|
||||||
|
t.Fatalf("fail: %v", err)
|
||||||
|
}
|
||||||
|
if err := repo.OcrJobs.Complete(deviceID, failed, "late"); !errors.Is(err, ErrJobStateConflict) {
|
||||||
|
t.Errorf("complete après failed : attendu ErrJobStateConflict, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestOcrJobsGetScopedByDevice(t *testing.T) {
|
func TestOcrJobsGetScopedByDevice(t *testing.T) {
|
||||||
repo, _, deviceID, fileID := seedOcrJobFixture(t)
|
repo, _, deviceID, fileID := seedOcrJobFixture(t)
|
||||||
jobID := NewID()
|
jobID := NewID()
|
||||||
@@ -179,3 +248,24 @@ func TestOcrJobsResetStaleProcessing(t *testing.T) {
|
|||||||
t.Errorf("status après reset = %q, attendu queued", row.Status)
|
t.Errorf("status après reset = %q, attendu queued", row.Status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOcrJobsResetStaleProcessingNullStartedAt(t *testing.T) {
|
||||||
|
repo, _, deviceID, fileID := seedOcrJobFixture(t)
|
||||||
|
jobID := NewID()
|
||||||
|
if err := repo.OcrJobs.Create(jobID, deviceID, fileID); err != nil {
|
||||||
|
t.Fatalf("create: %v", err)
|
||||||
|
}
|
||||||
|
// Job passé en `processing` sans `started_at` (anomalie de claim) → stale.
|
||||||
|
if _, err := repo.OcrJobs.DB.Exec(
|
||||||
|
`UPDATE ocr_jobs SET status = 'processing', started_at = NULL WHERE job_id = $1`, jobID); err != nil {
|
||||||
|
t.Fatalf("forcer processing sans started_at: %v", err)
|
||||||
|
}
|
||||||
|
if n, err := repo.OcrJobs.ResetStaleProcessing(time.Minute); err != nil {
|
||||||
|
t.Fatalf("reset: %v", err)
|
||||||
|
} else if n != 1 {
|
||||||
|
t.Errorf("reset started_at NULL : attendu 1, got %d", n)
|
||||||
|
}
|
||||||
|
if row, _ := repo.OcrJobs.Get(deviceID, jobID); row.Status != "queued" {
|
||||||
|
t.Errorf("status après reset = %q, attendu queued", row.Status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package repository
|
package repository
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -16,9 +18,26 @@ type Operations struct {
|
|||||||
DB *sql.DB
|
DB *sql.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Statuts de la trace outbox (CHECK status IN ('applied','failed'), 000004).
|
||||||
|
// 'failed' = brouillon claimé pas encore confirmé (ou apply en échec) ;
|
||||||
|
// 'applied' = effet durable confirmé.
|
||||||
|
const (
|
||||||
|
OpStatusApplied = "applied"
|
||||||
|
OpStatusFailed = "failed"
|
||||||
|
)
|
||||||
|
|
||||||
// ErrInvalidOperationID is returned when operation_id is not 32 lowercase hex.
|
// ErrInvalidOperationID is returned when operation_id is not 32 lowercase hex.
|
||||||
var ErrInvalidOperationID = errors.New("operation_id must be 32 lowercase hex chars")
|
var ErrInvalidOperationID = errors.New("operation_id must be 32 lowercase hex chars")
|
||||||
|
|
||||||
|
// ErrInvalidPayload is returned when the trace payload is not valid JSON.
|
||||||
|
// Le service le mappe en INVALID_REQUEST (faute client, message statique sûr).
|
||||||
|
var ErrInvalidPayload = errors.New("invalid payload JSON")
|
||||||
|
|
||||||
|
// ErrPayloadMismatch is returned when an operation is replayed under the same
|
||||||
|
// (device_id, operation_id) with a different payload (cf. A9). The replay
|
||||||
|
// must halt in INVALID_REQUEST, never skip silently.
|
||||||
|
var ErrPayloadMismatch = errors.New("operation replayed with different payload")
|
||||||
|
|
||||||
// Applied reports whether the operation was already processed for this device.
|
// Applied reports whether the operation was already processed for this device.
|
||||||
func (o *Operations) Applied(deviceID, operationID string) (bool, error) {
|
func (o *Operations) Applied(deviceID, operationID string) (bool, error) {
|
||||||
var exists int
|
var exists int
|
||||||
@@ -32,20 +51,157 @@ func (o *Operations) Applied(deviceID, operationID string) (bool, error) {
|
|||||||
return err == nil, err
|
return err == nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StoredOp is the persisted trace of an applied operation (replay compare).
|
||||||
|
type StoredOp struct {
|
||||||
|
OpType string
|
||||||
|
ResourceID string
|
||||||
|
Payload []byte
|
||||||
|
Status string
|
||||||
|
PayloadHash string // "" = trace héritée pré-000013 (hash recalculé depuis Payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns the stored trace for (device, operation), or sql.ErrNoRows.
|
||||||
|
func (o *Operations) Get(deviceID, operationID string) (StoredOp, error) {
|
||||||
|
var op StoredOp
|
||||||
|
var resourceID sql.NullString
|
||||||
|
var payloadHash sql.NullString
|
||||||
|
var payload []byte
|
||||||
|
err := o.DB.QueryRow(
|
||||||
|
`SELECT op_type, resource_id, COALESCE(payload::text, '{}'), status, payload_hash
|
||||||
|
FROM operations WHERE device_id = $1 AND operation_id = $2`,
|
||||||
|
deviceID, operationID,
|
||||||
|
).Scan(&op.OpType, &resourceID, &payload, &op.Status, &payloadHash)
|
||||||
|
if err != nil {
|
||||||
|
return StoredOp{}, err
|
||||||
|
}
|
||||||
|
if resourceID.Valid {
|
||||||
|
op.ResourceID = resourceID.String
|
||||||
|
}
|
||||||
|
if payloadHash.Valid {
|
||||||
|
op.PayloadHash = payloadHash.String
|
||||||
|
}
|
||||||
|
op.Payload = payload
|
||||||
|
return op, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Claim atomically takes ownership of an operation slot for this device
|
||||||
|
// (#7 : sérialisation des batches concurrents sur la contrainte
|
||||||
|
// UNIQUE(device_id, operation_id)). Premier appelant → claimed=true, la ligne
|
||||||
|
// est insérée en status='failed' (brouillon) avec le hash du payload ; le
|
||||||
|
// perdant du conflit (claimed=false) suit le chemin rejeu (comparer via Get).
|
||||||
|
// Après apply réussi, l'appelant promeut la ligne via MarkApplied ; en cas
|
||||||
|
// d'échec d'apply, la ligne reste 'failed' (rejouable à payload identique).
|
||||||
|
func (o *Operations) Claim(deviceID, operationID string, opType, refType string, refID *int64, resourceID string, payload []byte) (bool, error) {
|
||||||
|
if !operationIDPattern.MatchString(operationID) {
|
||||||
|
return false, ErrInvalidOperationID
|
||||||
|
}
|
||||||
|
if len(payload) != 0 && string(payload) != "null" && !json.Valid(payload) {
|
||||||
|
return false, ErrInvalidPayload
|
||||||
|
}
|
||||||
|
var refTypeValue any
|
||||||
|
if refType != "" {
|
||||||
|
refTypeValue = refType
|
||||||
|
}
|
||||||
|
res, err := o.DB.Exec(
|
||||||
|
`INSERT INTO operations (device_id, operation_id, op_type, ref_type, ref_id, resource_id, payload, payload_hash, status)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'failed')
|
||||||
|
ON CONFLICT (device_id, operation_id) DO NOTHING`,
|
||||||
|
deviceID, operationID, opType, refTypeValue, refID, nullable(resourceID), jsonBytes(payload), CanonicalPayloadHash(payload),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
affected, err := res.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return affected == 1, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarkApplied promotes a claimed ('failed') trace row to 'applied' once the
|
||||||
|
// operation effect is durable. Best-effort : l'échec est remonté pour que le
|
||||||
|
// batch retourne applied:i explicite (rejouable, pas 500 nu).
|
||||||
|
func (o *Operations) MarkApplied(deviceID, operationID string) error {
|
||||||
|
res, err := o.DB.Exec(
|
||||||
|
`UPDATE operations SET status = 'applied'
|
||||||
|
WHERE device_id = $1 AND operation_id = $2 AND status = 'failed'`,
|
||||||
|
deviceID, operationID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
affected, err := res.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if affected == 0 {
|
||||||
|
// Déjà promue (rejeu concurrent) ou ligne absente : pas d'erreur,
|
||||||
|
// l'idempotence est portée par la contrainte d'unicité.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MatchesReplay reports whether an incoming operation matches the stored
|
||||||
|
// trace: same op type, same resource, same canonical payload hash. Legacy
|
||||||
|
// rows (payload_hash NULL, pré-000013) comparent le hash recalculé depuis le
|
||||||
|
// payload stocké. Mismatch → ErrPayloadMismatch (wrappable via errors.Is).
|
||||||
|
func (op StoredOp) MatchesReplay(opType, resourceID string, payload []byte) error {
|
||||||
|
if op.OpType != opType || op.ResourceID != resourceID {
|
||||||
|
return ErrPayloadMismatch
|
||||||
|
}
|
||||||
|
want := CanonicalPayloadHash(payload)
|
||||||
|
got := op.PayloadHash
|
||||||
|
if got == "" {
|
||||||
|
got = CanonicalPayloadHash(op.Payload)
|
||||||
|
}
|
||||||
|
if got != want {
|
||||||
|
return ErrPayloadMismatch
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CanonicalPayloadHash is sha256-hex of the canonical payload form: empty /
|
||||||
|
// "null" → {}, sinon round-trip JSON (clés triées, espaces neutralisés) pour
|
||||||
|
// que deux sérialisations du même objet donnent le même hash.
|
||||||
|
func CanonicalPayloadHash(payload []byte) string {
|
||||||
|
sum := sha256.Sum256(canonicalPayloadBytes(payload))
|
||||||
|
return hex.EncodeToString(sum[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func canonicalPayloadBytes(payload []byte) []byte {
|
||||||
|
if len(payload) == 0 || string(payload) == "null" {
|
||||||
|
return []byte(`{}`)
|
||||||
|
}
|
||||||
|
var v any
|
||||||
|
if err := json.Unmarshal(payload, &v); err != nil {
|
||||||
|
return []byte(`{}`)
|
||||||
|
}
|
||||||
|
out, err := json.Marshal(v)
|
||||||
|
if err != nil {
|
||||||
|
return []byte(`{}`)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
// Record stores an applied operation trace (idempotent on replay).
|
// Record stores an applied operation trace (idempotent on replay).
|
||||||
|
// Le hash canonique du payload est persisté (000013, cf. A9) ; status='applied'.
|
||||||
func (o *Operations) Record(deviceID, operationID string, opType, refType string, refID *int64, resourceID string, payload []byte) error {
|
func (o *Operations) Record(deviceID, operationID string, opType, refType string, refID *int64, resourceID string, payload []byte) error {
|
||||||
if !operationIDPattern.MatchString(operationID) {
|
if !operationIDPattern.MatchString(operationID) {
|
||||||
return ErrInvalidOperationID
|
return ErrInvalidOperationID
|
||||||
}
|
}
|
||||||
|
if len(payload) != 0 && string(payload) != "null" && !json.Valid(payload) {
|
||||||
|
return ErrInvalidPayload
|
||||||
|
}
|
||||||
var refTypeValue any
|
var refTypeValue any
|
||||||
if refType != "" {
|
if refType != "" {
|
||||||
refTypeValue = refType
|
refTypeValue = refType
|
||||||
}
|
}
|
||||||
_, err := o.DB.Exec(
|
_, err := o.DB.Exec(
|
||||||
`INSERT INTO operations (device_id, operation_id, op_type, ref_type, ref_id, resource_id, payload)
|
`INSERT INTO operations (device_id, operation_id, op_type, ref_type, ref_id, resource_id, payload, payload_hash, status)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'applied')
|
||||||
ON CONFLICT (device_id, operation_id) DO NOTHING`,
|
ON CONFLICT (device_id, operation_id) DO NOTHING`,
|
||||||
deviceID, operationID, opType, refTypeValue, refID, nullable(resourceID), jsonBytes(payload),
|
deviceID, operationID, opType, refTypeValue, refID, nullable(resourceID), jsonBytes(payload), CanonicalPayloadHash(payload),
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -62,7 +218,9 @@ func jsonBytes(payload []byte) any {
|
|||||||
return []byte(`{}`)
|
return []byte(`{}`)
|
||||||
}
|
}
|
||||||
if !json.Valid(payload) {
|
if !json.Valid(payload) {
|
||||||
return []byte(`{}`)
|
// Trace infidèle refusée (cf. A15) : l'appelant doit rejeter en amont ;
|
||||||
|
// ici on ne normalise plus silencieusement vers {}.
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return payload
|
return payload
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package repository
|
package repository
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -66,7 +65,7 @@ func TestOperationsRecordRejectsInvalidOperationID(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOperationsRecordNormalizesInvalidPayload(t *testing.T) {
|
func TestOperationsRecordRejectsInvalidPayload(t *testing.T) {
|
||||||
o, devices := newTestOperations(t)
|
o, devices := newTestOperations(t)
|
||||||
deviceID := NewID()
|
deviceID := NewID()
|
||||||
if err := devices.Upsert(deviceID); err != nil {
|
if err := devices.Upsert(deviceID); err != nil {
|
||||||
@@ -74,20 +73,128 @@ func TestOperationsRecordNormalizesInvalidPayload(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
opID := NewID()
|
opID := NewID()
|
||||||
// Un payload non-JSON ne doit pas faire échouer la trace (jsonBytes → {}).
|
// Un payload non-JSON est refusé (pas de normalisation silencieuse vers {}) :
|
||||||
if err := o.Record(deviceID, opID, "create_resource", "resource", nil, NewID(), []byte(`not-json`)); err != nil {
|
// l'appelant (service) doit rejeter en amont en INVALID_REQUEST.
|
||||||
t.Fatalf("record: %v", err)
|
if err := o.Record(deviceID, opID, "create_resource", "resource", nil, NewID(), []byte(`not-json`)); err == nil {
|
||||||
|
t.Error("payload non-JSON : erreur attendue, got nil")
|
||||||
}
|
}
|
||||||
|
if applied, _ := o.Applied(deviceID, opID); applied {
|
||||||
|
t.Error("record refusé : l'op ne doit pas être tracée")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOperationsRecordStoresPayloadHash(t *testing.T) {
|
||||||
|
o, devices := newTestOperations(t)
|
||||||
|
deviceID := NewID()
|
||||||
|
if err := devices.Upsert(deviceID); err != nil {
|
||||||
|
t.Fatalf("register device: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
opID := NewID()
|
||||||
|
payload := []byte(`{"name":"x"}`)
|
||||||
|
if err := o.Record(deviceID, opID, "create_resource", "resource", nil, NewID(), payload); err != nil {
|
||||||
|
t.Fatalf("record: %v", err)
|
||||||
|
}
|
||||||
|
stored, err := o.Get(deviceID, opID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("get: %v", err)
|
||||||
|
}
|
||||||
|
if stored.Status != OpStatusApplied {
|
||||||
|
t.Errorf("status = %q, attendu %q", stored.Status, OpStatusApplied)
|
||||||
|
}
|
||||||
|
want := CanonicalPayloadHash(payload)
|
||||||
|
if stored.PayloadHash != want {
|
||||||
|
t.Errorf("payload_hash = %q, attendu %q", stored.PayloadHash, want)
|
||||||
|
}
|
||||||
|
if len(stored.PayloadHash) != 64 {
|
||||||
|
t.Errorf("payload_hash attendu 64-hex, got %q", stored.PayloadHash)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOperationsClaimAndMarkApplied(t *testing.T) {
|
||||||
|
o, devices := newTestOperations(t)
|
||||||
|
deviceID := NewID()
|
||||||
|
if err := devices.Upsert(deviceID); err != nil {
|
||||||
|
t.Fatalf("register device: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
var stored string
|
opID := NewID()
|
||||||
if err := o.DB.QueryRow(`SELECT payload::text FROM operations WHERE device_id = $1 AND operation_id = $2`, deviceID, opID).Scan(&stored); err != nil {
|
payload := []byte(`{"name":"x"}`)
|
||||||
t.Fatalf("read payload: %v", err)
|
// Premier claim → pris.
|
||||||
|
claimed, err := o.Claim(deviceID, opID, "create_resource", "resource", nil, NewID(), payload)
|
||||||
|
if err != nil || !claimed {
|
||||||
|
t.Fatalf("claim 1: claimed=%v err=%v", claimed, err)
|
||||||
|
}
|
||||||
|
// La trace brouillon est en status failed (pas encore confirmée).
|
||||||
|
stored, err := o.Get(deviceID, opID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("get: %v", err)
|
||||||
|
}
|
||||||
|
if stored.Status != OpStatusFailed {
|
||||||
|
t.Errorf("status après claim = %q, attendu %q", stored.Status, OpStatusFailed)
|
||||||
|
}
|
||||||
|
// Second claim (batch concurrent) → perdu, chemin rejeu.
|
||||||
|
claimed, err = o.Claim(deviceID, opID, "create_resource", "resource", nil, NewID(), payload)
|
||||||
|
if err != nil || claimed {
|
||||||
|
t.Fatalf("claim 2: claimed=%v err=%v", claimed, err)
|
||||||
|
}
|
||||||
|
// Promotion après apply réussi.
|
||||||
|
if err := o.MarkApplied(deviceID, opID); err != nil {
|
||||||
|
t.Fatalf("mark applied: %v", err)
|
||||||
|
}
|
||||||
|
stored, err = o.Get(deviceID, opID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("get 2: %v", err)
|
||||||
|
}
|
||||||
|
if stored.Status != OpStatusApplied {
|
||||||
|
t.Errorf("status après promote = %q, attendu %q", stored.Status, OpStatusApplied)
|
||||||
|
}
|
||||||
|
// MarkApplied idempotent (re-promote sans effet).
|
||||||
|
if err := o.MarkApplied(deviceID, opID); err != nil {
|
||||||
|
t.Errorf("re-promote: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStoredOpMatchesReplay(t *testing.T) {
|
||||||
|
o, devices := newTestOperations(t)
|
||||||
|
deviceID := NewID()
|
||||||
|
if err := devices.Upsert(deviceID); err != nil {
|
||||||
|
t.Fatalf("register device: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
opID := NewID()
|
||||||
|
resID := NewID()
|
||||||
|
payload := []byte(`{"name":"x"}`)
|
||||||
|
if err := o.Record(deviceID, opID, "create_resource", "resource", nil, resID, payload); err != nil {
|
||||||
|
t.Fatalf("record: %v", err)
|
||||||
|
}
|
||||||
|
stored, err := o.Get(deviceID, opID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("get: %v", err)
|
||||||
|
}
|
||||||
|
// Identique (espaces neutres via hash canonique) → match.
|
||||||
|
if err := stored.MatchesReplay("create_resource", resID, []byte("{ \"name\" : \"x\" }")); err != nil {
|
||||||
|
t.Errorf("rejeu identique: %v", err)
|
||||||
|
}
|
||||||
|
// Payload muté → ErrPayloadMismatch.
|
||||||
|
if err := stored.MatchesReplay("create_resource", resID, []byte(`{"name":"y"}`)); !errors.Is(err, ErrPayloadMismatch) {
|
||||||
|
t.Errorf("payload muté : attendu ErrPayloadMismatch, got %v", err)
|
||||||
|
}
|
||||||
|
// Type muté → ErrPayloadMismatch.
|
||||||
|
if err := stored.MatchesReplay("delete_resource", resID, payload); !errors.Is(err, ErrPayloadMismatch) {
|
||||||
|
t.Errorf("type muté : attendu ErrPayloadMismatch, got %v", err)
|
||||||
|
}
|
||||||
|
// Ressource mutée → ErrPayloadMismatch.
|
||||||
|
if err := stored.MatchesReplay("create_resource", NewID(), payload); !errors.Is(err, ErrPayloadMismatch) {
|
||||||
|
t.Errorf("ressource mutée : attendu ErrPayloadMismatch, got %v", err)
|
||||||
}
|
}
|
||||||
var parsed any
|
// Ligne héritée sans payload_hash (pré-000013) : hash recalculé depuis
|
||||||
if err := json.Unmarshal([]byte(stored), &parsed); err != nil {
|
// le payload stocké → match quand identique.
|
||||||
t.Fatalf("payload stocké invalide: %q", stored)
|
stored.PayloadHash = ""
|
||||||
|
if err := stored.MatchesReplay("create_resource", resID, payload); err != nil {
|
||||||
|
t.Errorf("ligne héritée identique: %v", err)
|
||||||
}
|
}
|
||||||
if stored != "{}" {
|
if err := stored.MatchesReplay("create_resource", resID, []byte(`{"name":"z"}`)); !errors.Is(err, ErrPayloadMismatch) {
|
||||||
t.Errorf("payload normalisé attendu {}, got %q", stored)
|
t.Errorf("ligne héritée mutée : attendu ErrPayloadMismatch, got %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+122
-48
@@ -20,12 +20,22 @@ var ErrNameConflict = errors.New("name conflict")
|
|||||||
|
|
||||||
// NewID returns an opaque lowercase 32-hex identifier (charset ^[0-9a-f]{32}$),
|
// NewID returns an opaque lowercase 32-hex identifier (charset ^[0-9a-f]{32}$),
|
||||||
// mirroring the mobile's lower(hex(randomblob(16))). Never a UUID.
|
// mirroring the mobile's lower(hex(randomblob(16))). Never a UUID.
|
||||||
|
// Panics on entropy failure (legacy) — prefer NewIDErr for new code.
|
||||||
func NewID() string {
|
func NewID() string {
|
||||||
buf := make([]byte, 16)
|
id, err := NewIDErr()
|
||||||
if _, err := rand.Read(buf); err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("crypto/rand failed: %v", err))
|
panic(fmt.Sprintf("crypto/rand failed: %v", err))
|
||||||
}
|
}
|
||||||
return hex.EncodeToString(buf)
|
return id
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewIDErr is the non-panicking variant (cf. FEEDBACK A5).
|
||||||
|
func NewIDErr() (string, error) {
|
||||||
|
buf := make([]byte, 16)
|
||||||
|
if _, err := rand.Read(buf); err != nil {
|
||||||
|
return "", fmt.Errorf("crypto/rand failed: %w", err)
|
||||||
|
}
|
||||||
|
return hex.EncodeToString(buf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func isUniqueViolation(err error) bool {
|
func isUniqueViolation(err error) bool {
|
||||||
@@ -33,6 +43,14 @@ func isUniqueViolation(err error) bool {
|
|||||||
return errors.As(err, &pqErr) && pqErr.Code == "23505"
|
return errors.As(err, &pqErr) && pqErr.Code == "23505"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isForeignKeyViolation reports a Postgres FK violation (23503). Une
|
||||||
|
// violation de FK parent (dossier cible disparu en course) est mappée vers
|
||||||
|
// ErrNotFound — cible inexistante — plutôt que de fuir en 500.
|
||||||
|
func isForeignKeyViolation(err error) bool {
|
||||||
|
var pqErr *pq.Error
|
||||||
|
return errors.As(err, &pqErr) && pqErr.Code == "23503"
|
||||||
|
}
|
||||||
|
|
||||||
type FileRow struct {
|
type FileRow struct {
|
||||||
ID string
|
ID string
|
||||||
Name string
|
Name string
|
||||||
@@ -108,42 +126,39 @@ const grantedCTE = `
|
|||||||
AND r.deleted_at IS NULL AND g.depth < 100
|
AND r.deleted_at IS NULL AND g.depth < 100
|
||||||
)`
|
)`
|
||||||
|
|
||||||
func (r *Resources) folderExists(ownerID, folderID string) (bool, error) {
|
// insert persiste une ligne file/folder avec parent-check ATOMIQUE
|
||||||
var exists int
|
// (single-statement INSERT...SELECT) : le dossier parent (quand non vide)
|
||||||
err := r.DB.QueryRow(
|
// doit exister, appartenir au même owner, être un folder non supprimé —
|
||||||
`SELECT 1 FROM resources
|
// sinon ErrNotFound. L'ancien SELECT-then-INSERT (TOCTOU : parent supprimé
|
||||||
WHERE resource_id = $1 AND user_id = $2 AND type = 'folder' AND deleted_at IS NULL`,
|
// entre check et insert → FK 500) est remplacé ; une violation FK résiduelle
|
||||||
folderID, ownerID,
|
// (course) est mappée vers ErrNotFound, jamais 500.
|
||||||
).Scan(&exists)
|
|
||||||
if err == sql.ErrNoRows {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
return err == nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Resources) insert(ownerID, id, name, parentResourceID, resourceType string, size int64, mimeType, extension *string) error {
|
func (r *Resources) insert(ownerID, id, name, parentResourceID, resourceType string, size int64, mimeType, extension *string) error {
|
||||||
if parentResourceID != "" {
|
result, err := r.DB.Exec(
|
||||||
ok, err := r.folderExists(ownerID, parentResourceID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("folder %s: %w", parentResourceID, ErrNotFound)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var parentID any
|
|
||||||
if parentResourceID != "" {
|
|
||||||
parentID = parentResourceID
|
|
||||||
}
|
|
||||||
_, err := r.DB.Exec(
|
|
||||||
`INSERT INTO resources (resource_id, type, name, parent_id, user_id, size_bytes, mime_type, extension)
|
`INSERT INTO resources (resource_id, type, name, parent_id, user_id, size_bytes, mime_type, extension)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
|
SELECT $1, $2, $3, NULLIF($4, ''), $5, $6, $7, $8
|
||||||
id, resourceType, name, parentID, ownerID, size, mimeType, extension,
|
WHERE $4 = '' OR EXISTS (
|
||||||
|
SELECT 1 FROM resources
|
||||||
|
WHERE resource_id = $4 AND user_id = $5 AND type = 'folder' AND deleted_at IS NULL
|
||||||
|
)`,
|
||||||
|
id, resourceType, name, parentResourceID, ownerID, size, mimeType, extension,
|
||||||
)
|
)
|
||||||
if isUniqueViolation(err) {
|
if isUniqueViolation(err) {
|
||||||
return ErrNameConflict
|
return ErrNameConflict
|
||||||
}
|
}
|
||||||
return err
|
if isForeignKeyViolation(err) {
|
||||||
|
return fmt.Errorf("folder %s: %w", parentResourceID, ErrNotFound)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
affected, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if affected == 0 {
|
||||||
|
return fmt.Errorf("folder %s: %w", parentResourceID, ErrNotFound)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// InsertFile persists a file metadata row for the owner device. The parent
|
// InsertFile persists a file metadata row for the owner device. The parent
|
||||||
@@ -270,6 +285,26 @@ func (r *Resources) GetResourceOwner(resourceID string) (string, error) {
|
|||||||
return ownerID, err
|
return ownerID, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetParentID returns the parent_id ('""' si racine) d'une ressource non
|
||||||
|
// supprimée. No-rows → ErrNotFound. Sert au check de cycle des moves.
|
||||||
|
func (r *Resources) GetParentID(resourceID string) (string, error) {
|
||||||
|
var parent sql.NullString
|
||||||
|
err := r.DB.QueryRow(
|
||||||
|
`SELECT parent_id FROM resources WHERE resource_id = $1 AND deleted_at IS NULL`,
|
||||||
|
resourceID,
|
||||||
|
).Scan(&parent)
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return "", ErrNotFound
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if !parent.Valid {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
return parent.String, nil
|
||||||
|
}
|
||||||
|
|
||||||
// DeleteFile soft-deletes the file (deleted_at), returning its id.
|
// DeleteFile soft-deletes the file (deleted_at), returning its id.
|
||||||
func (r *Resources) DeleteFile(ownerID, resourceID string) (string, error) {
|
func (r *Resources) DeleteFile(ownerID, resourceID string) (string, error) {
|
||||||
result, err := r.DB.Exec(
|
result, err := r.DB.Exec(
|
||||||
@@ -369,26 +404,28 @@ func (r *Resources) GetFolder(ownerID, resourceID string) (FolderRow, error) {
|
|||||||
return row, err
|
return row, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// MoveResource sets parent_id (root when parentResourceID empty). The target
|
// MoveResource sets parent_id (root when parentResourceID empty) avec
|
||||||
// folder must exist and belong to the owner. Absent source is a no-op.
|
// parent-check ATOMIQUE (single-statement) : la cible doit exister,
|
||||||
|
// appartenir au même owner, être un folder non supprimé. Source absente /
|
||||||
|
// non-owned OU cible absente → ErrNotFound (même code : le choix A8
|
||||||
|
// move-non-owned → NOT_FOUND est appliqué ici, cf. LOT 3 pour la doc
|
||||||
|
// sync). Violation FK résiduelle (course) → ErrNotFound, jamais 500.
|
||||||
func (r *Resources) MoveResource(ownerID, resourceID, parentResourceID string) error {
|
func (r *Resources) MoveResource(ownerID, resourceID, parentResourceID string) error {
|
||||||
if parentResourceID != "" {
|
|
||||||
if _, err := r.GetFolder(ownerID, parentResourceID); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var parentID any
|
|
||||||
if parentResourceID != "" {
|
|
||||||
parentID = parentResourceID
|
|
||||||
}
|
|
||||||
result, err := r.DB.Exec(
|
result, err := r.DB.Exec(
|
||||||
`UPDATE resources SET parent_id = $3, updated_at = NOW()
|
`UPDATE resources SET parent_id = NULLIF($3, ''), updated_at = NOW()
|
||||||
WHERE resource_id = $1 AND user_id = $2 AND deleted_at IS NULL`,
|
WHERE resource_id = $1 AND user_id = $2 AND deleted_at IS NULL
|
||||||
resourceID, ownerID, parentID,
|
AND ($3 = '' OR EXISTS (
|
||||||
|
SELECT 1 FROM resources
|
||||||
|
WHERE resource_id = $3 AND user_id = $2 AND type = 'folder' AND deleted_at IS NULL
|
||||||
|
))`,
|
||||||
|
resourceID, ownerID, parentResourceID,
|
||||||
)
|
)
|
||||||
if err != nil && isUniqueViolation(err) {
|
if err != nil && isUniqueViolation(err) {
|
||||||
return ErrNameConflict
|
return ErrNameConflict
|
||||||
}
|
}
|
||||||
|
if err != nil && isForeignKeyViolation(err) {
|
||||||
|
return fmt.Errorf("folder %s: %w", parentResourceID, ErrNotFound)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -402,7 +439,7 @@ func (r *Resources) MoveResource(ownerID, resourceID, parentResourceID string) e
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateName renames a resource. Absent source is a no-op.
|
// UpdateName renames a resource. Absent / not owned → ErrNotFound.
|
||||||
func (r *Resources) UpdateName(ownerID, resourceID, name string) error {
|
func (r *Resources) UpdateName(ownerID, resourceID, name string) error {
|
||||||
result, err := r.DB.Exec(
|
result, err := r.DB.Exec(
|
||||||
`UPDATE resources SET name = $3, updated_at = NOW()
|
`UPDATE resources SET name = $3, updated_at = NOW()
|
||||||
@@ -425,8 +462,18 @@ func (r *Resources) UpdateName(ownerID, resourceID, name string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateNameOwned renames a resource OWNED by ownerID. Absent / not owned →
|
||||||
|
// ErrNotFound, sibling homonyme → ErrNameConflict. Variante scopée
|
||||||
|
// canonique (sémantique identique à UpdateName, nom aligné sur TouchOwned
|
||||||
|
// pour LOT 3).
|
||||||
|
func (r *Resources) UpdateNameOwned(ownerID, resourceID, name string) error {
|
||||||
|
return r.UpdateName(ownerID, resourceID, name)
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateNameByID renames a resource without owner scoping — the caller
|
// UpdateNameByID renames a resource without owner scoping — the caller
|
||||||
// (service) has already verified the user's effective access (editor+).
|
// (service) has already verified the user's effective access (editor+).
|
||||||
|
// Conservée volontairement pour update_metadata : un editor non-owner peut
|
||||||
|
// renommer, UpdateNameOwned rejetterait à tort.
|
||||||
func (r *Resources) UpdateNameByID(resourceID, name string) error {
|
func (r *Resources) UpdateNameByID(resourceID, name string) error {
|
||||||
result, err := r.DB.Exec(
|
result, err := r.DB.Exec(
|
||||||
`UPDATE resources SET name = $2, updated_at = NOW()
|
`UPDATE resources SET name = $2, updated_at = NOW()
|
||||||
@@ -519,8 +566,35 @@ func (r *Resources) ExistsOwner(ownerID, resourceID string) (bool, error) {
|
|||||||
return err == nil, err
|
return err == nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TouchOwned bumps updated_at on a non-deleted resource OWNED by ownerID so
|
||||||
|
// that the grantee's delta snapshot picks it up after a share is created.
|
||||||
|
// Absent / not owned → ErrNotFound. C'est la variante scopée à utiliser
|
||||||
|
// (LOT 3 : migrer les appelants sync vers TouchOwned).
|
||||||
|
func (r *Resources) TouchOwned(ownerID, resourceID string) error {
|
||||||
|
result, err := r.DB.Exec(
|
||||||
|
`UPDATE resources SET updated_at = NOW()
|
||||||
|
WHERE resource_id = $1 AND user_id = $2 AND deleted_at IS NULL`,
|
||||||
|
resourceID, ownerID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
affected, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if affected == 0 {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// TouchResource bumps updated_at on a non-deleted resource so that the
|
// TouchResource bumps updated_at on a non-deleted resource so that the
|
||||||
// grantee's delta snapshot picks it up after a share is created.
|
// grantee's delta snapshot picks it up after a share is created.
|
||||||
|
//
|
||||||
|
// Deprecated: non scopée (touche n'importe quel owner) — préférer TouchOwned.
|
||||||
|
// Plus aucun appelant sync (migrés vers TouchOwned) ; conservée pour
|
||||||
|
// compatibilité des appelants externes éventuels.
|
||||||
func (r *Resources) TouchResource(resourceID string) error {
|
func (r *Resources) TouchResource(resourceID string) error {
|
||||||
_, err := r.DB.Exec(
|
_, err := r.DB.Exec(
|
||||||
`UPDATE resources SET updated_at = NOW() WHERE resource_id = $1 AND deleted_at IS NULL`,
|
`UPDATE resources SET updated_at = NOW() WHERE resource_id = $1 AND deleted_at IS NULL`,
|
||||||
|
|||||||
@@ -233,6 +233,156 @@ func TestListOwnedDelta(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInsertParentCrossOwnerNotFound(t *testing.T) {
|
||||||
|
repo := newTestResources(t)
|
||||||
|
owner := NewID()
|
||||||
|
other := NewID()
|
||||||
|
mustInsertUser(t, repo, owner)
|
||||||
|
mustInsertUser(t, repo, other)
|
||||||
|
|
||||||
|
ownerFolder := NewID()
|
||||||
|
if err := repo.InsertFolder(owner, ownerFolder, "Docs", ""); err != nil {
|
||||||
|
t.Fatalf("insert folder: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dossier d'un autre owner comme parent → NOT_FOUND (pas de fuite 500).
|
||||||
|
if err := repo.InsertFile(other, NewID(), "x.txt", ownerFolder, 1, nil, nil); !errors.Is(err, ErrNotFound) {
|
||||||
|
t.Errorf("parent autre owner : attendu ErrNotFound, got %v", err)
|
||||||
|
}
|
||||||
|
if err := repo.InsertFolder(other, NewID(), "Sub", ownerFolder); !errors.Is(err, ErrNotFound) {
|
||||||
|
t.Errorf("parent dossier autre owner : attendu ErrNotFound, got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Un fichier comme parent → NOT_FOUND (pas un folder).
|
||||||
|
fileID := NewID()
|
||||||
|
if err := repo.InsertFile(owner, fileID, "note.txt", "", 1, nil, nil); err != nil {
|
||||||
|
t.Fatalf("insert file: %v", err)
|
||||||
|
}
|
||||||
|
if err := repo.InsertFile(owner, NewID(), "y.txt", fileID, 1, nil, nil); !errors.Is(err, ErrNotFound) {
|
||||||
|
t.Errorf("parent fichier : attendu ErrNotFound, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMoveResourceAtomic(t *testing.T) {
|
||||||
|
repo := newTestResources(t)
|
||||||
|
owner := NewID()
|
||||||
|
other := NewID()
|
||||||
|
mustInsertUser(t, repo, owner)
|
||||||
|
mustInsertUser(t, repo, other)
|
||||||
|
|
||||||
|
folderA := NewID()
|
||||||
|
folderB := NewID()
|
||||||
|
if err := repo.InsertFolder(owner, folderA, "A", ""); err != nil {
|
||||||
|
t.Fatalf("insert A: %v", err)
|
||||||
|
}
|
||||||
|
if err := repo.InsertFolder(owner, folderB, "B", ""); err != nil {
|
||||||
|
t.Fatalf("insert B: %v", err)
|
||||||
|
}
|
||||||
|
otherFolder := NewID()
|
||||||
|
if err := repo.InsertFolder(other, otherFolder, "Other", ""); err != nil {
|
||||||
|
t.Fatalf("insert other: %v", err)
|
||||||
|
}
|
||||||
|
fileID := NewID()
|
||||||
|
if err := repo.InsertFile(owner, fileID, "note.txt", folderA, 1, nil, nil); err != nil {
|
||||||
|
t.Fatalf("insert file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move OK vers dossier owned.
|
||||||
|
if err := repo.MoveResource(owner, fileID, folderB); err != nil {
|
||||||
|
t.Fatalf("move: %v", err)
|
||||||
|
}
|
||||||
|
got, err := repo.GetFile(owner, fileID)
|
||||||
|
if err != nil || got.FolderID != folderB {
|
||||||
|
t.Errorf("après move : folder=%q err=%v", got.FolderID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cible d'un autre owner → NOT_FOUND, la source ne bouge pas.
|
||||||
|
if err := repo.MoveResource(owner, fileID, otherFolder); !errors.Is(err, ErrNotFound) {
|
||||||
|
t.Errorf("cible autre owner : attendu ErrNotFound, got %v", err)
|
||||||
|
}
|
||||||
|
got, _ = repo.GetFile(owner, fileID)
|
||||||
|
if got.FolderID != folderB {
|
||||||
|
t.Errorf("move refusé ne doit rien changer, folder=%q", got.FolderID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cible inexistante → NOT_FOUND.
|
||||||
|
if err := repo.MoveResource(owner, fileID, NewID()); !errors.Is(err, ErrNotFound) {
|
||||||
|
t.Errorf("cible inconnue : attendu ErrNotFound, got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Source inconnue → NOT_FOUND.
|
||||||
|
if err := repo.MoveResource(owner, NewID(), folderB); !errors.Is(err, ErrNotFound) {
|
||||||
|
t.Errorf("source inconnue : attendu ErrNotFound, got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retour à la racine.
|
||||||
|
if err := repo.MoveResource(owner, fileID, ""); err != nil {
|
||||||
|
t.Fatalf("move racine: %v", err)
|
||||||
|
}
|
||||||
|
got, _ = repo.GetFile(owner, fileID)
|
||||||
|
if got.FolderID != "" {
|
||||||
|
t.Errorf("racine attendue, folder=%q", got.FolderID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTouchOwnedAndUpdateNameOwned(t *testing.T) {
|
||||||
|
repo := newTestResources(t)
|
||||||
|
owner := NewID()
|
||||||
|
other := NewID()
|
||||||
|
mustInsertUser(t, repo, owner)
|
||||||
|
mustInsertUser(t, repo, other)
|
||||||
|
|
||||||
|
fileID := NewID()
|
||||||
|
if err := repo.InsertFile(owner, fileID, "note.txt", "", 1, nil, nil); err != nil {
|
||||||
|
t.Fatalf("insert: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
before, err := repo.GetFile(owner, fileID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("get: %v", err)
|
||||||
|
}
|
||||||
|
if err := repo.TouchOwned(owner, fileID); err != nil {
|
||||||
|
t.Fatalf("touch owned: %v", err)
|
||||||
|
}
|
||||||
|
after, err := repo.GetFile(owner, fileID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("get après touch: %v", err)
|
||||||
|
}
|
||||||
|
if !after.UpdatedAt.After(before.UpdatedAt) && !after.UpdatedAt.Equal(before.UpdatedAt) {
|
||||||
|
t.Errorf("touch doit bumperc updated_at : %v → %v", before.UpdatedAt, after.UpdatedAt)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Non-owned / inconnu → ErrNotFound.
|
||||||
|
if err := repo.TouchOwned(other, fileID); !errors.Is(err, ErrNotFound) {
|
||||||
|
t.Errorf("touch autre owner : attendu ErrNotFound, got %v", err)
|
||||||
|
}
|
||||||
|
if err := repo.TouchOwned(owner, NewID()); !errors.Is(err, ErrNotFound) {
|
||||||
|
t.Errorf("touch inconnu : attendu ErrNotFound, got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := repo.UpdateNameOwned(owner, fileID, "renamed.txt"); err != nil {
|
||||||
|
t.Fatalf("rename owned: %v", err)
|
||||||
|
}
|
||||||
|
renamed, err := repo.GetFile(owner, fileID)
|
||||||
|
if err != nil || renamed.Name != "renamed.txt" {
|
||||||
|
t.Errorf("rename : %+v err=%v", renamed, err)
|
||||||
|
}
|
||||||
|
if err := repo.UpdateNameOwned(other, fileID, "volé.txt"); !errors.Is(err, ErrNotFound) {
|
||||||
|
t.Errorf("rename autre owner : attendu ErrNotFound, got %v", err)
|
||||||
|
}
|
||||||
|
if err := repo.UpdateNameOwned(owner, NewID(), "x.txt"); !errors.Is(err, ErrNotFound) {
|
||||||
|
t.Errorf("rename inconnu : attendu ErrNotFound, got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Anciennes variantes (deprecated, compat LOT 3) : toujours fonctionnelles.
|
||||||
|
if err := repo.TouchResource(fileID); err != nil {
|
||||||
|
t.Errorf("touch legacy: %v", err)
|
||||||
|
}
|
||||||
|
if err := repo.UpdateNameByID(fileID, "legacy.txt"); err != nil {
|
||||||
|
t.Errorf("rename legacy: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestUpdateOcrTextAndReadBack(t *testing.T) {
|
func TestUpdateOcrTextAndReadBack(t *testing.T) {
|
||||||
repo := newTestResources(t)
|
repo := newTestResources(t)
|
||||||
owner := NewID()
|
owner := NewID()
|
||||||
|
|||||||
@@ -327,17 +327,44 @@ func (s *Shares) ResourceExists(resourceID string) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateLink stores a share link (token = id, client-generated 32-hex).
|
// CreateLink stores a share link (token = id, client-generated 32-hex).
|
||||||
|
// Idempotent sur rejeu même (token) : ON CONFLICT DO NOTHING puis vérifie
|
||||||
|
// que le lien existant vise la même ressource ET le même owner (created_by) —
|
||||||
|
// sinon ErrNameConflict (réutilisation de token, cf. FEEDBACK #3). Un lien
|
||||||
|
// révoqué identique reste révoqué (pas de résurrection par rejeu : revoke gagne).
|
||||||
func (s *Shares) CreateLink(token, resourceID, access string, expiresAt *time.Time, createdBy string) error {
|
func (s *Shares) CreateLink(token, resourceID, access string, expiresAt *time.Time, createdBy string) error {
|
||||||
var expires any
|
var expires any
|
||||||
if expiresAt != nil {
|
if expiresAt != nil {
|
||||||
expires = *expiresAt
|
expires = *expiresAt
|
||||||
}
|
}
|
||||||
_, err := s.DB.Exec(
|
res, err := s.DB.Exec(
|
||||||
`INSERT INTO share_links (id, resource_id, access, created_by, expires_at)
|
`INSERT INTO share_links (id, resource_id, access, created_by, expires_at)
|
||||||
VALUES ($1, $2, $3, $4, $5)`,
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
|
ON CONFLICT (id) DO NOTHING`,
|
||||||
token, resourceID, access, createdBy, expires,
|
token, resourceID, access, createdBy, expires,
|
||||||
)
|
)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
affected, err := res.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if affected == 1 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// Conflit : le token existe déjà — idempotent si même (ressource, owner),
|
||||||
|
// sinon conflit (le token est une clé d'idempotence client).
|
||||||
|
var existing struct {
|
||||||
|
resourceID string
|
||||||
|
createdBy string
|
||||||
|
}
|
||||||
|
if err := s.DB.QueryRow(`SELECT resource_id, created_by FROM share_links WHERE id = $1`, token).Scan(&existing.resourceID, &existing.createdBy); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if existing.resourceID != resourceID || existing.createdBy != createdBy {
|
||||||
|
return ErrNameConflict
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RevokeLink soft-revokes a link by token. Returns ErrNotFound if the link
|
// RevokeLink soft-revokes a link by token. Returns ErrNotFound if the link
|
||||||
|
|||||||
+42
-15
@@ -20,11 +20,12 @@ type Users struct {
|
|||||||
DB *sql.DB
|
DB *sql.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count retourne le nombre d'utilisateurs (y compris supprimés) — sert au
|
// Count retourne le nombre d'utilisateurs ACTIFS (deleted_at IS NULL) — sert
|
||||||
// bootstrap admin au démarrage.
|
// au bootstrap admin au démarrage. Une base dont tous les comptes sont
|
||||||
|
// soft-deleted est vue comme vide : EnsureAdmin recréera un admin (#9+A10).
|
||||||
func (u *Users) Count() (int, error) {
|
func (u *Users) Count() (int, error) {
|
||||||
var n int
|
var n int
|
||||||
err := u.DB.QueryRow(`SELECT COUNT(*) FROM users`).Scan(&n)
|
err := u.DB.QueryRow(`SELECT COUNT(*) FROM users WHERE deleted_at IS NULL`).Scan(&n)
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,8 +68,11 @@ func (u *Users) ResolveExact(normalized string) (*UserRow, error) {
|
|||||||
|
|
||||||
// Create insère un compte avec un id 32-hex généré. Retourne l'id.
|
// Create insère un compte avec un id 32-hex généré. Retourne l'id.
|
||||||
func (u *Users) Create(username, usernameNormalized, passwordHash string, isAdmin bool) (string, error) {
|
func (u *Users) Create(username, usernameNormalized, passwordHash string, isAdmin bool) (string, error) {
|
||||||
id := NewID()
|
id, err := NewIDErr()
|
||||||
_, err := u.DB.Exec(
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
_, err = u.DB.Exec(
|
||||||
`INSERT INTO users (id, username, username_normalized, password_hash, is_admin, created_at)
|
`INSERT INTO users (id, username, username_normalized, password_hash, is_admin, created_at)
|
||||||
VALUES ($1, $2, $3, $4, $5, NOW())`,
|
VALUES ($1, $2, $3, $4, $5, NOW())`,
|
||||||
id, username, usernameNormalized, passwordHash, isAdmin,
|
id, username, usernameNormalized, passwordHash, isAdmin,
|
||||||
@@ -82,24 +86,47 @@ func (u *Users) Create(username, usernameNormalized, passwordHash string, isAdmi
|
|||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdatePassword remplace le hash du compte. Les tokens déjà émis restent
|
// UpdatePassword remplace le hash du compte actif. Compte absent ou
|
||||||
// valides 7 jours : limite assumée V1 (pas de liste de révocation).
|
// soft-deleted → ErrNotFound. Les tokens déjà émis restent valides 7 jours :
|
||||||
|
// limite assumée V1 (pas de liste de révocation).
|
||||||
func (u *Users) UpdatePassword(id, newHash string) error {
|
func (u *Users) UpdatePassword(id, newHash string) error {
|
||||||
_, err := u.DB.Exec(
|
result, err := u.DB.Exec(
|
||||||
`UPDATE users SET password_hash = $2, updated_at = NOW() WHERE id = $1`,
|
`UPDATE users SET password_hash = $2, updated_at = NOW() WHERE id = $1 AND deleted_at IS NULL`,
|
||||||
id, newHash,
|
id, newHash,
|
||||||
)
|
)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
affected, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if affected == 0 {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarkDeleted soft-delete un compte : les tokens issus sont rejetés par le
|
// MarkDeleted soft-delete un compte actif : les tokens issus sont rejetés par
|
||||||
// middleware (GetByID filtre deleted_at).
|
// le middleware (GetByID filtre deleted_at). Compte absent ou déjà supprimé →
|
||||||
|
// ErrNotFound (pas d'idempotence silencieuse : le second delete signale que
|
||||||
|
// la cible n'est plus active).
|
||||||
func (u *Users) MarkDeleted(id string) error {
|
func (u *Users) MarkDeleted(id string) error {
|
||||||
_, err := u.DB.Exec(
|
result, err := u.DB.Exec(
|
||||||
`UPDATE users SET deleted_at = NOW(), updated_at = NOW() WHERE id = $1`,
|
`UPDATE users SET deleted_at = NOW(), updated_at = NOW() WHERE id = $1 AND deleted_at IS NULL`,
|
||||||
id,
|
id,
|
||||||
)
|
)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
affected, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if affected == 0 {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func scanUser(scan func(...any) error) (*UserRow, error) {
|
func scanUser(scan func(...any) error) (*UserRow, error) {
|
||||||
|
|||||||
@@ -64,6 +64,11 @@ func TestUsersUpdatePassword(t *testing.T) {
|
|||||||
if err != nil || row.PasswordHash != "hash-2" {
|
if err != nil || row.PasswordHash != "hash-2" {
|
||||||
t.Errorf("hash après update = %q (err=%v)", row.PasswordHash, err)
|
t.Errorf("hash après update = %q (err=%v)", row.PasswordHash, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Compte inconnu → ErrNotFound (#9+A10).
|
||||||
|
if err := u.UpdatePassword(NewID(), "hash-x"); !errors.Is(err, ErrNotFound) {
|
||||||
|
t.Errorf("update password inconnu : attendu ErrNotFound, got %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUsersMarkDeletedHidesAccount(t *testing.T) {
|
func TestUsersMarkDeletedHidesAccount(t *testing.T) {
|
||||||
@@ -82,7 +87,22 @@ func TestUsersMarkDeletedHidesAccount(t *testing.T) {
|
|||||||
if _, err := u.GetByUsernameNormalized("carol"); !errors.Is(err, ErrNotFound) {
|
if _, err := u.GetByUsernameNormalized("carol"); !errors.Is(err, ErrNotFound) {
|
||||||
t.Errorf("get par username après suppression : attendu ErrNotFound, got %v", err)
|
t.Errorf("get par username après suppression : attendu ErrNotFound, got %v", err)
|
||||||
}
|
}
|
||||||
if n, _ := u.Count(); n != 1 {
|
// Count ne compte que les ACTIFS : carol soft-deleted → 0 (#9+A10). Une
|
||||||
t.Errorf("count doit compter aussi les supprimés (bootstrap), got %d", n)
|
// base 100% soft-deleted est vue comme vide par le bootstrap.
|
||||||
|
if n, _ := u.Count(); n != 0 {
|
||||||
|
t.Errorf("count doit exclure les supprimés, got %d", n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdatePassword sur compte supprimé → ErrNotFound.
|
||||||
|
if err := u.UpdatePassword(id, "hash-2"); !errors.Is(err, ErrNotFound) {
|
||||||
|
t.Errorf("update password après suppression : attendu ErrNotFound, got %v", err)
|
||||||
|
}
|
||||||
|
// Second MarkDeleted → ErrNotFound (plus de cible active).
|
||||||
|
if err := u.MarkDeleted(id); !errors.Is(err, ErrNotFound) {
|
||||||
|
t.Errorf("second mark deleted : attendu ErrNotFound, got %v", err)
|
||||||
|
}
|
||||||
|
// MarkDeleted sur inconnu → ErrNotFound.
|
||||||
|
if err := u.MarkDeleted(NewID()); !errors.Is(err, ErrNotFound) {
|
||||||
|
t.Errorf("mark deleted inconnu : attendu ErrNotFound, got %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,9 @@ func NormalizeUsername(username string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// EnsureAdmin crée le compte admin au premier démarrage si la table users est
|
// EnsureAdmin crée le compte admin au premier démarrage si la table users est
|
||||||
// vide. Idempotent : l'env n'écrase jamais un compte existant.
|
// vide. Idempotent : l'env n'écrase jamais un compte existant. Seuls les
|
||||||
|
// comptes ACTIFS comptent (Users.Count filtre deleted_at, #9+A10) : une base
|
||||||
|
// 100% soft-deleted est vue comme vide et reçoit un nouvel admin.
|
||||||
func EnsureAdmin(repo *repository.Repository, username, password string) error {
|
func EnsureAdmin(repo *repository.Repository, username, password string) error {
|
||||||
count, err := repo.Users.Count()
|
count, err := repo.Users.Count()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -60,6 +60,32 @@ func TestEnsureAdminRefusesOnEmptyDBWithoutEnv(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEnsureAdminRecreatesWhenAllSoftDeleted(t *testing.T) {
|
||||||
|
s := newServiceStore(t)
|
||||||
|
repo := s.Repository
|
||||||
|
|
||||||
|
// Un seul compte, soft-deleted : Count (actifs seuls, #9+A10) voit une
|
||||||
|
// base vide → EnsureAdmin recrée un admin au lieu de refuser.
|
||||||
|
id, err := repo.Users.Create("alice", "alice", "existing-hash", false)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("seed user: %v", err)
|
||||||
|
}
|
||||||
|
if err := repo.Users.MarkDeleted(id); err != nil {
|
||||||
|
t.Fatalf("mark deleted: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := EnsureAdmin(repo, "admin", "admin-secret-123"); err != nil {
|
||||||
|
t.Fatalf("EnsureAdmin sur base 100%% soft-deleted : %v", err)
|
||||||
|
}
|
||||||
|
admin, err := repo.Users.GetByUsernameNormalized("admin")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("admin recréé introuvable: %v", err)
|
||||||
|
}
|
||||||
|
if !admin.IsAdmin {
|
||||||
|
t.Error("le compte recréé doit être admin")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestEnsureAdminNeverOverwritesExistingAccounts(t *testing.T) {
|
func TestEnsureAdminNeverOverwritesExistingAccounts(t *testing.T) {
|
||||||
s := newServiceStore(t)
|
s := newServiceStore(t)
|
||||||
repo := s.Repository
|
repo := s.Repository
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
package service
|
|
||||||
|
|
||||||
import "github.com/kazier/backend/models"
|
|
||||||
|
|
||||||
func (s *Services) CreateFolder(folderName string, destination *models.Document) (*models.Document, error) {
|
|
||||||
|
|
||||||
directory, err := models.NewDocument(folderName, models.DIRECTORY, 1)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return directory, nil
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
package service
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/kazier/backend/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (s *Services) MoveDocumentIntoDocument(from *models.Document, to *models.Document) error {
|
|
||||||
|
|
||||||
if from.UUID == nil {
|
|
||||||
return errors.New(models.ERROR_DOCUMENT_NOT_PERCISTED)
|
|
||||||
}
|
|
||||||
|
|
||||||
if to.Type != models.DIRECTORY {
|
|
||||||
return errors.New("the destination document must be a directory")
|
|
||||||
}
|
|
||||||
|
|
||||||
if canEdit, _ := UserCanEditDocument(s.ConnectedUser, from); !canEdit {
|
|
||||||
return errors.New("you cannot edit this folder")
|
|
||||||
}
|
|
||||||
|
|
||||||
if canEdit, _ := UserCanEditDocument(s.ConnectedUser, to); !canEdit {
|
|
||||||
return errors.New("you cannot edit this folder")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if document exist and move the document into document if is directory
|
|
||||||
|
|
||||||
fmt.Println("To implement move to ", to.UUID)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
package service
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/kazier/backend/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestMoveDocument(t *testing.T) {
|
|
||||||
|
|
||||||
userAntoine, err := models.NewUser("antoine")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("creating antoine user: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fakeUUID := "dsqdsq"
|
|
||||||
document, err := models.NewDocument("test.pdf", models.FILE, 1)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("creating document: %v", err)
|
|
||||||
}
|
|
||||||
document.UUID = &fakeUUID
|
|
||||||
|
|
||||||
folder, err := models.NewDocument("orga", models.DIRECTORY, 1)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("creating folder: %v", err)
|
|
||||||
}
|
|
||||||
folder.UUID = &fakeUUID
|
|
||||||
|
|
||||||
service := New(userAntoine)
|
|
||||||
|
|
||||||
if err := service.MoveDocumentIntoDocument(document, folder); err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMoveDocumentToAFile(t *testing.T) {
|
|
||||||
|
|
||||||
userAntoine, err := models.NewUser("antoine")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("creating antoine user: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
document, err := models.NewDocument("test.pdf", models.FILE, 1)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("creating document: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
notAFolder, err := models.NewDocument("orga", models.FILE, 1)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("creating file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
service := New(userAntoine)
|
|
||||||
|
|
||||||
err = service.MoveDocumentIntoDocument(document, notAFolder)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
+80
-12
@@ -3,8 +3,11 @@ package service
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -41,7 +44,29 @@ type Ocr struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewOcr(repo *repository.Repository, uploadDir, lang string, engine ocr.Engine) *Ocr {
|
func NewOcr(repo *repository.Repository, uploadDir, lang string, engine ocr.Engine) *Ocr {
|
||||||
return &Ocr{Repository: repo, UploadDir: uploadDir, Lang: lang, Engine: engine, StaleAfter: StaleClaimTimeout}
|
return &Ocr{Repository: repo, UploadDir: uploadDir, Lang: normalizeOcrLang(lang), Engine: engine, StaleAfter: StaleClaimTimeout}
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ocrLangPattern : whitelist OCR_LANG (A12) — lettres Tesseract + `+`
|
||||||
|
// uniquement (ex. `fra`, `eng`, `fra+eng`). Tout le reste (espaces,
|
||||||
|
// `;`, `-`, options `--...`) est une tentative d'injection `-l`.
|
||||||
|
ocrLangPattern = regexp.MustCompile(`^[a-z+]{2,20}$`)
|
||||||
|
// hexIDPattern : identifiants 32-hex (anti `..` dans physicalPath).
|
||||||
|
hexIDPattern = regexp.MustCompile(`^[0-9a-f]{32}$`)
|
||||||
|
)
|
||||||
|
|
||||||
|
// DefaultOcrLang est la langue Tesseract de repli (OCR_LANG, défaut fra+eng).
|
||||||
|
const DefaultOcrLang = "fra+eng"
|
||||||
|
|
||||||
|
// normalizeOcrLang applique la whitelist OCR_LANG : valeur invalide → repli
|
||||||
|
// sur le défaut avec log (jamais d'injection vers `tesseract -l`).
|
||||||
|
func normalizeOcrLang(lang string) string {
|
||||||
|
if ocrLangPattern.MatchString(lang) {
|
||||||
|
return lang
|
||||||
|
}
|
||||||
|
log.Printf("ocr: OCR_LANG %q invalide (whitelist ^[a-z+]{2,20}$), repli sur %q", lang, DefaultOcrLang)
|
||||||
|
return DefaultOcrLang
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create valide la ressource (ownership par user) et l'enfile uniquement
|
// Create valide la ressource (ownership par user) et l'enfile uniquement
|
||||||
@@ -110,44 +135,87 @@ func (o *Ocr) workerLoop(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// process exécute l'extraction pour un job déjà réclamé (`processing`).
|
// process exécute l'extraction pour un job déjà réclamé (`processing`).
|
||||||
|
// Ordre A13 : l'écriture `UpdateOcrText` est vérifiée AVANT le `Complete` —
|
||||||
|
// hôte supprimé entre-temps (0 ligne, ErrNotFound) → le job `Fail` au lieu
|
||||||
|
// de rester `done` avec un texte fantôme. Les erreurs `Fail`/`Complete`
|
||||||
|
// (conflit d'état : job requeue et repris par un autre worker) sont loggées,
|
||||||
|
// jamais silencieuses.
|
||||||
func (o *Ocr) process(ctx context.Context, row repository.OcrJobRow) {
|
func (o *Ocr) process(ctx context.Context, row repository.OcrJobRow) {
|
||||||
|
fail := func(message string) {
|
||||||
|
if err := o.Repository.OcrJobs.Fail(row.DeviceID, row.ID, message); err != nil {
|
||||||
|
log.Printf("ocr: fail job %s: %v", row.ID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
complete := func(text string) {
|
||||||
|
if err := o.Repository.OcrJobs.Complete(row.DeviceID, row.ID, text); err != nil {
|
||||||
|
log.Printf("ocr: complete job %s: %v", row.ID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
userID, err := o.Repository.Resources.GetResourceOwner(row.FileID)
|
userID, err := o.Repository.Resources.GetResourceOwner(row.FileID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = o.Repository.OcrJobs.Fail(row.DeviceID, row.ID, "file not readable")
|
fail("file not readable")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
path, err := o.physicalPath(userID, row.FileID)
|
path, err := o.physicalPath(userID, row.FileID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = o.Repository.OcrJobs.Fail(row.DeviceID, row.ID, "file not readable")
|
fail("file not readable")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
text, err := o.Engine.ExtractText(ctx, path, o.Lang)
|
text, err := o.Engine.ExtractText(ctx, path, o.Lang)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = o.Repository.OcrJobs.Fail(row.DeviceID, row.ID, err.Error())
|
fail(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_ = o.Repository.OcrJobs.Complete(row.DeviceID, row.ID, text)
|
|
||||||
// Le texte atterrit sur la ressource (updated_at bumpé) : consultable par
|
// Le texte atterrit sur la ressource (updated_at bumpé) : consultable par
|
||||||
// tous les devices du user et les grantees via GET /files/:id/ocr. SILENCIEUX
|
// tous les devices du user et les grantees via GET /files/:id/ocr.
|
||||||
// (hôte déjà supprimé ou lock d'écriture) : le job reste source d'historique.
|
|
||||||
if trimmed := strings.TrimSpace(text); trimmed != "" {
|
if trimmed := strings.TrimSpace(text); trimmed != "" {
|
||||||
if err := o.Repository.Resources.UpdateOcrText(userID, row.FileID, trimmed); err != nil {
|
if err := o.Repository.Resources.UpdateOcrText(userID, row.FileID, trimmed); err != nil {
|
||||||
log.Printf("ocr: persiste ocr_text de %s: %v", row.FileID, err)
|
if errors.Is(err, repository.ErrNotFound) {
|
||||||
|
fail("file not readable")
|
||||||
|
} else {
|
||||||
|
fail(err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
complete(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
// physicalPath résout UPLOAD_DIR/<user_id>/<resource_id>.<ext> — l'ext est
|
// physicalPath résout UPLOAD_DIR/<user_id>/<resource_id>.<ext> — l'ext est
|
||||||
// choisi à l'upload, le fichier réel est retrouvé par préfixe.
|
// choisi à l'upload, le fichier réel est retrouvé par préfixe. Durci (A12) :
|
||||||
|
// identifiants 32-hex uniquement (anti `..`), `Lstat` + fichier régulier
|
||||||
|
// (ni dossier, ni symlink brut), `EvalSymlinks` contenu dans UPLOAD_DIR
|
||||||
|
// (anti évasion par lien symbolique). Candidat rejeté → candidat suivant ;
|
||||||
|
// aucun candidat → ErrNotFound.
|
||||||
func (o *Ocr) physicalPath(userID, fileID string) (string, error) {
|
func (o *Ocr) physicalPath(userID, fileID string) (string, error) {
|
||||||
|
if !hexIDPattern.MatchString(userID) || !hexIDPattern.MatchString(fileID) {
|
||||||
|
return "", fmt.Errorf("physicalPath: identifiant non 32-hex")
|
||||||
|
}
|
||||||
|
base, err := filepath.EvalSymlinks(o.UploadDir)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("physicalPath: upload dir illisible: %w", err)
|
||||||
|
}
|
||||||
matches, err := filepath.Glob(filepath.Join(o.UploadDir, userID, fileID+".*"))
|
matches, err := filepath.Glob(filepath.Join(o.UploadDir, userID, fileID+".*"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
for _, m := range matches {
|
for _, m := range matches {
|
||||||
if strings.HasPrefix(filepath.Base(m), fileID+".") {
|
if !strings.HasPrefix(filepath.Base(m), fileID+".") {
|
||||||
return m, nil
|
continue
|
||||||
}
|
}
|
||||||
|
fi, err := os.Lstat(m)
|
||||||
|
if err != nil || !fi.Mode().IsRegular() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
resolved, err := filepath.EvalSymlinks(m)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
rel, err := filepath.Rel(base, resolved)
|
||||||
|
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return resolved, nil
|
||||||
}
|
}
|
||||||
return "", repository.ErrNotFound
|
return "", repository.ErrNotFound
|
||||||
}
|
}
|
||||||
@@ -161,4 +229,4 @@ func toOcrJobDTO(row repository.OcrJobRow) OcrJobDTO {
|
|||||||
dto.Error = *row.Error
|
dto.Error = *row.Error
|
||||||
}
|
}
|
||||||
return dto
|
return dto
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,6 +94,76 @@ func TestOcrPhysicalPath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOcrPhysicalPathRejectsNonHexIDs(t *testing.T) {
|
||||||
|
o := NewOcr(nil, t.TempDir(), "fra+eng", nil)
|
||||||
|
for _, tc := range [][2]string{
|
||||||
|
{"../evil", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"},
|
||||||
|
{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "../../etc/passwd.png"},
|
||||||
|
{"", ""},
|
||||||
|
} {
|
||||||
|
if _, err := o.physicalPath(tc[0], tc[1]); err == nil {
|
||||||
|
t.Errorf("ids %q/%q : attendu rejet non 32-hex", tc[0], tc[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOcrPhysicalPathRejectsSymlinkEscape(t *testing.T) {
|
||||||
|
uploadDir := t.TempDir()
|
||||||
|
userID := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||||
|
fileID := "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||||
|
outside := t.TempDir()
|
||||||
|
|
||||||
|
secret := filepath.Join(outside, "secret.png")
|
||||||
|
if err := os.WriteFile(secret, []byte("secret"), 0o644); err != nil {
|
||||||
|
t.Fatalf("write secret: %v", err)
|
||||||
|
}
|
||||||
|
userDir := filepath.Join(uploadDir, userID)
|
||||||
|
if err := os.MkdirAll(userDir, 0o755); err != nil {
|
||||||
|
t.Fatalf("mkdir: %v", err)
|
||||||
|
}
|
||||||
|
// Lien symbolique <fileID>.png → hors UPLOAD_DIR : doit être ignoré.
|
||||||
|
if err := os.Symlink(secret, filepath.Join(userDir, fileID+".png")); err != nil {
|
||||||
|
t.Fatalf("symlink: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
o := NewOcr(nil, uploadDir, "fra+eng", nil)
|
||||||
|
if _, err := o.physicalPath(userID, fileID); !errors.Is(err, repository.ErrNotFound) {
|
||||||
|
t.Errorf("symlink hors UPLOAD_DIR : attendu ErrNotFound, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOcrPhysicalPathRejectsDirectory(t *testing.T) {
|
||||||
|
uploadDir := t.TempDir()
|
||||||
|
userID := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||||
|
fileID := "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||||
|
userDir := filepath.Join(uploadDir, userID)
|
||||||
|
if err := os.MkdirAll(filepath.Join(userDir, fileID+".png"), 0o755); err != nil {
|
||||||
|
t.Fatalf("mkdir: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
o := NewOcr(nil, uploadDir, "fra+eng", nil)
|
||||||
|
if _, err := o.physicalPath(userID, fileID); !errors.Is(err, repository.ErrNotFound) {
|
||||||
|
t.Errorf("dossier à la place du fichier : attendu ErrNotFound, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNormalizeOcrLang(t *testing.T) {
|
||||||
|
for _, lang := range []string{"fra", "eng", "fra+eng"} {
|
||||||
|
if got := normalizeOcrLang(lang); got != lang {
|
||||||
|
t.Errorf("langue %q : attendue conservée, got %q", lang, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, lang := range []string{"", "e", "fra;id", "fra eng", "FRA", "--psm 6 fra"} {
|
||||||
|
if got := normalizeOcrLang(lang); got != DefaultOcrLang {
|
||||||
|
t.Errorf("langue %q : attendu repli %q, got %q", lang, DefaultOcrLang, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
o := NewOcr(nil, t.TempDir(), "fra;evil", nil)
|
||||||
|
if o.Lang != DefaultOcrLang {
|
||||||
|
t.Errorf("NewOcr langue hostile : attendu %q, got %q", DefaultOcrLang, o.Lang)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestOcrJobFailsWhenPhysicalFileMissing(t *testing.T) {
|
func TestOcrJobFailsWhenPhysicalFileMissing(t *testing.T) {
|
||||||
o, userID, deviceID, _ := newTestOcr(t, t.TempDir(), stubEngine{text: "x"})
|
o, userID, deviceID, _ := newTestOcr(t, t.TempDir(), stubEngine{text: "x"})
|
||||||
|
|
||||||
|
|||||||
+187
-21
@@ -4,9 +4,11 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -16,6 +18,10 @@ import (
|
|||||||
// ErrFileTooLarge signals an upload above MaxFileSize.
|
// ErrFileTooLarge signals an upload above MaxFileSize.
|
||||||
var ErrFileTooLarge = errors.New("file too large")
|
var ErrFileTooLarge = errors.New("file too large")
|
||||||
|
|
||||||
|
// ErrInvalidUpload signals a rejected upload (display name or path component).
|
||||||
|
// Handlers map it to 400 INVALID_REQUEST (never 500).
|
||||||
|
var ErrInvalidUpload = errors.New("invalid upload name")
|
||||||
|
|
||||||
// FileDTO serializes exactly as mobile/api/types.ts FileDto.
|
// FileDTO serializes exactly as mobile/api/types.ts FileDto.
|
||||||
type FileDTO struct {
|
type FileDTO struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
@@ -53,6 +59,66 @@ func NewResources(repo *repository.Repository, uploadDir string, maxFileSize int
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
extensionPattern = regexp.MustCompile(`^[a-z0-9]{1,10}$`)
|
||||||
|
idPattern = regexp.MustCompile(`^[0-9a-f]{32}$`)
|
||||||
|
)
|
||||||
|
|
||||||
|
// allowedMimeTypes whitelists client-declared Content-Types (parameters
|
||||||
|
// stripped, lowercased) ; anything absent falls back to
|
||||||
|
// application/octet-stream (cf. FEEDBACK A16).
|
||||||
|
var allowedMimeTypes = map[string]struct{}{
|
||||||
|
"application/octet-stream": {},
|
||||||
|
"application/pdf": {},
|
||||||
|
"application/json": {},
|
||||||
|
"application/zip": {},
|
||||||
|
"text/plain": {},
|
||||||
|
"text/csv": {},
|
||||||
|
"text/markdown": {},
|
||||||
|
"image/png": {},
|
||||||
|
"image/jpeg": {},
|
||||||
|
"image/gif": {},
|
||||||
|
"image/webp": {},
|
||||||
|
"audio/mpeg": {},
|
||||||
|
"video/mp4": {},
|
||||||
|
}
|
||||||
|
|
||||||
|
// sanitizeExtension normalise l'extension issue du filename client (cf.
|
||||||
|
// FEEDBACK #5) : lowercase, [a-z0-9]{1,10}, sinon "" (pas de suffixe).
|
||||||
|
func sanitizeExtension(filename string) string {
|
||||||
|
ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(filename), "."))
|
||||||
|
if !extensionPattern.MatchString(ext) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return ext
|
||||||
|
}
|
||||||
|
|
||||||
|
// sanitizeFileName nettoie le nom stocké (cf. FEEDBACK A16) : TrimSpace,
|
||||||
|
// cap 255, rejet vide / slash / backslash / retour ligne. Le nom n'est
|
||||||
|
// qu'une métadonnée (jamais un composant de chemin), mais on le verrouille
|
||||||
|
// quand même pour casser toute confusion downstream.
|
||||||
|
func sanitizeFileName(name string) (string, error) {
|
||||||
|
n := strings.TrimSpace(name)
|
||||||
|
if n == "" || len(n) > 255 || strings.ContainsAny(n, "/\\\x00\n\r") {
|
||||||
|
return "", ErrInvalidUpload
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func sanitizeMimeType(mime string) string {
|
||||||
|
m := strings.ToLower(strings.TrimSpace(mime))
|
||||||
|
if i := strings.IndexByte(m, ';'); i >= 0 {
|
||||||
|
m = strings.TrimSpace(m[:i])
|
||||||
|
}
|
||||||
|
if m == "" || len(m) > 128 {
|
||||||
|
return "application/octet-stream"
|
||||||
|
}
|
||||||
|
if _, ok := allowedMimeTypes[m]; !ok {
|
||||||
|
return "application/octet-stream"
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Resources) ListFiles(ownerID, folderID string, page, pageSize int, sort, order string) ([]FileDTO, int, error) {
|
func (s *Resources) ListFiles(ownerID, folderID string, page, pageSize int, sort, order string) ([]FileDTO, int, error) {
|
||||||
rows, total, err := s.Repo.ListFilesVisible(ownerID, folderID, pageSize, (page-1)*pageSize, sort, order)
|
rows, total, err := s.Repo.ListFilesVisible(ownerID, folderID, pageSize, (page-1)*pageSize, sort, order)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -132,66 +198,166 @@ func (s *Resources) SearchFiles(ownerID, q string, page, pageSize int) ([]FileDT
|
|||||||
// rename, never a move, never a duplicate row. Unknown / foreign resource →
|
// rename, never a move, never a duplicate row. Unknown / foreign resource →
|
||||||
// repo.ErrNotFound.
|
// repo.ErrNotFound.
|
||||||
func (s *Resources) Upload(ownerID string, file *multipart.FileHeader, folderID, resourceID string) (FileDTO, error) {
|
func (s *Resources) Upload(ownerID string, file *multipart.FileHeader, folderID, resourceID string) (FileDTO, error) {
|
||||||
if file.Size > s.MaxFileSize {
|
// Garde anti-traversal : seuls des IDs 32-hex (ou générés) deviennent des
|
||||||
return FileDTO{}, ErrFileTooLarge
|
// composants de chemin. ownerID vient de l'auth mais on ne lui fait pas
|
||||||
|
// confiance aveuglément ; resourceID fourni par le client est déjà
|
||||||
|
// pré-validé côté handler (404), ici on rejete en NotFound/400 pareil.
|
||||||
|
if strings.ContainsAny(ownerID, "/\\\x00\n\r") || strings.Contains(ownerID, "..") {
|
||||||
|
return FileDTO{}, ErrInvalidUpload
|
||||||
}
|
}
|
||||||
|
|
||||||
targetExisting := resourceID != ""
|
targetExisting := resourceID != ""
|
||||||
if targetExisting {
|
if targetExisting {
|
||||||
|
if !idPattern.MatchString(resourceID) {
|
||||||
|
return FileDTO{}, repository.ErrNotFound
|
||||||
|
}
|
||||||
if _, err := s.Repo.GetFile(ownerID, resourceID); err != nil {
|
if _, err := s.Repo.GetFile(ownerID, resourceID); err != nil {
|
||||||
return FileDTO{}, err
|
return FileDTO{}, err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
resourceID = repository.NewID()
|
var err error
|
||||||
|
resourceID, err = repository.NewIDErr()
|
||||||
|
if err != nil {
|
||||||
|
return FileDTO{}, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension := strings.TrimPrefix(filepath.Ext(file.Filename), ".")
|
name, err := sanitizeFileName(file.Filename)
|
||||||
|
if err != nil {
|
||||||
|
return FileDTO{}, err
|
||||||
|
}
|
||||||
|
extension := sanitizeExtension(file.Filename)
|
||||||
destDir := filepath.Join(s.UploadDir, ownerID)
|
destDir := filepath.Join(s.UploadDir, ownerID)
|
||||||
if err := os.MkdirAll(destDir, 0o755); err != nil {
|
if err := os.MkdirAll(destDir, 0o700); err != nil {
|
||||||
return FileDTO{}, fmt.Errorf("create upload dir: %w", err)
|
return FileDTO{}, fmt.Errorf("create upload dir: %w", err)
|
||||||
}
|
}
|
||||||
|
// MkdirAll respecte umask et ne touche pas aux dirs préexistants : on
|
||||||
|
// enforce 0700 explicitement (cf. FEEDBACK A4).
|
||||||
|
if err := os.Chmod(destDir, 0o700); err != nil {
|
||||||
|
return FileDTO{}, fmt.Errorf("chmod upload dir: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
destPath := filepath.Join(destDir, resourceID+"."+extension)
|
destName := resourceID
|
||||||
if err := copyMultipart(file, destPath); err != nil {
|
if extension != "" {
|
||||||
|
destName = resourceID + "." + extension
|
||||||
|
}
|
||||||
|
destPath := filepath.Join(destDir, destName)
|
||||||
|
|
||||||
|
// Mémorise les anciens physiques pour cleanup post-succès (changement d'ext).
|
||||||
|
// Note : Glob(id.*) ne matche pas le fichier sans extension, on traite le
|
||||||
|
// chemin nu séparément (cf. FEEDBACK #6).
|
||||||
|
oldMatches, _ := filepath.Glob(filepath.Join(destDir, resourceID+".*"))
|
||||||
|
barePath := filepath.Join(destDir, resourceID)
|
||||||
|
if barePath != destPath {
|
||||||
|
if _, err := os.Stat(barePath); err == nil {
|
||||||
|
oldMatches = append(oldMatches, barePath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
written, err := copyMultipartAtomic(file, destDir, destPath, s.MaxFileSize)
|
||||||
|
if err != nil {
|
||||||
return FileDTO{}, err
|
return FileDTO{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
mimeType := file.Header.Get("Content-Type")
|
mimeType := sanitizeMimeType(file.Header.Get("Content-Type"))
|
||||||
var err error
|
var extPtr *string
|
||||||
|
if extension != "" {
|
||||||
|
extPtr = &extension
|
||||||
|
}
|
||||||
if targetExisting {
|
if targetExisting {
|
||||||
err = s.Repo.UpdatePhysical(ownerID, resourceID, file.Size, &mimeType, &extension)
|
err = s.Repo.UpdatePhysical(ownerID, resourceID, written, &mimeType, extPtr)
|
||||||
} else {
|
} else {
|
||||||
err = s.Repo.InsertFile(ownerID, resourceID, file.Filename, folderID, file.Size, &mimeType, &extension)
|
err = s.Repo.InsertFile(ownerID, resourceID, name, folderID, written, &mimeType, extPtr)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = os.Remove(destPath)
|
// Best-effort : supprime le nouveau fichier sauf s'il préexistait à l'identique.
|
||||||
|
stillOld := false
|
||||||
|
for _, m := range oldMatches {
|
||||||
|
if m == destPath {
|
||||||
|
stillOld = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !stillOld {
|
||||||
|
_ = os.Remove(destPath)
|
||||||
|
}
|
||||||
return FileDTO{}, err
|
return FileDTO{}, err
|
||||||
}
|
}
|
||||||
|
// Succès : supprime les anciens suffixes orphelins (stale bytes, cf. #6).
|
||||||
|
for _, m := range oldMatches {
|
||||||
|
if m != destPath && strings.HasPrefix(filepath.Base(m), resourceID+".") {
|
||||||
|
_ = os.Remove(m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
row, err := s.Repo.GetFile(ownerID, resourceID)
|
row, err := s.Repo.GetFile(ownerID, resourceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// Lecture post-insert en échec : au moins logguer (cf. FEEDBACK #6).
|
||||||
|
// Sur création on tente en plus de retirer le fichier pour ne pas
|
||||||
|
// laisser d'orphelin (le client retry → NAME_CONFLICT → re-upload
|
||||||
|
// ciblé). Sur re-upload ciblé on ne touche à rien (données existantes).
|
||||||
|
log.Printf("upload: GetFile(%s) failed after persist: %v", resourceID, err)
|
||||||
|
if !targetExisting {
|
||||||
|
_ = os.Remove(destPath)
|
||||||
|
}
|
||||||
return FileDTO{}, err
|
return FileDTO{}, err
|
||||||
}
|
}
|
||||||
return toFileDTO(row), nil
|
return toFileDTO(row), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyMultipart(file *multipart.FileHeader, destPath string) error {
|
// copyMultipartAtomic écrit vers un temp dans destDir (0600) avec limite
|
||||||
|
// stricte maxSize+1 (header file.Size non fiable, cf. A2), fsync + rename
|
||||||
|
// atomique (cf. A3). Retourne les octets réellement écrits.
|
||||||
|
func copyMultipartAtomic(file *multipart.FileHeader, destDir, destPath string, maxSize int64) (int64, error) {
|
||||||
src, err := file.Open()
|
src, err := file.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("open multipart file: %w", err)
|
return 0, fmt.Errorf("open multipart file: %w", err)
|
||||||
}
|
}
|
||||||
defer src.Close()
|
defer src.Close()
|
||||||
|
|
||||||
dst, err := os.Create(destPath)
|
// Temp dans le même répertoire (même filesystem → rename atomique),
|
||||||
|
// pattern .tmp-<base>-* (cf. FEEDBACK A3). Jamais d'overwrite direct.
|
||||||
|
tmp, err := os.CreateTemp(destDir, ".tmp-"+filepath.Base(destPath)+".*")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("create file: %w", err)
|
return 0, fmt.Errorf("create tmp file: %w", err)
|
||||||
|
}
|
||||||
|
tmpName := tmp.Name()
|
||||||
|
// En cas d'échec avant rename, nettoyer le temp.
|
||||||
|
success := false
|
||||||
|
defer func() {
|
||||||
|
if !success {
|
||||||
|
_ = os.Remove(tmpName)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
if err := tmp.Chmod(0o600); err != nil {
|
||||||
|
tmp.Close()
|
||||||
|
return 0, fmt.Errorf("chmod tmp file: %w", err)
|
||||||
}
|
}
|
||||||
defer dst.Close()
|
|
||||||
|
|
||||||
if _, err := io.Copy(dst, src); err != nil {
|
limited := io.LimitReader(src, maxSize+1)
|
||||||
return fmt.Errorf("copy upload: %w", err)
|
written, err := io.Copy(tmp, limited)
|
||||||
|
if err != nil {
|
||||||
|
tmp.Close()
|
||||||
|
return 0, fmt.Errorf("copy upload: %w", err)
|
||||||
}
|
}
|
||||||
return nil
|
if written > maxSize {
|
||||||
|
tmp.Close()
|
||||||
|
return 0, ErrFileTooLarge
|
||||||
|
}
|
||||||
|
if err := tmp.Sync(); err != nil {
|
||||||
|
tmp.Close()
|
||||||
|
return 0, fmt.Errorf("fsync upload: %w", err)
|
||||||
|
}
|
||||||
|
if err := tmp.Close(); err != nil {
|
||||||
|
return 0, fmt.Errorf("close upload: %w", err)
|
||||||
|
}
|
||||||
|
if err := os.Rename(tmpName, destPath); err != nil {
|
||||||
|
return 0, fmt.Errorf("commit upload: %w", err)
|
||||||
|
}
|
||||||
|
if err := os.Chmod(destPath, 0o600); err != nil {
|
||||||
|
return 0, fmt.Errorf("chmod upload: %w", err)
|
||||||
|
}
|
||||||
|
success = true
|
||||||
|
return written, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func toFileDTO(row repository.FileRow) FileDTO {
|
func toFileDTO(row repository.FileRow) FileDTO {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/kazier/backend/repository"
|
"github.com/kazier/backend/repository"
|
||||||
@@ -162,3 +163,204 @@ func TestUploadTargetUnknownIsNotFound(t *testing.T) {
|
|||||||
t.Fatalf("upload inconnu : attendu ErrNotFound, got %v", err)
|
t.Fatalf("upload inconnu : attendu ErrNotFound, got %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A2 : le header multipart Size est déclaratif — seul le comptage réel des
|
||||||
|
// octets (LimitReader MaxFileSize+1) fait foi.
|
||||||
|
func TestUploadEnforcesActualSizeDespiteLyingHeader(t *testing.T) {
|
||||||
|
s := newServiceStore(t) // MaxFileSize = 100
|
||||||
|
userID := mustCreateUser(t, s.Repository, "upload-lying")
|
||||||
|
|
||||||
|
fh := multipartFileHeader(t, "big.txt", bytes.Repeat([]byte("x"), 150))
|
||||||
|
fh.Size = 10 // header menteur : annonce 10 alors que 150 arrivent
|
||||||
|
if _, err := s.Upload(userID, fh, "", ""); !errors.Is(err, ErrFileTooLarge) {
|
||||||
|
t.Fatalf("header menteur : attendu ErrFileTooLarge, got %v", err)
|
||||||
|
}
|
||||||
|
// Aucun résidu physique (ni fichier final, ni .tmp-* orphelin).
|
||||||
|
if entries, err := os.ReadDir(filepath.Join(s.UploadDir, userID)); err == nil && len(entries) != 0 {
|
||||||
|
names := make([]string, 0, len(entries))
|
||||||
|
for _, e := range entries {
|
||||||
|
names = append(names, e.Name())
|
||||||
|
}
|
||||||
|
t.Errorf("résidu physique après refus : %v", names)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// #5 : extension sanitizée (lowercase [a-z0-9]{1,10}), pas de "id." si vide.
|
||||||
|
func TestUploadSanitizesExtension(t *testing.T) {
|
||||||
|
s := newServiceStore(t)
|
||||||
|
userID := mustCreateUser(t, s.Repository, "upload-ext")
|
||||||
|
|
||||||
|
// Uppercase → lowercase.
|
||||||
|
dto, err := s.Upload(userID, multipartFileHeader(t, "PHOTO.JPG", []byte("data")), "", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("upload uppercase ext: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(s.UploadDir, userID, dto.ID+".jpg")); err != nil {
|
||||||
|
t.Errorf("fichier .jpg (lowercase) attendu: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pas d'extension → fichier nu, sans trailing dot.
|
||||||
|
dto2, err := s.Upload(userID, multipartFileHeader(t, "README", []byte("data")), "", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("upload sans ext: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(s.UploadDir, userID, dto2.ID)); err != nil {
|
||||||
|
t.Errorf("fichier sans extension attendu: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(s.UploadDir, userID, dto2.ID+".")); !os.IsNotExist(err) {
|
||||||
|
t.Errorf("trailing dot interdit (id.) : err=%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extension trop longue / non alnum → "" (fichier nu).
|
||||||
|
dto3, err := s.Upload(userID, multipartFileHeader(t, "run.verylongextensiontoolong", []byte("data")), "", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("upload ext invalide: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(s.UploadDir, userID, dto3.ID)); err != nil {
|
||||||
|
t.Errorf("ext invalide → fichier nu attendu: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Backslash survit au round-trip multipart stdlib → rejet prouvé de bout
|
||||||
|
// en bout (les autres cas slash/NUL/retour-ligne sont couverts en
|
||||||
|
// unitaire sur sanitizeFileName, la stdlib Go les neutralisant déjà).
|
||||||
|
if _, err := s.Upload(userID, multipartFileHeader(t, `a\b.txt`, []byte("data")), "", ""); !errors.Is(err, ErrInvalidUpload) {
|
||||||
|
t.Errorf(`nom %q : attendu ErrInvalidUpload, got %v`, `a\b.txt`, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// #5/A16 : sanitizeurs unitaires — un client non-Go peut envoyer un filename
|
||||||
|
// brut (slash, .., NUL, retour-ligne) que la stdlib Go ne neutraliserait pas
|
||||||
|
// côté lecture serveur.
|
||||||
|
func TestSanitizeFileName(t *testing.T) {
|
||||||
|
for _, bad := range []string{"", " ", "../evil.txt", "a/b.txt", "a\x00b.txt", "bad\nname.txt", "bad\rname.txt", strings.Repeat("a", 256)} {
|
||||||
|
if _, err := sanitizeFileName(bad); !errors.Is(err, ErrInvalidUpload) {
|
||||||
|
t.Errorf("sanitizeFileName(%q) : attendu ErrInvalidUpload, got %v", bad, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, good := range []string{"docs.txt", " pad.txt ", "vacances-août.jpg", "a b c.pdf"} {
|
||||||
|
got, err := sanitizeFileName(good)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("sanitizeFileName(%q) : rejet inattendu: %v", good, err)
|
||||||
|
} else if got != strings.TrimSpace(good) {
|
||||||
|
t.Errorf("sanitizeFileName(%q) = %q, attendu trim", good, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSanitizeExtension(t *testing.T) {
|
||||||
|
cases := map[string]string{
|
||||||
|
"photo.JPG": "jpg",
|
||||||
|
"archive.tar.gz": "gz",
|
||||||
|
"README": "",
|
||||||
|
"run.verylongextensiontoolong": "",
|
||||||
|
"evil.php/../../x": "",
|
||||||
|
"a.b/c": "",
|
||||||
|
"weird.HTM\nL": "",
|
||||||
|
"dot.": "",
|
||||||
|
}
|
||||||
|
for in, want := range cases {
|
||||||
|
if got := sanitizeExtension(in); got != want {
|
||||||
|
t.Errorf("sanitizeExtension(%q) = %q, attendu %q", in, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// #6 : un re-upload ciblé avec une nouvelle extension ne laisse qu'un seul
|
||||||
|
// fichier physique (l'ancien suffixe est supprimé).
|
||||||
|
func TestUploadReuploadNewExtensionRemovesStaleFile(t *testing.T) {
|
||||||
|
s := newServiceStore(t)
|
||||||
|
userID := mustCreateUser(t, s.Repository, "upload-reext")
|
||||||
|
|
||||||
|
resourceID := repository.NewID()
|
||||||
|
png := "png"
|
||||||
|
if err := s.Repository.Resources.InsertFile(userID, resourceID, "scan.png", "", 0, nil, &png); err != nil {
|
||||||
|
t.Fatalf("insert metadata-only: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := s.Upload(userID, multipartFileHeader(t, "scan.png", []byte("PNGDATA")), "", resourceID); err != nil {
|
||||||
|
t.Fatalf("premier upload ciblé: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dto, err := s.Upload(userID, multipartFileHeader(t, "scan.jpg", []byte("JPGDATA!")), "", resourceID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("re-upload nouvelle ext: %v", err)
|
||||||
|
}
|
||||||
|
if dto.ID != resourceID {
|
||||||
|
t.Fatalf("id attendu %s, got %s", resourceID, dto.ID)
|
||||||
|
}
|
||||||
|
entries, err := os.ReadDir(filepath.Join(s.UploadDir, userID))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("readdir: %v", err)
|
||||||
|
}
|
||||||
|
if len(entries) != 1 || entries[0].Name() != resourceID+".jpg" {
|
||||||
|
names := make([]string, 0, len(entries))
|
||||||
|
for _, e := range entries {
|
||||||
|
names = append(names, e.Name())
|
||||||
|
}
|
||||||
|
t.Errorf("un seul physique attendu (%s.jpg), got %v", resourceID, names)
|
||||||
|
}
|
||||||
|
content, err := os.ReadFile(filepath.Join(s.UploadDir, userID, resourceID+".jpg"))
|
||||||
|
if err != nil || string(content) != "JPGDATA!" {
|
||||||
|
t.Errorf("contenu = %q, err=%v (attendu JPGDATA!)", content, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A4 : répertoire 0700, fichier 0600 (enforcés malgré umask).
|
||||||
|
func TestUploadFileAndDirPermissions(t *testing.T) {
|
||||||
|
s := newServiceStore(t)
|
||||||
|
userID := mustCreateUser(t, s.Repository, "upload-perms")
|
||||||
|
|
||||||
|
dto, err := s.Upload(userID, multipartFileHeader(t, "secret.txt", []byte("data")), "", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("upload: %v", err)
|
||||||
|
}
|
||||||
|
dirInfo, err := os.Stat(filepath.Join(s.UploadDir, userID))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("stat dir: %v", err)
|
||||||
|
}
|
||||||
|
if perm := dirInfo.Mode().Perm(); perm != 0o700 {
|
||||||
|
t.Errorf("perms dossier = %04o, attendu 0700", perm)
|
||||||
|
}
|
||||||
|
fileInfo, err := os.Stat(filepath.Join(s.UploadDir, userID, dto.ID+".txt"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("stat file: %v", err)
|
||||||
|
}
|
||||||
|
if perm := fileInfo.Mode().Perm(); perm != 0o600 {
|
||||||
|
t.Errorf("perms fichier = %04o, attendu 0600", perm)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A16 : Content-Type hors whitelist → fallback application/octet-stream.
|
||||||
|
func TestUploadMimeTypeWhitelistFallback(t *testing.T) {
|
||||||
|
s := newServiceStore(t)
|
||||||
|
userID := mustCreateUser(t, s.Repository, "upload-mime")
|
||||||
|
|
||||||
|
evil := multipartFileHeader(t, "evil.bin", []byte("data"))
|
||||||
|
evil.Header.Set("Content-Type", "application/x-evil-tool")
|
||||||
|
dto, err := s.Upload(userID, evil, "", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("upload evil mime: %v", err)
|
||||||
|
}
|
||||||
|
if dto.MimeType != "application/octet-stream" {
|
||||||
|
t.Errorf("mime inattendu = %q, attendu fallback application/octet-stream", dto.MimeType)
|
||||||
|
}
|
||||||
|
|
||||||
|
withParams := multipartFileHeader(t, "page.bin", []byte("data"))
|
||||||
|
withParams.Header.Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
dto, err = s.Upload(userID, withParams, "", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("upload html mime: %v", err)
|
||||||
|
}
|
||||||
|
if dto.MimeType != "application/octet-stream" {
|
||||||
|
t.Errorf("text/html non whitelisté, fallback attendu, got %q", dto.MimeType)
|
||||||
|
}
|
||||||
|
|
||||||
|
allowed := multipartFileHeader(t, "img.bin", []byte("data"))
|
||||||
|
allowed.Header.Set("Content-Type", "image/png")
|
||||||
|
dto, err = s.Upload(userID, allowed, "", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("upload png mime: %v", err)
|
||||||
|
}
|
||||||
|
if dto.MimeType != "image/png" {
|
||||||
|
t.Errorf("mime whitelisté perdu, got %q", dto.MimeType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,31 +1,6 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import "github.com/kazier/backend/models"
|
// Stubs historiques UserCan* (toujours-true) supprimés — LOT 5 #11.
|
||||||
|
// L'autorisation V1 est portée par le scoping owner (`user_id`) dans
|
||||||
type Services struct {
|
// repository.Resources + les grants `shares` (cf. docs/api-v1.md) :
|
||||||
ConnectedUser *models.User
|
// aucune fonction toujours-true ne doit subsister ici.
|
||||||
}
|
|
||||||
|
|
||||||
func New(connectedUser *models.User) *Services {
|
|
||||||
|
|
||||||
return &Services{
|
|
||||||
ConnectedUser: connectedUser,
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func UserCanReadDocument(connectedUser *models.User, document *models.Document) (bool, string) {
|
|
||||||
return true, ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func UserCanEditDocument(connectedUser *models.User, document *models.Document) (bool, string) {
|
|
||||||
return true, ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func UserCanDeleteDocument(connectedUser *models.User, document *models.Document) (bool, string) {
|
|
||||||
return true, ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func UserCanCreateDocument(connectedUser *models.User, document *models.Document) (bool, string) {
|
|
||||||
return true, ""
|
|
||||||
}
|
|
||||||
|
|||||||
+168
-35
@@ -3,6 +3,7 @@ package service
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -92,33 +93,86 @@ func classifySyncError(err error) (string, string) {
|
|||||||
return "NOT_FOUND", "target resource or folder not found"
|
return "NOT_FOUND", "target resource or folder not found"
|
||||||
case errors.Is(err, repository.ErrGranteeNotFound):
|
case errors.Is(err, repository.ErrGranteeNotFound):
|
||||||
return "GRANTEE_NOT_FOUND", "the specified user does not exist"
|
return "GRANTEE_NOT_FOUND", "the specified user does not exist"
|
||||||
|
case errors.Is(err, repository.ErrPayloadMismatch):
|
||||||
|
return "INVALID_REQUEST", "operation replayed with different payload"
|
||||||
|
case errors.Is(err, errCycle):
|
||||||
|
return "INVALID_REQUEST", "cannot move a resource into its own subtree"
|
||||||
default:
|
default:
|
||||||
return "INVALID_REQUEST", err.Error()
|
// Volontairement générique : ne pas fuir SQL/FS internes (cf. #8).
|
||||||
|
// Le détail reste côté serveur (logs), le client reçoit un code stable.
|
||||||
|
log.Printf("sync: internal apply error: %v", err)
|
||||||
|
return "INVALID_REQUEST", "invalid operation"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var errCycle = errors.New("cannot move a resource into its own subtree")
|
||||||
|
|
||||||
// ApplyBatch applique les ops de l'outbox : les mutations de ressources sont
|
// ApplyBatch applique les ops de l'outbox : les mutations de ressources sont
|
||||||
// scopées par l'UTILISATEUR (userID), l'idempotence reste par DEVICE
|
// scopées par l'UTILISATEUR (userID), l'idempotence reste par DEVICE
|
||||||
// (deviceID) — cf. docs/api-v1.md §6.1.
|
// (deviceID) — cf. docs/api-v1.md §6.1.
|
||||||
|
//
|
||||||
|
// Atomicité par op (#7) : chaque op est sérialisée par un claim atomique sur
|
||||||
|
// la trace outbox (INSERT ... ON CONFLICT DO NOTHING portant la contrainte
|
||||||
|
// UNIQUE(device_id, operation_id), cf. Operations.Claim) — deux batches
|
||||||
|
// concurrents ne peuvent pas appliquer deux fois la même op : le perdant
|
||||||
|
// suit le chemin rejeu (comparaison de hash, cf. A9). Le claim est posé en
|
||||||
|
// status='failed' AVANT l'apply puis promu en 'applied' (MarkApplied) une
|
||||||
|
// fois l'effet durable : un crash entre les deux laisse une trace rejouable
|
||||||
|
// (ré-apply à payload identique), jamais un skip silencieux.
|
||||||
|
//
|
||||||
|
// Limite documentée : claim→apply→promote ne sont pas une transaction SQL
|
||||||
|
// unique — apply touche resources/shares via des statements atomiques
|
||||||
|
// (INSERT ... SELECT parent, TouchOwned). Le TOCTOU résiduel (deux owners
|
||||||
|
// créant le même resource_id) dégénère en NAME_CONFLICT explicite, jamais
|
||||||
|
// en double écriture silencieuse. Une atomicité totale exigerait un
|
||||||
|
// transaction manager transverse aux repos (vrai Tx ApplyBatch, reportée —
|
||||||
|
// cf. FEEDBACK risques résiduels).
|
||||||
|
//
|
||||||
|
// Erreurs de persistance (claim/promote) : retournées comme applied:i
|
||||||
|
// explicite avec code INTERNAL, jamais en erreur Go nue (500 nu) — le client
|
||||||
|
// sait exactement où reprendre.
|
||||||
func (s *Resources) ApplyBatch(userID, deviceID string, ops []SyncOperation) (SyncResult, error) {
|
func (s *Resources) ApplyBatch(userID, deviceID string, ops []SyncOperation) (SyncResult, error) {
|
||||||
for i := range ops {
|
for i := range ops {
|
||||||
op := &ops[i]
|
op := &ops[i]
|
||||||
if err := validateSyncOp(op); err != nil {
|
if err := validateSyncOp(op); err != nil {
|
||||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INVALID_REQUEST", Message: err.Error()}}, nil
|
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INVALID_REQUEST", Message: err.Error()}}, nil
|
||||||
}
|
}
|
||||||
already, err := s.Repository.Operations.Applied(deviceID, op.OperationID)
|
claimed, err := s.Repository.Operations.Claim(deviceID, op.OperationID, op.Operation, derefString(op.RefType), nullableInt64(derefInt64(op.RefID)), derefString(op.ResourceID), op.Payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return SyncResult{}, err
|
// Faute client (clé ou payload malformé) → INVALID_REQUEST halt,
|
||||||
|
// message statique sûr ; panne persistance → INTERNAL explicite.
|
||||||
|
if errors.Is(err, repository.ErrInvalidOperationID) {
|
||||||
|
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INVALID_REQUEST", Message: "operation_id must be 32 lowercase hex chars"}}, nil
|
||||||
|
}
|
||||||
|
if errors.Is(err, repository.ErrInvalidPayload) {
|
||||||
|
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INVALID_REQUEST", Message: "invalid payload JSON"}}, nil
|
||||||
|
}
|
||||||
|
log.Printf("sync: claim device=%s op=%s: %v", deviceID, op.OperationID, err)
|
||||||
|
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INTERNAL", Message: "failed to persist operation state"}}, nil
|
||||||
}
|
}
|
||||||
if already {
|
if !claimed {
|
||||||
continue
|
// Rejeu : même (device, op) déjà tracé — comparer (A9).
|
||||||
|
stored, err := s.Repository.Operations.Get(deviceID, op.OperationID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("sync: replay read device=%s op=%s: %v", deviceID, op.OperationID, err)
|
||||||
|
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INTERNAL", Message: "failed to read operation state"}}, nil
|
||||||
|
}
|
||||||
|
if err := stored.MatchesReplay(op.Operation, derefString(op.ResourceID), op.Payload); err != nil {
|
||||||
|
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INVALID_REQUEST", Message: "operation replayed with different payload"}}, nil
|
||||||
|
}
|
||||||
|
if stored.Status == repository.OpStatusApplied {
|
||||||
|
continue // déjà appliquée à l'identique → skip idempotent
|
||||||
|
}
|
||||||
|
// Trace 'failed' à payload identique : la première tentative est
|
||||||
|
// morte avant MarkApplied (crash) → on ré-applique (recovery).
|
||||||
}
|
}
|
||||||
if err := s.applySyncOp(userID, op); err != nil {
|
if err := s.applySyncOp(userID, op); err != nil {
|
||||||
code, message := classifySyncError(err)
|
code, message := classifySyncError(err)
|
||||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: code, Message: message}}, nil
|
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: code, Message: message}}, nil
|
||||||
}
|
}
|
||||||
if err := s.recordApplied(deviceID, op); err != nil {
|
if err := s.Repository.Operations.MarkApplied(deviceID, op.OperationID); err != nil {
|
||||||
return SyncResult{}, err
|
log.Printf("sync: promote device=%s op=%s: %v", deviceID, op.OperationID, err)
|
||||||
|
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INTERNAL", Message: "operation applied but trace update failed; safe to retry"}}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return SyncResult{Applied: len(ops)}, nil
|
return SyncResult{Applied: len(ops)}, nil
|
||||||
@@ -167,29 +221,47 @@ func (s *Resources) applySyncOp(ownerID string, op *SyncOperation) error {
|
|||||||
// Le parent doit exister et appartenir à l'utilisateur (NOT_FOUND sinon,
|
// Le parent doit exister et appartenir à l'utilisateur (NOT_FOUND sinon,
|
||||||
// cohérent avec move_resource). Ordre du SAF walk : parent avant enfant.
|
// cohérent avec move_resource). Ordre du SAF walk : parent avant enfant.
|
||||||
if p.ParentResourceID != "" {
|
if p.ParentResourceID != "" {
|
||||||
|
if !resourceIDPattern.MatchString(p.ParentResourceID) {
|
||||||
|
return errors.New("payload.parentResourceId must be 32 lowercase hex chars")
|
||||||
|
}
|
||||||
if _, err := s.Repo.GetFolder(ownerID, p.ParentResourceID); err != nil {
|
if _, err := s.Repo.GetFolder(ownerID, p.ParentResourceID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
var insertErr error
|
||||||
if derefString(op.ResourceType) == "folder" {
|
if derefString(op.ResourceType) == "folder" {
|
||||||
return s.Repo.InsertFolder(ownerID, resourceID, p.Name, p.ParentResourceID)
|
insertErr = s.Repo.InsertFolder(ownerID, resourceID, p.Name, p.ParentResourceID)
|
||||||
|
} else {
|
||||||
|
mime := p.MimeType
|
||||||
|
insertErr = s.Repo.InsertFile(ownerID, resourceID, p.Name, p.ParentResourceID, 0, &mime, nullableString(p.Extension))
|
||||||
}
|
}
|
||||||
mime := p.MimeType
|
if insertErr != nil {
|
||||||
return s.Repo.InsertFile(ownerID, resourceID, p.Name, p.ParentResourceID, 0, &mime, nullableString(p.Extension))
|
// Course ExistsOwner→Insert (concurrent ou rejeu croisé) : si la
|
||||||
|
// ressource est désormais owned, l'effet est atteint → idempotent.
|
||||||
|
// Sinon NAME_CONFLICT explicite (sibling ou resource_id d'autrui).
|
||||||
|
// Atomicité totale (INSERT ... SELECT) : inter-lots, LOT 4.
|
||||||
|
if errors.Is(insertErr, repository.ErrNameConflict) {
|
||||||
|
if owned, err := s.Repo.ExistsOwner(ownerID, resourceID); err == nil && owned {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return insertErr
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
|
||||||
case OpUpdateMetadata:
|
case OpUpdateMetadata:
|
||||||
// Owner can always rename. A shared resource can be renamed by an
|
// Sémantique uniforme (A8) : toute ressource que l'appelant ne peut
|
||||||
// editor+ (docs §6.2 effective_access ranking). Below editor, or on
|
// pas renommer — absente, d'autrui, ou partagée sous editor — halte
|
||||||
// a resource the caller cannot reach at all, we keep the documented
|
// en NOT_FOUND. Pas de no-op silencieux : un rename qui n'a rien
|
||||||
// terminal-state semantics: absent → no-op.
|
// renommé est un bug client ou une désync, le halt l'expose au lieu
|
||||||
|
// de valider un état qui n'existe pas. Le même code NOT_FOUND dans
|
||||||
|
// tous les cas évite tout oracle d'existence. Owner → rank owner,
|
||||||
|
// editor+ partagé → autorisé (docs §6.2).
|
||||||
access, err := s.Repository.Shares.EffectiveAccess(ownerID, resourceID)
|
access, err := s.Repository.Shares.EffectiveAccess(ownerID, resourceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if access.EffectiveRank < repository.AccessEditor {
|
if access.EffectiveRank < repository.AccessEditor {
|
||||||
if !access.Accessible {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return repository.ErrNotFound
|
return repository.ErrNotFound
|
||||||
}
|
}
|
||||||
var p renamePayload
|
var p renamePayload
|
||||||
@@ -199,16 +271,17 @@ func (s *Resources) applySyncOp(ownerID string, op *SyncOperation) error {
|
|||||||
if p.Name == "" {
|
if p.Name == "" {
|
||||||
return errors.New("payload.name required")
|
return errors.New("payload.name required")
|
||||||
}
|
}
|
||||||
return s.Repo.UpdateNameByID(resourceID, p.Name)
|
// NOTE : UpdateNameByID (non-scopé) est volontaire ici — un editor
|
||||||
|
// non-owner peut renommer (EffectiveAccess vérifié plus haut), donc
|
||||||
|
// UpdateNameOwned(ownerID, ...) rejetterait à tort. La garde
|
||||||
|
// d'autorisation précède immédiatement l'écriture.
|
||||||
|
return s.Repo.UpdateNameByID(resourceID, p.Name)
|
||||||
|
|
||||||
case OpMoveResource:
|
case OpMoveResource:
|
||||||
exists, err := s.Repo.ExistsOwner(ownerID, resourceID)
|
// A8 : move owner-only. Source non-owned (absente OU d'autrui) →
|
||||||
if err != nil {
|
// NOT_FOUND halt (MoveResource ne touche que les lignes owned).
|
||||||
return err
|
// Pas de applied silencieux : un move qui n'a rien déplacé
|
||||||
}
|
// désynchroniserait le client.
|
||||||
if !exists {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
var p movePayload
|
var p movePayload
|
||||||
if err := json.Unmarshal(op.Payload, &p); err != nil {
|
if err := json.Unmarshal(op.Payload, &p); err != nil {
|
||||||
return errors.New("invalid payload: " + err.Error())
|
return errors.New("invalid payload: " + err.Error())
|
||||||
@@ -216,9 +289,36 @@ func (s *Resources) applySyncOp(ownerID string, op *SyncOperation) error {
|
|||||||
if p.ToFolderResourceID == resourceID {
|
if p.ToFolderResourceID == resourceID {
|
||||||
return errors.New("cannot move a resource into itself")
|
return errors.New("cannot move a resource into itself")
|
||||||
}
|
}
|
||||||
|
if p.ToFolderResourceID != "" {
|
||||||
|
if !resourceIDPattern.MatchString(p.ToFolderResourceID) {
|
||||||
|
return errors.New("payload.toFolderResourceId must be 32 lowercase hex chars")
|
||||||
|
}
|
||||||
|
// Check de cycle : le dossier cible ne doit pas être la ressource
|
||||||
|
// elle-même ni l'un de ses descendants (cf. FEEDBACK #2).
|
||||||
|
if err := s.checkNoCycle(p.ToFolderResourceID, resourceID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
return s.Repo.MoveResource(ownerID, resourceID, p.ToFolderResourceID)
|
return s.Repo.MoveResource(ownerID, resourceID, p.ToFolderResourceID)
|
||||||
|
|
||||||
case OpDeleteResource:
|
case OpDeleteResource:
|
||||||
|
// A8 : delete owner-only. Ligne vivante d'autrui → NOT_FOUND halt
|
||||||
|
// (pas de applied silencieux). Aucune ligne vivante (jamais existé,
|
||||||
|
// ou déjà supprimée par son owner) → no-op idempotent : l'état
|
||||||
|
// terminal (absente) est déjà atteint, le rejeu reste sûr.
|
||||||
|
exists, err := s.Repo.ExistsOwner(ownerID, resourceID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
if _, err := s.Repo.GetResourceOwner(resourceID); err != nil {
|
||||||
|
if errors.Is(err, repository.ErrNotFound) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return repository.ErrNotFound
|
||||||
|
}
|
||||||
return s.Repo.SyncDelete(ownerID, resourceID)
|
return s.Repo.SyncDelete(ownerID, resourceID)
|
||||||
|
|
||||||
case OpShare, OpUpdateShare:
|
case OpShare, OpUpdateShare:
|
||||||
@@ -275,13 +375,18 @@ func (s *Resources) applyShareOp(ownerID, resourceID string, op *SyncOperation)
|
|||||||
var expiresAt *time.Time
|
var expiresAt *time.Time
|
||||||
if p.ExpiresAt != nil {
|
if p.ExpiresAt != nil {
|
||||||
t := time.UnixMilli(*p.ExpiresAt)
|
t := time.UnixMilli(*p.ExpiresAt)
|
||||||
|
if !t.After(time.Now()) {
|
||||||
|
return errors.New("payload.expiresAt must be in the future")
|
||||||
|
}
|
||||||
expiresAt = &t
|
expiresAt = &t
|
||||||
}
|
}
|
||||||
if err := s.Repository.Shares.Upsert(resourceID, p.GranteeUserID, p.Access, inherit, expiresAt, ownerID); err != nil {
|
if err := s.Repository.Shares.Upsert(resourceID, p.GranteeUserID, p.Access, inherit, expiresAt, ownerID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Bump resource updated_at so the grantee's delta snapshot picks it up.
|
// Bump resource updated_at so the grantee's delta snapshot picks it up.
|
||||||
return s.Repo.TouchResource(resourceID)
|
// Scopé owner (ExistsOwner vérifié plus haut) : TouchOwned, jamais le
|
||||||
|
// TouchResource non-scopé.
|
||||||
|
return s.Repo.TouchOwned(ownerID, resourceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// applyRevokeShareOp handles OpRevokeShare: soft-revoke a user-to-user grant.
|
// applyRevokeShareOp handles OpRevokeShare: soft-revoke a user-to-user grant.
|
||||||
@@ -303,10 +408,13 @@ func (s *Resources) applyRevokeShareOp(ownerID, resourceID string, op *SyncOpera
|
|||||||
if !resourceIDPattern.MatchString(p.GranteeUserID) {
|
if !resourceIDPattern.MatchString(p.GranteeUserID) {
|
||||||
return errors.New("payload.granteeUserId must be 32 lowercase hex chars")
|
return errors.New("payload.granteeUserId must be 32 lowercase hex chars")
|
||||||
}
|
}
|
||||||
// Revoke is idempotent: no-op if already revoked/missing.
|
// Revoke is idempotent: no-op if already revoked/missing — mais les vraies
|
||||||
_ = s.Repository.Shares.Revoke(resourceID, p.GranteeUserID)
|
// pannes DB remontent (cf. FEEDBACK §moyenne).
|
||||||
|
if err := s.Repository.Shares.Revoke(resourceID, p.GranteeUserID); err != nil && !errors.Is(err, repository.ErrNotFound) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
// Bump resource updated_at so the grantee's delta snapshot reflects the change.
|
// Bump resource updated_at so the grantee's delta snapshot reflects the change.
|
||||||
return s.Repo.TouchResource(resourceID)
|
return s.Repo.TouchOwned(ownerID, resourceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// applyCreateLinkOp handles OpCreateLink: create a public share link.
|
// applyCreateLinkOp handles OpCreateLink: create a public share link.
|
||||||
@@ -332,12 +440,15 @@ func (s *Resources) applyCreateLinkOp(ownerID, resourceID string, op *SyncOperat
|
|||||||
var expiresAt *time.Time
|
var expiresAt *time.Time
|
||||||
if p.ExpiresAt != nil {
|
if p.ExpiresAt != nil {
|
||||||
t := time.UnixMilli(*p.ExpiresAt)
|
t := time.UnixMilli(*p.ExpiresAt)
|
||||||
|
if !t.After(time.Now()) {
|
||||||
|
return errors.New("payload.expiresAt must be in the future")
|
||||||
|
}
|
||||||
expiresAt = &t
|
expiresAt = &t
|
||||||
}
|
}
|
||||||
if err := s.Repository.Shares.CreateLink(p.Token, resourceID, p.Access, expiresAt, ownerID); err != nil {
|
if err := s.Repository.Shares.CreateLink(p.Token, resourceID, p.Access, expiresAt, ownerID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return s.Repo.TouchResource(resourceID)
|
return s.Repo.TouchOwned(ownerID, resourceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// applyRevokeLinkOp handles OpRevokeLink: soft-revoke a public share link.
|
// applyRevokeLinkOp handles OpRevokeLink: soft-revoke a public share link.
|
||||||
@@ -356,13 +467,35 @@ func (s *Resources) applyRevokeLinkOp(ownerID, resourceID string, op *SyncOperat
|
|||||||
if !resourceIDPattern.MatchString(p.Token) {
|
if !resourceIDPattern.MatchString(p.Token) {
|
||||||
return errors.New("payload.token must be 32 lowercase hex chars")
|
return errors.New("payload.token must be 32 lowercase hex chars")
|
||||||
}
|
}
|
||||||
// Revoke is idempotent: no-op if already revoked/missing.
|
// Revoke is idempotent: no-op if already revoked/missing — mais les vraies
|
||||||
_ = s.Repository.Shares.RevokeLink(p.Token)
|
// pannes DB remontent.
|
||||||
return s.Repo.TouchResource(resourceID)
|
if err := s.Repository.Shares.RevokeLink(p.Token); err != nil && !errors.Is(err, repository.ErrNotFound) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return s.Repo.TouchOwned(ownerID, resourceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Resources) recordApplied(deviceID string, op *SyncOperation) error {
|
// checkNoCycle remonte la chaîne parentale depuis toFolder : si on rencontre
|
||||||
return s.Repository.Operations.Record(deviceID, op.OperationID, op.Operation, derefString(op.RefType), nullableInt64(derefInt64(op.RefID)), derefString(op.ResourceID), op.Payload)
|
// resourceID, le move créerait un cycle (cf. FEEDBACK #2).
|
||||||
|
func (s *Resources) checkNoCycle(toFolderID, resourceID string) error {
|
||||||
|
cur := toFolderID
|
||||||
|
for i := 0; i < 128; i++ {
|
||||||
|
if cur == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if cur == resourceID {
|
||||||
|
return errCycle
|
||||||
|
}
|
||||||
|
parent, err := s.Repo.GetParentID(cur)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, repository.ErrNotFound) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cur = parent
|
||||||
|
}
|
||||||
|
return errCycle
|
||||||
}
|
}
|
||||||
|
|
||||||
func derefString(v *string) string {
|
func derefString(v *string) string {
|
||||||
|
|||||||
@@ -208,6 +208,41 @@ func TestApplyBatchMoveToMissingFolder(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestApplyBatchMoveIntoDescendantRejected(t *testing.T) {
|
||||||
|
s := newServiceStore(t)
|
||||||
|
userID := mustCreateUser(t, s.Repository, "sync-move-cycle")
|
||||||
|
deviceID := mustRegisterDevice(t, s.Repository, shareTestDeviceID)
|
||||||
|
|
||||||
|
parentID := repository.NewID()
|
||||||
|
childID := repository.NewID()
|
||||||
|
if _, err := s.ApplyBatch(userID, deviceID, []SyncOperation{
|
||||||
|
syncOp(repository.NewID(), parentID, OpCreateResource, "folder", map[string]any{"name": "parent"}),
|
||||||
|
syncOp(repository.NewID(), childID, OpCreateResource, "folder", map[string]any{"name": "child", "parentResourceId": parentID}),
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("apply create parent+child: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Déplacer le parent dans son propre enfant créerait un cycle : halt.
|
||||||
|
result, err := s.ApplyBatch(userID, deviceID, []SyncOperation{
|
||||||
|
syncOp(repository.NewID(), parentID, OpMoveResource, "folder", map[string]any{"toFolderResourceId": childID}),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ApplyBatch: %v", err)
|
||||||
|
}
|
||||||
|
if result.Applied != 0 || result.Failed == nil || result.Failed.Code != "INVALID_REQUEST" {
|
||||||
|
t.Fatalf("move parent→enfant: attendu applied=0 INVALID_REQUEST, got %+v", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Le parent est toujours à la racine : aucun cycle créé.
|
||||||
|
got, err := s.Repository.Resources.GetParentID(parentID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetParentID: %v", err)
|
||||||
|
}
|
||||||
|
if got != "" {
|
||||||
|
t.Errorf("parent déplacé malgré le rejet: parent=%q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestApplyBatchRejectsInvalidPayloadJSON(t *testing.T) {
|
func TestApplyBatchRejectsInvalidPayloadJSON(t *testing.T) {
|
||||||
s := newServiceStore(t)
|
s := newServiceStore(t)
|
||||||
userID := mustCreateUser(t, s.Repository, "sync-badpayload")
|
userID := mustCreateUser(t, s.Repository, "sync-badpayload")
|
||||||
@@ -1146,15 +1181,16 @@ func TestUpdateMetadataRequiresEditor(t *testing.T) {
|
|||||||
t.Errorf("viewer update: attendu applied=0 NOT_FOUND, got %+v", result)
|
t.Errorf("viewer update: attendu applied=0 NOT_FOUND, got %+v", result)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stranger (no access) → no-op terminal (absent semantics), no failure.
|
// Stranger (no access) → NOT_FOUND halt (A8 : pas de no-op silencieux ;
|
||||||
|
// un rename qui n'a rien renommé est une désync, le halt l'expose).
|
||||||
result, err = s.ApplyBatch(strangerID, shareTestDeviceID, []SyncOperation{
|
result, err = s.ApplyBatch(strangerID, shareTestDeviceID, []SyncOperation{
|
||||||
syncOp(repository.NewID(), fileID, OpUpdateMetadata, "file", map[string]any{"name": "sneak.txt"}),
|
syncOp(repository.NewID(), fileID, OpUpdateMetadata, "file", map[string]any{"name": "sneak.txt"}),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("stranger update: %v", err)
|
t.Fatalf("stranger update: %v", err)
|
||||||
}
|
}
|
||||||
if result.Applied != 1 || result.Failed != nil {
|
if result.Applied != 0 || result.Failed == nil || result.Failed.Code != "NOT_FOUND" {
|
||||||
t.Errorf("stranger update: attendu applied=1 aucun échec, got %+v", result)
|
t.Errorf("stranger update: attendu applied=0 NOT_FOUND, got %+v", result)
|
||||||
}
|
}
|
||||||
row, err = s.Repo.GetFile(ownerID, fileID)
|
row, err = s.Repo.GetFile(ownerID, fileID)
|
||||||
if err != nil || row.Name != "renamed.txt" {
|
if err != nil || row.Name != "renamed.txt" {
|
||||||
@@ -1179,15 +1215,16 @@ func TestMoveAndDeleteOwnerOnly(t *testing.T) {
|
|||||||
t.Fatalf("create + share: %v", err)
|
t.Fatalf("create + share: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Editor tries to move the shared file → no-op (owner-only), nothing moves.
|
// Editor tries to move the shared file → NOT_FOUND halt (A8 : move
|
||||||
|
// owner-only, pas de applied silencieux).
|
||||||
result, err := s.ApplyBatch(granteeID, shareTestDeviceID, []SyncOperation{
|
result, err := s.ApplyBatch(granteeID, shareTestDeviceID, []SyncOperation{
|
||||||
syncOp(repository.NewID(), fileID, OpMoveResource, "file", map[string]any{"toFolderResourceId": targetID}),
|
syncOp(repository.NewID(), fileID, OpMoveResource, "file", map[string]any{"toFolderResourceId": targetID}),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("grantee move: %v", err)
|
t.Fatalf("grantee move: %v", err)
|
||||||
}
|
}
|
||||||
if result.Applied != 1 || result.Failed != nil {
|
if result.Applied != 0 || result.Failed == nil || result.Failed.Code != "NOT_FOUND" {
|
||||||
t.Fatalf("grantee move: applied=%d failed=%+v", result.Applied, result.Failed)
|
t.Fatalf("grantee move: attendu applied=0 NOT_FOUND, got %+v", result)
|
||||||
}
|
}
|
||||||
row, err := s.Repo.GetFile(ownerID, fileID)
|
row, err := s.Repo.GetFile(ownerID, fileID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1197,17 +1234,254 @@ func TestMoveAndDeleteOwnerOnly(t *testing.T) {
|
|||||||
t.Errorf("fichier déplacé par un non-owner: folderId=%q, attendu racine", row.FolderID)
|
t.Errorf("fichier déplacé par un non-owner: folderId=%q, attendu racine", row.FolderID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Editor tries to delete the shared file → no-op, still present for owner.
|
// Editor tries to delete the shared file → NOT_FOUND halt (A8), still
|
||||||
|
// present for owner.
|
||||||
result, err = s.ApplyBatch(granteeID, shareTestDeviceID, []SyncOperation{
|
result, err = s.ApplyBatch(granteeID, shareTestDeviceID, []SyncOperation{
|
||||||
syncOp(repository.NewID(), fileID, OpDeleteResource, "file", map[string]any{}),
|
syncOp(repository.NewID(), fileID, OpDeleteResource, "file", map[string]any{}),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("grantee delete: %v", err)
|
t.Fatalf("grantee delete: %v", err)
|
||||||
}
|
}
|
||||||
if result.Applied != 1 || result.Failed != nil {
|
if result.Applied != 0 || result.Failed == nil || result.Failed.Code != "NOT_FOUND" {
|
||||||
t.Fatalf("grantee delete: applied=%d failed=%+v", result.Applied, result.Failed)
|
t.Fatalf("grantee delete: attendu applied=0 NOT_FOUND, got %+v", result)
|
||||||
}
|
}
|
||||||
if _, err := s.Repo.GetFile(ownerID, fileID); err != nil {
|
if _, err := s.Repo.GetFile(ownerID, fileID); err != nil {
|
||||||
t.Errorf("fichier supprimé par un non-owner: %v", err)
|
t.Errorf("fichier supprimé par un non-owner: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- LOT 3 : rejeu, payload_hash, liens idempotents, sémantiques A8 ---
|
||||||
|
|
||||||
|
func TestApplyBatchReplaySameOpIsIdempotent(t *testing.T) {
|
||||||
|
s := newServiceStore(t)
|
||||||
|
userID := mustCreateUser(t, s.Repository, "replay-owner")
|
||||||
|
deviceID := mustRegisterDevice(t, s.Repository, shareTestDeviceID)
|
||||||
|
|
||||||
|
fileID := repository.NewID()
|
||||||
|
opID := repository.NewID()
|
||||||
|
op := syncOp(opID, fileID, OpCreateResource, "file", map[string]any{"name": "x.txt"})
|
||||||
|
|
||||||
|
result, err := s.ApplyBatch(userID, deviceID, []SyncOperation{op})
|
||||||
|
if err != nil || result.Applied != 1 || result.Failed != nil {
|
||||||
|
t.Fatalf("apply 1: applied=%d failed=%+v err=%v", result.Applied, result.Failed, err)
|
||||||
|
}
|
||||||
|
// Rejeu à l'identique (même operation_id, même payload) → skip succès.
|
||||||
|
result, err = s.ApplyBatch(userID, deviceID, []SyncOperation{op})
|
||||||
|
if err != nil || result.Applied != 1 || result.Failed != nil {
|
||||||
|
t.Errorf("rejeu identique: applied=%d failed=%+v err=%v", result.Applied, result.Failed, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestApplyBatchReplayMutatedPayloadRejected(t *testing.T) {
|
||||||
|
s := newServiceStore(t)
|
||||||
|
userID := mustCreateUser(t, s.Repository, "replay-mut")
|
||||||
|
deviceID := mustRegisterDevice(t, s.Repository, shareTestDeviceID)
|
||||||
|
|
||||||
|
fileID := repository.NewID()
|
||||||
|
opID := repository.NewID()
|
||||||
|
first := syncOp(opID, fileID, OpCreateResource, "file", map[string]any{"name": "x.txt"})
|
||||||
|
if _, err := s.ApplyBatch(userID, deviceID, []SyncOperation{first}); err != nil {
|
||||||
|
t.Fatalf("apply 1: %v", err)
|
||||||
|
}
|
||||||
|
// Même (device, operation_id), payload muté → INVALID_REQUEST halt (A9),
|
||||||
|
// pas de skip silencieux.
|
||||||
|
mutated := syncOp(opID, fileID, OpCreateResource, "file", map[string]any{"name": "y.txt"})
|
||||||
|
result, err := s.ApplyBatch(userID, deviceID, []SyncOperation{mutated})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ApplyBatch: %v", err)
|
||||||
|
}
|
||||||
|
if result.Applied != 0 || result.Failed == nil || result.Failed.Code != "INVALID_REQUEST" {
|
||||||
|
t.Errorf("payload muté: attendu applied=0 INVALID_REQUEST, got %+v", result)
|
||||||
|
}
|
||||||
|
// Même opération rejouée sous un autre type → rejet aussi.
|
||||||
|
retyped := syncOp(opID, fileID, OpDeleteResource, "file", map[string]any{})
|
||||||
|
result, err = s.ApplyBatch(userID, deviceID, []SyncOperation{retyped})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ApplyBatch: %v", err)
|
||||||
|
}
|
||||||
|
if result.Applied != 0 || result.Failed == nil || result.Failed.Code != "INVALID_REQUEST" {
|
||||||
|
t.Errorf("type muté: attendu applied=0 INVALID_REQUEST, got %+v", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestApplyBatchReplayWhitespaceEquivalentPayload(t *testing.T) {
|
||||||
|
s := newServiceStore(t)
|
||||||
|
userID := mustCreateUser(t, s.Repository, "replay-ws")
|
||||||
|
deviceID := mustRegisterDevice(t, s.Repository, shareTestDeviceID)
|
||||||
|
|
||||||
|
fileID := repository.NewID()
|
||||||
|
opID := repository.NewID()
|
||||||
|
first := syncOp(opID, fileID, OpCreateResource, "file", map[string]any{"name": "x.txt"})
|
||||||
|
if _, err := s.ApplyBatch(userID, deviceID, []SyncOperation{first}); err != nil {
|
||||||
|
t.Fatalf("apply 1: %v", err)
|
||||||
|
}
|
||||||
|
// Même objet, sérialisation différente (espaces) → hash canonique égal → skip.
|
||||||
|
second := SyncOperation{
|
||||||
|
OperationID: opID,
|
||||||
|
ResourceID: strp(fileID),
|
||||||
|
ResourceType: strp("file"),
|
||||||
|
Operation: OpCreateResource,
|
||||||
|
Payload: json.RawMessage(`{ "name" : "x.txt" }`),
|
||||||
|
}
|
||||||
|
result, err := s.ApplyBatch(userID, deviceID, []SyncOperation{second})
|
||||||
|
if err != nil || result.Applied != 1 || result.Failed != nil {
|
||||||
|
t.Errorf("rejeu équivalent: applied=%d failed=%+v err=%v", result.Applied, result.Failed, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateLinkReplayIdempotent(t *testing.T) {
|
||||||
|
s := newServiceStore(t)
|
||||||
|
ownerID := mustCreateUser(t, s.Repository, "linkreplay-owner")
|
||||||
|
mustRegisterDevice(t, s.Repository, shareTestDeviceID)
|
||||||
|
|
||||||
|
fileID := repository.NewID()
|
||||||
|
if _, err := s.ApplyBatch(ownerID, shareTestDeviceID, []SyncOperation{
|
||||||
|
syncOp(repository.NewID(), fileID, OpCreateResource, "file", map[string]any{"name": "x.txt"}),
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
token := repository.NewID()
|
||||||
|
link := func(opID string) SyncOperation {
|
||||||
|
return syncOp(opID, fileID, OpCreateLink, "file", map[string]any{
|
||||||
|
"token": token,
|
||||||
|
"access": "viewer",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// Premier create_link.
|
||||||
|
if _, err := s.ApplyBatch(ownerID, shareTestDeviceID, []SyncOperation{link(repository.NewID())}); err != nil {
|
||||||
|
t.Fatalf("create link 1: %v", err)
|
||||||
|
}
|
||||||
|
// Rejeu même token, même ressource, même owner (nouvel operation_id) → succès.
|
||||||
|
result, err := s.ApplyBatch(ownerID, shareTestDeviceID, []SyncOperation{link(repository.NewID())})
|
||||||
|
if err != nil || result.Applied != 1 || result.Failed != nil {
|
||||||
|
t.Errorf("rejeu lien identique: applied=%d failed=%+v err=%v", result.Applied, result.Failed, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateLinkTokenReuseConflict(t *testing.T) {
|
||||||
|
s := newServiceStore(t)
|
||||||
|
ownerID := mustCreateUser(t, s.Repository, "linkconf-owner")
|
||||||
|
mustRegisterDevice(t, s.Repository, shareTestDeviceID)
|
||||||
|
|
||||||
|
fileA := repository.NewID()
|
||||||
|
fileB := repository.NewID()
|
||||||
|
if _, err := s.ApplyBatch(ownerID, shareTestDeviceID, []SyncOperation{
|
||||||
|
syncOp(repository.NewID(), fileA, OpCreateResource, "file", map[string]any{"name": "a.txt"}),
|
||||||
|
syncOp(repository.NewID(), fileB, OpCreateResource, "file", map[string]any{"name": "b.txt"}),
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
token := repository.NewID()
|
||||||
|
if _, err := s.ApplyBatch(ownerID, shareTestDeviceID, []SyncOperation{
|
||||||
|
syncOp(repository.NewID(), fileA, OpCreateLink, "file", map[string]any{"token": token, "access": "viewer"}),
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create link: %v", err)
|
||||||
|
}
|
||||||
|
// Même token vers une AUTRE ressource du même owner → NAME_CONFLICT (#3).
|
||||||
|
result, err := s.ApplyBatch(ownerID, shareTestDeviceID, []SyncOperation{
|
||||||
|
syncOp(repository.NewID(), fileB, OpCreateLink, "file", map[string]any{"token": token, "access": "viewer"}),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ApplyBatch: %v", err)
|
||||||
|
}
|
||||||
|
if result.Applied != 0 || result.Failed == nil || result.Failed.Code != "NAME_CONFLICT" {
|
||||||
|
t.Errorf("token réutilisé: attendu applied=0 NAME_CONFLICT, got %+v", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateLinkTokenOwnerMismatch(t *testing.T) {
|
||||||
|
s := newServiceStore(t)
|
||||||
|
owner1 := mustCreateUser(t, s.Repository, "linkown-1")
|
||||||
|
owner2 := mustCreateUser(t, s.Repository, "linkown-2")
|
||||||
|
mustRegisterDevice(t, s.Repository, shareTestDeviceID)
|
||||||
|
|
||||||
|
fileA := repository.NewID()
|
||||||
|
fileB := repository.NewID()
|
||||||
|
if _, err := s.ApplyBatch(owner1, shareTestDeviceID, []SyncOperation{
|
||||||
|
syncOp(repository.NewID(), fileA, OpCreateResource, "file", map[string]any{"name": "a.txt"}),
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create A: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := s.ApplyBatch(owner2, shareTestDeviceID, []SyncOperation{
|
||||||
|
syncOp(repository.NewID(), fileB, OpCreateResource, "file", map[string]any{"name": "b.txt"}),
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create B: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
token := repository.NewID()
|
||||||
|
if _, err := s.ApplyBatch(owner1, shareTestDeviceID, []SyncOperation{
|
||||||
|
syncOp(repository.NewID(), fileA, OpCreateLink, "file", map[string]any{"token": token, "access": "viewer"}),
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create link: %v", err)
|
||||||
|
}
|
||||||
|
// Même token, autre owner → NAME_CONFLICT (#3).
|
||||||
|
result, err := s.ApplyBatch(owner2, shareTestDeviceID, []SyncOperation{
|
||||||
|
syncOp(repository.NewID(), fileB, OpCreateLink, "file", map[string]any{"token": token, "access": "viewer"}),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ApplyBatch: %v", err)
|
||||||
|
}
|
||||||
|
if result.Applied != 0 || result.Failed == nil || result.Failed.Code != "NAME_CONFLICT" {
|
||||||
|
t.Errorf("token d'autrui: attendu applied=0 NAME_CONFLICT, got %+v", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpdateMetadataAbsentHalts(t *testing.T) {
|
||||||
|
s := newServiceStore(t)
|
||||||
|
userID := mustCreateUser(t, s.Repository, "ren-absent")
|
||||||
|
deviceID := mustRegisterDevice(t, s.Repository, shareTestDeviceID)
|
||||||
|
|
||||||
|
// update_metadata sur ressource inexistante → NOT_FOUND halt (A8).
|
||||||
|
result, err := s.ApplyBatch(userID, deviceID, []SyncOperation{
|
||||||
|
syncOp(repository.NewID(), repository.NewID(), OpUpdateMetadata, "file", map[string]any{"name": "ghost.txt"}),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ApplyBatch: %v", err)
|
||||||
|
}
|
||||||
|
if result.Applied != 0 || result.Failed == nil || result.Failed.Code != "NOT_FOUND" {
|
||||||
|
t.Errorf("rename absent: attendu applied=0 NOT_FOUND, got %+v", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMoveForeignHalts(t *testing.T) {
|
||||||
|
s := newServiceStore(t)
|
||||||
|
ownerID := mustCreateUser(t, s.Repository, "movef-owner")
|
||||||
|
outsiderID := mustCreateUser(t, s.Repository, "movef-outsider")
|
||||||
|
mustRegisterDevice(t, s.Repository, shareTestDeviceID)
|
||||||
|
|
||||||
|
fileID := repository.NewID()
|
||||||
|
targetID := repository.NewID()
|
||||||
|
if _, err := s.ApplyBatch(ownerID, shareTestDeviceID, []SyncOperation{
|
||||||
|
syncOp(repository.NewID(), fileID, OpCreateResource, "file", map[string]any{"name": "x.txt"}),
|
||||||
|
syncOp(repository.NewID(), targetID, OpCreateResource, "folder", map[string]any{"name": "t"}),
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create: %v", err)
|
||||||
|
}
|
||||||
|
// Move d'une ressource d'autrui (jamais partagée) → NOT_FOUND halt.
|
||||||
|
result, err := s.ApplyBatch(outsiderID, shareTestDeviceID, []SyncOperation{
|
||||||
|
syncOp(repository.NewID(), fileID, OpMoveResource, "file", map[string]any{"toFolderResourceId": targetID}),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ApplyBatch: %v", err)
|
||||||
|
}
|
||||||
|
if result.Applied != 0 || result.Failed == nil || result.Failed.Code != "NOT_FOUND" {
|
||||||
|
t.Errorf("move étranger: attendu applied=0 NOT_FOUND, got %+v", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDeleteAbsentIsIdempotentNoop(t *testing.T) {
|
||||||
|
s := newServiceStore(t)
|
||||||
|
userID := mustCreateUser(t, s.Repository, "delabsent-owner")
|
||||||
|
deviceID := mustRegisterDevice(t, s.Repository, shareTestDeviceID)
|
||||||
|
|
||||||
|
// Delete d'une ressource sans ligne vivante → no-op idempotent (état
|
||||||
|
// terminal déjà atteint), pas de halt.
|
||||||
|
result, err := s.ApplyBatch(userID, deviceID, []SyncOperation{
|
||||||
|
syncOp(repository.NewID(), repository.NewID(), OpDeleteResource, "file", map[string]any{}),
|
||||||
|
})
|
||||||
|
if err != nil || result.Applied != 1 || result.Failed != nil {
|
||||||
|
t.Errorf("delete absent: applied=%d failed=%+v err=%v", result.Applied, result.Failed, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
package service
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/kazier/backend/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (s *Services) UploadDocument(file *string, destination *models.Document) error {
|
|
||||||
|
|
||||||
if canEdit, _ := UserCanEditDocument(s.ConnectedUser, destination); !canEdit {
|
|
||||||
return fmt.Errorf("cannot edit document")
|
|
||||||
}
|
|
||||||
|
|
||||||
models.NewDocument("test", models.FILE, 1)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
# Déploiement perso — pull l'image depuis le registre Gitea (pas de build local).
|
||||||
|
#
|
||||||
|
# Usage sur le serveur :
|
||||||
|
# 1. cp .env.prod.example .env.prod && adapter (mots de passe forts, jamais commité)
|
||||||
|
# 2. echo "<PAT read:package>" | docker login gitea.thirdshop.fr -u <user> --password-stdin
|
||||||
|
# 3. docker compose -f docker-compose.prod.yml --env-file .env.prod pull
|
||||||
|
# 4. docker compose -f docker-compose.prod.yml --env-file .env.prod up -d
|
||||||
|
# 5. curl http://localhost:${PORT:-8080}/api/v1/health
|
||||||
|
#
|
||||||
|
# MAJ : répéter pull + up -d (ou tag précis via VERSION=x.y.z).
|
||||||
|
|
||||||
|
services:
|
||||||
|
postgres:
|
||||||
|
image: postgres:17-alpine
|
||||||
|
environment:
|
||||||
|
POSTGRES_DB: ${POSTGRES_DB:-kazier}
|
||||||
|
POSTGRES_USER: ${POSTGRES_USER:-kazier}
|
||||||
|
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD requis (.env.prod)}
|
||||||
|
volumes:
|
||||||
|
- postgres-data:/var/lib/postgresql/data
|
||||||
|
restart: unless-stopped
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-kazier}"]
|
||||||
|
interval: 10s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
|
||||||
|
backend:
|
||||||
|
# Image publiée par la release Gitea (tag vX.Y.Z + latest).
|
||||||
|
image: ${REGISTRY_IMAGE:-gitea.thirdshop.fr/thirdshop/kazier}:${VERSION:-latest}
|
||||||
|
ports:
|
||||||
|
- "${PORT:-8080}:8080"
|
||||||
|
environment:
|
||||||
|
PORT: 8080
|
||||||
|
DATABASE_URL: postgres://${POSTGRES_USER:-kazier}:${POSTGRES_PASSWORD:?POSTGRES_PASSWORD requis}@postgres:5432/${POSTGRES_DB:-kazier}?sslmode=disable
|
||||||
|
UPLOAD_DIR: /app/uploads
|
||||||
|
MAX_FILE_SIZE_MB: ${MAX_FILE_SIZE_MB:-50}
|
||||||
|
OCR_LANG: ${OCR_LANG:-fra+eng}
|
||||||
|
AUTH_SECRET: ${AUTH_SECRET:?AUTH_SECRET requis (.env.prod)}
|
||||||
|
ADMIN_USERNAME: ${ADMIN_USERNAME:?ADMIN_USERNAME requis au premier démarrage}
|
||||||
|
ADMIN_PASSWORD: ${ADMIN_PASSWORD:?ADMIN_PASSWORD requis au premier démarrage}
|
||||||
|
volumes:
|
||||||
|
- backend-uploads:/app/uploads
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
postgres-data:
|
||||||
|
backend-uploads:
|
||||||
@@ -24,12 +24,15 @@ class MainActivity : ComponentActivity() {
|
|||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
enableEdgeToEdge()
|
enableEdgeToEdge()
|
||||||
|
|
||||||
lifecycleScope.launch { themePreferenceStore.load() }
|
// m37 : le contenu n'est posé qu'une fois le thème persisté chargé —
|
||||||
|
// avant, le défaut SYSTEM flashait aux premières frames.
|
||||||
setContent {
|
lifecycleScope.launch {
|
||||||
val theme by themePreferenceStore.theme.collectAsStateWithLifecycle()
|
themePreferenceStore.load()
|
||||||
KazierTheme(theme = theme) {
|
setContent {
|
||||||
KazierApp()
|
val theme by themePreferenceStore.theme.collectAsStateWithLifecycle()
|
||||||
|
KazierTheme(theme = theme) {
|
||||||
|
KazierApp()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,16 +15,20 @@ import javax.inject.Singleton
|
|||||||
* Persistance chiffrée du token paseto et du profil compte (Keystore + AES).
|
* Persistance chiffrée du token paseto et du profil compte (Keystore + AES).
|
||||||
* Miroir de `services/secureStore.ts` (expo-secure-store). Le token n'est
|
* Miroir de `services/secureStore.ts` (expo-secure-store). Le token n'est
|
||||||
* JAMAIS écrit en clair dans SQLite.
|
* JAMAIS écrit en clair dans SQLite.
|
||||||
|
*
|
||||||
|
* L'init Keystore/`EncryptedSharedPreferences` est paresseuse (m28) : aucun
|
||||||
|
* I/O au construct Hilt — le premier accès a lieu sur le thread de l'appelant
|
||||||
|
* ([SessionManager] bascule sur `Dispatchers.IO`).
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
class SecureTokenStore @Inject constructor(
|
class SecureTokenStore @Inject constructor(
|
||||||
@ApplicationContext context: Context,
|
@ApplicationContext private val context: Context,
|
||||||
moshi: Moshi,
|
moshi: Moshi,
|
||||||
) {
|
) : TokenStore {
|
||||||
|
|
||||||
private val userAdapter: JsonAdapter<UserDto> = moshi.adapter(UserDto::class.java)
|
private val userAdapter: JsonAdapter<UserDto> = moshi.adapter(UserDto::class.java)
|
||||||
|
|
||||||
private val prefs: SharedPreferences = run {
|
private val prefs: SharedPreferences by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
|
||||||
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
|
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
|
||||||
EncryptedSharedPreferences.create(
|
EncryptedSharedPreferences.create(
|
||||||
FILE_NAME,
|
FILE_NAME,
|
||||||
@@ -35,29 +39,36 @@ class SecureTokenStore @Inject constructor(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getToken(): String? = prefs.getString(KEY_TOKEN, null)
|
override fun getToken(): String? = prefs.getString(KEY_TOKEN, null)
|
||||||
|
|
||||||
fun getUser(): UserDto? {
|
override fun getUser(): UserDto? {
|
||||||
val raw = prefs.getString(KEY_ACCOUNT, null) ?: return null
|
val raw = prefs.getString(KEY_ACCOUNT, null) ?: return null
|
||||||
return runCatching { userAdapter.fromJson(raw) }
|
return runCatching { userAdapter.fromJson(raw) }
|
||||||
.getOrNull()
|
.getOrNull()
|
||||||
?.takeIf { it.id.isNotEmpty() && it.username.isNotEmpty() }
|
?.takeIf { it.id.isNotEmpty() && it.username.isNotEmpty() }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun save(token: String, user: UserDto) {
|
override fun getExpiresAt(): Long? =
|
||||||
|
prefs.getLong(KEY_EXPIRES_AT, -1L).takeIf { it >= 0L }
|
||||||
|
|
||||||
|
override fun save(token: String, user: UserDto, expiresAt: Long?) {
|
||||||
prefs.edit()
|
prefs.edit()
|
||||||
.putString(KEY_TOKEN, token)
|
.putString(KEY_TOKEN, token)
|
||||||
.putString(KEY_ACCOUNT, userAdapter.toJson(user))
|
.putString(KEY_ACCOUNT, userAdapter.toJson(user))
|
||||||
|
.apply {
|
||||||
|
if (expiresAt != null) putLong(KEY_EXPIRES_AT, expiresAt) else remove(KEY_EXPIRES_AT)
|
||||||
|
}
|
||||||
.apply()
|
.apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun clear() {
|
override fun clear() {
|
||||||
prefs.edit().remove(KEY_TOKEN).remove(KEY_ACCOUNT).apply()
|
prefs.edit().remove(KEY_TOKEN).remove(KEY_ACCOUNT).remove(KEY_EXPIRES_AT).apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val FILE_NAME = "kazier_secure"
|
private const val FILE_NAME = "kazier_secure"
|
||||||
private const val KEY_TOKEN = "kazier.auth_token"
|
private const val KEY_TOKEN = "kazier.auth_token"
|
||||||
private const val KEY_ACCOUNT = "kazier.account"
|
private const val KEY_ACCOUNT = "kazier.account"
|
||||||
|
private const val KEY_EXPIRES_AT = "kazier.expires_at"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,48 +1,105 @@
|
|||||||
package com.kazier.mobile.auth
|
package com.kazier.mobile.auth
|
||||||
|
|
||||||
|
import com.kazier.mobile.data.local.dao.PendingOperationDao
|
||||||
import com.kazier.mobile.data.remote.ApiClient
|
import com.kazier.mobile.data.remote.ApiClient
|
||||||
import com.kazier.mobile.data.remote.dto.LoginResponseDto
|
import com.kazier.mobile.data.remote.dto.UserDto
|
||||||
import com.kazier.mobile.domain.ActiveUserStore
|
import com.kazier.mobile.domain.ActiveUserStore
|
||||||
import com.kazier.mobile.domain.DeviceIdentity
|
|
||||||
import com.kazier.mobile.domain.LocalModeStore
|
import com.kazier.mobile.domain.LocalModeStore
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.SupervisorJob
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Orchestration de la session (restauration/sauvegarde/purge) : relie le store
|
* Orchestration de la session (restauration/sauvegarde/purge) : relie le store
|
||||||
* sécurisé, le miroir `active_user_id` et le token en mémoire lu par
|
* sécurisé, le miroir `active_user_id` et le token en mémoire lu par
|
||||||
* `AuthInterceptor`. Le drapeau « mode local » est persisté à part
|
* `AuthInterceptor`. Le drapeau « mode local » est persisté à part
|
||||||
* ([LocalModeStore]) pour survivre aux redémarrages.
|
* ([LocalModeStore]) pour survivre aux redémarrages.
|
||||||
|
*
|
||||||
|
* Le callback 401 est possédé ici (singleton, M25) — jamais par un ViewModel :
|
||||||
|
* après `onCleared`, un token expiré/révoqué continue de purger la session
|
||||||
|
* (le worker cesse alors de rejouer l'outbox sous un token mort).
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
class SessionManager @Inject constructor(
|
class SessionManager @Inject constructor(
|
||||||
private val secureTokenStore: SecureTokenStore,
|
private val tokenStore: TokenStore,
|
||||||
private val activeUserStore: ActiveUserStore,
|
private val activeUserStore: ActiveUserStore,
|
||||||
private val tokenProvider: TokenProvider,
|
private val tokenProvider: TokenProvider,
|
||||||
private val localModeStore: LocalModeStore,
|
private val localModeStore: LocalModeStore,
|
||||||
|
private val pendingOperationDao: PendingOperationDao,
|
||||||
|
private val apiClient: ApiClient,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||||
|
|
||||||
|
init {
|
||||||
|
// 401 sur un endpoint protégé (token expiré/révoqué, compte supprimé)
|
||||||
|
// → purge de la session. Le 401 du login et des routes publiques est
|
||||||
|
// exonéré côté ApiClient (skipUnauthorizedHandling).
|
||||||
|
apiClient.onUnauthorized = { handleUnauthorized() }
|
||||||
|
}
|
||||||
|
|
||||||
/** Restaure la session persistée (si présente), sinon null. */
|
/** Restaure la session persistée (si présente), sinon null. */
|
||||||
suspend fun restore(): AuthSession? {
|
suspend fun restore(): AuthSession? = withContext(Dispatchers.IO) {
|
||||||
val token = secureTokenStore.getToken() ?: return null
|
val token = tokenStore.getToken() ?: return@withContext null
|
||||||
val user = secureTokenStore.getUser() ?: return null
|
val user = tokenStore.getUser() ?: return@withContext null
|
||||||
tokenProvider.current = token
|
tokenProvider.current = token
|
||||||
return AuthSession(token = token, user = user)
|
AuthSession(token = token, user = user, expiresAt = tokenStore.getExpiresAt())
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun save(token: String, user: com.kazier.mobile.data.remote.dto.UserDto) {
|
suspend fun save(token: String, user: UserDto, expiresAt: Long? = null) =
|
||||||
secureTokenStore.save(token, user)
|
withContext(Dispatchers.IO) {
|
||||||
activeUserStore.set(user.id)
|
val previousUserId = activeUserStore.get()
|
||||||
localModeStore.clear()
|
tokenStore.save(token, user, expiresAt)
|
||||||
tokenProvider.current = token
|
activeUserStore.set(user.id)
|
||||||
}
|
localModeStore.clear()
|
||||||
|
tokenProvider.current = token
|
||||||
|
// Changement de compte sans sign-out explicite (login de B par
|
||||||
|
// dessus la session de A) : même purge que `clear()` — la file de
|
||||||
|
// A ne doit pas partir sous le token de B. Premier login
|
||||||
|
// (`previousUserId == null`, éventuel travail du mode local) et
|
||||||
|
// re-login du même compte : file conservée.
|
||||||
|
if (previousUserId != null && previousUserId != user.id) {
|
||||||
|
pendingOperationDao.deleteUnpushed()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun clear() {
|
suspend fun clear() = withContext(Dispatchers.IO) {
|
||||||
secureTokenStore.clear()
|
tokenStore.clear()
|
||||||
activeUserStore.clear()
|
activeUserStore.clear()
|
||||||
tokenProvider.current = null
|
tokenProvider.current = null
|
||||||
|
// Sign-out explicite : la file non poussée est abandonnée (C5, voir
|
||||||
|
// `deleteUnpushed`). Les ops `synced` survivent comme tombstones.
|
||||||
|
pendingOperationDao.deleteUnpushed()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Purge fire-and-forget sur 401 (appelée depuis le thread OkHttp via
|
||||||
|
* `ApiClient`) : le token en mémoire tombe synchronement pour ne plus
|
||||||
|
* émettre de requêtes authentifiées, la persistance suit en IO.
|
||||||
|
*/
|
||||||
|
fun handleUnauthorized() {
|
||||||
|
tokenProvider.current = null
|
||||||
|
scope.launch {
|
||||||
|
runCatching { tokenStore.clear() }
|
||||||
|
runCatching { activeUserStore.clear() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pré-relogin : vrai si l'`expires_at` persistée du login est atteinte
|
||||||
|
* (marge anti-course d'1 min par défaut). L'appelant peut alors reloginer
|
||||||
|
* avant de subir le 401 brutal (tokens 7 j, m27).
|
||||||
|
*/
|
||||||
|
suspend fun isTokenExpired(nowMs: Long = System.currentTimeMillis(), marginMs: Long = 60_000L): Boolean =
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
val expiresAt = tokenStore.getExpiresAt() ?: return@withContext false
|
||||||
|
nowMs + marginMs >= expiresAt
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun isLocalMode(): Boolean = localModeStore.get()
|
suspend fun isLocalMode(): Boolean = localModeStore.get()
|
||||||
|
|
||||||
suspend fun setLocalMode() = localModeStore.set()
|
suspend fun setLocalMode() = localModeStore.set()
|
||||||
@@ -52,5 +109,7 @@ class SessionManager @Inject constructor(
|
|||||||
|
|
||||||
data class AuthSession(
|
data class AuthSession(
|
||||||
val token: String,
|
val token: String,
|
||||||
val user: com.kazier.mobile.data.remote.dto.UserDto,
|
val user: UserDto,
|
||||||
)
|
/** Expiration persistée du token (ms epoch) — null si inconnue. */
|
||||||
|
val expiresAt: Long? = null,
|
||||||
|
)
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.kazier.mobile.auth
|
||||||
|
|
||||||
|
import com.kazier.mobile.data.remote.dto.UserDto
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persistance du token paseto, du profil compte et de l'expiration du token
|
||||||
|
* (claim `expires_at` du login, TTL 7 j). Implémentation : [SecureTokenStore]
|
||||||
|
* (Keystore + AES, jamais en clair dans SQLite).
|
||||||
|
*/
|
||||||
|
interface TokenStore {
|
||||||
|
|
||||||
|
fun getToken(): String?
|
||||||
|
|
||||||
|
fun getUser(): UserDto?
|
||||||
|
|
||||||
|
/** Expiration du token (ms epoch, `expires_at` du login) — null si inconnue. */
|
||||||
|
fun getExpiresAt(): Long?
|
||||||
|
|
||||||
|
fun save(token: String, user: UserDto, expiresAt: Long?)
|
||||||
|
|
||||||
|
fun clear()
|
||||||
|
}
|
||||||
@@ -23,13 +23,17 @@ import com.kazier.mobile.data.local.entity.UserPreferenceEntity
|
|||||||
* v10: ocr_text sur files (extrait OCR serveur persisté localement) ;
|
* v10: ocr_text sur files (extrait OCR serveur persisté localement) ;
|
||||||
* v11: ocr_attempts + ocr_queued_at sur files (driver OCR auto côté client —
|
* v11: ocr_attempts + ocr_queued_at sur files (driver OCR auto côté client —
|
||||||
* compteur d'échecs terminaux ≤ 3, marqueur « en cours » de l'upload).
|
* compteur d'échecs terminaux ≤ 3, marqueur « en cours » de l'upload).
|
||||||
|
* v12: device_id sur pending_operations + unicité UNIQUE(device_id,
|
||||||
|
* operation_id) (contrat outbox docs/api-v1.md §6.1).
|
||||||
|
* v13: index composites files(exists, processed, sync_status) et
|
||||||
|
* pending_operations(resource_id, operation, status) (requêtes review/OCR/gates).
|
||||||
*/
|
*/
|
||||||
@Database(
|
@Database(
|
||||||
entities = [
|
entities = [
|
||||||
FolderEntity::class, UserPreferenceEntity::class, FileEntity::class,
|
FolderEntity::class, UserPreferenceEntity::class, FileEntity::class,
|
||||||
PendingOperationEntity::class, ScanSessionEntity::class, ScanPageEntity::class,
|
PendingOperationEntity::class, ScanSessionEntity::class, ScanPageEntity::class,
|
||||||
],
|
],
|
||||||
version = 11,
|
version = 14,
|
||||||
exportSchema = false,
|
exportSchema = false,
|
||||||
)
|
)
|
||||||
abstract class AppDatabase : RoomDatabase() {
|
abstract class AppDatabase : RoomDatabase() {
|
||||||
|
|||||||
@@ -4,8 +4,11 @@ import com.kazier.mobile.data.local.entity.FileEntity
|
|||||||
|
|
||||||
/** Date de référence d'un fichier : modification SAF quand dispo, sinon ajout local. */
|
/** Date de référence d'un fichier : modification SAF quand dispo, sinon ajout local. */
|
||||||
val FileEntity.referenceDate: Long
|
val FileEntity.referenceDate: Long
|
||||||
get() = lastModified ?: addedAt
|
get() {
|
||||||
|
val modified = lastModified
|
||||||
|
return if (modified != null && modified > 0L) modified else addedAt
|
||||||
|
}
|
||||||
|
|
||||||
/** Tri documents : même ordre que la grille d'accueil, du plus récent au plus vieux. */
|
/** Tri documents : même ordre que la grille d'accueil, du plus récent au plus vieux. */
|
||||||
fun List<FileEntity>.orderedByReferenceDateDesc(): List<FileEntity> =
|
fun List<FileEntity>.orderedByReferenceDateDesc(): List<FileEntity> =
|
||||||
sortedByDescending { it.referenceDate }
|
sortedWith(compareByDescending<FileEntity> { it.referenceDate }.thenBy { it.name })
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
package com.kazier.mobile.data.local.dao
|
package com.kazier.mobile.data.local.dao
|
||||||
|
|
||||||
import androidx.room.Dao
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.OnConflictStrategy
|
||||||
import androidx.room.Query
|
import androidx.room.Query
|
||||||
|
import androidx.room.Update
|
||||||
import androidx.room.Upsert
|
import androidx.room.Upsert
|
||||||
import com.kazier.mobile.data.local.entity.FileEntity
|
import com.kazier.mobile.data.local.entity.FileEntity
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
@@ -12,6 +15,15 @@ interface FileDao {
|
|||||||
@Query("SELECT * FROM files ORDER BY name ASC")
|
@Query("SELECT * FROM files ORDER BY name ASC")
|
||||||
suspend fun getAll(): List<FileEntity>
|
suspend fun getAll(): List<FileEntity>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Repasse C4 : lignes déjà journalisées (`exists = 0`) dont les octets
|
||||||
|
* sont peut-être encore sur le disque (journal-first, crash ou refus SAF
|
||||||
|
* entre la transaction et la suppression physique). Borné pour ne jamais
|
||||||
|
* bloquer un tick de sync.
|
||||||
|
*/
|
||||||
|
@Query("SELECT * FROM files WHERE \"exists\" = 0 AND uri IS NOT NULL ORDER BY updated_at ASC LIMIT :limit")
|
||||||
|
suspend fun getMissingWithUri(limit: Int): List<FileEntity>
|
||||||
|
|
||||||
@Query("SELECT * FROM files WHERE folder_resource_id = :folderResourceId AND \"exists\" = 1 ORDER BY name ASC")
|
@Query("SELECT * FROM files WHERE folder_resource_id = :folderResourceId AND \"exists\" = 1 ORDER BY name ASC")
|
||||||
fun observeByFolder(folderResourceId: String): Flow<List<FileEntity>>
|
fun observeByFolder(folderResourceId: String): Flow<List<FileEntity>>
|
||||||
|
|
||||||
@@ -48,6 +60,19 @@ interface FileDao {
|
|||||||
@Upsert
|
@Upsert
|
||||||
suspend fun upsert(file: FileEntity)
|
suspend fun upsert(file: FileEntity)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert sans écrasement, résolu sur TOUTES les contraintes uniques
|
||||||
|
* (`resource_id`, `uri`) — pas seulement la PK. Retourne le rowId, ou -1
|
||||||
|
* si une ligne existe déjà : le repository relit alors le gagnant et
|
||||||
|
* applique [update]. Deux walks/refresh concurrents ne lèvent jamais
|
||||||
|
* `SQLiteConstraintException`.
|
||||||
|
*/
|
||||||
|
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||||
|
suspend fun insertIgnore(file: FileEntity): Long
|
||||||
|
|
||||||
|
@Update
|
||||||
|
suspend fun update(file: FileEntity)
|
||||||
|
|
||||||
@Query("UPDATE files SET \"exists\" = 0, updated_at = :updatedAt WHERE resource_id = :resourceId")
|
@Query("UPDATE files SET \"exists\" = 0, updated_at = :updatedAt WHERE resource_id = :resourceId")
|
||||||
suspend fun markMissing(resourceId: String, updatedAt: Long)
|
suspend fun markMissing(resourceId: String, updatedAt: Long)
|
||||||
|
|
||||||
@@ -86,8 +111,8 @@ interface FileDao {
|
|||||||
@Query("UPDATE files SET folder_resource_id = :folderId, updated_at = :updatedAt WHERE resource_id IN (:resourceIds)")
|
@Query("UPDATE files SET folder_resource_id = :folderId, updated_at = :updatedAt WHERE resource_id IN (:resourceIds)")
|
||||||
suspend fun moveToFolder(resourceIds: List<String>, folderId: String, updatedAt: Long)
|
suspend fun moveToFolder(resourceIds: List<String>, folderId: String, updatedAt: Long)
|
||||||
|
|
||||||
@Query("UPDATE files SET uri = :uri WHERE resource_id = :resourceId")
|
@Query("UPDATE files SET uri = :uri, updated_at = :updatedAt WHERE resource_id = :resourceId")
|
||||||
suspend fun updateUri(resourceId: String, uri: String?)
|
suspend fun updateUri(resourceId: String, uri: String?, updatedAt: Long)
|
||||||
|
|
||||||
@Query("DELETE FROM files WHERE resource_id = :resourceId")
|
@Query("DELETE FROM files WHERE resource_id = :resourceId")
|
||||||
suspend fun remove(resourceId: String)
|
suspend fun remove(resourceId: String)
|
||||||
@@ -129,7 +154,10 @@ interface FileDao {
|
|||||||
|
|
||||||
/** Candidate OCR auto : fichier `processed`, `local-cloud`, avec copie
|
/** Candidate OCR auto : fichier `processed`, `local-cloud`, avec copie
|
||||||
* locale, format potentiellement extractible, jamais extrait, pas en cours
|
* locale, format potentiellement extractible, jamais extrait, pas en cours
|
||||||
* et pas épuisé (< 3 échecs terminaux). Trié par dernier upload. */
|
* et pas épuisé (< 3 échecs terminaux). Trié par dernier upload.
|
||||||
|
*
|
||||||
|
* Les lignes pré-v4 sans `category` (NULL) restent éligibles sur la base
|
||||||
|
* de leur `mime_type`/`extension` (backfill logique, sans migration). */
|
||||||
@Query("""
|
@Query("""
|
||||||
SELECT * FROM files
|
SELECT * FROM files
|
||||||
WHERE "exists" = 1
|
WHERE "exists" = 1
|
||||||
@@ -139,7 +167,16 @@ interface FileDao {
|
|||||||
AND ocr_text IS NULL
|
AND ocr_text IS NULL
|
||||||
AND ocr_attempts < 3
|
AND ocr_attempts < 3
|
||||||
AND ocr_queued_at IS NULL
|
AND ocr_queued_at IS NULL
|
||||||
AND category IN ('PDF', 'IMAGE', 'TEXT')
|
AND (
|
||||||
|
category IN ('PDF', 'IMAGE', 'TEXT')
|
||||||
|
OR (category IS NULL AND (
|
||||||
|
mime_type = 'application/pdf'
|
||||||
|
OR mime_type LIKE 'image/%'
|
||||||
|
OR mime_type LIKE 'text/%'
|
||||||
|
OR extension IN ('pdf', 'png', 'jpg', 'jpeg', 'webp', 'gif', 'bmp',
|
||||||
|
'txt', 'md', 'html', 'xml', 'json')
|
||||||
|
))
|
||||||
|
)
|
||||||
ORDER BY COALESCE(last_modified, added_at) DESC, name ASC
|
ORDER BY COALESCE(last_modified, added_at) DESC, name ASC
|
||||||
""")
|
""")
|
||||||
suspend fun getOcrAutoCandidates(): List<FileEntity>
|
suspend fun getOcrAutoCandidates(): List<FileEntity>
|
||||||
@@ -148,8 +185,10 @@ interface FileDao {
|
|||||||
@Query("UPDATE files SET ocr_queued_at = :queuedAt, updated_at = :now WHERE resource_id = :resourceId")
|
@Query("UPDATE files SET ocr_queued_at = :queuedAt, updated_at = :now WHERE resource_id = :resourceId")
|
||||||
suspend fun markOcrQueued(resourceId: String, queuedAt: Long, now: Long)
|
suspend fun markOcrQueued(resourceId: String, queuedAt: Long, now: Long)
|
||||||
|
|
||||||
/** Levé du marqueur + reset du compteur d'échecs (succès / erreur transitoire). */
|
/** Levé du marqueur « en cours » (succès / erreur transitoire). Le
|
||||||
@Query("UPDATE files SET ocr_queued_at = NULL, ocr_attempts = 0, updated_at = :now WHERE resource_id = :resourceId")
|
* compteur d'échecs est préservé : seul [markOcrFailure] l'incrémente,
|
||||||
|
* un succès durable passe par `updateOcrText` + `markOcrQueued` réarmé. */
|
||||||
|
@Query("UPDATE files SET ocr_queued_at = NULL, updated_at = :now WHERE resource_id = :resourceId")
|
||||||
suspend fun clearOcrQueued(resourceId: String, now: Long)
|
suspend fun clearOcrQueued(resourceId: String, now: Long)
|
||||||
|
|
||||||
/** Échec terminal : +1 tentative, marqueur levé (ne rejoue pas à l'infini). */
|
/** Échec terminal : +1 tentative, marqueur levé (ne rejoue pas à l'infini). */
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import androidx.room.Dao
|
|||||||
import androidx.room.Insert
|
import androidx.room.Insert
|
||||||
import androidx.room.OnConflictStrategy
|
import androidx.room.OnConflictStrategy
|
||||||
import androidx.room.Query
|
import androidx.room.Query
|
||||||
|
import androidx.room.Update
|
||||||
import androidx.room.Upsert
|
import androidx.room.Upsert
|
||||||
import com.kazier.mobile.data.local.entity.FolderEntity
|
import com.kazier.mobile.data.local.entity.FolderEntity
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
@@ -17,6 +18,14 @@ interface FolderDao {
|
|||||||
@Query("SELECT * FROM folders ORDER BY name ASC")
|
@Query("SELECT * FROM folders ORDER BY name ASC")
|
||||||
suspend fun getAll(): List<FolderEntity>
|
suspend fun getAll(): List<FolderEntity>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Repasse C4 (dossiers) : voir `FileDao.getMissingWithUri`. Un dossier
|
||||||
|
* n'est re-supprimé physiquement que si toute sa descendance Room est
|
||||||
|
* aussi `exists = 0` (jamais de suppression d'un arbre encore vivant).
|
||||||
|
*/
|
||||||
|
@Query("SELECT * FROM folders WHERE \"exists\" = 0 AND uri IS NOT NULL ORDER BY updated_at ASC LIMIT :limit")
|
||||||
|
suspend fun getMissingWithUri(limit: Int): List<FolderEntity>
|
||||||
|
|
||||||
/** Dossiers créés dans l'app (`created_in_app = 1`) — cibles du picker de déplacement. */
|
/** Dossiers créés dans l'app (`created_in_app = 1`) — cibles du picker de déplacement. */
|
||||||
@Query("SELECT * FROM folders WHERE \"created_in_app\" = 1 ORDER BY name ASC")
|
@Query("SELECT * FROM folders WHERE \"created_in_app\" = 1 ORDER BY name ASC")
|
||||||
suspend fun getCreatedInApp(): List<FolderEntity>
|
suspend fun getCreatedInApp(): List<FolderEntity>
|
||||||
@@ -50,6 +59,19 @@ interface FolderDao {
|
|||||||
@Upsert
|
@Upsert
|
||||||
suspend fun upsert(folder: FolderEntity)
|
suspend fun upsert(folder: FolderEntity)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert sans écrasement, résolu sur TOUTES les contraintes uniques
|
||||||
|
* (`resource_id`, `uri`) — pas seulement la PK. Retourne le rowId, ou -1
|
||||||
|
* si une ligne existe déjà : le repository relit alors le gagnant et
|
||||||
|
* applique [update]. Deux walks concurrents ne lèvent jamais
|
||||||
|
* `SQLiteConstraintException`.
|
||||||
|
*/
|
||||||
|
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||||
|
suspend fun insertIgnore(folder: FolderEntity): Long
|
||||||
|
|
||||||
|
@Update
|
||||||
|
suspend fun update(folder: FolderEntity)
|
||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
suspend fun upsertAll(folders: List<FolderEntity>)
|
suspend fun upsertAll(folders: List<FolderEntity>)
|
||||||
|
|
||||||
|
|||||||
+109
-9
@@ -27,19 +27,48 @@ interface PendingOperationDao {
|
|||||||
@Query("UPDATE pending_operations SET status = 'synced', updated_at = :now WHERE id = :id")
|
@Query("UPDATE pending_operations SET status = 'synced', updated_at = :now WHERE id = :id")
|
||||||
suspend fun markSynced(id: Long, now: Long)
|
suspend fun markSynced(id: Long, now: Long)
|
||||||
|
|
||||||
/** Échec avec bump du compteur de tentatives (dead-letter après MAX_PENDING_ATTEMPTS). */
|
/**
|
||||||
|
* Échec définitif (dead-letter immédiate) ; `attempts` est un compteur
|
||||||
|
* diagnostic (bump), pas un seuil — il n'y a aucun `MAX_PENDING_ATTEMPTS` :
|
||||||
|
* une op `failed` n'est plus resélectionnée.
|
||||||
|
*/
|
||||||
@Query("UPDATE pending_operations SET status = 'failed', attempts = attempts + 1, updated_at = :now WHERE id = :id")
|
@Query("UPDATE pending_operations SET status = 'failed', attempts = attempts + 1, updated_at = :now WHERE id = :id")
|
||||||
suspend fun markFailed(id: Long, now: Long)
|
suspend fun markFailed(id: Long, now: Long)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invalidation en cascade : un `create_resource` définitivement échoué rend
|
* Invalidation en cascade : un `create_resource` définitivement échoué rend
|
||||||
* fautives toutes les ops en attente qui ciblent la même ressource (filles).
|
* fautives toutes les ops en attente qui ciblent la même ressource, ainsi
|
||||||
|
* que les creates enfants (autre `resource_id`, liés via la colonne
|
||||||
|
* `parent_resource_id` — le walk émet parent avant enfant, cf. C6).
|
||||||
|
* Le repli `LIKE` sur le payload ne sert que les lignes pré-v14 sans
|
||||||
|
* colonne renseignée.
|
||||||
*/
|
*/
|
||||||
@Query("UPDATE pending_operations SET status = 'failed', updated_at = :now WHERE resource_id = :resourceId AND status = 'pending'")
|
@Query("""
|
||||||
|
UPDATE pending_operations SET status = 'failed', updated_at = :now
|
||||||
|
WHERE status = 'pending' AND (
|
||||||
|
resource_id = :resourceId
|
||||||
|
OR parent_resource_id = :resourceId
|
||||||
|
OR (parent_resource_id IS NULL AND payload LIKE '%"parentResourceId":"' || :resourceId || '"%')
|
||||||
|
)
|
||||||
|
""")
|
||||||
suspend fun failDescendants(resourceId: String, now: Long)
|
suspend fun failDescendants(resourceId: String, now: Long)
|
||||||
|
|
||||||
/** Rétention : purge des ops synchronisées plus anciennes que `cutoff`. */
|
/**
|
||||||
@Query("DELETE FROM pending_operations WHERE status = 'synced' AND updated_at < :cutoff")
|
* Rétention : purge des ops synchronisées plus anciennes que `cutoff`.
|
||||||
|
*
|
||||||
|
* Décision C3 — les `create_resource` synced ne sont JAMAIS purgés
|
||||||
|
* (tombstones) : les gates delete/create (`hasCreateOperation`,
|
||||||
|
* `countCreateOperations…`) et le backfill de placement sont fondés sur
|
||||||
|
* `pending+synced`. Purger un create casserait le gate delete (fantôme
|
||||||
|
* serveur indélébile) et ré-enqueue un create dupliqué au prochain
|
||||||
|
* `markProcessed`. Les autres ops synced (> 7j) sont purgées normalement.
|
||||||
|
*/
|
||||||
|
@Query("""
|
||||||
|
DELETE FROM pending_operations
|
||||||
|
WHERE status = 'synced'
|
||||||
|
AND updated_at < :cutoff
|
||||||
|
AND operation != 'create_resource'
|
||||||
|
""")
|
||||||
suspend fun purgeSynced(cutoff: Long)
|
suspend fun purgeSynced(cutoff: Long)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -69,14 +98,85 @@ interface PendingOperationDao {
|
|||||||
""")
|
""")
|
||||||
suspend fun countCreateOperationsAnyType(resourceId: String): Int
|
suspend fun countCreateOperationsAnyType(resourceId: String): Int
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Annulation (M19) : supprime les `create_resource` encore `pending` pour
|
||||||
|
* cette ressource. Appelé quand la ressource disparaît localement
|
||||||
|
* (suppression, réconciliation du walk) AVANT que le serveur l'ait connue :
|
||||||
|
* pousser un create puis un delete serait du bruit — le serveur n'a rien
|
||||||
|
* à oublier. Les creates `synced` sont conservés (tombstones C3) : le gate
|
||||||
|
* delete reste fondé dessus.
|
||||||
|
*/
|
||||||
|
@Query("""
|
||||||
|
DELETE FROM pending_operations
|
||||||
|
WHERE resource_id = :resourceId
|
||||||
|
AND operation = 'create_resource'
|
||||||
|
AND status = 'pending'
|
||||||
|
""")
|
||||||
|
suspend fun deletePendingCreates(resourceId: String): Int
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Un `create_resource` a-t-il déjà été POUSSÉ (synced) pour ce fichier ?
|
||||||
|
* Gate delete précis : après [deletePendingCreates], `hasCreateOperation`
|
||||||
|
* (pending+synced) y est équivalent — ce compteur l'exprime directement.
|
||||||
|
*/
|
||||||
|
@Query("""
|
||||||
|
SELECT COUNT(*) FROM pending_operations
|
||||||
|
WHERE resource_id = :resourceId
|
||||||
|
AND operation = 'create_resource'
|
||||||
|
AND resource_type = 'file'
|
||||||
|
AND status = 'synced'
|
||||||
|
""")
|
||||||
|
suspend fun countSyncedCreateOperations(resourceId: String): Int
|
||||||
|
|
||||||
|
/** Variante sans filtre `resource_type` (dossiers) de [countSyncedCreateOperations]. */
|
||||||
|
@Query("""
|
||||||
|
SELECT COUNT(*) FROM pending_operations
|
||||||
|
WHERE resource_id = :resourceId
|
||||||
|
AND operation = 'create_resource'
|
||||||
|
AND status = 'synced'
|
||||||
|
""")
|
||||||
|
suspend fun countSyncedCreateOperationsAnyType(resourceId: String): Int
|
||||||
|
|
||||||
@Query("SELECT COUNT(*) FROM pending_operations WHERE status = 'pending'")
|
@Query("SELECT COUNT(*) FROM pending_operations WHERE status = 'pending'")
|
||||||
suspend fun countPending(): Int
|
suspend fun countPending(): Int
|
||||||
|
|
||||||
|
/** Compteur diagnostique par statut (tests, badge « liste des sync »). */
|
||||||
|
@Query("SELECT COUNT(*) FROM pending_operations WHERE status = :status")
|
||||||
|
suspend fun countByStatus(status: String): Int
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Une op `move_resource` encore en attente de push existe-t-elle pour cette
|
* Abandon de la file non poussée (C5) : supprime les ops `pending` et
|
||||||
* ressource ? L'antichambre outbox est alors la source de vérité du
|
* `failed` — jamais les `synced`. Au changement de compte (sign-out, ou
|
||||||
* placement : ni le refresh serveur ni le walk SAF ne doivent écraser le
|
* login d'un autre user), rejouer la file de A sous le token de B
|
||||||
* `folder_resource_id` local avant que le serveur ait accusé le move.
|
* appliquerait ses mutations dans le scope de B (`NOT_FOUND`,
|
||||||
|
* `NAME_CONFLICT`, voire créations croisées). Les ops `synced` sont
|
||||||
|
* conservées : ce sont des tombstones (C3) fondant les gates
|
||||||
|
* delete/create et le backfill — elles ne sont plus jamais sélectionnées
|
||||||
|
* (`selectPending` ne lit que `pending`) donc sans risque de rejeu.
|
||||||
|
*/
|
||||||
|
@Query("DELETE FROM pending_operations WHERE status IN ('pending', 'failed')")
|
||||||
|
suspend fun deleteUnpushed(): Int
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Un `delete_resource` est-il encore en attente de push pour cette
|
||||||
|
* ressource ? Le walk SAF ne doit alors jamais la ressusciter (`exists =
|
||||||
|
* 1`) : l'intention de suppression est déjà journalisée (ordre
|
||||||
|
* journal-first, C4) et la repasse physique va suivre. Sans ce garde, le
|
||||||
|
* walk réimporterait une ressource que le serveur s'apprête à supprimer.
|
||||||
|
*/
|
||||||
|
@Query("""
|
||||||
|
SELECT COUNT(*) FROM pending_operations
|
||||||
|
WHERE resource_id = :resourceId
|
||||||
|
AND operation = 'delete_resource'
|
||||||
|
AND status = 'pending'
|
||||||
|
""")
|
||||||
|
suspend fun countPendingDeleteOperations(resourceId: String): Int
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Un `move_resource` est-il encore en attente de push ? Tant que l'op est
|
||||||
|
* pendante, le `folder_resource_id` local reflète la cible future : ni le
|
||||||
|
* refresh serveur (qui renvoie l'ancien dossier) ni le walk SAF (snapshot
|
||||||
|
* périmé) ne doivent l'écraser.
|
||||||
*/
|
*/
|
||||||
@Query("""
|
@Query("""
|
||||||
SELECT COUNT(*) FROM pending_operations
|
SELECT COUNT(*) FROM pending_operations
|
||||||
|
|||||||
@@ -45,6 +45,9 @@ interface ScanDao {
|
|||||||
@Query("DELETE FROM scan_pages WHERE id = :pageId AND session_id = :sessionId")
|
@Query("DELETE FROM scan_pages WHERE id = :pageId AND session_id = :sessionId")
|
||||||
suspend fun deletePage(pageId: Long, sessionId: Long)
|
suspend fun deletePage(pageId: Long, sessionId: Long)
|
||||||
|
|
||||||
|
@Query("DELETE FROM scan_sessions WHERE id = :sessionId")
|
||||||
|
suspend fun deleteSession(sessionId: Long)
|
||||||
|
|
||||||
@Query(
|
@Query(
|
||||||
"""
|
"""
|
||||||
UPDATE scan_pages SET sort_order = CASE id
|
UPDATE scan_pages SET sort_order = CASE id
|
||||||
@@ -61,4 +64,12 @@ interface ScanDao {
|
|||||||
|
|
||||||
@Query("SELECT COUNT(*) FROM scan_pages WHERE session_id = :sessionId")
|
@Query("SELECT COUNT(*) FROM scan_pages WHERE session_id = :sessionId")
|
||||||
suspend fun pageCount(sessionId: Long): Int
|
suspend fun pageCount(sessionId: Long): Int
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plus grand `sort_order` de la session (NULL si vide) : l'ordre d'une
|
||||||
|
* nouvelle page est `MAX + 1`, jamais `COUNT` (qui collisionne après
|
||||||
|
* suppression : A,B → del A → C réutiliserait l'ordre de B).
|
||||||
|
*/
|
||||||
|
@Query("SELECT MAX(sort_order) FROM scan_pages WHERE session_id = :sessionId")
|
||||||
|
suspend fun maxSortOrder(sessionId: Long): Int?
|
||||||
}
|
}
|
||||||
@@ -22,6 +22,7 @@ import androidx.room.PrimaryKey
|
|||||||
Index(value = ["uri"], unique = true),
|
Index(value = ["uri"], unique = true),
|
||||||
Index(value = ["folder_resource_id"]),
|
Index(value = ["folder_resource_id"]),
|
||||||
Index(value = ["category"]),
|
Index(value = ["category"]),
|
||||||
|
Index(value = ["exists", "processed", "sync_status"]),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
data class FileEntity(
|
data class FileEntity(
|
||||||
|
|||||||
+14
-1
@@ -20,14 +20,18 @@ import androidx.room.PrimaryKey
|
|||||||
@Entity(
|
@Entity(
|
||||||
tableName = "pending_operations",
|
tableName = "pending_operations",
|
||||||
indices = [
|
indices = [
|
||||||
Index(value = ["operation_id"], unique = true),
|
Index(value = ["device_id", "operation_id"], unique = true),
|
||||||
Index(value = ["status"]),
|
Index(value = ["status"]),
|
||||||
Index(value = ["resource_id"]),
|
Index(value = ["resource_id"]),
|
||||||
|
Index(value = ["resource_id", "operation", "status"]),
|
||||||
|
Index(value = ["parent_resource_id"]),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
data class PendingOperationEntity(
|
data class PendingOperationEntity(
|
||||||
@PrimaryKey(autoGenerate = true)
|
@PrimaryKey(autoGenerate = true)
|
||||||
val id: Long = 0L,
|
val id: Long = 0L,
|
||||||
|
@ColumnInfo(name = "device_id")
|
||||||
|
val deviceId: String? = null,
|
||||||
@ColumnInfo(name = "operation_id")
|
@ColumnInfo(name = "operation_id")
|
||||||
val operationId: String,
|
val operationId: String,
|
||||||
@ColumnInfo(name = "ref_type")
|
@ColumnInfo(name = "ref_type")
|
||||||
@@ -42,6 +46,15 @@ data class PendingOperationEntity(
|
|||||||
val operation: String,
|
val operation: String,
|
||||||
@ColumnInfo(name = "payload")
|
@ColumnInfo(name = "payload")
|
||||||
val payload: String,
|
val payload: String,
|
||||||
|
/**
|
||||||
|
* Lien parent→enfant dénormalisé (P8) : `resource_id` du dossier parent
|
||||||
|
* pour un `create_resource` avec parent (les enfants portent un autre
|
||||||
|
* `resource_id`). Évite de parser le JSON du payload (`LIKE`) pour la
|
||||||
|
* cascade d'invalidation — robuste au format de sérialisation Moshi.
|
||||||
|
* NULL pour les ops sans parent et les lignes pré-v14 (repli LIKE).
|
||||||
|
*/
|
||||||
|
@ColumnInfo(name = "parent_resource_id")
|
||||||
|
val parentResourceId: String? = null,
|
||||||
@ColumnInfo(name = "status")
|
@ColumnInfo(name = "status")
|
||||||
val status: String = PendingOpStatus.PENDING,
|
val status: String = PendingOpStatus.PENDING,
|
||||||
@ColumnInfo(name = "attempts")
|
@ColumnInfo(name = "attempts")
|
||||||
|
|||||||
+66
-1
@@ -26,6 +26,13 @@ import androidx.sqlite.db.SupportSQLiteDatabase
|
|||||||
* l'app (fichier cloud-only, uri = NULL). Null pour les fichiers importés.
|
* l'app (fichier cloud-only, uri = NULL). Null pour les fichiers importés.
|
||||||
* v10 : ajout colonne `ocr_text` sur `files` — extrait OCR serveur persisté
|
* v10 : ajout colonne `ocr_text` sur `files` — extrait OCR serveur persisté
|
||||||
* localement (résultat d'un job `POST /ocr/jobs`, réaffichable sans relance).
|
* localement (résultat d'un job `POST /ocr/jobs`, réaffichable sans relance).
|
||||||
|
* v11 : `ocr_attempts` + `ocr_queued_at` sur `files` (driver OCR auto).
|
||||||
|
* v12 : ajout colonne `device_id` sur `pending_operations` + unicité
|
||||||
|
* composite `UNIQUE(device_id, operation_id)` (contrat docs/api-v1.md §6.1) ;
|
||||||
|
* backfill depuis `user_preferences.device_user_id` pour les lignes existantes.
|
||||||
|
* v13 : index composites `files(exists, processed, sync_status)` (review +
|
||||||
|
* candidats OCR auto) et `pending_operations(resource_id, operation, status)`
|
||||||
|
* (gates outbox create/move) — requêtes à chaud sans full-table scan.
|
||||||
*/
|
*/
|
||||||
object Migrations {
|
object Migrations {
|
||||||
|
|
||||||
@@ -92,6 +99,14 @@ object Migrations {
|
|||||||
db.execSQL("ALTER TABLE `files` ADD COLUMN `processed` INTEGER NOT NULL DEFAULT 0")
|
db.execSQL("ALTER TABLE `files` ADD COLUMN `processed` INTEGER NOT NULL DEFAULT 0")
|
||||||
// Backlog = déjà traité : la file de review ne contient que les
|
// Backlog = déjà traité : la file de review ne contient que les
|
||||||
// fichiers découverts après l'activation de la fonctionnalité.
|
// fichiers découverts après l'activation de la fonctionnalité.
|
||||||
|
//
|
||||||
|
// Décision M11 (documentée, assumée) : aucun `create_resource`
|
||||||
|
// rétroactif n'est journalisé pour ce backlog — ces fichiers ne
|
||||||
|
// seront jamais poussés vers le serveur (ni vizibles en review).
|
||||||
|
// Un backfill applicatif créerait des doublons serveur pour les
|
||||||
|
// ressources déjà connues par d'autres chemins (scan, second
|
||||||
|
// device) sans moyen fiable de les distinguer : la perte silencieuse
|
||||||
|
// du backlog pré-v6 est préférée à une résurrection massive.
|
||||||
db.execSQL("UPDATE `files` SET `processed` = 1")
|
db.execSQL("UPDATE `files` SET `processed` = 1")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -181,7 +196,57 @@ object Migrations {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val MIGRATION_11_12 = object : Migration(11, 12) {
|
||||||
|
override fun migrate(db: SupportSQLiteDatabase) {
|
||||||
|
db.execSQL("ALTER TABLE `pending_operations` ADD COLUMN `device_id` TEXT")
|
||||||
|
// Backfill : les ops pré-v12 héritent du device courant (une seule
|
||||||
|
// identité device par base en pratique).
|
||||||
|
db.execSQL(
|
||||||
|
"""
|
||||||
|
UPDATE `pending_operations` SET `device_id` =
|
||||||
|
(SELECT `value` FROM `user_preferences` WHERE `key` = 'device_user_id')
|
||||||
|
WHERE `device_id` IS NULL
|
||||||
|
""".trimIndent(),
|
||||||
|
)
|
||||||
|
db.execSQL("DROP INDEX IF EXISTS `index_pending_operations_operation_id`")
|
||||||
|
db.execSQL(
|
||||||
|
"CREATE UNIQUE INDEX IF NOT EXISTS " +
|
||||||
|
"`index_pending_operations_device_id_operation_id` " +
|
||||||
|
"ON `pending_operations` (`device_id`, `operation_id`)",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val MIGRATION_12_13 = object : Migration(12, 13) {
|
||||||
|
override fun migrate(db: SupportSQLiteDatabase) {
|
||||||
|
db.execSQL(
|
||||||
|
"CREATE INDEX IF NOT EXISTS `index_files_exists_processed_sync_status` " +
|
||||||
|
"ON `files` (`exists`, `processed`, `sync_status`)",
|
||||||
|
)
|
||||||
|
db.execSQL(
|
||||||
|
"CREATE INDEX IF NOT EXISTS `index_pending_operations_resource_id_operation_status` " +
|
||||||
|
"ON `pending_operations` (`resource_id`, `operation`, `status`)",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* P8 : colonne `parent_resource_id` dénormalisée (cascade sans parsing
|
||||||
|
* JSON). Les lignes pré-v14 gardent NULL et restent couvertes par le repli
|
||||||
|
* `LIKE` de `failDescendants` — pas de backfill (le `json_extract`
|
||||||
|
* SQLite n'est pas garanti sur tous les niveaux d'API).
|
||||||
|
*/
|
||||||
|
private val MIGRATION_13_14 = object : Migration(13, 14) {
|
||||||
|
override fun migrate(db: SupportSQLiteDatabase) {
|
||||||
|
db.execSQL("ALTER TABLE `pending_operations` ADD COLUMN `parent_resource_id` TEXT")
|
||||||
|
db.execSQL(
|
||||||
|
"CREATE INDEX IF NOT EXISTS `index_pending_operations_parent_resource_id` " +
|
||||||
|
"ON `pending_operations` (`parent_resource_id`)",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val ALL: Array<Migration> = arrayOf(
|
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_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,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -1,22 +1,29 @@
|
|||||||
package com.kazier.mobile.data.remote
|
package com.kazier.mobile.data.remote
|
||||||
|
|
||||||
import com.squareup.moshi.JsonAdapter
|
import com.squareup.moshi.JsonAdapter
|
||||||
|
import com.squareup.moshi.JsonDataException
|
||||||
|
import com.squareup.moshi.JsonEncodingException
|
||||||
import com.squareup.moshi.Moshi
|
import com.squareup.moshi.Moshi
|
||||||
import com.kazier.mobile.data.remote.dto.ApiEnvelope
|
import com.kazier.mobile.data.remote.dto.ApiEnvelope
|
||||||
import com.kazier.mobile.data.remote.dto.ApiErrorEnvelope
|
import com.kazier.mobile.data.remote.dto.ApiErrorEnvelope
|
||||||
|
import com.kazier.mobile.data.remote.dto.ApiMeta
|
||||||
|
import com.kazier.mobile.data.remote.dto.ChangePasswordDto
|
||||||
import com.kazier.mobile.data.remote.dto.DeviceRegistrationDto
|
import com.kazier.mobile.data.remote.dto.DeviceRegistrationDto
|
||||||
import com.kazier.mobile.data.remote.dto.FileDto
|
import com.kazier.mobile.data.remote.dto.FileDto
|
||||||
import com.kazier.mobile.data.remote.dto.FileOcrDto
|
import com.kazier.mobile.data.remote.dto.FileOcrDto
|
||||||
import com.kazier.mobile.data.remote.dto.FolderDto
|
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.LoginRequestDto
|
||||||
import com.kazier.mobile.data.remote.dto.LoginResponseDto
|
import com.kazier.mobile.data.remote.dto.LoginResponseDto
|
||||||
import com.kazier.mobile.data.remote.dto.OcrJobDto
|
import com.kazier.mobile.data.remote.dto.OcrJobDto
|
||||||
import com.kazier.mobile.data.remote.dto.OcrSubmitDto
|
import com.kazier.mobile.data.remote.dto.OcrSubmitDto
|
||||||
import com.kazier.mobile.data.remote.dto.ResolvedUserDto
|
import com.kazier.mobile.data.remote.dto.ResolvedUserDto
|
||||||
import com.kazier.mobile.data.remote.dto.ResourcePermissionDto
|
import com.kazier.mobile.data.remote.dto.ResourcePermissionDto
|
||||||
|
import com.kazier.mobile.data.remote.dto.ShareLinkDto
|
||||||
import com.kazier.mobile.data.remote.dto.SyncOpDto
|
import com.kazier.mobile.data.remote.dto.SyncOpDto
|
||||||
import com.kazier.mobile.data.remote.dto.SyncOpsRequest
|
import com.kazier.mobile.data.remote.dto.SyncOpsRequest
|
||||||
import com.kazier.mobile.data.remote.dto.SyncOpsResult
|
import com.kazier.mobile.data.remote.dto.SyncOpsResult
|
||||||
|
import kotlin.coroutines.cancellation.CancellationException
|
||||||
import okhttp3.MultipartBody
|
import okhttp3.MultipartBody
|
||||||
import okhttp3.MediaType.Companion.toMediaType
|
import okhttp3.MediaType.Companion.toMediaType
|
||||||
import okhttp3.RequestBody
|
import okhttp3.RequestBody
|
||||||
@@ -44,7 +51,12 @@ class ApiException(
|
|||||||
* - non-2xx → `ApiException` (code du body si présent, sinon `HTTP_<status>`),
|
* - non-2xx → `ApiException` (code du body si présent, sinon `HTTP_<status>`),
|
||||||
* et un 401 sur un endpoint protégé déclenche `onUnauthorized` (sauf si
|
* et un 401 sur un endpoint protégé déclenche `onUnauthorized` (sauf si
|
||||||
* `skipUnauthorizedHandling` — cas du login, où 401 = identifiants erronés) ;
|
* `skipUnauthorizedHandling` — cas du login, où 401 = identifiants erronés) ;
|
||||||
* - 2xx sans `data` (ou corps illisible) → `INVALID_RESPONSE`.
|
* - 2xx sans `data` (ou corps illisible) → `INVALID_RESPONSE` ;
|
||||||
|
* - un corps non parsable (Moshi) → `INVALID_RESPONSE`, jamais
|
||||||
|
* `NETWORK_ERROR` (pas de `retry()` infini sur contrat cassé) ;
|
||||||
|
* - `CancellationException` n'est jamais avalée (scopes/Worker sains).
|
||||||
|
* Les routes publiques (`/devices`, `/auth/login`, `/shares/links/:token`)
|
||||||
|
* désactivent le handling 401 : y purger la session serait faux.
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
class ApiClient @Inject constructor(
|
class ApiClient @Inject constructor(
|
||||||
@@ -55,7 +67,7 @@ class ApiClient @Inject constructor(
|
|||||||
private val errorEnvelopeAdapter: JsonAdapter<ApiErrorEnvelope> =
|
private val errorEnvelopeAdapter: JsonAdapter<ApiErrorEnvelope> =
|
||||||
moshi.adapter(ApiErrorEnvelope::class.java)
|
moshi.adapter(ApiErrorEnvelope::class.java)
|
||||||
|
|
||||||
/** Callback 401 (expiration/révocation) — branché par AuthViewModel. */
|
/** Callback 401 (expiration/révocation) — branché par SessionManager. */
|
||||||
@Volatile
|
@Volatile
|
||||||
var onUnauthorized: (() -> Unit)? = null
|
var onUnauthorized: (() -> Unit)? = null
|
||||||
|
|
||||||
@@ -64,8 +76,20 @@ class ApiClient @Inject constructor(
|
|||||||
suspend fun listFiles(folderId: String? = null, page: Int? = null, pageSize: Int? = null): List<FileDto> =
|
suspend fun listFiles(folderId: String? = null, page: Int? = null, pageSize: Int? = null): List<FileDto> =
|
||||||
unwrap({ apiService.listFiles(folderId, page, pageSize) })
|
unwrap({ apiService.listFiles(folderId, page, pageSize) })
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Variante paginée exposant `meta` (page/pageSize/total) — seule base
|
||||||
|
* saine pour drainer un dossier au-delà de la 1re page (M21).
|
||||||
|
*/
|
||||||
|
suspend fun listFilesPaged(
|
||||||
|
folderId: String? = null,
|
||||||
|
page: Int? = null,
|
||||||
|
pageSize: Int? = null,
|
||||||
|
): Pair<List<FileDto>, ApiMeta?> =
|
||||||
|
unwrapWithMeta({ apiService.listFiles(folderId, page, pageSize) })
|
||||||
|
|
||||||
suspend fun registerDevice(deviceId: String): String =
|
suspend fun registerDevice(deviceId: String): String =
|
||||||
unwrap({ apiService.registerDevice(DeviceRegistrationDto(deviceId)) }).deviceId
|
unwrap({ apiService.registerDevice(DeviceRegistrationDto(deviceId)) },
|
||||||
|
skipUnauthorizedHandling = true).deviceId
|
||||||
|
|
||||||
suspend fun login(username: String, password: String, deviceId: String): LoginResponseDto =
|
suspend fun login(username: String, password: String, deviceId: String): LoginResponseDto =
|
||||||
unwrap({ apiService.login(LoginRequestDto(username, password, deviceId)) },
|
unwrap({ apiService.login(LoginRequestDto(username, password, deviceId)) },
|
||||||
@@ -110,16 +134,59 @@ class ApiClient @Inject constructor(
|
|||||||
suspend fun getFileOcr(fileId: String): FileOcrDto =
|
suspend fun getFileOcr(fileId: String): FileOcrDto =
|
||||||
unwrap({ apiService.getFileOcr(fileId) })
|
unwrap({ apiService.getFileOcr(fileId) })
|
||||||
|
|
||||||
|
/** Détail serveur d'un fichier visible — `GET /files/:id`. */
|
||||||
|
suspend fun getFile(fileId: String): FileDto =
|
||||||
|
unwrap({ apiService.getFile(fileId) })
|
||||||
|
|
||||||
|
/** Suppression serveur — `DELETE /files/:id`, retourne l'id supprimé. */
|
||||||
|
suspend fun deleteFile(fileId: String): String =
|
||||||
|
unwrap({ apiService.deleteFile(fileId) }).id
|
||||||
|
|
||||||
|
/** Recherche serveur paginée — `GET /files/search?q=…` (+ `meta`). */
|
||||||
|
suspend fun searchFilesPaged(
|
||||||
|
query: String,
|
||||||
|
page: Int? = null,
|
||||||
|
pageSize: Int? = null,
|
||||||
|
): Pair<List<FileDto>, ApiMeta?> =
|
||||||
|
unwrapWithMeta({ apiService.searchFiles(query, page, pageSize) })
|
||||||
|
|
||||||
|
/** Rotation du mot de passe — `PATCH /users/me/password`. */
|
||||||
|
suspend fun changePassword(currentPassword: String, newPassword: String): String =
|
||||||
|
unwrap({ apiService.changePassword(ChangePasswordDto(currentPassword, newPassword)) }).id
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Résolution anonyme d'un lien de partage — `GET /shares/links/:token`
|
||||||
|
* (route publique : le 401 n'a aucun sens ici, handling désactivé).
|
||||||
|
*/
|
||||||
|
suspend fun getShareLink(token: String): ShareLinkDto =
|
||||||
|
unwrap({ apiService.getShareLink(token) }, skipUnauthorizedHandling = true)
|
||||||
|
|
||||||
private suspend fun <T> unwrap(
|
private suspend fun <T> unwrap(
|
||||||
call: suspend () -> Response<ApiEnvelope<T>>,
|
call: suspend () -> Response<ApiEnvelope<T>>,
|
||||||
skipUnauthorizedHandling: Boolean = false,
|
skipUnauthorizedHandling: Boolean = false,
|
||||||
): T {
|
): T = unwrapWithMeta(call, skipUnauthorizedHandling).first
|
||||||
|
|
||||||
|
private suspend fun <T> unwrapWithMeta(
|
||||||
|
call: suspend () -> Response<ApiEnvelope<T>>,
|
||||||
|
skipUnauthorizedHandling: Boolean = false,
|
||||||
|
): Pair<T, ApiMeta?> {
|
||||||
val response = try {
|
val response = try {
|
||||||
call()
|
call()
|
||||||
|
} catch (e: CancellationException) {
|
||||||
|
// Ne jamais avaler l'annulation : la convertir en ApiException
|
||||||
|
// corromprait les scopes et le WorkManager (M22).
|
||||||
|
throw e
|
||||||
|
} catch (e: JsonDataException) {
|
||||||
|
Timber.w(e, "api: corps inattendu (parsing)")
|
||||||
|
throw ApiException("INVALID_RESPONSE", "Réponse serveur invalide", 0)
|
||||||
|
} catch (e: JsonEncodingException) {
|
||||||
|
Timber.w(e, "api: corps inencodable (parsing)")
|
||||||
|
throw ApiException("INVALID_RESPONSE", "Réponse serveur invalide", 0)
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
Timber.d(e, "api: transport error")
|
Timber.d(e, "api: transport error")
|
||||||
throw ApiException("NETWORK_ERROR", "Serveur injoignable", 0)
|
throw ApiException("NETWORK_ERROR", "Serveur injoignable", 0)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
if (e is CancellationException) throw e
|
||||||
Timber.d(e, "api: unexpected error")
|
Timber.d(e, "api: unexpected error")
|
||||||
throw ApiException("NETWORK_ERROR", "Serveur injoignable", 0)
|
throw ApiException("NETWORK_ERROR", "Serveur injoignable", 0)
|
||||||
}
|
}
|
||||||
@@ -151,6 +218,6 @@ class ApiClient @Inject constructor(
|
|||||||
httpCode = response.code(),
|
httpCode = response.code(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return data
|
return data to envelope.meta
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,24 +1,29 @@
|
|||||||
package com.kazier.mobile.data.remote
|
package com.kazier.mobile.data.remote
|
||||||
|
|
||||||
import com.kazier.mobile.data.remote.dto.ApiEnvelope
|
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.DeviceRegistrationDto
|
||||||
import com.kazier.mobile.data.remote.dto.FileDto
|
import com.kazier.mobile.data.remote.dto.FileDto
|
||||||
import com.kazier.mobile.data.remote.dto.FileOcrDto
|
import com.kazier.mobile.data.remote.dto.FileOcrDto
|
||||||
import com.kazier.mobile.data.remote.dto.FolderDto
|
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.LoginRequestDto
|
||||||
import com.kazier.mobile.data.remote.dto.LoginResponseDto
|
import com.kazier.mobile.data.remote.dto.LoginResponseDto
|
||||||
import com.kazier.mobile.data.remote.dto.OcrJobDto
|
import com.kazier.mobile.data.remote.dto.OcrJobDto
|
||||||
import com.kazier.mobile.data.remote.dto.OcrSubmitDto
|
import com.kazier.mobile.data.remote.dto.OcrSubmitDto
|
||||||
import com.kazier.mobile.data.remote.dto.ResolvedUserDto
|
import com.kazier.mobile.data.remote.dto.ResolvedUserDto
|
||||||
import com.kazier.mobile.data.remote.dto.ResourcePermissionDto
|
import com.kazier.mobile.data.remote.dto.ResourcePermissionDto
|
||||||
|
import com.kazier.mobile.data.remote.dto.ShareLinkDto
|
||||||
import com.kazier.mobile.data.remote.dto.SyncOpsRequest
|
import com.kazier.mobile.data.remote.dto.SyncOpsRequest
|
||||||
import com.kazier.mobile.data.remote.dto.SyncOpsResult
|
import com.kazier.mobile.data.remote.dto.SyncOpsResult
|
||||||
import okhttp3.MultipartBody
|
import okhttp3.MultipartBody
|
||||||
import okhttp3.RequestBody
|
import okhttp3.RequestBody
|
||||||
import retrofit2.Response
|
import retrofit2.Response
|
||||||
import retrofit2.http.Body
|
import retrofit2.http.Body
|
||||||
|
import retrofit2.http.DELETE
|
||||||
import retrofit2.http.GET
|
import retrofit2.http.GET
|
||||||
import retrofit2.http.Multipart
|
import retrofit2.http.Multipart
|
||||||
|
import retrofit2.http.PATCH
|
||||||
import retrofit2.http.POST
|
import retrofit2.http.POST
|
||||||
import retrofit2.http.Part
|
import retrofit2.http.Part
|
||||||
import retrofit2.http.Path
|
import retrofit2.http.Path
|
||||||
@@ -107,4 +112,46 @@ interface ApiService {
|
|||||||
suspend fun getFileOcr(
|
suspend fun getFileOcr(
|
||||||
@Path("id") fileId: String,
|
@Path("id") fileId: String,
|
||||||
): Response<ApiEnvelope<FileOcrDto>>
|
): Response<ApiEnvelope<FileOcrDto>>
|
||||||
|
|
||||||
|
/** Détail d'un fichier visible (own ou shared) — `GET /files/:id`. */
|
||||||
|
@GET("files/{id}")
|
||||||
|
suspend fun getFile(
|
||||||
|
@Path("id") fileId: String,
|
||||||
|
): Response<ApiEnvelope<FileDto>>
|
||||||
|
|
||||||
|
/** Suppression serveur d'un fichier possédé — `DELETE /files/:id`. */
|
||||||
|
@DELETE("files/{id}")
|
||||||
|
suspend fun deleteFile(
|
||||||
|
@Path("id") fileId: String,
|
||||||
|
): Response<ApiEnvelope<IdResultDto>>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recherche serveur (`GET /files/search?q=…`, paginé `meta`). Non consommée
|
||||||
|
* en V1 (recherche 100 % locale via Room) — exposée pour parité contrat.
|
||||||
|
*/
|
||||||
|
@GET("files/search")
|
||||||
|
suspend fun searchFiles(
|
||||||
|
@Query("q") query: String,
|
||||||
|
@Query("page") page: Int?,
|
||||||
|
@Query("pageSize") pageSize: Int?,
|
||||||
|
): Response<ApiEnvelope<List<FileDto>>>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rotation du mot de passe du compte connecté (`PATCH /users/me/password`,
|
||||||
|
* `current_password` requis). Limite V1 : les tokens déjà émis restent
|
||||||
|
* valides jusqu'à expiration (7 j, pas de révocation).
|
||||||
|
*/
|
||||||
|
@PATCH("users/me/password")
|
||||||
|
suspend fun changePassword(
|
||||||
|
@Body body: ChangePasswordDto,
|
||||||
|
): Response<ApiEnvelope<IdResultDto>>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Résolution anonyme d'un lien de partage (`GET /shares/links/:token`,
|
||||||
|
* route publique — jamais de `Bearer`, cf. `AuthInterceptor`).
|
||||||
|
*/
|
||||||
|
@GET("shares/links/{token}")
|
||||||
|
suspend fun getShareLink(
|
||||||
|
@Path("token") token: String,
|
||||||
|
): Response<ApiEnvelope<ShareLinkDto>>
|
||||||
}
|
}
|
||||||
@@ -7,9 +7,12 @@ import javax.inject.Singleton
|
|||||||
import com.kazier.mobile.auth.TokenProvider
|
import com.kazier.mobile.auth.TokenProvider
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Injecte `Authorization: Bearer <token>` sur chaque requête si un token est
|
* Injecte `Authorization: Bearer <token>` sur chaque requête protégée si un
|
||||||
* disponible. Sans token (mode local / non connecté), aucune entête n'est posée
|
* token est disponible. Les routes publiques (`POST /devices`,
|
||||||
* — le serveur répondra 401, géré proprement par `ApiClient`.
|
* `POST /auth/login`, `GET /shares/links/:token`, `/health`) ne reçoivent
|
||||||
|
* jamais de `Bearer` (hygiène + pas d'amplification de fuite, cf. M26/m26).
|
||||||
|
* Sans token (mode local / non connecté), aucune entête n'est posée — le
|
||||||
|
* serveur répondra 401, géré proprement par `ApiClient`.
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
class AuthInterceptor @Inject constructor(
|
class AuthInterceptor @Inject constructor(
|
||||||
@@ -17,11 +20,31 @@ class AuthInterceptor @Inject constructor(
|
|||||||
) : Interceptor {
|
) : Interceptor {
|
||||||
|
|
||||||
override fun intercept(chain: Interceptor.Chain): Response {
|
override fun intercept(chain: Interceptor.Chain): Response {
|
||||||
|
val request = chain.request()
|
||||||
|
val requestBuilder = request.newBuilder()
|
||||||
val token = tokenProvider.current
|
val token = tokenProvider.current
|
||||||
val requestBuilder = chain.request().newBuilder()
|
if (token != null && !isPublic(request.url.encodedPath)) {
|
||||||
if (token != null) {
|
|
||||||
requestBuilder.header("Authorization", "Bearer $token")
|
requestBuilder.header("Authorization", "Bearer $token")
|
||||||
}
|
}
|
||||||
return chain.proceed(requestBuilder.build())
|
return chain.proceed(requestBuilder.build())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
/**
|
||||||
|
* Segments terminaux des routes publiques V1 (après réécriture par
|
||||||
|
* `ServerUrlInterceptor`, la base inclut `/api/v1` — on matche en
|
||||||
|
* suffixe pour rester indépendant du préfixe configuré).
|
||||||
|
*/
|
||||||
|
private val PUBLIC_SUFFIXES = listOf(
|
||||||
|
"/devices",
|
||||||
|
"/auth/login",
|
||||||
|
"/health",
|
||||||
|
)
|
||||||
|
|
||||||
|
fun isPublic(encodedPath: String): Boolean {
|
||||||
|
if (PUBLIC_SUFFIXES.any { encodedPath.endsWith(it) }) return true
|
||||||
|
// Lien de partage anonyme : /api/v1/shares/links/:token.
|
||||||
|
return encodedPath.contains("/shares/links/")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+20
-1
@@ -3,6 +3,7 @@ package com.kazier.mobile.data.remote
|
|||||||
import com.kazier.mobile.domain.ServerConfigStore
|
import com.kazier.mobile.domain.ServerConfigStore
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||||
import okhttp3.Interceptor
|
import okhttp3.Interceptor
|
||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
@@ -13,6 +14,11 @@ import timber.log.Timber
|
|||||||
* La base Retrofit n'est qu'un placeholder ; c'est cet interceptor qui décide
|
* La base Retrofit n'est qu'un placeholder ; c'est cet interceptor qui décide
|
||||||
* du schéma/host/port réels, en préservant le préfixe de chemin de la base
|
* du schéma/host/port réels, en préservant le préfixe de chemin de la base
|
||||||
* configurée (ex. `/api/v1`) ainsi que la query d'origine.
|
* configurée (ex. `/api/v1`) ainsi que la query d'origine.
|
||||||
|
*
|
||||||
|
* Garde M26 : si le `load()` du cold start n'a pas encore restauré l'URL
|
||||||
|
* persistée, l'interceptor le fait lui-même (bloquant, thread OkHttp — jamais
|
||||||
|
* le Main) au lieu de laisser partir la requête vers l'hôte par défaut
|
||||||
|
* (avec le `Bearer` restauré — fuite).
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
class ServerUrlInterceptor @Inject constructor(
|
class ServerUrlInterceptor @Inject constructor(
|
||||||
@@ -22,6 +28,10 @@ class ServerUrlInterceptor @Inject constructor(
|
|||||||
override fun intercept(chain: Interceptor.Chain): Response {
|
override fun intercept(chain: Interceptor.Chain): Response {
|
||||||
val request = chain.request()
|
val request = chain.request()
|
||||||
|
|
||||||
|
if (!serverConfigStore.isLoaded) {
|
||||||
|
runBlocking { serverConfigStore.load() }
|
||||||
|
}
|
||||||
|
|
||||||
val targetUrl = runCatching {
|
val targetUrl = runCatching {
|
||||||
(serverConfigStore.current.trimEnd('/') + "/").toHttpUrl()
|
(serverConfigStore.current.trimEnd('/') + "/").toHttpUrl()
|
||||||
}.getOrNull()
|
}.getOrNull()
|
||||||
@@ -32,8 +42,17 @@ class ServerUrlInterceptor @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
val original = request.url
|
val original = request.url
|
||||||
|
// La base Retrofit placeholder contient déjà le préfixe de chemin
|
||||||
|
// (`/api/v1`) : ne pas le dupliquer quand le chemin d'origine le porte.
|
||||||
|
val basePath = targetUrl.encodedPath.trimEnd('/')
|
||||||
|
val origPath = original.encodedPath
|
||||||
|
val mergedPath = if (basePath.isNotEmpty() && (origPath == basePath || origPath.startsWith("$basePath/"))) {
|
||||||
|
origPath
|
||||||
|
} else {
|
||||||
|
basePath + "/" + origPath.trimStart('/')
|
||||||
|
}
|
||||||
val rewritten = targetUrl.newBuilder()
|
val rewritten = targetUrl.newBuilder()
|
||||||
.encodedPath(targetUrl.encodedPath.trimEnd('/') + "/" + original.encodedPath.trimStart('/'))
|
.encodedPath(mergedPath)
|
||||||
.encodedQuery(original.encodedQuery)
|
.encodedQuery(original.encodedQuery)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
|
|||||||
@@ -88,6 +88,30 @@ data class LoginResponseDto(
|
|||||||
@Json(name = "user") val user: UserDto,
|
@Json(name = "user") val user: UserDto,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/** Requête `PATCH /users/me/password` — `{ current_password, new_password }`. */
|
||||||
|
data class ChangePasswordDto(
|
||||||
|
@Json(name = "current_password") val currentPassword: String,
|
||||||
|
@Json(name = "new_password") val newPassword: String,
|
||||||
|
)
|
||||||
|
|
||||||
|
/** Réponse `PATCH /users/me/password` et `DELETE /files/:id` — `{ id }`. */
|
||||||
|
data class IdResultDto(
|
||||||
|
@Json(name = "id") val id: String,
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Réponse `GET /shares/links/:token` — métadonnées publiques du lien
|
||||||
|
* (jamais d'identité du propriétaire au-delà du contrat).
|
||||||
|
*/
|
||||||
|
data class ShareLinkDto(
|
||||||
|
@Json(name = "token") val token: String,
|
||||||
|
@Json(name = "resource_id") val resourceId: String,
|
||||||
|
@Json(name = "resourceType") val resourceType: String,
|
||||||
|
@Json(name = "name") val name: String,
|
||||||
|
@Json(name = "access") val access: String,
|
||||||
|
@Json(name = "expiresAt") val expiresAt: String? = null,
|
||||||
|
)
|
||||||
|
|
||||||
/** Outbox — une op du batch `POST /sync/ops` (docs/api-v1.md §6.1). */
|
/** Outbox — une op du batch `POST /sync/ops` (docs/api-v1.md §6.1). */
|
||||||
data class SyncOpDto(
|
data class SyncOpDto(
|
||||||
@Json(name = "operation_id") val operationId: String,
|
@Json(name = "operation_id") val operationId: String,
|
||||||
|
|||||||
+32
-6
@@ -1,8 +1,10 @@
|
|||||||
package com.kazier.mobile.data.repository
|
package com.kazier.mobile.data.repository
|
||||||
|
|
||||||
import com.kazier.mobile.data.remote.ApiClient
|
import com.kazier.mobile.data.remote.ApiClient
|
||||||
|
import com.kazier.mobile.data.remote.ApiException
|
||||||
import com.kazier.mobile.data.remote.dto.LoginResponseDto
|
import com.kazier.mobile.data.remote.dto.LoginResponseDto
|
||||||
import com.kazier.mobile.domain.DeviceIdentity
|
import com.kazier.mobile.domain.DeviceIdentity
|
||||||
|
import kotlin.coroutines.cancellation.CancellationException
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
@@ -17,11 +19,25 @@ class AuthRepository @Inject constructor(
|
|||||||
private val deviceIdentity: DeviceIdentity,
|
private val deviceIdentity: DeviceIdentity,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
/** Enregistrement best-effort du device (offline → ignoré). */
|
/**
|
||||||
|
* Enregistrement best-effort du device : seules les erreurs réseau et 5xx
|
||||||
|
* sont ignorées (offline → retry au prochain tick/login). Une 4xx
|
||||||
|
* (400 `INVALID_DEVICE_ID`, 429 `RATE_LIMITED`, …) est remontée — l'avaler
|
||||||
|
* rendrait le 401 du login incompréhensible (m23).
|
||||||
|
*/
|
||||||
suspend fun registerDevice(): String {
|
suspend fun registerDevice(): String {
|
||||||
val deviceId = deviceIdentity.getOrCreate()
|
val deviceId = deviceIdentity.getOrCreate()
|
||||||
runCatching { apiClient.registerDevice(deviceId) }
|
try {
|
||||||
.onFailure { Timber.d(it, "auth: device registration failed (offline?)") }
|
apiClient.registerDevice(deviceId)
|
||||||
|
} catch (e: CancellationException) {
|
||||||
|
throw e
|
||||||
|
} catch (e: ApiException) {
|
||||||
|
if (e.httpCode == 0 || e.httpCode >= 500) {
|
||||||
|
Timber.d(e, "auth: device registration failed (offline?)")
|
||||||
|
} else {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
return deviceId
|
return deviceId
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,8 +45,18 @@ class AuthRepository @Inject constructor(
|
|||||||
val deviceId = deviceIdentity.getOrCreate()
|
val deviceId = deviceIdentity.getOrCreate()
|
||||||
// Re-registration idempotente juste avant le login : un device inconnu
|
// Re-registration idempotente juste avant le login : un device inconnu
|
||||||
// du serveur (ex. restart de la base) répondrait INVALID_DEVICE_ID.
|
// du serveur (ex. restart de la base) répondrait INVALID_DEVICE_ID.
|
||||||
runCatching { apiClient.registerDevice(deviceId) }
|
// Même politique que registerDevice : 4xx remontée, réseau/5xx ignorés.
|
||||||
.onFailure { Timber.d(it, "auth: pre-login registration failed") }
|
try {
|
||||||
|
apiClient.registerDevice(deviceId)
|
||||||
|
} catch (e: CancellationException) {
|
||||||
|
throw e
|
||||||
|
} catch (e: ApiException) {
|
||||||
|
if (e.httpCode == 0 || e.httpCode >= 500) {
|
||||||
|
Timber.d(e, "auth: pre-login registration failed")
|
||||||
|
} else {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
return apiClient.login(username.trim(), password, deviceId)
|
return apiClient.login(username.trim(), password, deviceId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+87
-19
@@ -3,6 +3,7 @@ package com.kazier.mobile.data.repository
|
|||||||
import androidx.room.withTransaction
|
import androidx.room.withTransaction
|
||||||
import com.kazier.mobile.data.local.AppDatabase
|
import com.kazier.mobile.data.local.AppDatabase
|
||||||
import com.kazier.mobile.data.local.dao.FileDao
|
import com.kazier.mobile.data.local.dao.FileDao
|
||||||
|
import com.kazier.mobile.data.local.dao.FolderDao
|
||||||
import com.kazier.mobile.data.local.entity.FileEntity
|
import com.kazier.mobile.data.local.entity.FileEntity
|
||||||
import com.kazier.mobile.data.local.entity.FileStatus
|
import com.kazier.mobile.data.local.entity.FileStatus
|
||||||
import com.kazier.mobile.data.remote.ApiClient
|
import com.kazier.mobile.data.remote.ApiClient
|
||||||
@@ -27,6 +28,7 @@ import javax.inject.Singleton
|
|||||||
@Singleton
|
@Singleton
|
||||||
class FileRepository @Inject constructor(
|
class FileRepository @Inject constructor(
|
||||||
private val fileDao: FileDao,
|
private val fileDao: FileDao,
|
||||||
|
private val folderDao: FolderDao,
|
||||||
private val apiClient: ApiClient,
|
private val apiClient: ApiClient,
|
||||||
private val generateId: GenerateId,
|
private val generateId: GenerateId,
|
||||||
private val appDatabase: AppDatabase,
|
private val appDatabase: AppDatabase,
|
||||||
@@ -154,7 +156,7 @@ class FileRepository @Inject constructor(
|
|||||||
updatedAt = now,
|
updatedAt = now,
|
||||||
)
|
)
|
||||||
appDatabase.withTransaction {
|
appDatabase.withTransaction {
|
||||||
fileDao.upsert(entity)
|
upsertTolerant(entity)
|
||||||
outboxRepository.enqueueCreateResource(
|
outboxRepository.enqueueCreateResource(
|
||||||
resourceId = entity.resourceId,
|
resourceId = entity.resourceId,
|
||||||
resourceType = "file",
|
resourceType = "file",
|
||||||
@@ -184,11 +186,32 @@ class FileRepository @Inject constructor(
|
|||||||
|
|
||||||
val category = computeCategory(input.mimeType, input.extension).dbValue
|
val category = computeCategory(input.mimeType, input.extension).dbValue
|
||||||
|
|
||||||
|
// Garde anti-résurrection (C4) : un `delete_resource` déjà journalisé
|
||||||
|
// (ordre journal-first) interdit de réimporter la ressource — le
|
||||||
|
// serveur s'apprête à la supprimer et la repasse physique va suivre.
|
||||||
|
if (existing != null && outboxRepository.hasPendingDeleteOperation(existing.resourceId)) {
|
||||||
|
if (existing.exists != 0) fileDao.markMissing(existing.resourceId, now)
|
||||||
|
return existing.copy(exists = 0, updatedAt = now)
|
||||||
|
}
|
||||||
|
|
||||||
// Un `move_resource` encore en attente fait de la cible outbox la
|
// Un `move_resource` encore en attente fait de la cible outbox la
|
||||||
// source de vérité du placement : un snapshot SAF périmé (walk lancé
|
// source de vérité du placement : un snapshot SAF périmé (walk lancé
|
||||||
// avant le move) ne doit pas ré-attribuer le dossier à l'ancien parent.
|
// avant le move) ne doit pas ré-attribuer le dossier à l'ancien parent.
|
||||||
val movePending = existing != null && outboxRepository.hasPendingMoveOperation(existing.resourceId)
|
val movePending = existing != null && outboxRepository.hasPendingMoveOperation(existing.resourceId)
|
||||||
val effectiveFolder = if (movePending) existing.folderResourceId else folderResourceId
|
val effectiveFolder = if (movePending) existing.folderResourceId else folderResourceId
|
||||||
|
// Pas de FK `files → folders` : le parent est validé ici (orphelin
|
||||||
|
// refusé au lieu d'une ligne fantôme jamais affichable).
|
||||||
|
require(folderDao.getByResourceId(effectiveFolder) != null) {
|
||||||
|
"unknown parent folder: $effectiveFolder"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Un fichier cloud-only (snapshot serveur) réimporté en SAF devient
|
||||||
|
// `local-cloud` : le serveur le connaît déjà, la copie physique aussi.
|
||||||
|
val placement = if (existing != null && existing.uri == null && existing.syncStatus == FileStatus.CLOUD) {
|
||||||
|
SyncPlacement.confirmed(hasLocalUri = true)
|
||||||
|
} else {
|
||||||
|
existing?.syncStatus ?: input.syncStatus ?: FileStatus.LOCAL
|
||||||
|
}
|
||||||
|
|
||||||
val entity = FileEntity(
|
val entity = FileEntity(
|
||||||
id = existing?.id ?: 0L,
|
id = existing?.id ?: 0L,
|
||||||
@@ -203,7 +226,7 @@ class FileRepository @Inject constructor(
|
|||||||
exists = if (input.exists) 1 else 0,
|
exists = if (input.exists) 1 else 0,
|
||||||
lastModified = input.lastModified,
|
lastModified = input.lastModified,
|
||||||
ownerId = ownerId ?: existing?.ownerId,
|
ownerId = ownerId ?: existing?.ownerId,
|
||||||
syncStatus = existing?.syncStatus ?: input.syncStatus ?: FileStatus.LOCAL,
|
syncStatus = placement,
|
||||||
// Une ligne existante liée reste à son état ; une nouvelle ligne est
|
// Une ligne existante liée reste à son état ; une nouvelle ligne est
|
||||||
// « traitée » si la source l'a demandé (ex. scan), sinon à traiter.
|
// « traitée » si la source l'a demandé (ex. scan), sinon à traiter.
|
||||||
processed = existing?.processed ?: input.processed,
|
processed = existing?.processed ?: input.processed,
|
||||||
@@ -211,7 +234,7 @@ class FileRepository @Inject constructor(
|
|||||||
updatedAt = now,
|
updatedAt = now,
|
||||||
)
|
)
|
||||||
appDatabase.withTransaction {
|
appDatabase.withTransaction {
|
||||||
fileDao.upsert(entity)
|
upsertTolerant(entity)
|
||||||
if (existing == null && entity.processed) {
|
if (existing == null && entity.processed) {
|
||||||
// Nouveau fichier physique déjà « traité » (ex. scan export) :
|
// Nouveau fichier physique déjà « traité » (ex. scan export) :
|
||||||
// poussé immédiatement. Un fichier SAF tout juste découvert
|
// poussé immédiatement. Un fichier SAF tout juste découvert
|
||||||
@@ -234,24 +257,35 @@ class FileRepository @Inject constructor(
|
|||||||
suspend fun markMissing(resourceId: String, updatedAt: Long) =
|
suspend fun markMissing(resourceId: String, updatedAt: Long) =
|
||||||
fileDao.markMissing(resourceId, updatedAt)
|
fileDao.markMissing(resourceId, updatedAt)
|
||||||
|
|
||||||
/** Met à jour la cible dossier d'un fichier (déplacement local / cloud-only). */
|
/** Met à jour la cible dossier d'un fichier (déplacement local / cloud-only).
|
||||||
|
*
|
||||||
|
* Le `move_resource` n'est journalisé que si le serveur connaît déjà la
|
||||||
|
* ressource (gate `create`) : déplacer un fichier jamais poussé est une
|
||||||
|
* opération purement locale, un move serveur serait `NOT_FOUND`/no-op et
|
||||||
|
* figerait la ligne derrière `hasPendingMoveOperation`. */
|
||||||
suspend fun applyMove(resourceId: String, folderId: String, newUri: String?) {
|
suspend fun applyMove(resourceId: String, folderId: String, newUri: String?) {
|
||||||
|
require(folderDao.getByResourceId(folderId) != null) {
|
||||||
|
"unknown target folder: $folderId"
|
||||||
|
}
|
||||||
val now = System.currentTimeMillis()
|
val now = System.currentTimeMillis()
|
||||||
appDatabase.withTransaction {
|
appDatabase.withTransaction {
|
||||||
fileDao.moveToFolder(listOf(resourceId), folderId, now)
|
fileDao.moveToFolder(listOf(resourceId), folderId, now)
|
||||||
if (newUri != null) fileDao.updateUri(resourceId, newUri)
|
if (newUri != null) fileDao.updateUri(resourceId, newUri, now)
|
||||||
// Déplacement reflété localement ←→ poussé vers le serveur.
|
// Déplacement reflété localement ←→ poussé vers le serveur.
|
||||||
outboxRepository.enqueueMoveResource(
|
if (outboxRepository.hasCreateOperation(resourceId)) {
|
||||||
resourceId = resourceId,
|
outboxRepository.enqueueMoveResource(
|
||||||
resourceType = "file",
|
resourceId = resourceId,
|
||||||
toFolderResourceId = folderId,
|
resourceType = "file",
|
||||||
)
|
toFolderResourceId = folderId,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* `GET /files?folderId=...` (1re page, tri serveur) puis upsert cloud de
|
* `GET /files?folderId=...` drainé page par page en suivant `meta`
|
||||||
* chaque fichier. Ne supprime jamais de lignes locales.
|
* (page/pageSize/total), puis upsert cloud de chaque fichier. Ne supprime
|
||||||
|
* jamais de lignes locales.
|
||||||
*
|
*
|
||||||
* Garde-fou outbox : un fichier dont le `move_resource` n'a pas encore été
|
* Garde-fou outbox : un fichier dont le `move_resource` n'a pas encore été
|
||||||
* poussé garde son placement local. Le serveur renvoie encore l'ancien
|
* poussé garde son placement local. Le serveur renvoie encore l'ancien
|
||||||
@@ -259,15 +293,47 @@ class FileRepository @Inject constructor(
|
|||||||
* du dossier cible (et réapparaître dans l'ancien).
|
* du dossier cible (et réapparaître dans l'ancien).
|
||||||
*/
|
*/
|
||||||
suspend fun refreshFromServer(folderResourceId: String) {
|
suspend fun refreshFromServer(folderResourceId: String) {
|
||||||
val files = apiClient.listFiles(folderId = folderResourceId, pageSize = PAGE_SIZE)
|
|
||||||
if (files.isEmpty()) return
|
|
||||||
val now = System.currentTimeMillis()
|
val now = System.currentTimeMillis()
|
||||||
files.forEach { dto ->
|
var page = 1
|
||||||
if (outboxRepository.hasPendingMoveOperation(dto.id)) {
|
while (page <= MAX_PAGES) {
|
||||||
// L'antichambre outbox fait foi tant que le move n'est pas synced.
|
val (files, meta) = apiClient.listFilesPaged(
|
||||||
return@forEach
|
folderId = folderResourceId,
|
||||||
|
page = page,
|
||||||
|
pageSize = PAGE_SIZE,
|
||||||
|
)
|
||||||
|
files.forEach { dto ->
|
||||||
|
if (outboxRepository.hasPendingMoveOperation(dto.id)) {
|
||||||
|
// L'antichambre outbox fait foi tant que le move n'est pas synced.
|
||||||
|
return@forEach
|
||||||
|
}
|
||||||
|
upsertTolerant(toEntity(dto, folderResourceId, now))
|
||||||
}
|
}
|
||||||
fileDao.upsert(toEntity(dto, folderResourceId, now))
|
if (files.isEmpty()) break
|
||||||
|
val total = meta?.total
|
||||||
|
if (total != null) {
|
||||||
|
// `total` contractuel : arrêter dès la couverture atteinte.
|
||||||
|
if (page * PAGE_SIZE >= total) break
|
||||||
|
} else if (files.size < PAGE_SIZE) {
|
||||||
|
// Sans `meta` (vieux serveur) : dernière page partielle.
|
||||||
|
break
|
||||||
|
}
|
||||||
|
page++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upsert résolu sur les contraintes uniques (`resource_id`, `uri`), pas
|
||||||
|
* sur la PK : insert-or-ignore puis update du gagnant en cas de conflit.
|
||||||
|
* Les courses walk/refresh ne lèvent jamais `SQLiteConstraintException`.
|
||||||
|
*/
|
||||||
|
private suspend fun upsertTolerant(entity: FileEntity) {
|
||||||
|
if (fileDao.insertIgnore(entity) != -1L) return
|
||||||
|
val winner = entity.uri?.let { fileDao.getByUri(it) }
|
||||||
|
?: fileDao.getByResourceId(entity.resourceId)
|
||||||
|
if (winner != null) {
|
||||||
|
fileDao.update(entity.copy(id = winner.id, resourceId = winner.resourceId))
|
||||||
|
} else {
|
||||||
|
fileDao.upsert(entity)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -301,6 +367,8 @@ class FileRepository @Inject constructor(
|
|||||||
companion object {
|
companion object {
|
||||||
private const val PAGE_SIZE = 50
|
private const val PAGE_SIZE = 50
|
||||||
private const val RECENT_LIMIT = 5
|
private const val RECENT_LIMIT = 5
|
||||||
|
/** Garde anti-boucle si le serveur renvoie un `total` incohérent. */
|
||||||
|
private const val MAX_PAGES = 2000
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+73
-14
@@ -7,7 +7,7 @@ import com.kazier.mobile.data.local.entity.FolderEntity
|
|||||||
import com.kazier.mobile.data.local.entity.FolderStatus
|
import com.kazier.mobile.data.local.entity.FolderStatus
|
||||||
import com.kazier.mobile.data.remote.ApiClient
|
import com.kazier.mobile.data.remote.ApiClient
|
||||||
import com.kazier.mobile.data.remote.dto.FolderDto
|
import com.kazier.mobile.data.remote.dto.FolderDto
|
||||||
import com.kazier.mobile.domain.DeviceIdentity
|
import com.kazier.mobile.domain.ActiveUserStore
|
||||||
import com.kazier.mobile.domain.GenerateId
|
import com.kazier.mobile.domain.GenerateId
|
||||||
import com.kazier.mobile.domain.SyncPlacement
|
import com.kazier.mobile.domain.SyncPlacement
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
@@ -26,7 +26,7 @@ class FolderRepository @Inject constructor(
|
|||||||
private val folderDao: FolderDao,
|
private val folderDao: FolderDao,
|
||||||
private val apiClient: ApiClient,
|
private val apiClient: ApiClient,
|
||||||
private val generateId: GenerateId,
|
private val generateId: GenerateId,
|
||||||
private val deviceIdentity: DeviceIdentity,
|
private val activeUserStore: ActiveUserStore,
|
||||||
private val appDatabase: AppDatabase,
|
private val appDatabase: AppDatabase,
|
||||||
private val outboxRepository: OutboxRepository,
|
private val outboxRepository: OutboxRepository,
|
||||||
) {
|
) {
|
||||||
@@ -66,7 +66,14 @@ class FolderRepository @Inject constructor(
|
|||||||
val folders = apiClient.listFolders()
|
val folders = apiClient.listFolders()
|
||||||
if (folders.isEmpty()) return
|
if (folders.isEmpty()) return
|
||||||
val now = System.currentTimeMillis()
|
val now = System.currentTimeMillis()
|
||||||
folderDao.upsertAll(folders.map { toEntity(it, now) })
|
val rows = folders
|
||||||
|
.filterNot { dto ->
|
||||||
|
// L'antichambre outbox fait foi tant que le move n'est pas
|
||||||
|
// synced : le serveur renvoie encore l'ancien parent.
|
||||||
|
outboxRepository.hasPendingMoveOperation(dto.id)
|
||||||
|
}
|
||||||
|
.map { toEntity(it, now) }
|
||||||
|
if (rows.isNotEmpty()) folderDao.upsertAll(rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun toEntity(dto: FolderDto, now: Long): FolderEntity {
|
private suspend fun toEntity(dto: FolderDto, now: Long): FolderEntity {
|
||||||
@@ -93,8 +100,10 @@ class FolderRepository @Inject constructor(
|
|||||||
* ajoute une ligne avec sa `uri` si absente, ou met à jour ses champs.
|
* ajoute une ligne avec sa `uri` si absente, ou met à jour ses champs.
|
||||||
*
|
*
|
||||||
* `parentResourceId` non-null écrase le parent existant (un déplacement dans
|
* `parentResourceId` non-null écrase le parent existant (un déplacement dans
|
||||||
* l'arborescence SAF est reflété) ; null préserve le parent courant. `ownerId`
|
* l'arborescence SAF est reflété) ; null préserve le parent courant — sauf
|
||||||
* optionnel évite de relire l'identité device à chaque ligne d'un walk.
|
* `move_resource` en attente, qui fait de la cible outbox la source de
|
||||||
|
* vérité du placement (miroir du garde fichiers). `ownerId`
|
||||||
|
* optionnel évite de relire le compte actif à chaque ligne d'un walk.
|
||||||
*/
|
*/
|
||||||
suspend fun saveFolder(
|
suspend fun saveFolder(
|
||||||
input: SaveFolderInput,
|
input: SaveFolderInput,
|
||||||
@@ -105,21 +114,47 @@ class FolderRepository @Inject constructor(
|
|||||||
val existing = input.resourceId?.let { folderDao.getByResourceId(it) }
|
val existing = input.resourceId?.let { folderDao.getByResourceId(it) }
|
||||||
?: input.uri?.let { folderDao.getByUri(it) }
|
?: input.uri?.let { folderDao.getByUri(it) }
|
||||||
|
|
||||||
|
// Garde anti-résurrection (C4) : voir `FileRepository.saveLocalFile`.
|
||||||
|
if (existing != null && outboxRepository.hasPendingDeleteOperation(existing.resourceId)) {
|
||||||
|
if (existing.exists != 0) folderDao.markMissing(existing.resourceId, now)
|
||||||
|
return existing.copy(exists = 0, updatedAt = now)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Garde pending-move (M1) : un snapshot SAF périmé ne revert jamais un
|
||||||
|
// reparent/merge en cours de push.
|
||||||
|
val movePending = existing != null && outboxRepository.hasPendingMoveOperation(existing.resourceId)
|
||||||
|
val effectiveParent = if (movePending) existing.parentResourceId else (parentResourceId ?: existing?.parentResourceId)
|
||||||
|
// Pas de FK `files → folders` : le parent est validé ici (orphelin
|
||||||
|
// refusé au lieu d'une ligne fantôme jamais affichable).
|
||||||
|
if (effectiveParent != null && folderDao.getByResourceId(effectiveParent) == null) {
|
||||||
|
throw IllegalArgumentException("unknown parent folder: $effectiveParent")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Un dossier cloud-only réimporté en SAF devient `local-cloud`.
|
||||||
|
val placement = if (existing != null && existing.uri == null && input.uri != null &&
|
||||||
|
existing.syncStatus == FolderStatus.CLOUD
|
||||||
|
) {
|
||||||
|
SyncPlacement.confirmed(hasLocalUri = true)
|
||||||
|
} else {
|
||||||
|
existing?.syncStatus ?: FolderStatus.LOCAL
|
||||||
|
}
|
||||||
|
|
||||||
val entity = FolderEntity(
|
val entity = FolderEntity(
|
||||||
id = existing?.id ?: 0L,
|
id = existing?.id ?: 0L,
|
||||||
resourceId = existing?.resourceId ?: input.resourceId ?: generateId.newResourceId(),
|
resourceId = existing?.resourceId ?: input.resourceId ?: generateId.newResourceId(),
|
||||||
uri = input.uri,
|
uri = input.uri,
|
||||||
name = input.name,
|
name = input.name,
|
||||||
exists = input.exists?.let { if (it) 1 else 0 },
|
exists = input.exists?.let { if (it) 1 else 0 },
|
||||||
parentResourceId = parentResourceId ?: existing?.parentResourceId,
|
parentResourceId = effectiveParent,
|
||||||
ownerId = ownerId ?: existing?.ownerId ?: deviceIdentity.getOrCreate(),
|
// `owner_id` Room = user_id du compte actif, jamais l'identité device.
|
||||||
syncStatus = existing?.syncStatus ?: FolderStatus.LOCAL,
|
ownerId = ownerId ?: existing?.ownerId ?: activeUserStore.get(),
|
||||||
|
syncStatus = placement,
|
||||||
createdInApp = existing?.createdInApp ?: input.createdInApp,
|
createdInApp = existing?.createdInApp ?: input.createdInApp,
|
||||||
addedAt = existing?.addedAt ?: now,
|
addedAt = existing?.addedAt ?: now,
|
||||||
updatedAt = now,
|
updatedAt = now,
|
||||||
)
|
)
|
||||||
appDatabase.withTransaction {
|
appDatabase.withTransaction {
|
||||||
folderDao.upsert(entity)
|
upsertTolerant(entity)
|
||||||
if (existing == null) {
|
if (existing == null) {
|
||||||
// Nouvelle ressource physique → la pousser vers le serveur.
|
// Nouvelle ressource physique → la pousser vers le serveur.
|
||||||
outboxRepository.enqueueCreateResource(
|
outboxRepository.enqueueCreateResource(
|
||||||
@@ -140,21 +175,45 @@ class FolderRepository @Inject constructor(
|
|||||||
/**
|
/**
|
||||||
* Re-parente des sous-dossiers vers un nouveau parent (fusion de dossiers).
|
* Re-parente des sous-dossiers vers un nouveau parent (fusion de dossiers).
|
||||||
* Transaction atomique : reparent Room + outbox `move_resource` par dossier.
|
* Transaction atomique : reparent Room + outbox `move_resource` par dossier.
|
||||||
|
*
|
||||||
|
* Le `move_resource` n'est journalisé que si le serveur connaît déjà le
|
||||||
|
* dossier (gate `create`, miroir fichiers) : sinon le move serait
|
||||||
|
* `NOT_FOUND` et figerait la ligne derrière `hasPendingMoveOperation`.
|
||||||
*/
|
*/
|
||||||
suspend fun reparentSubFolders(resourceIds: List<String>, newParentId: String) {
|
suspend fun reparentSubFolders(resourceIds: List<String>, newParentId: String) {
|
||||||
if (resourceIds.isEmpty()) return
|
if (resourceIds.isEmpty()) return
|
||||||
|
require(folderDao.getByResourceId(newParentId) != null) {
|
||||||
|
"unknown target folder: $newParentId"
|
||||||
|
}
|
||||||
val now = System.currentTimeMillis()
|
val now = System.currentTimeMillis()
|
||||||
appDatabase.withTransaction {
|
appDatabase.withTransaction {
|
||||||
folderDao.reparent(resourceIds, newParentId, now)
|
folderDao.reparent(resourceIds, newParentId, now)
|
||||||
for (resourceId in resourceIds) {
|
for (resourceId in resourceIds) {
|
||||||
outboxRepository.enqueueMoveResource(
|
if (outboxRepository.hasCreateOperationAnyType(resourceId)) {
|
||||||
resourceId = resourceId,
|
outboxRepository.enqueueMoveResource(
|
||||||
resourceType = "folder",
|
resourceId = resourceId,
|
||||||
toFolderResourceId = newParentId,
|
resourceType = "folder",
|
||||||
)
|
toFolderResourceId = newParentId,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upsert résolu sur les contraintes uniques (`resource_id`, `uri`), pas
|
||||||
|
* sur la PK : insert-or-ignore puis update du gagnant en cas de conflit.
|
||||||
|
*/
|
||||||
|
private suspend fun upsertTolerant(entity: FolderEntity) {
|
||||||
|
if (folderDao.insertIgnore(entity) != -1L) return
|
||||||
|
val winner = entity.uri?.let { folderDao.getByUri(it) }
|
||||||
|
?: folderDao.getByResourceId(entity.resourceId)
|
||||||
|
if (winner != null) {
|
||||||
|
folderDao.update(entity.copy(id = winner.id, resourceId = winner.resourceId))
|
||||||
|
} else {
|
||||||
|
folderDao.upsert(entity)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data class SaveFolderInput(
|
data class SaveFolderInput(
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import com.kazier.mobile.data.local.dao.PendingOperationDao
|
|||||||
import com.kazier.mobile.data.local.entity.PendingOpStatus
|
import com.kazier.mobile.data.local.entity.PendingOpStatus
|
||||||
import com.kazier.mobile.data.local.entity.PendingOperationEntity
|
import com.kazier.mobile.data.local.entity.PendingOperationEntity
|
||||||
import com.kazier.mobile.data.local.entity.PendingOperationType
|
import com.kazier.mobile.data.local.entity.PendingOperationType
|
||||||
|
import com.kazier.mobile.domain.DeviceIdentity
|
||||||
import com.kazier.mobile.domain.GenerateId
|
import com.kazier.mobile.domain.GenerateId
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
@@ -23,6 +24,7 @@ import javax.inject.Singleton
|
|||||||
class OutboxRepository @Inject constructor(
|
class OutboxRepository @Inject constructor(
|
||||||
private val pendingOperationDao: PendingOperationDao,
|
private val pendingOperationDao: PendingOperationDao,
|
||||||
private val generateId: GenerateId,
|
private val generateId: GenerateId,
|
||||||
|
private val deviceIdentity: DeviceIdentity,
|
||||||
moshi: Moshi,
|
moshi: Moshi,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
@@ -46,6 +48,9 @@ class OutboxRepository @Inject constructor(
|
|||||||
val now = System.currentTimeMillis()
|
val now = System.currentTimeMillis()
|
||||||
pendingOperationDao.insert(
|
pendingOperationDao.insert(
|
||||||
PendingOperationEntity(
|
PendingOperationEntity(
|
||||||
|
// Clé d'idempotence locale UNIQUE(device_id, operation_id)
|
||||||
|
// (contrat docs/api-v1.md §6.1).
|
||||||
|
deviceId = deviceIdentity.getOrCreate(),
|
||||||
operationId = operationId,
|
operationId = operationId,
|
||||||
refType = refType,
|
refType = refType,
|
||||||
refId = refId,
|
refId = refId,
|
||||||
@@ -53,6 +58,9 @@ class OutboxRepository @Inject constructor(
|
|||||||
resourceType = resourceType,
|
resourceType = resourceType,
|
||||||
operation = operation,
|
operation = operation,
|
||||||
payload = payloadAdapter.toJson(payload),
|
payload = payloadAdapter.toJson(payload),
|
||||||
|
// Lien parent→enfant dénormalisé pour la cascade (P8) —
|
||||||
|
// même clé que le payload JSON ci-dessous.
|
||||||
|
parentResourceId = payload[PARENT_PAYLOAD_KEY] as? String,
|
||||||
status = PendingOpStatus.PENDING,
|
status = PendingOpStatus.PENDING,
|
||||||
createdAt = now,
|
createdAt = now,
|
||||||
updatedAt = now,
|
updatedAt = now,
|
||||||
@@ -158,6 +166,27 @@ class OutboxRepository @Inject constructor(
|
|||||||
suspend fun hasCreateOperationAnyType(resourceId: String): Boolean =
|
suspend fun hasCreateOperationAnyType(resourceId: String): Boolean =
|
||||||
pendingOperationDao.countCreateOperationsAnyType(resourceId) > 0
|
pendingOperationDao.countCreateOperationsAnyType(resourceId) > 0
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Le serveur connaît-il déjà ce fichier (create `synced`) ? Gate delete
|
||||||
|
* précis (M19) : après [cancelPendingCreates], un `delete_resource` n'est
|
||||||
|
* émis que dans ce cas — jamais pour une ressource que le serveur n'a
|
||||||
|
* jamais vue (ni bruit create-puis-delete, ni `NOT_FOUND`).
|
||||||
|
*/
|
||||||
|
suspend fun hasSyncedCreateOperation(resourceId: String): Boolean =
|
||||||
|
pendingOperationDao.countSyncedCreateOperations(resourceId) > 0
|
||||||
|
|
||||||
|
/** Variante dossiers de [hasSyncedCreateOperation]. */
|
||||||
|
suspend fun hasSyncedCreateOperationAnyType(resourceId: String): Boolean =
|
||||||
|
pendingOperationDao.countSyncedCreateOperationsAnyType(resourceId) > 0
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Annulation (M19) : supprime les `create_resource` encore `pending` pour
|
||||||
|
* cette ressource qui disparaît localement avant que le serveur l'ait
|
||||||
|
* connue. Retourne le nombre d'ops annulées.
|
||||||
|
*/
|
||||||
|
suspend fun cancelPendingCreates(resourceId: String): Int =
|
||||||
|
pendingOperationDao.deletePendingCreates(resourceId)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Un `move_resource` est-il encore en attente de push ? Tant que l'op est
|
* Un `move_resource` est-il encore en attente de push ? Tant que l'op est
|
||||||
* pendante, le `folder_resource_id` local reflète la cible future : ni le
|
* pendante, le `folder_resource_id` local reflète la cible future : ni le
|
||||||
@@ -167,6 +196,23 @@ class OutboxRepository @Inject constructor(
|
|||||||
suspend fun hasPendingMoveOperation(resourceId: String): Boolean =
|
suspend fun hasPendingMoveOperation(resourceId: String): Boolean =
|
||||||
pendingOperationDao.countPendingMoveOperations(resourceId) > 0
|
pendingOperationDao.countPendingMoveOperations(resourceId) > 0
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Un `delete_resource` est-il encore en attente de push ? Garde
|
||||||
|
* anti-résurrection du walk (C4, voir DAO) : une ressource dont la
|
||||||
|
* suppression est journalisée ne doit plus être réimportée.
|
||||||
|
*/
|
||||||
|
suspend fun hasPendingDeleteOperation(resourceId: String): Boolean =
|
||||||
|
pendingOperationDao.countPendingDeleteOperations(resourceId) > 0
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
/**
|
||||||
|
* Clé du lien parent→enfant, à la fois dans le payload JSON
|
||||||
|
* `create_resource` (protocole, lu par `OutboxSyncWorker`) et dans la
|
||||||
|
* colonne dénormalisée `parent_resource_id` (cascade SQL).
|
||||||
|
*/
|
||||||
|
const val PARENT_PAYLOAD_KEY = "parentResourceId"
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Un `move_resource` (pending ou synced) a-t-il jamais été journalisé ?
|
* Un `move_resource` (pending ou synced) a-t-il jamais été journalisé ?
|
||||||
* Utilisé par la réconciliation du walk : une ligne en transition physique
|
* Utilisé par la réconciliation du walk : une ligne en transition physique
|
||||||
|
|||||||
+39
-17
@@ -9,6 +9,8 @@ import com.kazier.mobile.data.local.entity.ScanSessionEntity
|
|||||||
import com.kazier.mobile.data.local.entity.ScanSessionStatus
|
import com.kazier.mobile.data.local.entity.ScanSessionStatus
|
||||||
import com.kazier.mobile.domain.GenerateId
|
import com.kazier.mobile.domain.GenerateId
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@@ -24,20 +26,28 @@ class ScanRepository @Inject constructor(
|
|||||||
private val generateId: GenerateId,
|
private val generateId: GenerateId,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
suspend fun getOrCreateActiveSession(rootFolderId: String?): ScanSessionEntity {
|
/**
|
||||||
scanDao.getActiveSession()?.let { return it }
|
* Single-flight de création de session : sans garde, deux appels
|
||||||
val now = System.currentTimeMillis()
|
* concurrents créent deux sessions `active` (aucune unicité partielle
|
||||||
val sessionId = scanDao.insertSession(
|
* côté Room) et orphelinent les pages de l'une d'elles.
|
||||||
ScanSessionEntity(
|
*/
|
||||||
resourceId = generateId.newResourceId(),
|
private val sessionMutex = Mutex()
|
||||||
rootFolderId = rootFolderId,
|
|
||||||
status = ScanSessionStatus.ACTIVE,
|
suspend fun getOrCreateActiveSession(rootFolderId: String?): ScanSessionEntity =
|
||||||
createdAt = now,
|
sessionMutex.withLock {
|
||||||
updatedAt = now,
|
scanDao.getActiveSession()?.let { return@withLock it }
|
||||||
),
|
val now = System.currentTimeMillis()
|
||||||
)
|
val sessionId = scanDao.insertSession(
|
||||||
return checkNotNull(scanDao.getSession(sessionId))
|
ScanSessionEntity(
|
||||||
}
|
resourceId = generateId.newResourceId(),
|
||||||
|
rootFolderId = rootFolderId,
|
||||||
|
status = ScanSessionStatus.ACTIVE,
|
||||||
|
createdAt = now,
|
||||||
|
updatedAt = now,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
checkNotNull(scanDao.getSession(sessionId))
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun setRootFolder(sessionId: Long, rootFolderId: String) {
|
suspend fun setRootFolder(sessionId: Long, rootFolderId: String) {
|
||||||
appDatabase.withTransaction {
|
appDatabase.withTransaction {
|
||||||
@@ -61,7 +71,10 @@ class ScanRepository @Inject constructor(
|
|||||||
height: Int,
|
height: Int,
|
||||||
): ScanPageEntity {
|
): ScanPageEntity {
|
||||||
val now = System.currentTimeMillis()
|
val now = System.currentTimeMillis()
|
||||||
val order = scanDao.pageCount(session.id)
|
// MAX + 1, jamais COUNT : après suppression, COUNT réutilise un ordre
|
||||||
|
// existant (doublons → `ORDER BY sort_order` instable, `swapOrder`
|
||||||
|
// ambigu), MAX + 1 reste strictement unique et croissant.
|
||||||
|
val order = (scanDao.maxSortOrder(session.id) ?: -1) + 1
|
||||||
val page = ScanPageEntity(
|
val page = ScanPageEntity(
|
||||||
resourceId = generateId.newResourceId(),
|
resourceId = generateId.newResourceId(),
|
||||||
sessionId = session.id,
|
sessionId = session.id,
|
||||||
@@ -73,8 +86,10 @@ class ScanRepository @Inject constructor(
|
|||||||
status = ScanPageStatus.PENDING,
|
status = ScanPageStatus.PENDING,
|
||||||
createdAt = now,
|
createdAt = now,
|
||||||
)
|
)
|
||||||
scanDao.insertPage(page)
|
// L'id auto-généré est indispensable : sans lui, deletePage/movePage
|
||||||
return page
|
// ciblent l'id 0 et ne touchent aucune ligne.
|
||||||
|
val rowId = scanDao.insertPage(page)
|
||||||
|
return page.copy(id = rowId)
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun deletePage(pageId: Long, sessionId: Long) {
|
suspend fun deletePage(pageId: Long, sessionId: Long) {
|
||||||
@@ -83,6 +98,13 @@ class ScanRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Supprime une session abandonnée vide (jamais de session orpheline). */
|
||||||
|
suspend fun deleteSession(sessionId: Long) {
|
||||||
|
appDatabase.withTransaction {
|
||||||
|
scanDao.deleteSession(sessionId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun movePage(sessionId: Long, pages: List<ScanPageEntity>, pageId: Long, delta: Int) {
|
suspend fun movePage(sessionId: Long, pages: List<ScanPageEntity>, pageId: Long, delta: Int) {
|
||||||
val index = pages.indexOfFirst { it.id == pageId }
|
val index = pages.indexOfFirst { it.id == pageId }
|
||||||
val target = index + delta
|
val target = index + delta
|
||||||
|
|||||||
+17
-11
@@ -8,7 +8,7 @@ import com.kazier.mobile.data.local.entity.FileEntity
|
|||||||
import com.kazier.mobile.data.local.entity.FolderEntity
|
import com.kazier.mobile.data.local.entity.FolderEntity
|
||||||
import com.kazier.mobile.data.remote.ApiClient
|
import com.kazier.mobile.data.remote.ApiClient
|
||||||
import com.kazier.mobile.data.remote.dto.ResourcePermissionDto
|
import com.kazier.mobile.data.remote.dto.ResourcePermissionDto
|
||||||
import com.kazier.mobile.domain.DeviceIdentity
|
import com.kazier.mobile.domain.ActiveUserStore
|
||||||
import com.kazier.mobile.domain.SyncPlacement
|
import com.kazier.mobile.domain.SyncPlacement
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@@ -28,7 +28,7 @@ import javax.inject.Singleton
|
|||||||
class ShareRepository @Inject constructor(
|
class ShareRepository @Inject constructor(
|
||||||
private val apiClient: ApiClient,
|
private val apiClient: ApiClient,
|
||||||
private val appDatabase: AppDatabase,
|
private val appDatabase: AppDatabase,
|
||||||
private val deviceIdentity: DeviceIdentity,
|
private val activeUserStore: ActiveUserStore,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -42,7 +42,8 @@ class ShareRepository @Inject constructor(
|
|||||||
* marquées `exists = 0`.
|
* marquées `exists = 0`.
|
||||||
*/
|
*/
|
||||||
suspend fun syncSnapshot() {
|
suspend fun syncSnapshot() {
|
||||||
val currentUserId = deviceIdentity.getOrCreate()
|
// `ownerId` serveur = user_id : comparer au compte actif, jamais au device.
|
||||||
|
val currentUserId = activeUserStore.get()
|
||||||
val permissions = apiClient.syncPermissions()
|
val permissions = apiClient.syncPermissions()
|
||||||
|
|
||||||
val now = System.currentTimeMillis()
|
val now = System.currentTimeMillis()
|
||||||
@@ -67,18 +68,23 @@ class ShareRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convergence : marquer les lignes cloud-only absentes du snapshot
|
// Convergence : marquer les lignes cloud-only d'autrui absentes du
|
||||||
// comme disparues (révocation / expiration côté serveur).
|
// snapshot comme disparues (révocation / expiration côté serveur).
|
||||||
|
// Sans compte actif, impossible d'attribuer : ne rien masquer. Le
|
||||||
|
// snapshot exclut les ressources possédées → une ligne cloud-only
|
||||||
|
// au vrai ownerId=user ne doit jamais être markMissing.
|
||||||
for (folder in folderDao.getAll()) {
|
for (folder in folderDao.getAll()) {
|
||||||
if (isCloudOnlyForOtherUser(folder.uri, folder.ownerId, currentUserId, folder.exists)
|
if (currentUserId != null &&
|
||||||
&& folder.resourceId !in snapshotFolderIds
|
isCloudOnlyForOtherUser(folder.uri, folder.ownerId, currentUserId, folder.exists) &&
|
||||||
|
folder.resourceId !in snapshotFolderIds
|
||||||
) {
|
) {
|
||||||
folderDao.markMissing(folder.resourceId, now)
|
folderDao.markMissing(folder.resourceId, now)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (file in fileDao.getAll()) {
|
for (file in fileDao.getAll()) {
|
||||||
if (isCloudOnlyForOtherUser(file.uri, file.ownerId, currentUserId, file.exists)
|
if (currentUserId != null &&
|
||||||
&& file.resourceId !in snapshotFileIds
|
isCloudOnlyForOtherUser(file.uri, file.ownerId, currentUserId, file.exists) &&
|
||||||
|
file.resourceId !in snapshotFileIds
|
||||||
) {
|
) {
|
||||||
fileDao.markMissing(file.resourceId, now)
|
fileDao.markMissing(file.resourceId, now)
|
||||||
}
|
}
|
||||||
@@ -99,9 +105,9 @@ class ShareRepository @Inject constructor(
|
|||||||
private fun isCloudOnlyForOtherUser(
|
private fun isCloudOnlyForOtherUser(
|
||||||
uri: String?,
|
uri: String?,
|
||||||
ownerId: String?,
|
ownerId: String?,
|
||||||
currentNodeId: String,
|
currentUserId: String,
|
||||||
exists: Int?,
|
exists: Int?,
|
||||||
): Boolean = uri == null && ownerId != null && ownerId != currentNodeId && exists != 0
|
): Boolean = uri == null && ownerId != null && ownerId != currentUserId && exists != 0
|
||||||
|
|
||||||
private suspend fun upsertSharedFolder(
|
private suspend fun upsertSharedFolder(
|
||||||
perm: ResourcePermissionDto,
|
perm: ResourcePermissionDto,
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package com.kazier.mobile.di
|
package com.kazier.mobile.di
|
||||||
|
|
||||||
|
import com.kazier.mobile.auth.SecureTokenStore
|
||||||
|
import com.kazier.mobile.auth.TokenStore
|
||||||
|
import dagger.Binds
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.hilt.InstallIn
|
import dagger.hilt.InstallIn
|
||||||
import dagger.hilt.components.SingletonComponent
|
import dagger.hilt.components.SingletonComponent
|
||||||
@@ -9,4 +12,8 @@ import dagger.hilt.components.SingletonComponent
|
|||||||
*/
|
*/
|
||||||
@Module
|
@Module
|
||||||
@InstallIn(SingletonComponent::class)
|
@InstallIn(SingletonComponent::class)
|
||||||
object AppModule
|
abstract class AppModule {
|
||||||
|
|
||||||
|
@Binds
|
||||||
|
abstract fun bindTokenStore(impl: SecureTokenStore): TokenStore
|
||||||
|
}
|
||||||
@@ -2,7 +2,6 @@ package com.kazier.mobile.di
|
|||||||
|
|
||||||
import com.squareup.moshi.Moshi
|
import com.squareup.moshi.Moshi
|
||||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||||
import com.kazier.mobile.BuildConfig
|
|
||||||
import com.kazier.mobile.data.remote.ApiService
|
import com.kazier.mobile.data.remote.ApiService
|
||||||
import com.kazier.mobile.data.remote.AuthInterceptor
|
import com.kazier.mobile.data.remote.AuthInterceptor
|
||||||
import com.kazier.mobile.data.remote.ServerUrlInterceptor
|
import com.kazier.mobile.data.remote.ServerUrlInterceptor
|
||||||
@@ -34,12 +33,10 @@ object NetworkModule {
|
|||||||
serverUrlInterceptor: ServerUrlInterceptor,
|
serverUrlInterceptor: ServerUrlInterceptor,
|
||||||
authInterceptor: AuthInterceptor,
|
authInterceptor: AuthInterceptor,
|
||||||
): OkHttpClient {
|
): OkHttpClient {
|
||||||
|
// Logging désactivé même en DEBUG : BASIC loggue les lignes de requête
|
||||||
|
// avec query PII (`GET /users/resolve?username=…`) dans logcat (m25).
|
||||||
val logging = HttpLoggingInterceptor().apply {
|
val logging = HttpLoggingInterceptor().apply {
|
||||||
level = if (BuildConfig.DEBUG) {
|
level = HttpLoggingInterceptor.Level.NONE
|
||||||
HttpLoggingInterceptor.Level.BASIC
|
|
||||||
} else {
|
|
||||||
HttpLoggingInterceptor.Level.NONE
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return OkHttpClient.Builder()
|
return OkHttpClient.Builder()
|
||||||
.addInterceptor(serverUrlInterceptor)
|
.addInterceptor(serverUrlInterceptor)
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import com.kazier.mobile.data.local.dao.UserPreferenceDao
|
|||||||
import com.kazier.mobile.data.local.entity.UserPreferenceEntity
|
import com.kazier.mobile.data.local.entity.UserPreferenceEntity
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Identité locale du device : 32-hex généré une seule fois à la première
|
* Identité locale du device : 32-hex généré une seule fois à la première
|
||||||
@@ -15,13 +17,22 @@ class DeviceIdentity @Inject constructor(
|
|||||||
private val generateId: GenerateId,
|
private val generateId: GenerateId,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
suspend fun getOrCreate(): String {
|
/**
|
||||||
|
* Single-flight intra-processus : deux `getOrCreate()` concurrents ne
|
||||||
|
* génèrent/inserent qu'une seule fois (`insertIgnore` tranche, le perdant
|
||||||
|
* relit la valeur gagnante).
|
||||||
|
*/
|
||||||
|
private val mutex = Mutex()
|
||||||
|
|
||||||
|
suspend fun getOrCreate(): String = mutex.withLock {
|
||||||
userPreferenceDao.getValue(PrefKeys.DEVICE_USER_ID)?.let { return it }
|
userPreferenceDao.getValue(PrefKeys.DEVICE_USER_ID)?.let { return it }
|
||||||
val id = generateId.newResourceId()
|
val id = generateId.newResourceId()
|
||||||
userPreferenceDao.insertIgnore(
|
userPreferenceDao.insertIgnore(
|
||||||
UserPreferenceEntity(PrefKeys.DEVICE_USER_ID, id, System.currentTimeMillis()),
|
UserPreferenceEntity(PrefKeys.DEVICE_USER_ID, id, System.currentTimeMillis()),
|
||||||
)
|
)
|
||||||
return userPreferenceDao.getValue(PrefKeys.DEVICE_USER_ID)
|
// Jamais de fallback fantôme : si la rellecture échoue, échouer
|
||||||
?: generateId.newResourceId()
|
// proprement plutôt que de renvoyer un ID non persisté.
|
||||||
|
userPreferenceDao.getValue(PrefKeys.DEVICE_USER_ID)
|
||||||
|
?: error("device identity not persisted")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -13,6 +13,8 @@ enum class FileCategory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun computeCategory(mimeType: String?, extension: String?): FileCategory {
|
fun computeCategory(mimeType: String?, extension: String?): FileCategory {
|
||||||
|
// Le CSV est toujours TEXT (MIME `text/csv` comme extension `csv`) :
|
||||||
|
// un même fichier ne change jamais de catégorie selon le chemin d'entrée.
|
||||||
val mime = mimeType?.lowercase().orEmpty()
|
val mime = mimeType?.lowercase().orEmpty()
|
||||||
val ext = extension?.lowercase().orEmpty()
|
val ext = extension?.lowercase().orEmpty()
|
||||||
|
|
||||||
@@ -41,11 +43,11 @@ private val OFFICE_MIMES = setOf(
|
|||||||
|
|
||||||
private val OFFICE_EXTENSIONS = setOf(
|
private val OFFICE_EXTENSIONS = setOf(
|
||||||
"doc", "docx", "odt", "rtf",
|
"doc", "docx", "odt", "rtf",
|
||||||
"xls", "xlsx", "ods", "csv",
|
"xls", "xlsx", "ods",
|
||||||
"ppt", "pptx", "odp",
|
"ppt", "pptx", "odp",
|
||||||
)
|
)
|
||||||
|
|
||||||
private val TEXT_EXTENSIONS = setOf(
|
private val TEXT_EXTENSIONS = setOf(
|
||||||
"txt", "md", "html", "xml", "json", "css", "js", "kt", "py", "java",
|
"txt", "md", "html", "xml", "json", "css", "js", "kt", "py", "java",
|
||||||
"log", "ini", "cfg", "yaml", "yml", "toml",
|
"log", "ini", "cfg", "yaml", "yml", "toml", "csv",
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -16,6 +16,6 @@ class GenerateId @Inject constructor() {
|
|||||||
fun newResourceId(): String {
|
fun newResourceId(): String {
|
||||||
val bytes = ByteArray(16)
|
val bytes = ByteArray(16)
|
||||||
random.nextBytes(bytes)
|
random.nextBytes(bytes)
|
||||||
return bytes.joinToString("") { "%02x".format(it) }
|
return bytes.joinToString("") { "%02x".format(it.toInt() and 0xFF) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -21,7 +21,7 @@ class LocalModeStore @Inject constructor(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun get(): Boolean = userPreferenceDao.getValue(PrefKeys.LOCAL_MODE) != null
|
suspend fun get(): Boolean = userPreferenceDao.getValue(PrefKeys.LOCAL_MODE) == "1"
|
||||||
|
|
||||||
suspend fun clear() = userPreferenceDao.delete(PrefKeys.LOCAL_MODE)
|
suspend fun clear() = userPreferenceDao.delete(PrefKeys.LOCAL_MODE)
|
||||||
}
|
}
|
||||||
@@ -32,6 +32,10 @@ class ServerConfigStore @Inject constructor(
|
|||||||
private val _baseUrl = MutableStateFlow(DEFAULT_BASE_URL)
|
private val _baseUrl = MutableStateFlow(DEFAULT_BASE_URL)
|
||||||
val baseUrl: StateFlow<String> = _baseUrl.asStateFlow()
|
val baseUrl: StateFlow<String> = _baseUrl.asStateFlow()
|
||||||
|
|
||||||
|
/** Vrai une fois [load] passé — lu par `ServerUrlInterceptor` (garde M26). */
|
||||||
|
val isLoaded: Boolean
|
||||||
|
get() = loaded.get()
|
||||||
|
|
||||||
/** Dernière base effective — lu par `ServerUrlInterceptor` (filaire, volatile). */
|
/** Dernière base effective — lu par `ServerUrlInterceptor` (filaire, volatile). */
|
||||||
val current: String
|
val current: String
|
||||||
get() = _baseUrl.value
|
get() = _baseUrl.value
|
||||||
@@ -43,12 +47,48 @@ class ServerConfigStore @Inject constructor(
|
|||||||
_baseUrl.value = stored?.takeIf { it.isNotBlank() }?.let(::normalize) ?: DEFAULT_BASE_URL
|
_baseUrl.value = stored?.takeIf { it.isNotBlank() }?.let(::normalize) ?: DEFAULT_BASE_URL
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun set(url: String) {
|
/**
|
||||||
val normalized = normalize(url).ifEmpty { DEFAULT_BASE_URL }
|
* Réarme le garde once-only pour permettre un rechargement (ex. après
|
||||||
_baseUrl.value = normalized
|
* wipe/logout où la valeur en mémoire est périmée).
|
||||||
|
*/
|
||||||
|
fun invalidate() {
|
||||||
|
loaded.set(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Recharge depuis la DB même si déjà chargé (ex. après wipe/logout). */
|
||||||
|
suspend fun reload() {
|
||||||
|
loaded.set(false)
|
||||||
|
load()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persiste la base serveur après validation (M28). Retourne `false` —
|
||||||
|
* sans rien persister ni publier — si l'URL est invalide (schéma non
|
||||||
|
* http/https, host vide, illisible par OkHttp) : l'appelant par frappe
|
||||||
|
* ([com.kazier.mobile.ui.settings.SettingsViewModel.onServerUrlChange])
|
||||||
|
* ne brique jamais l'app sur une saisie intermédiaire.
|
||||||
|
*/
|
||||||
|
suspend fun set(url: String): Boolean {
|
||||||
|
val normalized = validate(url) ?: return false
|
||||||
userPreferenceDao.upsert(
|
userPreferenceDao.upsert(
|
||||||
UserPreferenceEntity(PrefKeys.SERVER_URL, normalized, System.currentTimeMillis()),
|
UserPreferenceEntity(PrefKeys.SERVER_URL, normalized, System.currentTimeMillis()),
|
||||||
)
|
)
|
||||||
|
// Publier après succès DB pour ne jamais diverger UI/DB si l'upsert échoue.
|
||||||
|
_baseUrl.value = normalized
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalise puis valide : schéma http/https et host non vide exigés.
|
||||||
|
* Retourne la base normalisée, ou `null` si invalide.
|
||||||
|
*/
|
||||||
|
fun validate(url: String): String? {
|
||||||
|
val normalized = normalize(url)
|
||||||
|
if (normalized.isEmpty()) return null
|
||||||
|
val parsed = runCatching { normalized.toHttpUrl() }.getOrNull() ?: return null
|
||||||
|
if (parsed.scheme != "http" && parsed.scheme != "https") return null
|
||||||
|
if (parsed.host.isBlank()) return null
|
||||||
|
return normalized
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Trim, ajoute `http://` si le schéma est absent, retire les `/` finaux. */
|
/** Trim, ajoute `http://` si le schéma est absent, retire les `/` finaux. */
|
||||||
|
|||||||
@@ -31,10 +31,25 @@ class ThemePreferenceStore @Inject constructor(
|
|||||||
_theme.value = ThemePreference.fromStored(stored)
|
_theme.value = ThemePreference.fromStored(stored)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Réarme le garde once-only pour permettre un rechargement (ex. après
|
||||||
|
* wipe/logout où la valeur en mémoire est périmée).
|
||||||
|
*/
|
||||||
|
fun invalidate() {
|
||||||
|
loaded.set(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Recharge depuis la DB même si déjà chargé (ex. après wipe/logout). */
|
||||||
|
suspend fun reload() {
|
||||||
|
loaded.set(false)
|
||||||
|
load()
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun set(theme: ThemePreference) {
|
suspend fun set(theme: ThemePreference) {
|
||||||
_theme.value = theme
|
|
||||||
userPreferenceDao.upsert(
|
userPreferenceDao.upsert(
|
||||||
UserPreferenceEntity(PrefKeys.THEME, theme.storedValue, System.currentTimeMillis()),
|
UserPreferenceEntity(PrefKeys.THEME, theme.storedValue, System.currentTimeMillis()),
|
||||||
)
|
)
|
||||||
|
// Publier après succès DB pour ne jamais diverger UI/DB si l'upsert échoue.
|
||||||
|
_theme.value = theme
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,7 +88,11 @@ class FileDeleter @Inject constructor(
|
|||||||
private suspend fun deleteInCloud(file: FileEntity): Boolean {
|
private suspend fun deleteInCloud(file: FileEntity): Boolean {
|
||||||
var ok = false
|
var ok = false
|
||||||
runCatching {
|
runCatching {
|
||||||
if (outboxRepository.hasCreateOperation(file.resourceId)) {
|
// Jamais poussé (create encore pending) : annulation (M19), pas
|
||||||
|
// de delete — le serveur n'a rien à oublier. Poussé (synced) :
|
||||||
|
// `delete_resource`, le serveur connaît la ressource.
|
||||||
|
outboxRepository.cancelPendingCreates(file.resourceId)
|
||||||
|
if (outboxRepository.hasSyncedCreateOperation(file.resourceId)) {
|
||||||
outboxRepository.enqueueDeleteResource(file.resourceId, "file")
|
outboxRepository.enqueueDeleteResource(file.resourceId, "file")
|
||||||
}
|
}
|
||||||
ok = true
|
ok = true
|
||||||
@@ -100,34 +104,75 @@ class FileDeleter @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Suppression complète : physique + cloud.
|
* Suppression complète : journal d'abord, physique ensuite (C4).
|
||||||
* Transaction atomique : SAF delete + markMissing + outbox.
|
*
|
||||||
|
* Ordre crash-safe : la transaction Room (`exists = 0` + annulation du
|
||||||
|
* create pending + éventuel `delete_resource`) est committée AVANT la
|
||||||
|
* suppression SAF — un kill entre les deux ne perd plus le journal, le
|
||||||
|
* serveur est toujours notifié. Si la suppression physique échoue (ou si
|
||||||
|
* le kill survient juste après), la ligne reste `exists = 0` avec son
|
||||||
|
* `uri` : la repasse [sweepPendingPhysicalDeletes] retentera le
|
||||||
|
* `deleteDocument` au prochain tick (le walk peut la ressusciter
|
||||||
|
* entre-temps, elle sera re-marquée sans nouveau journal — convergence).
|
||||||
*/
|
*/
|
||||||
private suspend fun deleteFull(file: FileEntity): Boolean {
|
private suspend fun deleteFull(file: FileEntity): Boolean {
|
||||||
val uri = file.uri
|
val uri = file.uri
|
||||||
return if (uri != null) {
|
return if (uri != null) {
|
||||||
runCatching {
|
val now = System.currentTimeMillis()
|
||||||
DocumentsContract.deleteDocument(resolver, Uri.parse(uri))
|
appDatabase.withTransaction {
|
||||||
}.onSuccess { deleted ->
|
fileRepository.markMissing(file.resourceId, now)
|
||||||
if (deleted) {
|
outboxRepository.cancelPendingCreates(file.resourceId)
|
||||||
val now = System.currentTimeMillis()
|
if (outboxRepository.hasSyncedCreateOperation(file.resourceId)) {
|
||||||
appDatabase.withTransaction {
|
outboxRepository.enqueueDeleteResource(file.resourceId, "file")
|
||||||
fileRepository.markMissing(file.resourceId, now)
|
|
||||||
if (outboxRepository.hasCreateOperation(file.resourceId)) {
|
|
||||||
outboxRepository.enqueueDeleteResource(file.resourceId, "file")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Timber.d("deleted full %s", uri)
|
|
||||||
thumbnailStore.delete(file.resourceId)
|
|
||||||
} else {
|
|
||||||
Timber.w("deleteDocument returned false for %s", uri)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
val deleted = runCatching {
|
||||||
|
DocumentsContract.deleteDocument(resolver, Uri.parse(uri))
|
||||||
}.onFailure { e ->
|
}.onFailure { e ->
|
||||||
Timber.w(e, "deleteDocument failed for %s", uri)
|
Timber.w(e, "deleteDocument failed for %s (sweep will retry)", uri)
|
||||||
}.getOrDefault(false)
|
}.getOrDefault(false)
|
||||||
|
if (deleted) {
|
||||||
|
Timber.d("deleted full %s", uri)
|
||||||
|
thumbnailStore.delete(file.resourceId)
|
||||||
|
} else {
|
||||||
|
Timber.w("deleteDocument returned false for %s (sweep will retry)", uri)
|
||||||
|
}
|
||||||
|
deleted
|
||||||
} else {
|
} else {
|
||||||
// Cloud-only : outbox uniquement.
|
// Cloud-only : outbox uniquement.
|
||||||
deleteInCloud(file)
|
deleteInCloud(file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Repasse C4 : retente la suppression physique des lignes déjà
|
||||||
|
* journalisées (`exists = 0`, `uri` présente) dont les octets ont survécu
|
||||||
|
* (kill entre journal et SAF, refus du provider). Bornée ([SWEEP_LIMIT])
|
||||||
|
* et best-effort — appelée à chaque tick de sync et après chaque import.
|
||||||
|
* Retourne le nombre d'octets effectivement supprimés.
|
||||||
|
*/
|
||||||
|
suspend fun sweepPendingPhysicalDeletes(limit: Int = SWEEP_LIMIT): Int {
|
||||||
|
var swept = 0
|
||||||
|
val stale = try {
|
||||||
|
appDatabase.fileDao().getMissingWithUri(limit)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.w(e, "sweep: lecture impossible")
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
for (file in stale) {
|
||||||
|
val uri = file.uri ?: continue
|
||||||
|
val deleted = runCatching {
|
||||||
|
DocumentsContract.deleteDocument(resolver, Uri.parse(uri))
|
||||||
|
}.getOrDefault(false)
|
||||||
|
if (deleted) {
|
||||||
|
thumbnailStore.delete(file.resourceId)
|
||||||
|
swept++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return swept
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val SWEEP_LIMIT = 50
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user