Skip to content
Merged
Show file tree
Hide file tree
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
48 changes: 21 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@types/react-dom": "^18.2.17",
"@types/semver": "^7.5.8",
"@types/shell-quote": "^1.7.5",
"@types/yauzl": "^2.10.3",
"@typescript-eslint/eslint-plugin": "^8.21.0",
"@typescript-eslint/parser": "^8.21.0",
"@vercel/webpack-asset-relocator-loader": "1.7.3",
Expand Down Expand Up @@ -110,13 +111,11 @@
"@rive-app/react-canvas": "^4.12.0",
"@sentry/electron": "^4.24.0",
"@sentry/webpack-plugin": "^2.16.1",
"@types/adm-zip": "^0.5.5",
"@wordpress/compose": "^6.27.0",
"@wordpress/i18n": "^4.50.0",
"@wordpress/icons": "^10.10.0",
"@wp-playground/blueprints": "^0.9.44",
"@wp-playground/wordpress": "^0.9.44",
"adm-zip": "^0.5.14",
"archiver": "^6.0.1",
"atomically": "^2.0.3",
"compressible": "2.0.18",
Expand All @@ -140,7 +139,8 @@
"unzipper": "0.10.11",
"url-loader": "^4.1.1",
"wpcom": "^5.4.2",
"yargs": "17.7.2"
"yargs": "17.7.2",
"yauzl": "^3.2.0"
},
"optionalDependencies": {
"appdmg": "^0.6.6"
Expand Down
89 changes: 68 additions & 21 deletions src/lib/import-export/import/handlers/backup-handler-zip.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,88 @@
import { EventEmitter } from 'events';
import AdmZip from 'adm-zip';
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';
import fse from 'fs-extra';
import yauzl from 'yauzl';
import { ImportEvents } from '../events';
import { BackupArchiveInfo, BackupExtractProgressEventData } from '../types';
import { BackupHandler, isFileAllowed } from './backup-handler-factory';

const openZip = promisify< string, yauzl.Options, yauzl.ZipFile >( yauzl.open );

export class BackupHandlerZip extends EventEmitter implements BackupHandler {
async listFiles( backup: BackupArchiveInfo ): Promise< string[] > {
const zip = new AdmZip( backup.path );
return zip
.getEntries()
.map( ( entry ) => {
if ( entry.entryName.startsWith( '/' ) ) {
return entry.entryName.slice( 1 );
const zipFile = await openZip( backup.path, { lazyEntries: true } );
const fileNames: string[] = [];

return new Promise( ( resolve, reject ) => {
zipFile.on( 'entry', ( entry ) => {
if ( isFileAllowed( entry.fileName ) ) {
fileNames.push( entry.fileName );
}
return entry.entryName;
} )
.filter( isFileAllowed );
zipFile.readEntry();
} );

zipFile.on( 'end', () => {
resolve( fileNames );
} );

zipFile.on( 'error', reject );
zipFile.readEntry();
} );
}

async extractFiles( file: BackupArchiveInfo, extractionDirectory: string ): Promise< void > {
const zipFile = await openZip( file.path, { lazyEntries: true } );
Comment thread
ashfame marked this conversation as resolved.
const openReadStream = promisify( zipFile.openReadStream.bind( zipFile ) );
const totalSize = fs.statSync( file.path ).size;
let processedSize = 0;

this.emit( ImportEvents.BACKUP_EXTRACT_START );

return new Promise( ( resolve, reject ) => {
this.emit( ImportEvents.BACKUP_EXTRACT_PROGRESS, {
progress: 0,
} as BackupExtractProgressEventData );
const zip = new AdmZip( file.path );
zip.extractAllToAsync( extractionDirectory, true, undefined, ( error?: Error ) => {
if ( error ) {
this.emit( ImportEvents.BACKUP_EXTRACT_ERROR, { error } );
reject( error );
zipFile.on( 'entry', async ( entry ) => {
if ( ! isFileAllowed( entry.fileName ) ) {
zipFile.readEntry();
return;
}

const fullPath = path.join( extractionDirectory, entry.fileName );
await fse.ensureDir( path.dirname( fullPath ) );

if ( entry.fileName.endsWith( '/' ) ) {
zipFile.readEntry();
return;
}
this.emit( ImportEvents.BACKUP_EXTRACT_PROGRESS, {
progress: 1,
} as BackupExtractProgressEventData );

const readStream = await openReadStream( entry );
const writeStream = fs.createWriteStream( fullPath );

readStream.on( 'data', ( chunk ) => {
processedSize += chunk.length;
this.emit( ImportEvents.BACKUP_EXTRACT_PROGRESS, {
progress: processedSize / totalSize,
} as BackupExtractProgressEventData );
} );

writeStream.on( 'finish', () => {
zipFile.readEntry();
} );

readStream.pipe( writeStream );
} );

zipFile.on( 'end', () => {
this.emit( ImportEvents.BACKUP_EXTRACT_COMPLETE );
resolve();
} );

zipFile.on( 'error', ( error ) => {
this.emit( ImportEvents.BACKUP_EXTRACT_ERROR, { error } );
reject( error );
} );

zipFile.readEntry();
} );
}
}
Loading