diff --git a/src/lib/extension/entities/MaintenanceProfile.js b/src/lib/extension/entities/MaintenanceProfile.js index 6456ce2..a206437 100644 --- a/src/lib/extension/entities/MaintenanceProfile.js +++ b/src/lib/extension/entities/MaintenanceProfile.js @@ -1,5 +1,6 @@ import StorageEntity from "$lib/extension/base/StorageEntity.js"; import EntitiesController from "$lib/extension/EntitiesController.js"; +import {compressToEncodedURIComponent, decompressFromEncodedURIComponent} from "lz-string"; /** * @typedef {Object} MaintenanceProfileSettings @@ -29,6 +30,26 @@ class MaintenanceProfile extends StorageEntity { return super.settings; } + /** + * Export the profile to the formatted JSON. + * + * @type {string} + */ + toJSON() { + return JSON.stringify({ + v: 1, + id: this.id, + name: this.settings.name, + tags: this.settings.tags, + }, null, 2); + } + + toCompressedJSON() { + return compressToEncodedURIComponent( + this.toJSON() + ); + } + static _entityName = "profiles"; /** @@ -58,6 +79,58 @@ class MaintenanceProfile extends StorageEntity { callback ); } + + /** + * Validate and import the profile from the JSON. + * @param {string} exportedString JSON for profile. + * @return {MaintenanceProfile} Maintenance profile imported from the JSON. Note that profile is not automatically + * saved. + * @throws {Error} When version is unsupported or format is invalid. + */ + static importFromJSON(exportedString) { + let importedObject; + + try { + importedObject = JSON.parse(exportedString); + } catch (e) { + throw new Error('Invalid JSON!'); + } + + if (importedObject.v !== 1) { + throw new Error('Unsupported version!'); + } + + if ( + !importedObject.id + || typeof importedObject.id !== "string" + || !importedObject.name + || typeof importedObject.name !== "string" + || !importedObject.tags + || !Array.isArray(importedObject.tags) + ) { + throw new Error('Invalid profile format detected!'); + } + + return new MaintenanceProfile( + importedObject.id, + { + name: importedObject.name, + tags: importedObject.tags, + } + ); + } + + /** + * Validate and import the profile from the compressed JSON string. + * @param {string} compressedString + * @return {MaintenanceProfile} + * @throws {Error} When version is unsupported or format is invalid. + */ + static importFromCompressedJSON(compressedString) { + return this.importFromJSON( + decompressFromEncodedURIComponent(compressedString) + ); + } } -export default MaintenanceProfile; \ No newline at end of file +export default MaintenanceProfile; diff --git a/src/routes/settings/maintenance/[id]/export/+page.svelte b/src/routes/settings/maintenance/[id]/export/+page.svelte index 4fbd67b..edb8226 100644 --- a/src/routes/settings/maintenance/[id]/export/+page.svelte +++ b/src/routes/settings/maintenance/[id]/export/+page.svelte @@ -5,7 +5,6 @@ import Menu from "$components/ui/menu/Menu.svelte"; import MenuItem from "$components/ui/menu/MenuItem.svelte"; import FormContainer from "$components/ui/forms/FormContainer.svelte"; - import {compressToEncodedURIComponent} from "lz-string"; const profileId = $page.params.id; @@ -22,14 +21,8 @@ if (!profile) { goto('/settings/maintenance/'); } else { - exportedProfile = JSON.stringify({ - v: 1, - id: profileId, - name: profile?.settings.name, - tags: profile.settings.tags, - }, null, 2); - - compressedProfile = compressToEncodedURIComponent(exportedProfile); + exportedProfile = profile.toJSON(); + compressedProfile = profile.toCompressedJSON(); } let isCompressedProfileShown = true; @@ -40,19 +33,21 @@ Back