diff options
| author | HJ <30-hj@users.noreply.git.pleroma.social> | 2021-04-07 17:40:07 +0000 |
|---|---|---|
| committer | HJ <30-hj@users.noreply.git.pleroma.social> | 2021-04-07 17:40:07 +0000 |
| commit | 8b96ea93776fd1eb462a7c54822d4f8ad6a9e776 (patch) | |
| tree | 002a88cdb6e25ea3177b712bc3cdc05a085f19e5 /src/services/export_import | |
| parent | 4e3c4ec1dbbf88b09a84a80519f4ccced5a4dc2c (diff) | |
| parent | 5c064ccf553ffcd3286ad1b3305633c5c4c4c0c4 (diff) | |
Merge branch 'settings-import-export' into 'develop'
Settings backup/restore + small fixes
See merge request pleroma/pleroma-fe!1372
Diffstat (limited to 'src/services/export_import')
| -rw-r--r-- | src/services/export_import/export_import.js | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/services/export_import/export_import.js b/src/services/export_import/export_import.js new file mode 100644 index 00000000..ac67cf9c --- /dev/null +++ b/src/services/export_import/export_import.js @@ -0,0 +1,55 @@ +export const newExporter = ({ + filename = 'data', + getExportedObject +}) => ({ + exportData () { + const stringified = JSON.stringify(getExportedObject(), null, 2) // Pretty-print and indent with 2 spaces + + // Create an invisible link with a data url and simulate a click + const e = document.createElement('a') + e.setAttribute('download', `${filename}.json`) + e.setAttribute('href', 'data:application/json;base64,' + window.btoa(stringified)) + e.style.display = 'none' + + document.body.appendChild(e) + e.click() + document.body.removeChild(e) + } +}) + +export const newImporter = ({ + onImport, + onImportFailure, + validator = () => true +}) => ({ + importData () { + const filePicker = document.createElement('input') + filePicker.setAttribute('type', 'file') + filePicker.setAttribute('accept', '.json') + + filePicker.addEventListener('change', event => { + if (event.target.files[0]) { + // eslint-disable-next-line no-undef + const reader = new FileReader() + reader.onload = ({ target }) => { + try { + const parsed = JSON.parse(target.result) + const validationResult = validator(parsed) + if (validationResult === true) { + onImport(parsed) + } else { + onImportFailure({ validationResult }) + } + } catch (error) { + onImportFailure({ error }) + } + } + reader.readAsText(event.target.files[0]) + } + }) + + document.body.appendChild(filePicker) + filePicker.click() + document.body.removeChild(filePicker) + } +}) |
