Skip to content

Commit

Permalink
Move export logic to PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiomorales committed Jan 20, 2023
1 parent deea9ee commit f3fad90
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 42 deletions.
8 changes: 8 additions & 0 deletions src/php-wasm-browser/worker-thread/window-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ export class SpawnedWorkerThread {
return await this.#rpc('readFile', { path });
}

/**
* @param path
* @see {PHP.readFile}
*/
async readFileAsBuffer(path: string): Promise<string> {
return await this.#rpc('readFileAsBuffer', { path });
}

/**
* @param path
* @param contents
Expand Down
2 changes: 2 additions & 0 deletions src/php-wasm-browser/worker-thread/worker-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export async function initializeWorkerThread(
return scope;
} else if (message.type === 'readFile') {
return phpBrowser.server.php.readFileAsText(message.path);
} else if (message.type === 'readFileAsBuffer') {
return phpBrowser.server.php.readFileAsBuffer(message.path);
} else if (message.type === 'listFiles') {
return phpBrowser.server.php.listFiles(message.path);
} else if (message.type === 'unlink') {
Expand Down
83 changes: 41 additions & 42 deletions src/wordpress-playground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -309,51 +309,50 @@ async function overwriteFile() {
await workerThread.writeFile(targetFile, targetContent);
}

async function populateZip(zipFile, filePaths) {
for (let i = 0; i < filePaths.length; i++) {
const path = filePaths[i];
const isDir = await workerThread.isDir(path);
if (isDir) {
const directoryFileNames = await workerThread.listFiles(path);
const directoryFilePaths = directoryFileNames.map(
(childFilePath) => `${path}/${childFilePath}`
);
await populateZip(zipFile, directoryFilePaths);
} else {
const fileContents = await workerThread.readFile(path);
zipFile.file(path, fileContents);
}
}
}

async function generateZip() {
const fileNames = await workerThread.listFiles('/wordpress');
const filePaths = fileNames.map((fileName) => `/wordpress/${fileName}`);
const zipFile = new JSZip();
await populateZip(zipFile, filePaths);
zipFile
.generateAsync({ type: 'blob' }, function updateCallback(metadata) {
let msg = 'progression : ' + metadata.percent.toFixed(2) + ' %';
if (metadata.currentFile) {
msg += ', current file = ' + metadata.currentFile;
}
console.log(msg);
// updatePercent(metadata.percent | 0);
})
.then(
function callback(blob) {
console.log('first part');
console.log(blob);
// see FileSaver.js
saveAs(blob, 'wordpress-playground-export.zip');
await workerThread.run(`
<?php
$zip = new ZipArchive;
$res = $zip->open('/wordpress-playground-export.zip', ZipArchive::CREATE);
if ($res === TRUE) {
$directories = array();
$directories[] = '/wordpress/';
// showMessage('done !');
},
function (e) {
console.log('second part');
console.log(e);
while(sizeof($directories)) {
$dir = array_pop($directories);
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
if ($entry == '.' || $entry == '..') {
continue;
}
$entry = $dir . $entry;
if (is_dir($entry)) {
$directory_path = $entry . '/';
array_push($directories, $directory_path);
} elseif (is_file($entry)) {
$zip->addFile($entry);
}
}
closedir($handle);
}
}
$zip->close();
}
);
?>
`);
const fileBuffer = await workerThread.readFileAsBuffer(
'/wordpress-playground-export.zip'
);
const file = new File([fileBuffer], 'wordpress-playground-export.zip');
saveAs(file);
}

async function importFile() {
Expand Down

0 comments on commit f3fad90

Please sign in to comment.