Improve profile import and export regarding IDs, fingerprints and defaults
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user