smartarchive/ts/classes.archiveanalyzer.ts

83 lines
2.9 KiB
TypeScript
Raw Permalink Normal View History

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;
2023-11-13 22:14:39 +00:00
resultStream: plugins.smartstream.SmartDuplex;
2023-11-06 17:14:21 +00:00
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);
}
2023-11-11 17:28:50 +00:00
private async getDecompressionStream(
2023-11-06 17:14:21 +00:00
mimeTypeArg: plugins.fileType.FileTypeResult['mime']
2023-11-11 17:28:50 +00:00
): Promise<plugins.stream.Transform | plugins.stream.Duplex | plugins.tarStream.Extract> {
2023-11-06 17:14:21 +00:00
switch (mimeTypeArg) {
case 'application/gzip':
return this.smartArchiveRef.gzipTools.getDecompressionStream();
2024-03-16 23:29:42 +00:00
case 'application/zip':
return this.smartArchiveRef.zipTools.getDecompressionStream();
2023-11-06 17:14:21 +00:00
case 'application/x-bzip2':
2023-11-11 17:28:50 +00:00
return await this.smartArchiveRef.bzip2Tools.getDecompressionStream(); // replace with your own bzip2 decompression stream
2023-11-06 17:14:21 +00:00
case 'application/x-tar':
return this.smartArchiveRef.tarTools.getDecompressionStream(); // replace with your own tar decompression stream
default:
// Handle unsupported formats or no decompression needed
2023-11-13 21:11:24 +00:00
return plugins.smartstream.createPassThrough();
2023-11-06 17:14:21 +00:00
}
}
public getAnalyzedStream() {
let firstRun = true;
2023-11-13 21:11:24 +00:00
const resultStream = plugins.smartstream.createPassThrough();
2023-11-06 17:14:21 +00:00
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
if (firstRun) {
firstRun = false;
2023-11-14 09:55:19 +00:00
const fileType = await plugins.fileType.fileTypeFromBuffer(chunkArg);
const decompressionStream = await this.getDecompressionStream(fileType?.mime as any);
2024-03-16 23:29:42 +00:00
/**
* analyzed stream emits once with this object
*/
2023-11-06 17:14:21 +00:00
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,
};
2023-11-13 21:11:24 +00:00
await streamtools.push(result);
2023-11-06 17:14:21 +00:00
}
2023-11-14 09:55:19 +00:00
await resultStream.backpressuredPush(chunkArg);
return null;
2023-11-06 17:14:21 +00:00
},
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;
}
}