Spaces:
Runtime error
Runtime error
File size: 1,699 Bytes
142f91b a6e7e8f 142f91b 70b8e47 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import { dev } from '$app/environment';
export function randomSeed() {
return BigInt(13248873089935215612 & (((1 << 63) - 1) * Math.random()));
}
export function base64ToBlob(base64image: string): Promise<Blob> {
return new Promise((resolve) => {
const img = new Image();
img.onload = async () => {
const w = img.width;
const h = img.height;
const canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
ctx.drawImage(img, 0, 0, w, h);
const imgBlob: Blob = await new Promise((_resolve) =>
canvas.toBlob(_resolve, 'image/jpeg', 0.95)
);
resolve(imgBlob);
};
img.src = base64image;
});
}
export async function uploadImage(imagBlob: Blob, prompt: string): string {
// simple regex slugify string for file name
const promptSlug = slugify(prompt);
const UPLOAD_URL = dev ? 'moon/uploads' : 'https://huggingface.co/uploads';
const hash = crypto.randomUUID().split('-')[0];
const fileName = `color-palette-${hash}-${promptSlug}.jpeg`;
const file = new File([imagBlob], fileName, { type: 'image/jpeg' });
console.log('uploading image', file);
const response = await fetch(UPLOAD_URL, {
method: 'POST',
headers: {
'Content-Type': file.type,
'X-Requested-With': 'XMLHttpRequest'
},
body: file /// <- File inherits from Blob
});
const url = await response.text();
console.log('uploaded images', url);
return url;
}
function slugify(text: string) {
if (!text) return '';
return text
.toString()
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-')
.replace(/^-+/, '')
.replace(/-+$/, '');
}
|