mirror of
https://github.com/open-webui/open-webui.git
synced 2025-12-16 11:57:51 +01:00
fix/refac: heic image handling
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<script lang="ts">
|
||||
import DOMPurify from 'dompurify';
|
||||
import { marked } from 'marked';
|
||||
import heic2any from 'heic2any';
|
||||
|
||||
import { toast } from 'svelte-sonner';
|
||||
|
||||
@@ -320,7 +321,7 @@
|
||||
return;
|
||||
}
|
||||
|
||||
inputFiles.forEach((file) => {
|
||||
inputFiles.forEach(async (file) => {
|
||||
console.log('Processing file:', {
|
||||
name: file.name,
|
||||
type: file.type,
|
||||
@@ -344,46 +345,53 @@
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
['image/gif', 'image/webp', 'image/jpeg', 'image/png', 'image/avif'].includes(file['type'])
|
||||
) {
|
||||
if (file['type'].startsWith('image/')) {
|
||||
if (visionCapableModels.length === 0) {
|
||||
toast.error($i18n.t('Selected model(s) do not support image inputs'));
|
||||
return;
|
||||
}
|
||||
|
||||
const compressImageHandler = async (imageUrl, settings = {}, config = {}) => {
|
||||
// Quick shortcut so we don’t do unnecessary work.
|
||||
const settingsCompression = settings?.imageCompression ?? false;
|
||||
const configWidth = config?.file?.image_compression?.width ?? null;
|
||||
const configHeight = config?.file?.image_compression?.height ?? null;
|
||||
|
||||
// If neither settings nor config wants compression, return original URL.
|
||||
if (!settingsCompression && !configWidth && !configHeight) {
|
||||
return imageUrl;
|
||||
}
|
||||
|
||||
// Default to null (no compression unless set)
|
||||
let width = null;
|
||||
let height = null;
|
||||
|
||||
// If user/settings want compression, pick their preferred size.
|
||||
if (settingsCompression) {
|
||||
width = settings?.imageCompressionSize?.width ?? null;
|
||||
height = settings?.imageCompressionSize?.height ?? null;
|
||||
}
|
||||
|
||||
// Apply config limits as an upper bound if any
|
||||
if (configWidth && (width === null || width > configWidth)) {
|
||||
width = configWidth;
|
||||
}
|
||||
if (configHeight && (height === null || height > configHeight)) {
|
||||
height = configHeight;
|
||||
}
|
||||
|
||||
// Do the compression if required
|
||||
if (width || height) {
|
||||
return await compressImage(imageUrl, width, height);
|
||||
}
|
||||
return imageUrl;
|
||||
};
|
||||
|
||||
let reader = new FileReader();
|
||||
reader.onload = async (event) => {
|
||||
let imageUrl = event.target.result;
|
||||
|
||||
if (
|
||||
($settings?.imageCompression ?? false) ||
|
||||
($config?.file?.image_compression?.width ?? null) ||
|
||||
($config?.file?.image_compression?.height ?? null)
|
||||
) {
|
||||
let width = null;
|
||||
let height = null;
|
||||
|
||||
if ($settings?.imageCompression ?? false) {
|
||||
width = $settings?.imageCompressionSize?.width ?? null;
|
||||
height = $settings?.imageCompressionSize?.height ?? null;
|
||||
}
|
||||
|
||||
if (
|
||||
($config?.file?.image_compression?.width ?? null) ||
|
||||
($config?.file?.image_compression?.height ?? null)
|
||||
) {
|
||||
if (width > ($config?.file?.image_compression?.width ?? null)) {
|
||||
width = $config?.file?.image_compression?.width ?? null;
|
||||
}
|
||||
if (height > ($config?.file?.image_compression?.height ?? null)) {
|
||||
height = $config?.file?.image_compression?.height ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
if (width || height) {
|
||||
imageUrl = await compressImage(imageUrl, width, height);
|
||||
}
|
||||
}
|
||||
imageUrl = await compressImageHandler(imageUrl, $settings, $config);
|
||||
|
||||
files = [
|
||||
...files,
|
||||
@@ -393,7 +401,11 @@
|
||||
}
|
||||
];
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
reader.readAsDataURL(
|
||||
file['type'] === 'image/heic'
|
||||
? await heic2any({ blob: file, toType: 'image/jpeg' })
|
||||
: file
|
||||
);
|
||||
} else {
|
||||
uploadFileHandler(file);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user