Spaces:
Runtime error
Runtime error
new images endpoint
Browse files- frontend/src/lib/App.svelte +3 -3
- frontend/src/lib/PaintCanvas.svelte +1 -2
- frontend/src/lib/utils.ts +10 -11
- frontend/vite.config.dev.ts +3 -3
frontend/src/lib/App.svelte
CHANGED
|
@@ -31,7 +31,7 @@
|
|
| 31 |
};
|
| 32 |
myPresence.update(initialPresence);
|
| 33 |
|
| 34 |
-
function getKey({
|
| 35 |
return `${position.x}_${position.y}`;
|
| 36 |
}
|
| 37 |
|
|
@@ -121,8 +121,9 @@
|
|
| 121 |
if (isNSWF) {
|
| 122 |
throw new Error('NFSW');
|
| 123 |
}
|
|
|
|
| 124 |
const imgBlob = await base64ToBlob(imgBase64);
|
| 125 |
-
const imgURL = await uploadImage(imgBlob, prompt);
|
| 126 |
const promptImg = {
|
| 127 |
prompt,
|
| 128 |
imgURL: imgURL,
|
|
@@ -130,7 +131,6 @@
|
|
| 130 |
date: new Date().getTime(),
|
| 131 |
id: nanoid()
|
| 132 |
};
|
| 133 |
-
const key = getKey(promptImg);
|
| 134 |
$promptImgStorage.set(key, promptImg);
|
| 135 |
console.log(imgURL);
|
| 136 |
$loadingState = data.success ? 'Complete' : 'Error';
|
|
|
|
| 31 |
};
|
| 32 |
myPresence.update(initialPresence);
|
| 33 |
|
| 34 |
+
function getKey(position: { x: number; y: number }): PromptImgKey {
|
| 35 |
return `${position.x}_${position.y}`;
|
| 36 |
}
|
| 37 |
|
|
|
|
| 121 |
if (isNSWF) {
|
| 122 |
throw new Error('NFSW');
|
| 123 |
}
|
| 124 |
+
const key = getKey(position);
|
| 125 |
const imgBlob = await base64ToBlob(imgBase64);
|
| 126 |
+
const imgURL = await uploadImage(imgBlob, prompt, key);
|
| 127 |
const promptImg = {
|
| 128 |
prompt,
|
| 129 |
imgURL: imgURL,
|
|
|
|
| 131 |
date: new Date().getTime(),
|
| 132 |
id: nanoid()
|
| 133 |
};
|
|
|
|
| 134 |
$promptImgStorage.set(key, promptImg);
|
| 135 |
console.log(imgURL);
|
| 136 |
$loadingState = data.success ? 'Complete' : 'Error';
|
frontend/src/lib/PaintCanvas.svelte
CHANGED
|
@@ -90,8 +90,7 @@
|
|
| 90 |
canvasCtx.drawImage(img, position.x, position.y, img.width, img.height);
|
| 91 |
resolve(res);
|
| 92 |
};
|
| 93 |
-
|
| 94 |
-
img.src = `${PUBLIC_UPLOADS}/${url.slice(3).join('/')}`;
|
| 95 |
})
|
| 96 |
)
|
| 97 |
).then((images) => {
|
|
|
|
| 90 |
canvasCtx.drawImage(img, position.x, position.y, img.width, img.height);
|
| 91 |
resolve(res);
|
| 92 |
};
|
| 93 |
+
img.src = `${PUBLIC_UPLOADS}/${imgURL}`;
|
|
|
|
| 94 |
})
|
| 95 |
)
|
| 96 |
).then((images) => {
|
frontend/src/lib/utils.ts
CHANGED
|
@@ -20,30 +20,29 @@ export function base64ToBlob(base64image: string): Promise<Blob> {
|
|
| 20 |
img.src = base64image;
|
| 21 |
});
|
| 22 |
}
|
| 23 |
-
export async function uploadImage(imagBlob: Blob, prompt: string): Promise<string> {
|
| 24 |
// simple regex slugify string for file name
|
| 25 |
const promptSlug = slugify(prompt);
|
| 26 |
-
const UPLOAD_URL = dev ? '
|
| 27 |
|
| 28 |
const hash = crypto.randomUUID().split('-')[0];
|
| 29 |
-
const fileName = `color-palette-${hash}-${promptSlug}.jpeg`;
|
| 30 |
|
| 31 |
const file = new File([imagBlob], fileName, { type: 'image/jpeg' });
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
console.log('uploading image', file);
|
| 34 |
|
| 35 |
const response = await fetch(UPLOAD_URL, {
|
| 36 |
method: 'POST',
|
| 37 |
-
|
| 38 |
-
'Content-Type': file.type,
|
| 39 |
-
'X-Requested-With': 'XMLHttpRequest'
|
| 40 |
-
},
|
| 41 |
-
body: file /// <- File inherits from Blob
|
| 42 |
});
|
| 43 |
-
const
|
| 44 |
|
| 45 |
-
console.log('uploaded images',
|
| 46 |
-
return
|
| 47 |
}
|
| 48 |
const MAX = 512 * 5 - 512
|
| 49 |
|
|
|
|
| 20 |
img.src = base64image;
|
| 21 |
});
|
| 22 |
}
|
| 23 |
+
export async function uploadImage(imagBlob: Blob, prompt: string, key: string): Promise<string> {
|
| 24 |
// simple regex slugify string for file name
|
| 25 |
const promptSlug = slugify(prompt);
|
| 26 |
+
const UPLOAD_URL = dev ? 'server/uploadfile/' : 'uploadfile/';
|
| 27 |
|
| 28 |
const hash = crypto.randomUUID().split('-')[0];
|
| 29 |
+
const fileName = `color-palette-${hash}-${promptSlug}-${key}.jpeg`;
|
| 30 |
|
| 31 |
const file = new File([imagBlob], fileName, { type: 'image/jpeg' });
|
| 32 |
|
| 33 |
+
const formData = new FormData()
|
| 34 |
+
formData.append('file', file)
|
| 35 |
+
|
| 36 |
console.log('uploading image', file);
|
| 37 |
|
| 38 |
const response = await fetch(UPLOAD_URL, {
|
| 39 |
method: 'POST',
|
| 40 |
+
body: formData
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
});
|
| 42 |
+
const res = await response.json();
|
| 43 |
|
| 44 |
+
console.log('uploaded images', res);
|
| 45 |
+
return res.filename;
|
| 46 |
}
|
| 47 |
const MAX = 512 * 5 - 512
|
| 48 |
|
frontend/vite.config.dev.ts
CHANGED
|
@@ -6,11 +6,11 @@ const config: UserConfig = {
|
|
| 6 |
server: {
|
| 7 |
// host: "0.0.0.0",
|
| 8 |
proxy: {
|
| 9 |
-
'/
|
| 10 |
-
target: '
|
| 11 |
changeOrigin: true,
|
| 12 |
cookieDomainRewrite: 'localhost',
|
| 13 |
-
rewrite: (path) => path.replace(/^\/
|
| 14 |
}
|
| 15 |
}
|
| 16 |
}
|
|
|
|
| 6 |
server: {
|
| 7 |
// host: "0.0.0.0",
|
| 8 |
proxy: {
|
| 9 |
+
'/server': {
|
| 10 |
+
target: 'http://localhost:7860',
|
| 11 |
changeOrigin: true,
|
| 12 |
cookieDomainRewrite: 'localhost',
|
| 13 |
+
rewrite: (path) => path.replace(/^\/server/, '')
|
| 14 |
}
|
| 15 |
}
|
| 16 |
}
|