Files
Strata/app/host/server/artifacts.mjs
T
Niko1221 4058aa782b Layout move for 0.2.16: the top level becomes app/, engine/, terminal/, docs/, tools/ and .github/
Made by tools/layout/move_0216.py (the commit before this one) on db8e0be9c, the final code head of 0.2.16:
- 75 git mv (renames, so `git log --follow` and blame keep working; packaging/acceptance_matrix.md shows as delete + add because its links
  changed too) and 3 removals: SETUP.bat, UPDATE.bat and update.sh (setup.py --update pulls the code now);
- 1,425 path edits in 307 files: root-relative paths in CI, Docker, .gitignore and the release scripts; Markdown links; Python and
  Node joins from a root that moved with its folder; explicit edits for code that computes a root or names a moved path; tables of
  repository paths inside tests.

engine/ holds what CMake builds (src, include, cmake, metal, sycl, spark, third_party, the C++ tests and benches). app/ is the desktop
app and its host (was apps/). terminal/ is the Terminal in the layout of the installed one (strata.py, setup.py, strata_cli,
strata_control, serve, bin, data, ref, crates, the Python tools and tests). docs/ has the documentation, docs/lang/ the translated
READMEs, docs/history and docs/bench-results. tools/ has the repository's own checks, packaging, docker, research and nix.
tests/core/test_tqp_ref.py is a pytest file and stays with the Python tests (terminal/tests/core/).
AGENTS.md, README.md, LICENSE, setup.sh and START-HERE.bat stay at the root.

No behaviour change: the same command lines, the same sources. For this head a replay of the script gave the same numbers and the path
scan (tools/layout/checks/path_scan.py) reports no hit; the suites and the builds run on this commit next.
2026-11-17 13:19:21 +01:00

44 lines
3.4 KiB
JavaScript

import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
import { mkdir, mkdtemp, realpath, stat, writeFile } from 'node:fs/promises';
import { basename, dirname, extname, join, resolve, sep } from 'node:path';
import { externalEnvironment } from './external-env.mjs';
const run=(command,args)=>promisify(execFile)(command,args,{env:externalEnvironment()}); // xdg-open and the apps it starts: the environment the AppImage was started from
const 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']);
/** A request this opener will not carry out (a scheme other than http(s), a file outside the previews): a 400 with its reason, not a server error. */
const refused=message=>Object.assign(new Error(message),{status:400});
export async function openArtifact(body,{examplesDir,previewsDir,platform=process.platform,launch=run}) {
if(!['open','reveal'].includes(body.action))throw refused('Choose Open or Show in file explorer.');
if(typeof body.url==='string') {
// #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.
if(body.action!=='open')throw refused('A web address can only be opened.');
let address;try{address=new URL(body.url);}catch{throw refused('This is not a web address.');}
if(!['http:','https:'].includes(address.protocol)||body.url.length>4096)throw refused('Only http and https addresses can be opened.');
if(platform==='darwin')await launch('open',[address.href]);
else if(platform==='win32')await launch('rundll32.exe',['url.dll,FileProtocolHandler',address.href]);
else await launch('xdg-open',[address.href]);
return {ok:true};
}
let file;
if(typeof body.path==='string') {
// Only public example assets can be addressed by path. Never accept host paths.
if(!body.path.startsWith('/examples/'))throw refused('This file is not a local preview.');
const base=await realpath(examplesDir);
file=await realpath(resolve(base,body.path.slice('/examples/'.length)));
if(!file.startsWith(base+sep)||!(await stat(file)).isFile())throw refused('This file is not a local preview.');
} else {
const name=basename(String(body.name??'document.txt')).replace(/[^\w. -]/g,'_').slice(0,120);
if(!extensions.has(extname(name).toLowerCase()))throw refused('This file type cannot be opened from a preview.');
if(typeof body.content!=='string'&&typeof body.base64!=='string')throw refused('No file content was supplied.');
const bytes=typeof body.base64==='string'?Buffer.from(body.base64,'base64'):Buffer.from(body.content,'utf8');
if(bytes.length>20*1024*1024)throw refused('Choose a file smaller than 20 MB.');
await mkdir(previewsDir,{recursive:true});const folder=await mkdtemp(join(previewsDir,'preview-'));
file=join(folder,name);await writeFile(file,bytes,{flag:'wx',mode:0o600});
}
if(!extensions.has(extname(file).toLowerCase()))throw refused('This file type cannot be opened from a preview.');
if(platform==='darwin')await launch('open',body.action==='reveal'?['-R',file]:[file]);
else if(platform==='win32')await launch('explorer.exe',body.action==='reveal'?[`/select,${file}`]:[file]);
else await launch('xdg-open',[body.action==='reveal'?dirname(file):file]);
return {ok:true};
}