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;
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),
});
}