mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-12 21:08:46 +00:00
브라우저가 heic 디코딩을 지원하는 경우 heic2any를 사용하지 않도록 개선 및 브라우저가 webp 인코딩을 지원하지 않는 경우 썸네일을 생성하지 않도록 수정
This commit is contained in:
@@ -32,7 +32,7 @@ const capture = (
|
|||||||
|
|
||||||
drawer(context, scaledWidth, scaledHeight);
|
drawer(context, scaledWidth, scaledHeight);
|
||||||
canvas.toBlob((blob) => {
|
canvas.toBlob((blob) => {
|
||||||
if (blob) {
|
if (blob && blob.type === "image/webp") {
|
||||||
resolve(blob);
|
resolve(blob);
|
||||||
} else {
|
} else {
|
||||||
reject(new Error("Failed to generate thumbnail"));
|
reject(new Error("Failed to generate thumbnail"));
|
||||||
@@ -83,18 +83,26 @@ const generateVideoThumbnail = (videoUrl: string, time = 0) => {
|
|||||||
export const generateThumbnail = async (fileBuffer: ArrayBuffer, fileType: string) => {
|
export const generateThumbnail = async (fileBuffer: ArrayBuffer, fileType: string) => {
|
||||||
let url;
|
let url;
|
||||||
try {
|
try {
|
||||||
if (fileType === "image/heic") {
|
if (fileType.startsWith("image/")) {
|
||||||
const { default: heic2any } = await import("heic2any");
|
const fileBlob = new Blob([fileBuffer], { type: fileType });
|
||||||
url = URL.createObjectURL(
|
url = URL.createObjectURL(fileBlob);
|
||||||
(await heic2any({
|
|
||||||
blob: new Blob([fileBuffer], { type: fileType }),
|
try {
|
||||||
toType: "image/png",
|
return await generateImageThumbnail(url);
|
||||||
})) as Blob,
|
} catch {
|
||||||
);
|
URL.revokeObjectURL(url);
|
||||||
return await generateImageThumbnail(url);
|
url = undefined;
|
||||||
} else if (fileType.startsWith("image/")) {
|
|
||||||
url = URL.createObjectURL(new Blob([fileBuffer], { type: fileType }));
|
if (fileType === "image/heic") {
|
||||||
return await generateImageThumbnail(url);
|
const { default: heic2any } = await import("heic2any");
|
||||||
|
url = URL.createObjectURL(
|
||||||
|
(await heic2any({ blob: fileBlob, toType: "image/png" })) as Blob,
|
||||||
|
);
|
||||||
|
return await generateImageThumbnail(url);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if (fileType.startsWith("video/")) {
|
} else if (fileType.startsWith("video/")) {
|
||||||
url = URL.createObjectURL(new Blob([fileBuffer], { type: fileType }));
|
url = URL.createObjectURL(new Blob([fileBuffer], { type: fileType }));
|
||||||
return await generateVideoThumbnail(url);
|
return await generateVideoThumbnail(url);
|
||||||
|
|||||||
@@ -43,22 +43,31 @@
|
|||||||
let isDownloadRequested = $state(false);
|
let isDownloadRequested = $state(false);
|
||||||
let viewerType: "image" | "video" | undefined = $state();
|
let viewerType: "image" | "video" | undefined = $state();
|
||||||
let fileBlobUrl: string | undefined = $state();
|
let fileBlobUrl: string | undefined = $state();
|
||||||
|
let heicBlob: Blob | undefined = $state();
|
||||||
let videoElement: HTMLVideoElement | undefined = $state();
|
let videoElement: HTMLVideoElement | undefined = $state();
|
||||||
|
|
||||||
const updateViewer = async (buffer: ArrayBuffer, contentType: string) => {
|
const updateViewer = async (buffer: ArrayBuffer, contentType: string) => {
|
||||||
const fileBlob = new Blob([buffer], { type: contentType });
|
const fileBlob = new Blob([buffer], { type: contentType });
|
||||||
if (contentType === "image/heic") {
|
if (viewerType) {
|
||||||
const { default: heic2any } = await import("heic2any");
|
|
||||||
fileBlobUrl = URL.createObjectURL(
|
|
||||||
(await heic2any({ blob: fileBlob, toType: "image/jpeg" })) as Blob,
|
|
||||||
);
|
|
||||||
} else if (viewerType) {
|
|
||||||
fileBlobUrl = URL.createObjectURL(fileBlob);
|
fileBlobUrl = URL.createObjectURL(fileBlob);
|
||||||
|
heicBlob = contentType === "image/heic" ? fileBlob : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
return fileBlob;
|
return fileBlob;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const convertHeicToJpeg = async () => {
|
||||||
|
if (!heicBlob) return;
|
||||||
|
|
||||||
|
URL.revokeObjectURL(fileBlobUrl!);
|
||||||
|
fileBlobUrl = undefined;
|
||||||
|
|
||||||
|
const { default: heic2any } = await import("heic2any");
|
||||||
|
fileBlobUrl = URL.createObjectURL(
|
||||||
|
(await heic2any({ blob: heicBlob, toType: "image/jpeg" })) as Blob,
|
||||||
|
);
|
||||||
|
heicBlob = undefined;
|
||||||
|
};
|
||||||
|
|
||||||
const updateThumbnail = async (dataKey: CryptoKey, dataKeyVersion: Date) => {
|
const updateThumbnail = async (dataKey: CryptoKey, dataKeyVersion: Date) => {
|
||||||
const thumbnail = await captureVideoThumbnail(videoElement!);
|
const thumbnail = await captureVideoThumbnail(videoElement!);
|
||||||
await requestThumbnailUpload(data.id, thumbnail, dataKey, dataKeyVersion);
|
await requestThumbnailUpload(data.id, thumbnail, dataKey, dataKeyVersion);
|
||||||
@@ -136,7 +145,7 @@
|
|||||||
|
|
||||||
{#if viewerType === "image"}
|
{#if viewerType === "image"}
|
||||||
{#if fileBlobUrl}
|
{#if fileBlobUrl}
|
||||||
<img src={fileBlobUrl} alt={$info.name} />
|
<img src={fileBlobUrl} alt={$info.name} onerror={convertHeicToJpeg} />
|
||||||
{:else}
|
{:else}
|
||||||
{@render viewerLoading("이미지를 불러오고 있어요.")}
|
{@render viewerLoading("이미지를 불러오고 있어요.")}
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
Reference in New Issue
Block a user