Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions js/image-viewer/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { isArray, isString } from 'lodash-es';
import type { ImageInfo, Images } from './types';

const COMPRESSED_FORMATS = ['jpg', 'jpeg', 'webp'];

const isSameOrigin = (url: string) => {
try {
const imgUrl = new URL(url, window.location.href);
return imgUrl.origin === window.location.origin;
} catch {
return true;
return false;
}
};

Expand All @@ -29,17 +31,27 @@ const canvasDownload = (imgSrc: string, name: string) => {

const context = canvas.getContext('2d');
context.drawImage(image, 0, 0, image.width, image.height);
canvas.toBlob((blob) => {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.download = name;
a.href = url;
a.click();
a.remove();
URL.revokeObjectURL(url);
});
};

const extension = name.split('.').pop()?.toLowerCase() || 'png';
const mimeType = `image/${extension === 'jpg' ? 'jpeg' : extension}`;

let quality = 1.0;
if (COMPRESSED_FORMATS.includes(extension)) quality = 0.95;

canvas.toBlob(
(blob) => {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.download = name;
a.href = url;
a.click();
a.remove();
URL.revokeObjectURL(url);
},
mimeType,
quality
);
};
image.src = imgSrc;
};

Expand All @@ -53,7 +65,7 @@ const fileDownload = (file: File, name: string) => {
URL.revokeObjectURL(url);
};

export const downloadFile = (imgSrc: string | File) => {
export const downloadImage = (imgSrc: string | File) => {
const randomName = Math.random().toString(32).slice(2);

if (imgSrc instanceof File) {
Expand Down