Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a cross-device mv error by using a tmp directory inside document root #886

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions packages/playground/blueprints/src/lib/steps/install-asset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { UniversalPHP } from '@php-wasm/universal';
import { joinPaths } from '@php-wasm/util';
import { writeFile } from './write-file';
import { unzip } from './unzip';

Expand Down Expand Up @@ -29,30 +30,26 @@ export async function installAsset(
assetFolderName: string;
}> {
// Extract to temporary folder so we can find asset folder name

const zipFileName = zipFile.name;
const assetNameGuess = zipFileName.replace(/\.zip$/, '');

const tmpUnzippedFilesPath = `/tmp/assets/${assetNameGuess}`;
const tmpZipPath = `/tmp/${zipFileName}`;
const wpContent = joinPaths(await playground.documentRoot, 'wp-content');
const tmpDir = joinPaths(wpContent, crypto.randomUUID());
const tmpZipPath = joinPaths(tmpDir, zipFileName);
const tmpUnzippedFilesPath = joinPaths(tmpDir, 'assets', assetNameGuess);

const removeTmpFolder = () =>
playground.rmdir(tmpUnzippedFilesPath, {
if (await playground.fileExists(tmpUnzippedFilesPath)) {
await playground.rmdir(tmpDir, {
recursive: true,
});

if (await playground.fileExists(tmpUnzippedFilesPath)) {
await removeTmpFolder();
}
await playground.mkdir(tmpDir);

await writeFile(playground, {
path: tmpZipPath,
data: zipFile,
});

const cleanup = () =>
Promise.all([removeTmpFolder, () => playground.unlink(tmpZipPath)]);

try {
await unzip(playground, {
zipPath: tmpZipPath,
Expand Down Expand Up @@ -84,14 +81,14 @@ export async function installAsset(
// Move asset folder to target path
const assetFolderPath = `${targetPath}/${assetFolderName}`;
await playground.mv(tmpAssetPath, assetFolderPath);
await cleanup();

return {
assetFolderPath,
assetFolderName,
};
} catch (error) {
await cleanup();
throw error;
} finally {
await playground.rmdir(tmpDir, {
recursive: true,
});
}
}
Loading