server/database/entities/Attachment.ts

70 lines
2.2 KiB
TypeScript
Raw Normal View History

import type { Attachment } from "@prisma/client";
2024-03-11 00:57:26 +01:00
import type { ConfigType } from "config-manager";
import { MediaBackendType } from "media-manager";
import type { APIAsyncAttachment } from "~types/entities/async_attachment";
import type { APIAttachment } from "~types/entities/attachment";
2023-11-22 01:56:58 +01:00
export const attachmentToAPI = (
attachment: Attachment
): APIAsyncAttachment | APIAttachment => {
let type = "unknown";
if (attachment.mime_type.startsWith("image/")) {
type = "image";
} else if (attachment.mime_type.startsWith("video/")) {
type = "video";
} else if (attachment.mime_type.startsWith("audio/")) {
type = "audio";
}
return {
id: attachment.id,
type: type as any,
url: attachment.url,
remote_url: attachment.remote_url,
preview_url: attachment.thumbnail_url,
text_url: null,
meta: {
width: attachment.width || undefined,
height: attachment.height || undefined,
fps: attachment.fps || undefined,
2023-11-22 04:00:14 +01:00
size:
attachment.width && attachment.height
? `${attachment.width}x${attachment.height}`
: undefined,
2023-11-22 01:56:58 +01:00
duration: attachment.duration || undefined,
length: attachment.size?.toString() || undefined,
2023-11-22 04:00:14 +01:00
aspect:
attachment.width && attachment.height
? attachment.width / attachment.height
: undefined,
original: {
width: attachment.width || undefined,
height: attachment.height || undefined,
size:
attachment.width && attachment.height
? `${attachment.width}x${attachment.height}`
: undefined,
aspect:
attachment.width && attachment.height
? attachment.width / attachment.height
: undefined,
},
2023-11-22 01:56:58 +01:00
// Idk whether size or length is the right value
},
description: attachment.description,
blurhash: attachment.blurhash,
};
};
2024-03-11 00:57:26 +01:00
export const getUrl = (name: string, config: ConfigType) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
if (config.media.backend === MediaBackendType.LOCAL) {
return `${config.http.base_url}/media/${name}`;
2024-03-14 04:39:32 +01:00
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison, @typescript-eslint/no-unnecessary-condition
2024-03-11 00:57:26 +01:00
} else if (config.media.backend === MediaBackendType.S3) {
return `${config.s3.public_url}/${name}`;
2023-11-22 01:56:58 +01:00
}
return "";
};