package service import ( "crypto/sha256" "encoding/hex" "errors" "io" "log" "os" "path/filepath" "strings" "github.com/kazier/backend/repository" ) // RelationDTO serializes one edge of GET /files/:id/relations (V3 S5). type RelationDTO struct { ID string `json:"id"` OtherID string `json:"otherId"` Type string `json:"type"` Confidence float64 `json:"confidence"` CreatedBy string `json:"createdBy"` OtherName string `json:"otherName"` OtherType string `json:"otherType"` OtherSize int64 `json:"otherSize"` } // validLinkTypes mirrors the resource_links CHECK (migration 000021). var validLinkTypes = map[string]bool{ repository.LinkSameScan: true, repository.LinkDuplicateOf: true, repository.LinkSameIssuer: true, repository.LinkSameReference: true, } // ListRelations reconciles then returns the live edges of an owned resource // (owner-only : shared docs don't expose the owner's graph, S5 scope). func (s *Resources) ListRelations(ownerID, resourceID string) ([]RelationDTO, error) { owned, err := s.Repo.ExistsOwner(ownerID, resourceID) if err != nil { return nil, err } if !owned { return nil, repository.ErrNotFound } if err := s.Repo.ReconcileLinks(ownerID, resourceID); err != nil { return nil, err } rows, err := s.Repo.ListLinks(resourceID) if err != nil { return nil, err } out := make([]RelationDTO, 0, len(rows)) for _, row := range rows { out = append(out, RelationDTO{ ID: row.ID, OtherID: row.OtherID, Type: row.Type, Confidence: row.Confidence, CreatedBy: row.CreatedBy, OtherName: row.OtherName, OtherType: row.OtherType, OtherSize: row.OtherSize, }) } return out, nil } // LinkVerdict records an explicit verdict on an edge (owner-only, both ends // owned-live). Confirm → 1.0, invalidate → 0.0 (kept, blocks re-creation). func (s *Resources) LinkVerdict(ownerID, resourceID, otherID, linkType string, confirmed bool) error { if !validLinkTypes[linkType] { return ErrInvalidLinkType } if otherID == resourceID { return ErrInvalidLinkType } for _, id := range []string{resourceID, otherID} { owned, err := s.Repo.ExistsOwner(ownerID, id) if err != nil { return err } if !owned { return repository.ErrNotFound } } return s.Repo.SetUserVerdict(resourceID, otherID, linkType, confirmed) } // DuplicateGroupDTO serializes one set of byte-identical files (B2). type DuplicateGroupDTO struct { ContentHash string `json:"contentHash"` Count int `json:"count"` TotalBytes int64 `json:"totalBytes"` ReclaimableBytes int64 `json:"reclaimableBytes"` Files []DuplicateFileDTO `json:"files"` } // DuplicateFileDTO serializes one member (newest first). type DuplicateFileDTO struct { ID string `json:"id"` Name string `json:"name"` Size int64 `json:"size"` UpdatedAt int64 `json:"updatedAt"` } // ListDuplicates returns the owner's byte-identical file sets, biggest // first. Owner scope (own files only) — `[]` when clean. func (s *Resources) ListDuplicates(ownerID string) ([]DuplicateGroupDTO, error) { groups, err := s.Repo.ListDuplicateGroups(ownerID) if err != nil { return nil, err } out := make([]DuplicateGroupDTO, 0, len(groups)) for _, g := range groups { dto := DuplicateGroupDTO{ ContentHash: g.ContentHash, Count: g.Count, TotalBytes: g.TotalBytes, ReclaimableBytes: g.ReclaimableBytes, Files: make([]DuplicateFileDTO, 0, len(g.Files)), } for _, f := range g.Files { dto.Files = append(dto.Files, DuplicateFileDTO{ ID: f.ID, Name: f.Name, Size: f.Size, UpdatedAt: f.UpdatedAt, }) } out = append(out, dto) } return out, nil } // ErrInvalidLinkType marks client verdict-validation failures (400). var ErrInvalidLinkType = errors.New("invalid link type or pair") // hashPhysical computes the SHA-256 of the live bytes (safe resolution, // same pattern as OpenContent). Absent → ErrNotFound. func (s *Resources) hashPhysical(ownerID, resourceID string) (string, error) { resolved, err := s.resolvePhysical(ownerID, resourceID) if err != nil { return "", err } f, err := os.Open(resolved) if err != nil { return "", err } defer f.Close() h := sha256.New() if _, err := io.Copy(h, f); err != nil { return "", err } return hex.EncodeToString(h.Sum(nil)), nil } // resolvePhysical returns the absolute path of the live bytes (safe : // 32-hex only, regular file, symlink contained in UploadDir). // Absent → repository.ErrNotFound. func (s *Resources) resolvePhysical(ownerID, resourceID string) (string, error) { if !idPattern.MatchString(ownerID) || !idPattern.MatchString(resourceID) { return "", repository.ErrNotFound } base, err := filepath.EvalSymlinks(s.UploadDir) if err != nil { return "", err } matches, err := filepath.Glob(filepath.Join(s.UploadDir, ownerID, resourceID+".*")) if err != nil { return "", err } 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 } return resolved, nil } return "", repository.ErrNotFound } // refreshContentHash (re)computes and stores the hash after bytes arrive. // Best-effort : un échec ne fait jamais échouer l'upload (loggé). func (s *Resources) refreshContentHash(ownerID, resourceID string) { hash, err := s.hashPhysical(ownerID, resourceID) if err != nil { return } if err := s.Repo.SetContentHash(ownerID, resourceID, hash); err != nil { log.Printf("content: SetContentHash(%s): %v", resourceID, err) return } // Des octets identiques à une autre ressource = doublon à relier. if err := s.Repo.ReconcileLinks(ownerID, resourceID); err != nil { log.Printf("content: reconcile(%s): %v", resourceID, err) } } // BackfillHashes hashes live files missing content_hash (bounded batches, // boot pass — uploads d'avant la V3). Returns files hashed. func (s *Resources) BackfillHashes(batch int) (int, error) { total := 0 for { pending, err := s.Repo.ListMissingHashes(batch) if err != nil { return total, err } if len(pending) == 0 { return total, nil } for _, p := range pending { hash, err := s.hashPhysical(p.UserID, p.ResourceID) if err != nil { continue // metadata-only : rien à hasher, re-tenté au prochain upload. } if err := s.Repo.SetContentHash(p.UserID, p.ResourceID, hash); err != nil { log.Printf("content: backfill hash %s: %v", p.ResourceID, err) continue } if err := s.Repo.ReconcileLinks(p.UserID, p.ResourceID); err != nil { log.Printf("content: backfill reconcile %s: %v", p.ResourceID, err) } total++ } } }