Skip to content

Commit

Permalink
Filter out exclude files when processing a backup file
Browse files Browse the repository at this point in the history
  • Loading branch information
fluiddot committed Jul 16, 2024
1 parent 2c06a8c commit caa31a0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 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

0 comments on commit caa31a0

Please sign in to comment.