okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1import { execFile } from 'node:child_process' ; 2import { promisify } from 'node:util' ; 3import { mkdir , mkdtemp , realpath , stat , writeFile } from 'node:fs/promises' ; 4import { basename , dirname , extname , join , resolve , sep } from 'node:path' ; 5import { externalEnvironment } from './external-env.mjs' ; 6const run = ( command , args ) => promisify ( execFile )( command , args ,{ env :externalEnvironment ()}); // xdg-open and the apps it starts: the environment the AppImage was started from 7const extensions = new Set ([ '.md' , '.txt' , '.pdf' , '.html' , '.htm' , '.png' , '.jpg' , '.jpeg' , '.webp' , '.gif' , '.svg' , '.csv' , '.ts' , '.tsx' , '.js' , '.jsx' , '.py' , '.json' , '.css' , '.rs' , '.go' , '.sh' , '.mp4' , '.webm' ]); 8export async function openArtifact ( body ,{ examplesDir, previewsDir, platform= process . platform , launch= run }) { 9if ( ! [ 'open' , 'reveal' ]. includes ( body . action )) throw new Error ( 'Choose Open or Show in file explorer.' ); 10if ( typeof body . url === 'string' ) { 11// #4721: a web address goes to the person's default browser (the app window drops window.open: WebView2 has no new-window handler). http and https only, no shell. 12if ( body . action !== 'open' ) throw new Error ( 'A web address can only be opened.' ); 13let address ; try { address = new URL ( body . url );} catch { throw new Error ( 'This is not a web address.' );} 14if ( ! [ 'http:' , 'https:' ]. includes ( address . protocol ) || body . url . length > 4096 ) throw new Error ( 'Only http and https addresses can be opened.' ); 15if ( platform === 'darwin' ) await launch ( 'open' ,[ address . href ]); 16else if ( platform === 'win32' ) await launch ( 'rundll32.exe' ,[ 'url.dll,FileProtocolHandler' , address . href ]); 17else await launch ( 'xdg-open' ,[ address . href ]); 18return { ok :true }; 19} 20let file ; 21if ( typeof body . path === 'string' ) { 22// Only public example assets can be addressed by path. Never accept host paths. 23if ( ! body . path . startsWith ( '/examples/' )) throw new Error ( 'This file is not a local preview.' ); 24const base = await realpath ( examplesDir ); 25file = await realpath ( resolve ( base , body . path . slice ( '/examples/' . length ))); 26if ( ! file . startsWith ( base + sep ) ||! ( await stat ( file )). isFile ()) throw new Error ( 'This file is not a local preview.' ); 27} else { 28const name = basename ( String ( body . name ?? 'document.txt' )). replace ( / [ ^ \w . - ] / g , '_' ). slice ( 0 , 120 ); 29if ( ! extensions . has ( extname ( name ). toLowerCase ())) throw new Error ( 'This file type cannot be opened from a preview.' ); 30if ( typeof body . content !== 'string' && typeof body . base64 !== 'string' ) throw new Error ( 'No file content was supplied.' ); 31const bytes = typeof body . base64 === 'string' ?Buffer . from ( body . base64 , 'base64' ) :Buffer . from ( body . content , 'utf8' ); 32if ( bytes . length > 20 * 1024 * 1024 ) throw new Error ( 'Choose a file smaller than 20 MB.' ); 33await mkdir ( previewsDir ,{ recursive :true }); const folder = await mkdtemp ( join ( previewsDir , 'preview-' )); 34file = join ( folder , name ); await writeFile ( file , bytes ,{ flag :'wx' , mode :0o600 }); 35} 36if ( ! extensions . has ( extname ( file ). toLowerCase ())) throw new Error ( 'This file type cannot be opened from a preview.' ); 37if ( platform === 'darwin' ) await launch ( 'open' , body . action === 'reveal' ?[ '-R' , file ] :[ file ]); 38else if ( platform === 'win32' ) await launch ( 'explorer.exe' , body . action === 'reveal' ?[ `/select, ${ file } ` ] :[ file ]); 39else await launch ( 'xdg-open' ,[ body . action === 'reveal' ?dirname ( file ) :file ]); 40return { ok :true }; 41}