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,35 +348,62 @@ 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 fileType = const contentType = await fetch(attachment.url, {
attachment.pleroma?.mime_type ?? attachment.type.indexOf("/") > -1 method: "HEAD",
? attachment.type }).then((res) => res.headers.get("Content-Type"));
: attachment.type +
"/" + if (contentType) {
(url.match(/\.([a-z0-9]{3,4})$/)?.[0] ?? if (contentType.startsWith("image/")) {
attachment.type == "image" images.push({
? "png" url: attachment.url,
: attachment.type == "video" desc: attachment.description ?? attachment.comment,
? "mp4" type: contentType,
: "mpeg"); });
if (attachment.type.startsWith("image")) { } else if (contentType.startsWith("video/")) {
images.push({ videos.push({
url: attachment.url, url: attachment.url,
desc: attachment.description ?? attachment.comment, desc: attachment.description ?? attachment.comment,
type: fileType, type: contentType,
}); });
} else if (attachment.type.startsWith("video")) { } else if (contentType.startsWith("audio/")) {
videos.push({ audios.push({
url: attachment.url, url: attachment.url,
desc: attachment.description ?? attachment.comment, desc: attachment.description ?? attachment.comment,
type: fileType, type: contentType,
}); });
} else if (attachment.type.startsWith("audio")) { }
audios.push({ } else {
url: attachment.url, const type = attachment.type?.toLowerCase();
desc: attachment.description ?? attachment.comment,
type: fileType, 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})); .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,13 +550,17 @@ 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),
}); });
} }