From 307fb5a7675d71d8fe0b263f36a583297e28c6f1 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 13 Dec 2023 15:54:25 +0100 Subject: [PATCH] Improve profile import and export regarding IDs, fingerprints and defaults --- sync/profile.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/sync/profile.go b/sync/profile.go index c6e936d5..20aeb7dd 100644 --- a/sync/profile.go +++ b/sync/profile.go @@ -254,6 +254,9 @@ func ExportProfile(scopedID string) (*ProfileExport, error) { export.Created = &created } + // Derive ID to ensure the ID is always correct. + export.ID = profile.DeriveProfileID(p.Fingerprints) + // Add first exportable icon to export. if len(p.Icons) > 0 { var err error @@ -270,6 +273,14 @@ func ExportProfile(scopedID string) (*ProfileExport, error) { } } + // Remove presentation path if both Name and Icon are set. + if export.Name != "" && export.IconData != "" { + p.UsePresentationPath = false + } + if !p.UsePresentationPath { + p.PresentationPath = "" + } + return export, nil } @@ -284,11 +295,15 @@ func ImportProfile(r *ProfileImportRequest, requiredProfileSource profile.Profil if r.Export.Source != "" && r.Export.Source != requiredProfileSource { return nil, ErrMismatch } - // Check ID. + // Convert fingerprints to internal representation. fingerprints := convertFingerprintsToInternal(r.Export.Fingerprints) + if len(fingerprints) == 0 { + return nil, fmt.Errorf("%w: the export contains no fingerprints", ErrInvalidProfileData) + } + // Derive ID from fingerprints. profileID := profile.DeriveProfileID(fingerprints) if r.Export.ID != "" && r.Export.ID != profileID { - return nil, ErrMismatch + return nil, fmt.Errorf("%w: the export profile ID does not match the fingerprints, remove to ignore", ErrInvalidProfileData) } r.Export.ID = profileID // Check Fingerprints. @@ -385,6 +400,14 @@ func ImportProfile(r *ProfileImportRequest, requiredProfileSource profile.Profil p.Created = in.Created.Unix() } + // Fill in required values. + if p.Config == nil { + p.Config = make(map[string]any) + } + if p.Created == 0 { + p.Created = time.Now().Unix() + } + // Add icon to profile, if set. if in.IconData != "" { du, err := dataurl.DecodeString(in.IconData)