char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
92d1bb0c9
char/aliceai-t5-moe
1import { FileExtensionImage , MimeTypeImage } from '$lib/enums' ; 2 3/** 4* Convert a WebP base64 data URL to a PNG data URL 5* @param base64UrlWebp - The WebP base64 data URL to convert 6* @param backgroundColor - Background color for the PNG (default: 'white') 7* @returns Promise resolving to PNG data URL 8*/ 9export function webpBase64UrlToPngDataURL ( 10base64UrlWebp :string , 11backgroundColor :string = 'white' 12) :Promise < string > { 13return new Promise (( resolve , reject ) => { 14try { 15const img = new Image (); 16 17img . onload = () => { 18const canvas = document . createElement ( 'canvas' ); 19const ctx = canvas . getContext ( '2d' ); 20 21if ( ! ctx ) { 22reject ( new Error ( 'Failed to get 2D canvas context.' )); 23 24return ; 25} 26 27const targetWidth = img . naturalWidth || 300 ; 28const targetHeight = img . naturalHeight || 300 ; 29 30canvas . width = targetWidth ; 31canvas . height = targetHeight ; 32 33if ( backgroundColor ) { 34ctx . fillStyle = backgroundColor ; 35ctx . fillRect ( 0 , 0 , canvas . width , canvas . height ); 36} 37 38ctx . drawImage ( img , 0 , 0 , targetWidth , targetHeight ); 39 40resolve ( canvas . toDataURL ( MimeTypeImage . PNG )); 41}; 42 43img . onerror = () => { 44reject ( new Error ( 'Failed to load WebP image. Ensure the WebP data is valid.' )); 45}; 46 47img . src = base64UrlWebp ; 48} catch ( error ) { 49const message = error instanceof Error ?error . message :String ( error ); 50const errorMessage = `Error converting WebP to PNG: ${ message } ` ; 51 52console . error ( errorMessage , error ); 53reject ( new Error ( errorMessage )); 54} 55}); 56} 57 58/** 59* Check if a file is a WebP based on its MIME type 60* @param file - The file to check 61* @returns True if the file is a WebP 62*/ 63export function isWebpFile ( file :File ) :boolean { 64return ( 65file . type === MimeTypeImage . WEBP || file . name . toLowerCase (). endsWith ( FileExtensionImage . WEBP ) 66); 67} 68 69/** 70* Check if a MIME type represents a WebP 71* @param mimeType - The MIME type to check 72* @returns True if the MIME type is image/webp 73*/ 74export function isWebpMimeType ( mimeType :string ) :boolean { 75return mimeType === MimeTypeImage . WEBP ; 76}