mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
fix(ui): error handling if unable to convert image URL to blob
This commit is contained in:
parent
63494dfca7
commit
2298be0e6b
@ -8,7 +8,7 @@ import { $authToken } from 'app/store/nanostores/authToken';
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export const convertImageUrlToBlob = async (url: string) =>
|
export const convertImageUrlToBlob = async (url: string) =>
|
||||||
new Promise<Blob | null>((resolve) => {
|
new Promise<Blob | null>((resolve, reject) => {
|
||||||
const img = new Image();
|
const img = new Image();
|
||||||
img.onload = () => {
|
img.onload = () => {
|
||||||
const canvas = document.createElement('canvas');
|
const canvas = document.createElement('canvas');
|
||||||
@ -17,17 +17,23 @@ export const convertImageUrlToBlob = async (url: string) =>
|
|||||||
|
|
||||||
const context = canvas.getContext('2d');
|
const context = canvas.getContext('2d');
|
||||||
if (!context) {
|
if (!context) {
|
||||||
|
reject(new Error('Failed to get canvas context'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
context.drawImage(img, 0, 0);
|
context.drawImage(img, 0, 0);
|
||||||
resolve(
|
canvas.toBlob((blob) => {
|
||||||
new Promise<Blob | null>((resolve) => {
|
if (blob) {
|
||||||
canvas.toBlob(function (blob) {
|
|
||||||
resolve(blob);
|
resolve(blob);
|
||||||
|
} else {
|
||||||
|
reject(new Error('Failed to convert image to blob'));
|
||||||
|
}
|
||||||
}, 'image/png');
|
}, 'image/png');
|
||||||
})
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
img.onerror = () => {
|
||||||
|
reject(new Error('Image failed to load. The URL may be invalid or the object may not exist.'));
|
||||||
|
};
|
||||||
|
|
||||||
img.crossOrigin = $authToken.get() ? 'use-credentials' : 'anonymous';
|
img.crossOrigin = $authToken.get() ? 'use-credentials' : 'anonymous';
|
||||||
img.src = url;
|
img.src = url;
|
||||||
});
|
});
|
||||||
|
@ -48,10 +48,14 @@ export const StylePresetModal = () => {
|
|||||||
} else {
|
} else {
|
||||||
let file = null;
|
let file = null;
|
||||||
if (data.imageUrl) {
|
if (data.imageUrl) {
|
||||||
|
try {
|
||||||
const blob = await convertImageUrlToBlob(data.imageUrl);
|
const blob = await convertImageUrlToBlob(data.imageUrl);
|
||||||
if (blob) {
|
if (blob) {
|
||||||
file = new File([blob], 'style_preset.png', { type: 'image/png' });
|
file = new File([blob], 'style_preset.png', { type: 'image/png' });
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
setFormData({
|
setFormData({
|
||||||
...data,
|
...data,
|
||||||
|
Loading…
Reference in New Issue
Block a user