fedimbed: attachment fixes, mainly for cohost

This commit is contained in:
Cynthia Foxwell 2024-01-07 13:22:04 -07:00
parent a7ca9fd347
commit 022202bd1c

View file

@ -348,30 +348,56 @@ async function processUrl(msg, url, spoiler = false) {
const attachments = postData2.media_attachments ?? postData2.files; const attachments = postData2.media_attachments ?? postData2.files;
if (attachments) { if (attachments) {
for (const attachment of attachments) { for (const attachment of attachments) {
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 = const fileType =
attachment.pleroma?.mime_type ?? attachment.type.indexOf("/") > -1 attachment.pleroma?.mime_type ?? type.indexOf("/") > -1
? attachment.type ? type
: attachment.type + : type +
"/" + "/" +
(url.match(/\.([a-z0-9]{3,4})$/)?.[0] ?? (url.match(/\.([a-z0-9]{3,4})$/)?.[0] ?? type == "image"
attachment.type == "image"
? "png" ? "png"
: attachment.type == "video" : type == "video"
? "mp4" ? "mp4"
: "mpeg"); : "mpeg");
if (attachment.type.startsWith("image")) { if (type.startsWith("image")) {
images.push({ images.push({
url: attachment.url, url: attachment.url,
desc: attachment.description ?? attachment.comment, desc: attachment.description ?? attachment.comment,
type: fileType, type: fileType,
}); });
} else if (attachment.type.startsWith("video")) { } else if (type.startsWith("video")) {
videos.push({ videos.push({
url: attachment.url, url: attachment.url,
desc: attachment.description ?? attachment.comment, desc: attachment.description ?? attachment.comment,
type: fileType, type: fileType,
}); });
} else if (attachment.type.startsWith("audio")) { } else if (type.startsWith("audio")) {
audios.push({ audios.push({
url: attachment.url, url: attachment.url,
desc: attachment.description ?? attachment.comment, desc: attachment.description ?? attachment.comment,
@ -380,6 +406,7 @@ async function processUrl(msg, url, spoiler = false) {
} }
} }
} }
}
if (!spoiler && postData2.sensitive && attachments.length > 0) { if (!spoiler && postData2.sensitive && attachments.length > 0) {
spoiler = true; spoiler = true;
} }
@ -423,7 +450,7 @@ async function processUrl(msg, url, spoiler = false) {
.map((x) => ({name: x.name, url: x.icon.url})); .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) const attachments = Array.isArray(postData.attachment)
? postData.attachment ? postData.attachment
: [postData.attachment]; : [postData.attachment];
@ -432,22 +459,89 @@ async function processUrl(msg, url, spoiler = false) {
if (attachment.mediaType.startsWith("video/")) { if (attachment.mediaType.startsWith("video/")) {
videos.push({ videos.push({
url: attachment.url, url: attachment.url,
desc: attachment.name, desc:
attachment.name ?? attachment.description ?? attachment.comment,
type: attachment.mediaType, type: attachment.mediaType,
}); });
} else if (attachment.mediaType.startsWith("image/")) { } else if (attachment.mediaType.startsWith("image/")) {
images.push({ images.push({
url: attachment.url, url: attachment.url,
desc: attachment.name, desc:
attachment.name ?? attachment.description ?? attachment.comment,
type: attachment.mediaType, type: attachment.mediaType,
}); });
} else if (attachment.mediaType.startsWith("audio/")) { } else if (attachment.mediaType.startsWith("audio/")) {
audios.push({ audios.push({
url: attachment.url, url: attachment.url,
desc: attachment.name, desc:
attachment.name ?? attachment.description ?? attachment.comment,
type: attachment.mediaType, 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,11 +550,15 @@ async function processUrl(msg, url, spoiler = false) {
} }
if (postData.image?.url) { 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({ images.push({
url: postData.image?.url, url: postData.image.url,
desc: "", desc: "",
type: type:
contentType ??
"image/" + "image/" +
imageUrl.pathname.substring(imageUrl.pathname.lastIndexOf(".") + 1), imageUrl.pathname.substring(imageUrl.pathname.lastIndexOf(".") + 1),
}); });