Compare commits
1 commit
main
...
feature/ur
| Author | SHA1 | Date | |
|---|---|---|---|
| c70b8f938a |
3 changed files with 74 additions and 1 deletions
|
|
@ -73,7 +73,8 @@
|
|||
"error": {
|
||||
"db-out-of-date": "Database out of date, please try again.",
|
||||
"document-ID-404": "Cannot find {dbType} with ID: {id}",
|
||||
"no-upload-permission": "Cannot save due to missing the \"Upload Files\" permission."
|
||||
"no-upload-permission": "Cannot save due to missing the \"Upload Files\" permission.",
|
||||
"invalid-import-type": "Invalid import type: {type}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import { ImageApp } from "./apps/ImageApp.mjs";
|
|||
|
||||
// Utils
|
||||
import { convertToWebp, getFileSize, hashFile, lastModifiedAt } from "./utils/fs.mjs";
|
||||
import { importFromURL } from "./utils/imports.mjs";
|
||||
|
||||
export const api = foundry.utils.deepFreeze({
|
||||
Apps: {
|
||||
|
|
@ -21,5 +22,6 @@ export const api = foundry.utils.deepFreeze({
|
|||
lastModifiedAt,
|
||||
getFileSize,
|
||||
},
|
||||
importFromURL,
|
||||
},
|
||||
});
|
||||
|
|
|
|||
70
module/utils/imports.mjs
Normal file
70
module/utils/imports.mjs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
import { getFile } from "./fs.mjs";
|
||||
|
||||
const { Dialog } = foundry.applications.api;
|
||||
const { fetchJsonWithTimeout, mergeObject } = foundry.utils;
|
||||
|
||||
const TRUSTED_DOMAINS = new Set([
|
||||
`git.varify.ca`,
|
||||
`cdn.varify.ca`,
|
||||
window.location.host,
|
||||
]);
|
||||
|
||||
/**
|
||||
* Imports an existing JSON DB into the module's current database.
|
||||
*
|
||||
* @param {string} url The URL pointing to a raw JSON file
|
||||
* @param {`keep`|`replace`|`merge`} type The type of import that should be performed
|
||||
*/
|
||||
export async function importFromURL(url, type) {
|
||||
|
||||
if (![`keep`, `replace`, `merge`].includes(type)) {
|
||||
ui.notifications.error(_loc(`IT.notifs.error.invalid-import-type`, { type }));
|
||||
return false;
|
||||
};
|
||||
|
||||
const domain = new URL(url);
|
||||
if (!TRUSTED_DOMAINS.has(domain.host)) {
|
||||
const confirmed = await Dialog.confirm({
|
||||
content: `An import is trying to happen from: <br>${url}<br>Do you trust this website?`,
|
||||
rejectClose: false,
|
||||
});
|
||||
if (!confirmed) {
|
||||
return false;
|
||||
};
|
||||
};
|
||||
|
||||
let data;
|
||||
try {
|
||||
data = await fetchJsonWithTimeout(url);
|
||||
} catch (err) {
|
||||
throw err;
|
||||
};
|
||||
|
||||
const images = await getFile(`storage/db/images.json`);
|
||||
const artists = await getFile(`storage/db/artists.json`);
|
||||
|
||||
switch (type) {
|
||||
case `keep`: {
|
||||
importKeepExisting(data, images, artists);
|
||||
break;
|
||||
};
|
||||
case `replace`: {
|
||||
importReplaceExisting(data, images, artists);
|
||||
break;
|
||||
};
|
||||
case `merge`: {
|
||||
await importMerge(data, images, artists);
|
||||
break;
|
||||
};
|
||||
};
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
async function importKeepExisting(data, imageDB, artistDB) {};
|
||||
|
||||
async function importReplaceExisting(data, imageDB, artistDB) {};
|
||||
|
||||
async function importMerge(data, imageDB, artistDB) {
|
||||
throw `Merge-based importing is not implemented yet`;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue