mirror of
https://github.com/aljazceru/ditto.git
synced 2025-12-29 03:04:25 +01:00
media: add attachment view, unify types
This commit is contained in:
34
src/views/attachment.ts
Normal file
34
src/views/attachment.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { UnattachedMedia } from '@/db/unattached-media.ts';
|
||||
import { type TypeFest } from '@/deps.ts';
|
||||
|
||||
type DittoAttachment = TypeFest.SetOptional<UnattachedMedia, 'id' | 'pubkey' | 'uploaded_at'>;
|
||||
|
||||
function renderAttachment(media: DittoAttachment) {
|
||||
const { id, data, url } = media;
|
||||
return {
|
||||
id: id ?? url ?? data.cid,
|
||||
type: getAttachmentType(data.mime ?? ''),
|
||||
url,
|
||||
preview_url: url,
|
||||
remote_url: null,
|
||||
description: data.description ?? '',
|
||||
blurhash: data.blurhash || null,
|
||||
cid: data.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 { type DittoAttachment, renderAttachment };
|
||||
Reference in New Issue
Block a user