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/. — 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() }