diff --git a/index.html b/index.html index 1ec4ebf..ce38316 100644 --- a/index.html +++ b/index.html @@ -33,6 +33,7 @@ + @@ -40,7 +41,6 @@ - diff --git a/src/core.js b/src/core.js index 732edc0..c4c9315 100644 --- a/src/core.js +++ b/src/core.js @@ -110,7 +110,7 @@ function addDots() { const input = document.createElement("input") input.id = inputName input.type = "file" - input.accept = "image/*" + input.accept = "image/*,.json" input.name = inputName wrapper.appendChild(input) @@ -122,7 +122,12 @@ function addDots() { lbl.addEventListener("click", () => input.click()) input.addEventListener("change", async () => { const file = input.files[0] - await loadFile(file) + + if (file.type === "application/json") { + await importMastodonJson(file) + } else { + await loadFile(file) + } }) return wrapper diff --git a/src/import-export.js b/src/import-export.js index f789032..006fbbd 100644 --- a/src/import-export.js +++ b/src/import-export.js @@ -30,29 +30,49 @@ function importMastodonTgz() { } function importMastodonJson(jsonFile, sampleUrl, reportPct) { - const host = new URL(sampleUrl).host readJsonFile(jsonFile).then(async mastoArchive => { const items = mastoArchive.orderedItems let noAlt = 0 - let toFetch = [] - for (let [note] of items) { - if (note.object && note.object.attachment) { - for (let attach of note.object.attachment) { - if (attach.type === "Document" - && attach.mediaType.startsWith("image/") - && attach.url - ) { - if (attach.name) { + const toFetch = [] + + items + // NOTE: This could be replaced with a single `item?.object?.attachment?.length` check + // depending on browser compatibility requirements. + .filter(item => item.object && item.object.attachment && item.object.attachment.length) + .map(item => item.object.attachment) + .forEach((attachments) => { + attachments + .filter(a => a.url && a.type === "Document" && a.mediaType.startsWith("image/")) + .forEach((attachment) => { + if (attachment.name) { toFetch.push({ - alt: attach.name, - url: `${host}${attach.url}` + alt: attachment.name, + url: attachment.url, }) } else { noAlt++ } - } - } - } + }) + + }) + + if (toFetch.length > 0) { + // NOTE: Replace with a dialog element potentially. + const defaultAnswer = "" + const answer = prompt("Please fill in your mastodon server media url", defaultAnswer) + const url = new URL(answer) + // TODO: This needs checking for traling slashes etc. + const host = url.href + + toFetch.forEach(async (item) => { + // TODO: Not sure how feeding each into `loadFile()` should work in terms of + // generating multiple image area previews. Also, since these are CORS enabled + // per the Mastodon spec, we should be able to render onto canvas direct? + item.url = `${host}${item.url}` + + // TODO: More details need filing in. + addInFlight(item.alt) + }) } }).catch((e) => { console.log(e)