From 21d299d1be75d235cdfe0d8011c56982fb5f7bf8 Mon Sep 17 00:00:00 2001 From: Artemio Morales Date: Fri, 20 Jan 2023 15:22:44 -0500 Subject: [PATCH] Remove reference to JSZip; use ZipArchive via worker thread instead --- package.json | 1 - src/wordpress-playground/index.tsx | 106 +++++++++++++++-------------- 2 files changed, 56 insertions(+), 51 deletions(-) diff --git a/package.json b/package.json index d0ef6101e1..ef0250918f 100644 --- a/package.json +++ b/package.json @@ -103,7 +103,6 @@ "is-reference": "^3.0.0", "jest": "^29.3.1", "jest-esbuild": "^0.2.9", - "jszip": "^3.10.1", "live-server": "^1.2.2", "magic-string": "^0.26.7", "npm-run-all": "^4.1.5", diff --git a/src/wordpress-playground/index.tsx b/src/wordpress-playground/index.tsx index 4f263d8f1e..ee02bd3cda 100644 --- a/src/wordpress-playground/index.tsx +++ b/src/wordpress-playground/index.tsx @@ -6,7 +6,6 @@ import { } from '../php-wasm-browser/index'; import { ProgressObserver, ProgressType } from './progress-observer'; import { PromiseQueue } from './promise-queue'; -import JSZip from 'jszip'; import { saveAs } from 'file-saver'; const query = new URL(document.location.href).searchParams as any; @@ -310,44 +309,46 @@ async function overwriteFile() { } async function generateZip() { - await workerThread.run(` - open('/wordpress-playground-export.zip', ZipArchive::CREATE); - if ($res === TRUE) { - $directories = array(); - $directories[] = '/wordpress/'; - - while(sizeof($directories)) { - $dir = array_pop($directories); - - if ($handle = opendir($dir)) { - - while (false !== ($entry = readdir($handle))) { - - if ($entry == '.' || $entry == '..') { - continue; - } + await workerThread.run({ + code: ` + open('/wordpress-playground-export.zip', ZipArchive::CREATE); + if ($res === TRUE) { + $directories = array(); + $directories[] = '/wordpress/'; + + while(sizeof($directories)) { + $dir = array_pop($directories); + + if ($handle = opendir($dir)) { + + while (false !== ($entry = readdir($handle))) { + + if ($entry == '.' || $entry == '..') { + continue; + } - $entry = $dir . $entry; + $entry = $dir . $entry; - if (is_dir($entry)) { + if (is_dir($entry)) { - $directory_path = $entry . '/'; - array_push($directories, $directory_path); + $directory_path = $entry . '/'; + array_push($directories, $directory_path); - } elseif (is_file($entry)) { + } elseif (is_file($entry)) { - $zip->addFile($entry); + $zip->addFile($entry); + } } + closedir($handle); } - closedir($handle); } + $zip->close(); } - $zip->close(); - } - ?> - `); + ?> + `, + }); const fileBuffer = await workerThread.readFileAsBuffer( '/wordpress-playground-export.zip' ); @@ -357,27 +358,32 @@ async function generateZip() { async function importFile() { const selectedFile = document.getElementById('file-input').files[0]; - - JSZip.loadAsync(selectedFile) // 1) read the Blob - .then( - function (zip) { - zip.forEach(async function (relativePath, zipEntry: any) { - // 2) print entries - if ( - !zipEntry.dir && - !relativePath.includes('wp-content/database') && - !relativePath.includes('wp-includes') - ) { - console.log(relativePath); - const content = new TextDecoder().decode( - zipEntry._data.compressedContent - ); - await workerThread.writeFile(relativePath, content); + const fileContents = await selectedFile.arrayBuffer(); + await workerThread.writeFile('/import.zip', new Uint8Array(fileContents)); + await workerThread.run({ + code: ` + open('/import.zip'); + if ($res === TRUE) { + $counter = 0; + while ($zip->statIndex($counter)) { + $file = $zip->statIndex($counter); + $fileString .= $file['name'] . ','; + if ( + strpos($file['name'], 'wp-content/database') === false && + strpos($file['name'], 'wp-includes') === false + ) { + $overwrite = fopen($file['name'], 'w'); + fwrite($overwrite, $zip->getFromIndex($counter)); + } + $counter++; } - }); - }, - function (e) {} - ); + $zip->close(); + } + ?> + `, + }); } const overwriteButton = document.getElementById('overwrite-button');