Files
smartarchive/ts/classes.archiveanalyzer.ts

92 lines
2.9 KiB
TypeScript
Raw Permalink Normal View History

2023-11-06 18:14:21 +01: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 23:14:39 +01:00
resultStream: plugins.smartstream.SmartDuplex;
decompressionStream:
| plugins.stream.Transform
| plugins.stream.Duplex
| plugins.tarStream.Extract;
2023-11-06 18:14:21 +01:00
}
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
]);
2023-11-06 18:14:21 +01:00
return archiveMimeTypes.has(mimeType);
}
2023-11-11 18:28:50 +01:00
private async getDecompressionStream(
mimeTypeArg: plugins.fileType.FileTypeResult['mime'],
): Promise<
plugins.stream.Transform | plugins.stream.Duplex | plugins.tarStream.Extract
> {
2023-11-06 18:14:21 +01:00
switch (mimeTypeArg) {
case 'application/gzip':
return this.smartArchiveRef.gzipTools.getDecompressionStream();
2024-03-17 00:29:42 +01:00
case 'application/zip':
return this.smartArchiveRef.zipTools.getDecompressionStream();
2023-11-06 18:14:21 +01:00
case 'application/x-bzip2':
2023-11-11 18:28:50 +01:00
return await this.smartArchiveRef.bzip2Tools.getDecompressionStream(); // replace with your own bzip2 decompression stream
2023-11-06 18:14:21 +01: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 22:11:24 +01:00
return plugins.smartstream.createPassThrough();
2023-11-06 18:14:21 +01:00
}
}
public getAnalyzedStream() {
let firstRun = true;
2023-11-13 22:11:24 +01:00
const resultStream = plugins.smartstream.createPassThrough();
const analyzerstream = new plugins.smartstream.SmartDuplex<
Buffer,
IAnalyzedResult
>({
2023-11-06 18:14:21 +01:00
readableObjectMode: true,
2023-11-08 17:01:48 +01:00
writeFunction: async (chunkArg: Buffer, streamtools) => {
2023-11-06 18:14:21 +01:00
if (firstRun) {
firstRun = false;
2023-11-14 10:55:19 +01:00
const fileType = await plugins.fileType.fileTypeFromBuffer(chunkArg);
const decompressionStream = await this.getDecompressionStream(
fileType?.mime as any,
);
2024-03-17 00:29:42 +01:00
/**
* analyzed stream emits once with this object
*/
2023-11-06 18:14:21 +01:00
const result: IAnalyzedResult = {
fileType,
2023-11-07 04:19:54 +01:00
isArchive: await this.mimeTypeIsArchive(fileType?.mime),
2023-11-06 18:14:21 +01:00
resultStream,
decompressionStream,
};
2023-11-13 22:11:24 +01:00
await streamtools.push(result);
2023-11-06 18:14:21 +01:00
}
2023-11-14 10:55:19 +01:00
await resultStream.backpressuredPush(chunkArg);
return null;
2023-11-06 18:14:21 +01:00
},
2023-11-07 04:19:54 +01:00
finalFunction: async (tools) => {
resultStream.push(null);
return null;
},
2023-11-06 18:14:21 +01:00
});
return analyzerstream;
}
}