134 lines
4.1 KiB
TypeScript
134 lines
4.1 KiB
TypeScript
import type { SmartArchive } from './classes.smartarchive.js';
|
|
import type { TSupportedMime } from '../ts_shared/interfaces.js';
|
|
import * as plugins from './plugins.js';
|
|
|
|
/**
|
|
* Type for decompression streams
|
|
*/
|
|
export type TDecompressionStream =
|
|
| plugins.stream.Transform
|
|
| plugins.stream.Duplex
|
|
| plugins.smartstream.SmartDuplex<any, any>;
|
|
|
|
/**
|
|
* Result of archive analysis
|
|
*/
|
|
export interface IAnalyzedResult {
|
|
fileType: plugins.fileType.FileTypeResult | undefined;
|
|
isArchive: boolean;
|
|
resultStream: plugins.smartstream.SmartDuplex<Buffer, Buffer>;
|
|
decompressionStream: TDecompressionStream;
|
|
}
|
|
|
|
/**
|
|
* Analyzes archive streams to detect format and provide decompression
|
|
*/
|
|
export class ArchiveAnalyzer {
|
|
private smartArchiveRef: SmartArchive;
|
|
|
|
constructor(smartArchiveRefArg: SmartArchive) {
|
|
this.smartArchiveRef = smartArchiveRefArg;
|
|
}
|
|
|
|
/**
|
|
* Check if a MIME type represents an archive format
|
|
*/
|
|
private async mimeTypeIsArchive(mimeType: string | undefined): Promise<boolean> {
|
|
if (!mimeType) return false;
|
|
|
|
const archiveMimeTypes: Set<string> = new Set([
|
|
'application/zip',
|
|
'application/x-rar-compressed',
|
|
'application/x-tar',
|
|
'application/gzip',
|
|
'application/x-7z-compressed',
|
|
'application/x-bzip2',
|
|
]);
|
|
|
|
return archiveMimeTypes.has(mimeType);
|
|
}
|
|
|
|
/**
|
|
* Get the appropriate decompression stream for a MIME type
|
|
*/
|
|
private async getDecompressionStream(mimeTypeArg: TSupportedMime): Promise<TDecompressionStream> {
|
|
switch (mimeTypeArg) {
|
|
case 'application/gzip': {
|
|
// Use fflate streaming Gunzip - instance must be created once and reused
|
|
let gunzip: plugins.fflate.Gunzip;
|
|
return new plugins.stream.Transform({
|
|
construct(callback) {
|
|
gunzip = new plugins.fflate.Gunzip((data, final) => {
|
|
this.push(Buffer.from(data));
|
|
});
|
|
callback();
|
|
},
|
|
transform(chunk, encoding, callback) {
|
|
try {
|
|
gunzip.push(chunk, false);
|
|
callback();
|
|
} catch (err) {
|
|
callback(err as Error);
|
|
}
|
|
},
|
|
flush(callback) {
|
|
try {
|
|
// Signal end of stream with empty final chunk
|
|
gunzip.push(new Uint8Array(0), true);
|
|
callback();
|
|
} catch (err) {
|
|
callback(err as Error);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
case 'application/zip':
|
|
return this.smartArchiveRef.zipTools.getDecompressionStream();
|
|
case 'application/x-bzip2':
|
|
return this.smartArchiveRef.bzip2Tools.getDecompressionStream();
|
|
case 'application/x-tar':
|
|
// TAR doesn't need decompression, just pass through
|
|
return plugins.smartstream.createPassThrough();
|
|
default:
|
|
// Handle unsupported formats or no decompression needed
|
|
return plugins.smartstream.createPassThrough();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create an analyzed stream that detects archive type and provides decompression
|
|
* Emits a single IAnalyzedResult object
|
|
*/
|
|
public getAnalyzedStream(): plugins.smartstream.SmartDuplex<Buffer, IAnalyzedResult> {
|
|
let firstRun = true;
|
|
const resultStream = plugins.smartstream.createPassThrough();
|
|
|
|
const analyzerstream = new plugins.smartstream.SmartDuplex<Buffer, IAnalyzedResult>({
|
|
readableObjectMode: true,
|
|
writeFunction: async (chunkArg: Buffer, streamtools) => {
|
|
if (firstRun) {
|
|
firstRun = false;
|
|
const fileType = await plugins.fileType.fileTypeFromBuffer(chunkArg);
|
|
const decompressionStream = await this.getDecompressionStream(fileType?.mime as TSupportedMime);
|
|
|
|
const result: IAnalyzedResult = {
|
|
fileType,
|
|
isArchive: await this.mimeTypeIsArchive(fileType?.mime),
|
|
resultStream,
|
|
decompressionStream,
|
|
};
|
|
await streamtools.push(result);
|
|
}
|
|
await resultStream.backpressuredPush(chunkArg);
|
|
return null;
|
|
},
|
|
finalFunction: async () => {
|
|
resultStream.push(null);
|
|
return null;
|
|
},
|
|
});
|
|
|
|
return analyzerstream;
|
|
}
|
|
}
|