Add save actions and cross-platform file manager support (#848)

* files-download-feature

* feat: add save option to file directory context menus and viewer

* fix: open files in the system file manager
This commit is contained in:
Dave Otero
2026-04-16 23:33:55 +03:00
committed by GitHub
parent 79a9f93ae2
commit fd8972a7d9
7 changed files with 102 additions and 6 deletions
+24 -1
View File
@@ -428,6 +428,12 @@ export const registerFsRoutes = (app, dependencies) => {
};
const mimeType = mimeMap[ext] || 'application/octet-stream';
const download = req.query.download === 'true';
if (download) {
const fileName = path.basename(canonicalPath);
res.setHeader('Content-Disposition', `attachment; filename="${fileName}"`);
}
const content = await fsPromises.readFile(canonicalPath);
res.setHeader('Cache-Control', 'no-store');
return res.type(mimeType).send(content);
@@ -589,7 +595,24 @@ export const registerFsRoutes = (app, dependencies) => {
spawn('open', ['-R', resolved], { windowsHide: true, stdio: 'ignore', detached: true }).unref();
}
} else if (platform === 'win32') {
spawn('explorer', ['/select,', resolved], { windowsHide: true, stdio: 'ignore', detached: true }).unref();
const stat = await fsPromises.stat(resolved);
const escapedPath = resolved.replace(/'/g, "''");
const explorerArg = stat.isDirectory() ? escapedPath : `/select,${escapedPath}`;
const command = `Start-Process -FilePath explorer.exe -ArgumentList '${explorerArg}'`;
await new Promise((resolve, reject) => {
const child = spawn('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', command], {
windowsHide: true,
stdio: 'ignore',
});
child.once('error', reject);
child.once('exit', (code) => {
if (code === 0) {
resolve();
return;
}
reject(new Error(`Explorer launch failed with code ${code ?? 'unknown'}`));
});
});
} else {
const stat = await fsPromises.stat(resolved);
const dir = stat.isDirectory() ? resolved : path.dirname(resolved);
+11
View File
@@ -211,4 +211,15 @@ export const createWebFilesAPI = (): FilesAPI => ({
const result = await response.json().catch(() => ({}));
return { success: Boolean((result as { success?: boolean }).success) };
},
async downloadFile(path: string): Promise<void> {
const target = normalizePath(path);
const url = `/api/fs/raw?path=${encodeURIComponent(target)}&download=true`;
const a = document.createElement('a');
a.href = url;
a.download = target.split('/').pop() || 'file';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
},
});