Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions apps/local/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export async function startLocalServer( options: LocalServerOptions ): Promise<

const cliRunner = createCliRunner( { cliBinary, nodeBinary } );
const execute = cliRunner.executeCliCommand;
const uploads = new Map< string, { path: string; directory: string } >();

// --- Server-Sent Events: one stream carries live UI updates ----------------
const sseClients = new Set< Response >();
Expand Down Expand Up @@ -823,6 +824,7 @@ export async function startLocalServer( options: LocalServerOptions ): Promise<
rm( dir, { recursive: true, force: true }, () => undefined );
throw error;
}
uploads.set( dest, { path: dest, directory: dir } );
res.json( { path: dest } );
} )
);
Expand Down Expand Up @@ -850,8 +852,8 @@ export async function startLocalServer( options: LocalServerOptions ): Promise<
api.post(
'/sites/:id/import',
asyncHandler( async ( req: Request, res: Response ) => {
const archivePath = req.body?.path;
if ( typeof archivePath !== 'string' || ! archivePath ) {
const requestedPath = req.body?.path;
if ( typeof requestedPath !== 'string' ) {
res.status( 400 ).json( { error: 'Missing backup path' } );
return;
}
Expand All @@ -860,10 +862,16 @@ export async function startLocalServer( options: LocalServerOptions ): Promise<
res.status( 404 ).json( { error: `Site ${ req.params.id } not found` } );
return;
}
const upload = uploads.get( requestedPath );
if ( ! upload ) {
res.status( 400 ).json( { error: 'Unknown backup path' } );
return;
}
uploads.delete( requestedPath );
try {
await new Promise< void >( ( resolve, reject ) => {
const [ emitter ] = execute(
[ 'import', '--path', site.path, archivePath, '--start-server' ],
[ 'import', '--path', site.path, upload.path, '--start-server' ],
{ output: 'capture' }
);
emitter.on( 'data', ( { data } ) => {
Expand All @@ -880,16 +888,7 @@ export async function startLocalServer( options: LocalServerOptions ): Promise<
emitter.on( 'error', ( { error } ) => reject( error ) );
} );
} finally {
// Drop the uploaded temp copy — but only a temp dir we created: a
// direct child of the OS temp dir. Resolve first so `..` in the
// supplied path can't escape the temp root and delete elsewhere.
const uploadDir = path.dirname( path.resolve( archivePath ) );
if (
path.dirname( uploadDir ) === path.resolve( os.tmpdir() ) &&
path.basename( uploadDir ).startsWith( 'studio-upload-' )
) {
rm( uploadDir, { recursive: true, force: true }, () => undefined );
}
rm( upload.directory, { recursive: true, force: true }, () => undefined );
}
res.status( 204 ).end();
} )
Expand Down
Loading