From 022202bd1c0ef1b282e4c7d16eb05dc68df77b2e Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Sun, 7 Jan 2024 13:22:04 -0700 Subject: [PATCH] fedimbed: attachment fixes, mainly for cohost --- src/modules/fedimbed.js | 170 +++++++++++++++++++++++++++++++--------- 1 file changed, 134 insertions(+), 36 deletions(-) diff --git a/src/modules/fedimbed.js b/src/modules/fedimbed.js index 4e77f7b..ada6688 100644 --- a/src/modules/fedimbed.js +++ b/src/modules/fedimbed.js @@ -348,35 +348,62 @@ async function processUrl(msg, url, spoiler = false) { const attachments = postData2.media_attachments ?? postData2.files; if (attachments) { for (const attachment of attachments) { - const fileType = - attachment.pleroma?.mime_type ?? attachment.type.indexOf("/") > -1 - ? attachment.type - : attachment.type + - "/" + - (url.match(/\.([a-z0-9]{3,4})$/)?.[0] ?? - attachment.type == "image" - ? "png" - : attachment.type == "video" - ? "mp4" - : "mpeg"); - if (attachment.type.startsWith("image")) { - images.push({ - url: attachment.url, - desc: attachment.description ?? attachment.comment, - type: fileType, - }); - } else if (attachment.type.startsWith("video")) { - videos.push({ - url: attachment.url, - desc: attachment.description ?? attachment.comment, - type: fileType, - }); - } else if (attachment.type.startsWith("audio")) { - audios.push({ - url: attachment.url, - desc: attachment.description ?? attachment.comment, - type: fileType, - }); + const contentType = await fetch(attachment.url, { + method: "HEAD", + }).then((res) => res.headers.get("Content-Type")); + + if (contentType) { + if (contentType.startsWith("image/")) { + images.push({ + url: attachment.url, + desc: attachment.description ?? attachment.comment, + type: contentType, + }); + } else if (contentType.startsWith("video/")) { + videos.push({ + url: attachment.url, + desc: attachment.description ?? attachment.comment, + type: contentType, + }); + } else if (contentType.startsWith("audio/")) { + audios.push({ + url: attachment.url, + desc: attachment.description ?? attachment.comment, + type: contentType, + }); + } + } else { + const type = attachment.type?.toLowerCase(); + + const fileType = + attachment.pleroma?.mime_type ?? type.indexOf("/") > -1 + ? type + : type + + "/" + + (url.match(/\.([a-z0-9]{3,4})$/)?.[0] ?? type == "image" + ? "png" + : type == "video" + ? "mp4" + : "mpeg"); + if (type.startsWith("image")) { + images.push({ + url: attachment.url, + desc: attachment.description ?? attachment.comment, + type: fileType, + }); + } else if (type.startsWith("video")) { + videos.push({ + url: attachment.url, + desc: attachment.description ?? attachment.comment, + type: fileType, + }); + } else if (type.startsWith("audio")) { + audios.push({ + url: attachment.url, + desc: attachment.description ?? attachment.comment, + type: fileType, + }); + } } } } @@ -423,7 +450,7 @@ async function processUrl(msg, url, spoiler = false) { .map((x) => ({name: x.name, url: x.icon.url})); } - // NB: gts doesnt send singular attachments as areay + // NB: gts doesnt send singular attachments as array const attachments = Array.isArray(postData.attachment) ? postData.attachment : [postData.attachment]; @@ -432,22 +459,89 @@ async function processUrl(msg, url, spoiler = false) { if (attachment.mediaType.startsWith("video/")) { videos.push({ url: attachment.url, - desc: attachment.name, + desc: + attachment.name ?? attachment.description ?? attachment.comment, type: attachment.mediaType, }); } else if (attachment.mediaType.startsWith("image/")) { images.push({ url: attachment.url, - desc: attachment.name, + desc: + attachment.name ?? attachment.description ?? attachment.comment, type: attachment.mediaType, }); } else if (attachment.mediaType.startsWith("audio/")) { audios.push({ url: attachment.url, - desc: attachment.name, + desc: + attachment.name ?? attachment.description ?? attachment.comment, type: attachment.mediaType, }); } + } else { + const contentType = await fetch(attachment.url, { + method: "HEAD", + }).then((res) => res.headers.get("Content-Type")); + + if (contentType) { + if (contentType.startsWith("image/")) { + images.push({ + url: attachment.url, + desc: + attachment.name ?? attachment.description ?? attachment.comment, + type: contentType, + }); + } else if (contentType.startsWith("video/")) { + videos.push({ + url: attachment.url, + desc: + attachment.name ?? attachment.description ?? attachment.comment, + type: contentType, + }); + } else if (contentType.startsWith("audio/")) { + audios.push({ + url: attachment.url, + desc: + attachment.name ?? attachment.description ?? attachment.comment, + type: contentType, + }); + } + } else { + const type = attachment.type?.toLowerCase(); + + const fileType = + type.indexOf("/") > -1 + ? type + : type + + "/" + + (url.match(/\.([a-z0-9]{3,4})$/)?.[0] ?? type == "image" + ? "png" + : type == "video" + ? "mp4" + : "mpeg"); + if (type.startsWith("image")) { + images.push({ + url: attachment.url, + desc: + attachment.name ?? attachment.description ?? attachment.comment, + type: fileType, + }); + } else if (type.startsWith("video")) { + videos.push({ + url: attachment.url, + desc: + attachment.name ?? attachment.description ?? attachment.comment, + type: fileType, + }); + } else if (type.startsWith("audio")) { + audios.push({ + url: attachment.url, + desc: + attachment.name ?? attachment.description ?? attachment.comment, + type: fileType, + }); + } + } } } @@ -456,13 +550,17 @@ async function processUrl(msg, url, spoiler = false) { } if (postData.image?.url) { - const imageUrl = new URL(postData.image?.url); + const imageUrl = new URL(postData.image.url); + const contentType = await fetch(postData.image.url, { + method: "HEAD", + }).then((res) => res.headers.get("Content-Type")); images.push({ - url: postData.image?.url, + url: postData.image.url, desc: "", type: + contentType ?? "image/" + - imageUrl.pathname.substring(imageUrl.pathname.lastIndexOf(".") + 1), + imageUrl.pathname.substring(imageUrl.pathname.lastIndexOf(".") + 1), }); }