Files
Kazier/backend/service/resources.go
T
venture 95b4cf79cf V3 S1-S2 : corbeille + versions + extract générique
S1 filet (backend + mobile) : corbeille 30j (deleted_at réutilisé,
trash/restore récursifs, purge auto serveur), versions N=3 à chaque
overwrite, ops outbox trash/restore/purge (delete legacy mappé), Room
v19 (trashed_at), Deleters -> trash, écran Corbeille, walk guards.

S2 extract (backend) : package extract (règles FR stdlib, corpus
testé), resource_facts 000019 (origin auto/user), hook worker OCR +
backfill boot, GET /files/:id/facts (viewer+).

Contrat docs/api-v1.md étendu, V3.md (vision + plan).
2026-09-26 10:14:07 +02:00

486 lines
15 KiB
Go

package service
import (
"errors"
"fmt"
"io"
"log"
"mime/multipart"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/kazier/backend/repository"
)
// ErrFileTooLarge signals an upload above MaxFileSize.
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.
type FileDTO struct {
ID string `json:"id"`
Name string `json:"name"`
Size int64 `json:"size"`
MimeType string `json:"mimeType,omitempty"`
FolderID string `json:"folderId,omitempty"`
Tags []string `json:"tags,omitempty"`
HasContent bool `json:"hasContent"`
CreatedAt string `json:"createdAt,omitempty"`
UpdatedAt string `json:"updatedAt,omitempty"`
}
// FolderDTO serializes exactly as mobile/api/types.ts FolderDto.
type FolderDTO struct {
ID string `json:"id"`
Name string `json:"name"`
ParentID string `json:"parentId,omitempty"`
}
// Resources holds the business logic for files/folders list-get-delete-upload,
// always scoped by the requesting USER (resources.user_id after 000007).
type Resources struct {
Repo *repository.Resources
Repository *repository.Repository
UploadDir string
MaxFileSize int64
}
func NewResources(repo *repository.Repository, uploadDir string, maxFileSize int64) *Resources {
return &Resources{
Repo: repo.Resources,
Repository: repo,
UploadDir: uploadDir,
MaxFileSize: maxFileSize,
}
}
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) {
rows, total, err := s.Repo.ListFilesVisible(ownerID, folderID, pageSize, (page-1)*pageSize, sort, order)
if err != nil {
return nil, 0, err
}
files := make([]FileDTO, 0, len(rows))
for _, row := range rows {
files = append(files, toFileDTO(row))
}
return files, total, nil
}
func (s *Resources) GetFile(ownerID, id string) (FileDTO, error) {
row, err := s.Repo.GetFileVisible(ownerID, id)
if err != nil {
return FileDTO{}, err
}
return toFileDTO(row), nil
}
// FileOcrDTO is the payload of GET /files/:id/ocr: the server-computed OCR
// extract of the resource, readable by its owner and by grantees (viewer+).
type FileOcrDTO struct {
Text string `json:"text"`
UpdatedAt string `json:"updatedAt,omitempty"`
}
// ContentInfo describes the physical bytes of a file the user can see
// (viewer+, own or shared) for GET /files/:id/content (streaming lecture,
// jamais persisté côté client en V1 streaming).
type ContentInfo struct {
Path string
Name string
MimeType string
Size int64
}
// OpenContent resolves the physical file of a visible resource. Auth is
// viewer+ (GetFileVisible, own or shared) ; the bytes live under the OWNER
// dir (UPLOAD_DIR/<owner_id>/<resource_id>.<ext>), never under the requester.
// No physical file (metadata-only outbox resource, never uploaded) →
// repository.ErrNotFound (handler maps to 404 NOT_FOUND, same as invisible).
func (s *Resources) OpenContent(userID, resourceID string) (ContentInfo, error) {
row, err := s.Repo.GetFileVisible(userID, resourceID)
if err != nil {
return ContentInfo{}, err
}
ownerID, err := s.Repo.GetResourceOwner(resourceID)
if err != nil {
return ContentInfo{}, err
}
if !idPattern.MatchString(ownerID) || !idPattern.MatchString(resourceID) {
return ContentInfo{}, repository.ErrNotFound
}
base, err := filepath.EvalSymlinks(s.UploadDir)
if err != nil {
return ContentInfo{}, repository.ErrNotFound
}
matches, err := filepath.Glob(filepath.Join(s.UploadDir, ownerID, resourceID+".*"))
if err != nil {
return ContentInfo{}, repository.ErrNotFound
}
for _, m := range matches {
if !strings.HasPrefix(filepath.Base(m), resourceID+".") {
continue
}
fi, err := os.Lstat(m)
if err != nil || !fi.Mode().IsRegular() {
continue
}
resolved, err := filepath.EvalSymlinks(m)
if err != nil {
continue
}
rel, err := filepath.Rel(base, resolved)
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
continue
}
mime := row.MimeType
if mime == "" {
mime = "application/octet-stream"
}
// Octets constatés : heal `has_content` → TRUE (V2 lazy, best-effort).
if !row.HasContent {
if herr := s.Repo.SetHasContent(ownerID, resourceID, true); herr != nil {
log.Printf("content: heal has_content(%s): %v", resourceID, herr)
}
}
return ContentInfo{Path: resolved, Name: row.Name, MimeType: mime, Size: fi.Size()}, nil
}
// Aucun octet mais flag TRUE (suppression manuelle sous UPLOAD_DIR) :
// heal → FALSE (V2 lazy, best-effort).
if row.HasContent {
if herr := s.Repo.SetHasContent(ownerID, resourceID, false); herr != nil {
log.Printf("content: heal has_content(%s): %v", resourceID, herr)
}
}
return ContentInfo{}, repository.ErrNotFound
}
// GetFileOcr returns the OCR text of a file the user can see (viewer+, own or
// shared). Absent text → Text empty ("") with 200 (the caller decides).
func (s *Resources) GetFileOcr(ownerID, id string) (FileOcrDTO, error) {
row, err := s.Repo.GetFileVisible(ownerID, id)
if err != nil {
return FileOcrDTO{}, err
}
return FileOcrDTO{
Text: row.OcrText,
UpdatedAt: row.UpdatedAt.UTC().Format(time.RFC3339),
}, nil
}
func (s *Resources) ListRootFolders(ownerID string) ([]FolderDTO, error) {
rows, err := s.Repo.ListRootFoldersVisible(ownerID)
if err != nil {
return nil, err
}
folders := make([]FolderDTO, 0, len(rows))
for _, row := range rows {
folders = append(folders, FolderDTO{ID: row.ID, Name: row.Name, ParentID: row.ParentID})
}
return folders, nil
}
func (s *Resources) SearchFiles(ownerID, q string, page, pageSize int) ([]FileDTO, int, error) {
rows, total, err := s.Repo.SearchFilesVisible(ownerID, q, pageSize, (page-1)*pageSize)
if err != nil {
return nil, 0, err
}
files := make([]FileDTO, 0, len(rows))
for _, row := range rows {
files = append(files, toFileDTO(row))
}
return files, total, nil
}
// Upload persists the multipart-sourced file under UploadDir/<user> and
// records its metadata, returning the FileDTO. The physical file is removed
// if metadata persistence fails (e.g. name conflict).
//
// When resourceID is non-empty the upload TARGETS an existing file owned by
// the user (metadata-only resource from the outbox, or a previously uploaded
// file): the bytes are written under UploadDir/<user>/<resourceID>.<ext> and
// only the physical metadata (size/mime/extension) is refreshed — never a
// rename, never a move, never a duplicate row. Unknown / foreign resource →
// repo.ErrNotFound.
func (s *Resources) Upload(ownerID string, file *multipart.FileHeader, folderID, resourceID string) (FileDTO, error) {
// Garde anti-traversal : seuls des IDs 32-hex (ou générés) deviennent des
// 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 != ""
var cur repository.FileRow
if targetExisting {
if !idPattern.MatchString(resourceID) {
return FileDTO{}, repository.ErrNotFound
}
var err error
// GetFile ne voit que le live : un upload vers une ressource
// trashée/absente/d'autrui → NOT_FOUND (jamais de résurrection
// d'octets sur une ligne en corbeille).
cur, err = s.Repo.GetFile(ownerID, resourceID)
if err != nil {
return FileDTO{}, err
}
} else {
var err error
resourceID, err = repository.NewIDErr()
if err != nil {
return FileDTO{}, err
}
}
name, err := sanitizeFileName(file.Filename)
if err != nil {
return FileDTO{}, err
}
extension := sanitizeExtension(file.Filename)
destDir := filepath.Join(s.UploadDir, ownerID)
if err := os.MkdirAll(destDir, 0o700); err != nil {
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)
}
destName := resourceID
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)
}
}
// V3 versions : sur upload ciblé, figer l'ancienne génération AVANT
// l'écriture (le rename atomique de copyMultipartAtomic détruirait
// l'ancien contenu). Staging = copie, jamais move : l'échec reste safe.
// Metadata-only (aucun octet live) → rien à versionner.
var staged, stagedExt string
var stagedSize int64
var stagedMime string
var hasStaged bool
if targetExisting {
var ferr error
staged, stagedExt, stagedSize, stagedMime, hasStaged, ferr = s.freezeLive(ownerID, resourceID, cur, destDir)
if ferr != nil {
return FileDTO{}, ferr
}
}
written, err := copyMultipartAtomic(file, destDir, destPath, s.MaxFileSize)
if err != nil {
if hasStaged {
_ = os.Remove(staged)
}
return FileDTO{}, err
}
mimeType := sanitizeMimeType(file.Header.Get("Content-Type"))
var extPtr *string
if extension != "" {
extPtr = &extension
}
if targetExisting {
err = s.Repo.UpdatePhysical(ownerID, resourceID, written, &mimeType, extPtr)
} else {
err = s.Repo.InsertFile(ownerID, resourceID, name, folderID, written, &mimeType, extPtr)
if err == nil {
// Upload avec octets : `has_content` TRUE (V2 lazy).
err = s.Repo.SetHasContent(ownerID, resourceID, true)
}
}
if err != nil {
// 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)
}
if hasStaged {
_ = os.Remove(staged)
}
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)
}
}
// Overwrite ciblée : l'ancien live devient une version figée (N=3).
// Best-effort après succès des métadonnées : un échec ici ne fait pas
// échouer l'upload (loggé, staging nettoyé).
if hasStaged {
if ferr := s.finalizeVersion(ownerID, resourceID, staged, stagedExt, stagedSize, stagedMime); ferr != nil {
log.Printf("upload: finalize version(%s): %v", resourceID, ferr)
_ = os.Remove(staged)
}
}
row, err := s.Repo.GetFile(ownerID, resourceID)
if err != nil {
// 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 toFileDTO(row), nil
}
// 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()
if err != nil {
return 0, fmt.Errorf("open multipart file: %w", err)
}
defer src.Close()
// 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 {
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)
}
limited := io.LimitReader(src, maxSize+1)
written, err := io.Copy(tmp, limited)
if err != nil {
tmp.Close()
return 0, fmt.Errorf("copy upload: %w", err)
}
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 {
return FileDTO{
ID: row.ID,
Name: row.Name,
Size: row.Size,
MimeType: row.MimeType,
FolderID: row.FolderID,
HasContent: row.HasContent,
CreatedAt: row.CreatedAt.UTC().Format(time.RFC3339),
UpdatedAt: row.UpdatedAt.UTC().Format(time.RFC3339),
}
}