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

Support compressed files that contain a SQL file in importer #363

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we exclude /^\.DS_Store$/ too ? Or is that already covered by /^\..*/, // Unix hidden files at root?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. As you pointed out, we don't need to explicitly exclude .DS_Store as it will be covered by that pattern. In fact, the compressed files I added in backup-files.zip contain that hidden folder and are not being processed.

/^\..*/, // 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
Loading