Skip to content

Commit

Permalink
Support compressed files that contain a SQL file in importer (#363)
Browse files Browse the repository at this point in the history
* Filter out exclude files when processing a backup file

* Update unit tests of `listFiles` function
  • Loading branch information
fluiddot authored Jul 18, 2024
1 parent 49312d6 commit 171ee77
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/lib/import-export/import/handlers/backup-handler-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ export interface BackupHandler {
extractFiles( file: BackupArchiveInfo, extractionDirectory: string ): Promise< void >;
}

const EXCLUDED_FILES_PATTERNS = [
/^__MACOSX\/.*/, // MacOS meta folder
/^\..*/, // Unix hidden files at root
/\/\..*/, // Unix hidden files at subfolders
];

export function isFileAllowed( filePath: string ) {
return EXCLUDED_FILES_PATTERNS.every( ( pattern ) => ! pattern.test( filePath ) );
}

export class BackupHandlerFactory {
private static zipTypes = [
'application/zip',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import fs from 'fs';
import zlib from 'zlib';
import * as tar from 'tar';
import { BackupArchiveInfo } from '../types';
import { BackupHandler } from './backup-handler-factory';
import { BackupHandler, isFileAllowed } from './backup-handler-factory';

export class BackupHandlerTarGz implements BackupHandler {
async listFiles( backup: BackupArchiveInfo ): Promise< string[] > {
const files: string[] = [];
await tar.t( {
file: backup.path,
onReadEntry: ( entry ) => files.push( entry.path ),
onReadEntry: ( entry ) => isFileAllowed( entry.path ) && files.push( entry.path ),
} );
return files;
}
Expand Down
7 changes: 5 additions & 2 deletions src/lib/import-export/import/handlers/backup-handler-zip.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import AdmZip from 'adm-zip';
import { BackupArchiveInfo } from '../types';
import { BackupHandler } from './backup-handler-factory';
import { BackupHandler, isFileAllowed } from './backup-handler-factory';

export class BackupHandlerZip implements BackupHandler {
async listFiles( backup: BackupArchiveInfo ): Promise< string[] > {
const zip = new AdmZip( backup.path );
return zip.getEntries().map( ( entry ) => entry.entryName );
return zip
.getEntries()
.map( ( entry ) => entry.entryName )
.filter( isFileAllowed );
}

async extractFiles( file: BackupArchiveInfo, extractionDirectory: string ): Promise< void > {
Expand Down
13 changes: 11 additions & 2 deletions src/lib/import-export/tests/import/handlers/backup-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ describe( 'BackupHandlerFactory', () => {

describe( 'listFiles', () => {
const archiveFiles = [
'index.php',
'.hidden-file',
'wp-content/.hidden-file',
'wp-content/plugins/hello.php',
'wp-content/themes/twentytwentyfour/theme.json',
'wp-content/uploads/2024/07/image.png',
'__MACOSX/meta-file',
];
const expectedArchiveFiles = [
'index.php',
'wp-content/plugins/hello.php',
'wp-content/themes/twentytwentyfour/theme.json',
Expand All @@ -83,7 +92,7 @@ describe( 'BackupHandlerFactory', () => {
archiveFiles.forEach( ( path ) => onReadEntry?.( { path } as tar.ReadEntry ) );
} );

await expect( handler.listFiles( archiveInfo ) ).resolves.toEqual( archiveFiles );
await expect( handler.listFiles( archiveInfo ) ).resolves.toEqual( expectedArchiveFiles );
} );

it( 'should list files from a zip archive', async () => {
Expand All @@ -99,7 +108,7 @@ describe( 'BackupHandlerFactory', () => {
.mockReturnValue( archiveFiles.map( ( file ) => ( { entryName: file } ) ) ),
} );

await expect( handler.listFiles( archiveInfo ) ).resolves.toEqual( archiveFiles );
await expect( handler.listFiles( archiveInfo ) ).resolves.toEqual( expectedArchiveFiles );
} );

it( 'should list a single SQL file', async () => {
Expand Down

0 comments on commit 171ee77

Please sign in to comment.