2023-11-06 17:14:21 +00:00
|
|
|
import type { SmartArchive } from './classes.smartarchive.js';
|
|
|
|
import * as plugins from './plugins.js';
|
|
|
|
|
|
|
|
export interface IAnalyzedResult {
|
|
|
|
fileType: plugins.fileType.FileTypeResult;
|
|
|
|
isArchive: boolean;
|
|
|
|
resultStream: plugins.smartstream.PassThrough;
|
|
|
|
decompressionStream: plugins.stream.Transform | plugins.stream.Duplex | plugins.tarStream.Extract;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ArchiveAnalyzer {
|
|
|
|
smartArchiveRef: SmartArchive;
|
|
|
|
|
|
|
|
constructor(smartArchiveRefArg: SmartArchive) {
|
|
|
|
this.smartArchiveRef = smartArchiveRefArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async mimeTypeIsArchive(mimeType: string): Promise<boolean> {
|
|
|
|
const archiveMimeTypes: Set<string> = new Set([
|
|
|
|
'application/zip',
|
|
|
|
'application/x-rar-compressed',
|
|
|
|
'application/x-tar',
|
|
|
|
'application/gzip',
|
|
|
|
'application/x-7z-compressed',
|
|
|
|
'application/x-bzip2',
|
|
|
|
// Add other archive mime types here
|
|
|
|
]);
|
|
|
|
|
|
|
|
return archiveMimeTypes.has(mimeType);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private getDecompressionStream(
|
|
|
|
mimeTypeArg: plugins.fileType.FileTypeResult['mime']
|
|
|
|
): plugins.stream.Transform | plugins.stream.Duplex | plugins.tarStream.Extract {
|
|
|
|
switch (mimeTypeArg) {
|
|
|
|
case 'application/gzip':
|
|
|
|
return this.smartArchiveRef.gzipTools.getDecompressionStream();
|
|
|
|
case 'application/x-bzip2':
|
|
|
|
return this.smartArchiveRef.bzip2Tools.getDecompressionStream(); // replace with your own bzip2 decompression stream
|
|
|
|
case 'application/x-tar':
|
|
|
|
return this.smartArchiveRef.tarTools.getDecompressionStream(); // replace with your own tar decompression stream
|
|
|
|
default:
|
|
|
|
// Handle unsupported formats or no decompression needed
|
|
|
|
return new plugins.smartstream.PassThrough();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public getAnalyzedStream() {
|
|
|
|
let firstRun = true;
|
|
|
|
const resultStream = new plugins.smartstream.PassThrough();
|
|
|
|
const analyzerstream = new plugins.smartstream.SmartDuplex<Buffer, IAnalyzedResult>({
|
|
|
|
readableObjectMode: true,
|
2023-11-08 16:01:48 +00:00
|
|
|
writeFunction: async (chunkArg: Buffer, streamtools) => {
|
2023-11-06 17:14:21 +00:00
|
|
|
const fileType = await plugins.fileType.fileTypeFromBuffer(chunkArg);
|
2023-11-07 03:19:54 +00:00
|
|
|
const decompressionStream = this.getDecompressionStream(fileType?.mime as any);
|
2023-11-06 17:14:21 +00:00
|
|
|
resultStream.push(chunkArg);
|
|
|
|
if (firstRun) {
|
|
|
|
firstRun = false;
|
|
|
|
const result: IAnalyzedResult = {
|
|
|
|
fileType,
|
2023-11-07 03:19:54 +00:00
|
|
|
isArchive: await this.mimeTypeIsArchive(fileType?.mime),
|
2023-11-06 17:14:21 +00:00
|
|
|
resultStream,
|
|
|
|
decompressionStream,
|
|
|
|
};
|
|
|
|
streamtools.push(result);
|
|
|
|
streamtools.push(null);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
2023-11-07 03:19:54 +00:00
|
|
|
finalFunction: async (tools) => {
|
|
|
|
resultStream.push(null);
|
|
|
|
return null;
|
|
|
|
}
|
2023-11-06 17:14:21 +00:00
|
|
|
});
|
|
|
|
return analyzerstream;
|
|
|
|
}
|
|
|
|
}
|