瀏覽代碼

feat: dynamically load heic2any to accelerate initial page loading speed

Shirasawa 1 月之前
父節點
當前提交
a74ec200b3
共有 3 個文件被更改,包括 16 次插入6 次删除
  1. 2 3
      src/lib/components/chat/MessageInput.svelte
  2. 2 3
      src/lib/components/notes/NoteEditor.svelte
  3. 12 0
      src/lib/utils/index.ts

+ 2 - 3
src/lib/components/chat/MessageInput.svelte

@@ -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 {

+ 2 - 3
src/lib/components/notes/NoteEditor.svelte

@@ -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
 				);
 			});

+ 12 - 0
src/lib/utils/index.ts

@@ -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;
+	}
+};