Support setting file fingerprints (#4376)

* Support setting file fingerprints
* Disallow modifying managed hashes
This commit is contained in:
WithoutPants
2023-12-22 14:07:10 +11:00
committed by GitHub
parent a1bd7cf817
commit afda6decf2
7 changed files with 156 additions and 0 deletions

View File

@@ -207,3 +207,68 @@ func (r *mutationResolver) DeleteFiles(ctx context.Context, ids []string) (ret b
return true, nil
}
func (r *mutationResolver) FileSetFingerprints(ctx context.Context, input FileSetFingerprintsInput) (bool, error) {
fileIDInt, err := strconv.Atoi(input.ID)
if err != nil {
return false, fmt.Errorf("converting id: %w", err)
}
fileID := models.FileID(fileIDInt)
// determine what we're doing
var (
fingerprints []models.Fingerprint
toDelete []string
)
for _, i := range input.Fingerprints {
if i.Type == models.FingerprintTypeMD5 || i.Type == models.FingerprintTypeOshash {
return false, fmt.Errorf("cannot modify %s fingerprint", i.Type)
}
if i.Value == nil {
toDelete = append(toDelete, i.Type)
} else {
// phashes need to be converted from string into uint64
var v interface{}
v = *i.Value
if i.Type == models.FingerprintTypePhash {
vInt, err := strconv.ParseUint(*i.Value, 16, 64)
if err != nil {
return false, fmt.Errorf("converting phash %s: %w", *i.Value, err)
}
v = vInt
}
fingerprints = append(fingerprints, models.Fingerprint{
Type: i.Type,
Fingerprint: v,
})
}
}
if err := r.withTxn(ctx, func(ctx context.Context) error {
qb := r.repository.File
if len(fingerprints) > 0 {
if err := qb.ModifyFingerprints(ctx, fileID, fingerprints); err != nil {
return fmt.Errorf("modifying fingerprints: %w", err)
}
}
if len(toDelete) > 0 {
if err := qb.DestroyFingerprints(ctx, fileID, toDelete); err != nil {
return fmt.Errorf("destroying fingerprints: %w", err)
}
}
return nil
}); err != nil {
return false, err
}
return true, nil
}