|
@@ -1,6 +1,7 @@
|
|
<script lang="ts">
|
|
<script lang="ts">
|
|
import { toast } from 'svelte-sonner';
|
|
import { toast } from 'svelte-sonner';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
+ import heic2any from 'heic2any';
|
|
|
|
|
|
import { tick, getContext, onMount, onDestroy } from 'svelte';
|
|
import { tick, getContext, onMount, onDestroy } from 'svelte';
|
|
|
|
|
|
@@ -78,7 +79,7 @@
|
|
};
|
|
};
|
|
|
|
|
|
const inputFilesHandler = async (inputFiles) => {
|
|
const inputFilesHandler = async (inputFiles) => {
|
|
- inputFiles.forEach((file) => {
|
|
|
|
|
|
+ inputFiles.forEach(async (file) => {
|
|
console.info('Processing file:', {
|
|
console.info('Processing file:', {
|
|
name: file.name,
|
|
name: file.name,
|
|
type: file.type,
|
|
type: file.type,
|
|
@@ -102,44 +103,51 @@
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
- if (
|
|
|
|
- ['image/gif', 'image/webp', 'image/jpeg', 'image/png', 'image/avif'].includes(file['type'])
|
|
|
|
- ) {
|
|
|
|
- let reader = new FileReader();
|
|
|
|
|
|
+ if (file['type'].startsWith('image/')) {
|
|
|
|
+ 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;
|
|
|
|
|
|
- reader.onload = async (event) => {
|
|
|
|
- let imageUrl = event.target.result;
|
|
|
|
|
|
+ // If neither settings nor config wants compression, return original URL.
|
|
|
|
+ if (!settingsCompression && !configWidth && !configHeight) {
|
|
|
|
+ return imageUrl;
|
|
|
|
+ }
|
|
|
|
|
|
- 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;
|
|
|
|
- }
|
|
|
|
|
|
+ // Default to null (no compression unless set)
|
|
|
|
+ let width = null;
|
|
|
|
+ let 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 user/settings want compression, pick their preferred size.
|
|
|
|
+ if (settingsCompression) {
|
|
|
|
+ width = settings?.imageCompressionSize?.width ?? null;
|
|
|
|
+ height = settings?.imageCompressionSize?.height ?? null;
|
|
|
|
+ }
|
|
|
|
|
|
- if (width || height) {
|
|
|
|
- imageUrl = await compressImage(imageUrl, width, height);
|
|
|
|
- }
|
|
|
|
|
|
+ // 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;
|
|
|
|
+
|
|
|
|
+ // Compress the image if settings or config require it
|
|
|
|
+ imageUrl = await compressImageHandler(imageUrl, $settings, $config);
|
|
|
|
+
|
|
files = [
|
|
files = [
|
|
...files,
|
|
...files,
|
|
{
|
|
{
|
|
@@ -149,7 +157,11 @@
|
|
];
|
|
];
|
|
};
|
|
};
|
|
|
|
|
|
- reader.readAsDataURL(file);
|
|
|
|
|
|
+ reader.readAsDataURL(
|
|
|
|
+ file['type'] === 'image/heic'
|
|
|
|
+ ? await heic2any({ blob: file, toType: 'image/jpeg' })
|
|
|
|
+ : file
|
|
|
|
+ );
|
|
} else {
|
|
} else {
|
|
uploadFileHandler(file);
|
|
uploadFileHandler(file);
|
|
}
|
|
}
|