Files
venture 4316da578f
Release / release-apk (push) Successful in 8m58s
Release / release-docker (push) Successful in 3m47s
CI / docker (push) Successful in 7s
CI / lint-backend (push) Successful in 1m44s
CI / lint-mobile (push) Failing after 18m59s
CI / test-backend (push) Successful in 2m12s
CI / test-mobile (push) Successful in 8m50s
CI / build-backend (push) Successful in 45s
V3 S7 : preview cloud + export chiffré + release 1.2.0
Backend : resource_previews (000024), GET /files/:id/preview (JPEG
512, images pur Go, PDF page 1 via pdftoppm + poppler au Dockerfile,
cache régénéré sur changement, 404 NO_PREVIEW sinon), POST /export
{password 8+} -> zip AES-256 streamé (fichiers vifs possédés +
manifest.json avec faits, metadata-only sans octets). Deps :
alexmullins/zip, x/image directs. Tests service + handlers + migrations.

Mobile : vignettes distantes (une requête/session puis cache WebP
versionné, garde tried, icône sinon), FileThumbnail cloud-only,
export (mot de passe jamais persisté, cache + partage FileProvider),
bouton Partager ACTION_SEND. Impression papier reportée V4. FR/EN.

Contrat docs/api-v1.md, V3.md (S7 livré, V3 close), AGENTS.md,
VERSION 1.2.0 + CHANGELOG.
2026-09-26 15:08:31 +02:00

156 lines
4.8 KiB
Go

package service
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"time"
"github.com/alexmullins/zip"
)
// Export password floor (V3 S7 C3) : un backup chiffré avec "1234" est un
// placebo — même seuil que les mots de passe compte (8).
const exportMinPasswordLength = 8
// ExportMaxResources bounds a full export (V3 S7 C3) : au-delà, l'export est
// tronqué honnêtement (manifest.truncated = true) plutôt que de gonfler sans
// fin la mémoire/temps d'une requête HTTP.
const ExportMaxResources = 20000
// ExportManifestFile is the encrypted manifest inside the zip.
type ExportManifestFile struct {
Version int `json:"version"`
ExportedAt string `json:"exportedAt"`
UserID string `json:"userId"`
Truncated bool `json:"truncated"`
Folders []ExportFolderEntry `json:"folders"`
Files []ExportFileEntry `json:"files"`
}
// ExportFolderEntry mirrors a live folder (tree rebuilt via parentId).
type ExportFolderEntry struct {
ID string `json:"id"`
Name string `json:"name"`
ParentID string `json:"parentId,omitempty"`
}
// ExportFileEntry mirrors a live file (+ extracted knowledge for re-import).
type ExportFileEntry struct {
ID string `json:"id"`
Name string `json:"name"`
ParentID string `json:"parentId,omitempty"`
Size int64 `json:"size"`
MimeType string `json:"mimeType,omitempty"`
Extension string `json:"extension,omitempty"`
Hash string `json:"hash,omitempty"`
OcrText string `json:"ocrText,omitempty"`
Facts []FactDTO `json:"facts,omitempty"`
Path string `json:"path,omitempty"` // files/<id>.<ext> — absent si metadata-only
HasBytes bool `json:"hasBytes"`
}
// Export streams an AES-256 encrypted zip of the account (V3 S7 C3) : every
// entry — files AND manifest — is encrypted with password (WinZip AES, cf.
// alexmullins/zip). Scope : own live resources only (ni trashés, ni
// partagés d'autrui). Metadata-only files appear in the manifest without
// bytes (Path vide, HasBytes false). Password < 8 → error (400 côté handler).
func (s *Resources) Export(w io.Writer, ownerID, password string) error {
if len(password) < exportMinPasswordLength {
return fmt.Errorf("export password must be at least %d characters", exportMinPasswordLength)
}
folders, err := s.Repo.ExportFolders(ownerID, ExportMaxResources)
if err != nil {
return err
}
files, err := s.Repo.ExportFiles(ownerID, ExportMaxResources)
if err != nil {
return err
}
truncated := len(folders) == ExportMaxResources || len(files) == ExportMaxResources
manifest := ExportManifestFile{
Version: 1, ExportedAt: time.Now().UTC().Format(time.RFC3339),
UserID: ownerID, Truncated: truncated,
Folders: make([]ExportFolderEntry, 0, len(folders)),
Files: make([]ExportFileEntry, 0, len(files)),
}
for _, f := range folders {
manifest.Folders = append(manifest.Folders, ExportFolderEntry{
ID: f.ID, Name: f.Name, ParentID: f.ParentID,
})
}
zw := zip.NewWriter(w)
writeEncrypted := func(name string, data []byte) error {
fh := &zip.FileHeader{Name: name, Method: zip.Deflate}
fh.SetPassword(password)
fw, err := zw.CreateHeader(fh)
if err != nil {
return err
}
_, err = fw.Write(data)
return err
}
for _, f := range files {
entry := ExportFileEntry{
ID: f.ID, Name: f.Name, ParentID: f.ParentID, Size: f.Size,
MimeType: f.MimeType, Extension: f.Extension, Hash: f.Hash,
OcrText: f.OcrText,
}
// Faits joints (auto + corrections — la connaissance voyage avec).
if facts, ferr := s.Repository.Resources.ListFacts(f.ID); ferr == nil {
for _, fact := range facts {
entry.Facts = append(entry.Facts, FactDTO{
FactType: fact.Type, ValueText: fact.ValueText,
ValueNum: fact.ValueNum, ValueDate: derefString(fact.ValueDate),
Confidence: fact.Confidence, Origin: fact.Origin,
})
}
}
src, serr := s.resolvePhysical(ownerID, f.ID)
if serr != nil {
entry.HasBytes = false
manifest.Files = append(manifest.Files, entry)
continue
}
zipName := "files/" + f.ID
if f.Extension != "" {
zipName += "." + f.Extension
}
entry.Path = zipName
entry.HasBytes = true
manifest.Files = append(manifest.Files, entry)
in, err := os.Open(src)
if err != nil {
log.Printf("export: open %s: %v", f.ID, err)
continue
}
fh := &zip.FileHeader{Name: zipName, Method: zip.Deflate}
fh.SetPassword(password)
fw, err := zw.CreateHeader(fh)
if err != nil {
in.Close()
return err
}
if _, err := io.Copy(fw, in); err != nil {
in.Close()
return err
}
in.Close()
}
raw, err := json.MarshalIndent(manifest, "", " ")
if err != nil {
return err
}
if err := writeEncrypted("manifest.json", raw); err != nil {
return err
}
return zw.Close()
}