From 67ebc743217eafd977cc72893a9f92336adb516c Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 1 Dec 2022 11:15:31 +0800 Subject: [PATCH] fix: guess attachment type --- components/status/StatusAttachment.vue | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/components/status/StatusAttachment.vue b/components/status/StatusAttachment.vue index 228c1586..db4cb8b0 100644 --- a/components/status/StatusAttachment.vue +++ b/components/status/StatusAttachment.vue @@ -32,10 +32,28 @@ const objectPosition = computed(() => { .map(v => v ? `${v * 100}%` : '50%') .join(' ') }) + +const typeExtsMap = { + video: ['mp4', 'webm', 'mov', 'avi', 'mkv', 'flv', 'wmv', 'mpg', 'mpeg'], + audio: ['mp3', 'wav', 'ogg', 'flac', 'aac', 'm4a', 'wma'], + image: ['jpg', 'jpeg', 'png', 'svg', 'webp', 'bmp'], + gifv: ['gifv', 'gif'], +} + +const type = $computed(() => { + if (attachment.type && attachment.type !== 'unknown') + return attachment.type + // some server returns unknown type, we need to guess it based on file extension + for (const [type, exts] of Object.entries(typeExtsMap)) { + if (exts.some(ext => src?.toLowerCase().endsWith(`.${ext}`))) + return type + } + return 'unknown' +})