feat: dynamically load heic2any to accelerate initial page loading speed

This commit is contained in:
Shirasawa 2025-09-05 20:07:12 +08:00
parent c0a47169fa
commit a74ec200b3
3 changed files with 16 additions and 6 deletions

View file

@ -5,7 +5,6 @@
import DOMPurify from 'dompurify';
import { marked } from 'marked';
import heic2any from 'heic2any';
import { toast } from 'svelte-sonner';
@ -32,7 +31,7 @@
} from '$lib/stores';
import {
blobToFile,
convertHeicToJpeg,
compressImage,
createMessagesList,
extractContentFromFile,
@ -765,7 +764,7 @@
};
reader.readAsDataURL(
file['type'] === 'image/heic'
? await heic2any({ blob: file, toType: 'image/jpeg' })
? await convertHeicToJpeg(file)
: file
);
} else {

View file

@ -1,7 +1,6 @@
<script lang="ts">
import { getContext, onDestroy, onMount, tick } from 'svelte';
import { v4 as uuidv4 } from 'uuid';
import heic2any from 'heic2any';
import fileSaver from 'file-saver';
const { saveAs } = fileSaver;
@ -26,7 +25,7 @@
import { PaneGroup, Pane, PaneResizer } from 'paneforge';
import { compressImage, copyToClipboard, splitStream } from '$lib/utils';
import { compressImage, copyToClipboard, splitStream, convertHeicToJpeg } from '$lib/utils';
import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
import { uploadFile } from '$lib/apis/files';
import { chatCompletion, generateOpenAIChatCompletion } from '$lib/apis/openai';
@ -547,7 +546,7 @@ ${content}
reader.readAsDataURL(
file['type'] === 'image/heic'
? await heic2any({ blob: file, toType: 'image/jpeg' })
? await convertHeicToJpeg(file)
: file
);
});

View file

@ -1528,3 +1528,15 @@ export const getAge = (birthDate) => {
}
return age.toString();
};
export const convertHeicToJpeg = async (file: File) => {
const { default: heic2any } = await import('heic2any');
try {
return await heic2any({ blob: file, toType: 'image/jpeg' });
} catch (err: any) {
if (err?.message?.includes('already browser readable')) {
return file;
}
throw err;
}
};