mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import { getUrlMediaType } from '@/utils/media.ts';
|
|
|
|
/** Render Mastodon media attachment. */
|
|
function renderAttachment(media: { id?: string; data: string[][] }) {
|
|
const { id, data: tags } = media;
|
|
|
|
const url = tags.find(([name]) => name === 'url')?.[1];
|
|
|
|
const m = tags.find(([name]) => name === 'm')?.[1] ?? getUrlMediaType(url!);
|
|
const alt = tags.find(([name]) => name === 'alt')?.[1];
|
|
const cid = tags.find(([name]) => name === 'cid')?.[1];
|
|
const dim = tags.find(([name]) => name === 'dim')?.[1];
|
|
const blurhash = tags.find(([name]) => name === 'blurhash')?.[1];
|
|
|
|
if (!url) return;
|
|
|
|
const [width, height] = dim?.split('x').map(Number) ?? [null, null];
|
|
|
|
const meta = (width && height)
|
|
? {
|
|
original: {
|
|
width,
|
|
height,
|
|
aspect: width / height,
|
|
},
|
|
}
|
|
: undefined;
|
|
|
|
return {
|
|
id: id ?? url,
|
|
type: getAttachmentType(m ?? ''),
|
|
url,
|
|
preview_url: url,
|
|
remote_url: null,
|
|
description: alt ?? '',
|
|
blurhash: blurhash || null,
|
|
meta,
|
|
cid: cid,
|
|
};
|
|
}
|
|
|
|
/** MIME to Mastodon API `Attachment` type. */
|
|
function getAttachmentType(mime: string): string {
|
|
const [type] = mime.split('/');
|
|
|
|
switch (type) {
|
|
case 'image':
|
|
case 'video':
|
|
case 'audio':
|
|
return type;
|
|
default:
|
|
return 'unknown';
|
|
}
|
|
}
|
|
|
|
export { renderAttachment };
|