121 lines
4.3 KiB
TypeScript
121 lines
4.3 KiB
TypeScript
|
import * as plugins from './plugins.js';
|
||
|
import * as paths from './paths.js';
|
||
|
|
||
|
import { GzipTools } from './classes.gziptools.js';
|
||
|
import { TarTools } from './classes.tartools.js';
|
||
|
import { Bzip2Tools } from './classes.bzip2tools.js';
|
||
|
|
||
|
import { ArchiveAnalyzer, type IAnalyzedResult } from './classes.archiveanalyzer.js';
|
||
|
|
||
|
import type { from } from '@push.rocks/smartrx/dist_ts/smartrx.plugins.rxjs.js';
|
||
|
|
||
|
export class SmartArchive {
|
||
|
// STATIC
|
||
|
public static async fromArchiveUrl(urlArg: string): Promise<SmartArchive> {
|
||
|
const smartArchiveInstance = new SmartArchive();
|
||
|
smartArchiveInstance.sourceUrl = urlArg;
|
||
|
return smartArchiveInstance;
|
||
|
}
|
||
|
|
||
|
public static async fromArchiveFile(filePathArg: string): Promise<SmartArchive> {
|
||
|
const smartArchiveInstance = new SmartArchive();
|
||
|
smartArchiveInstance.sourceFilePath = filePathArg;
|
||
|
return smartArchiveInstance;
|
||
|
}
|
||
|
|
||
|
public static async fromArchiveStream(
|
||
|
streamArg: plugins.stream.Readable | plugins.stream.Duplex | plugins.stream.Transform
|
||
|
): Promise<SmartArchive> {
|
||
|
const smartArchiveInstance = new SmartArchive();
|
||
|
smartArchiveInstance.sourceStream = streamArg;
|
||
|
return smartArchiveInstance;
|
||
|
}
|
||
|
|
||
|
// INSTANCE
|
||
|
public tarTools = new TarTools(this);
|
||
|
public gzipTools = new GzipTools(this);
|
||
|
public bzip2Tools = new Bzip2Tools(this);
|
||
|
public archiveAnalyzer = new ArchiveAnalyzer(this);
|
||
|
|
||
|
public sourceUrl: string;
|
||
|
public sourceFilePath: string;
|
||
|
public sourceStream: plugins.stream.Readable | plugins.stream.Duplex | plugins.stream.Transform;
|
||
|
|
||
|
public archiveName: string;
|
||
|
public singleFileMode: boolean = false;
|
||
|
|
||
|
public addedDirectories: string[] = [];
|
||
|
public addedFiles: (plugins.smartfile.SmartFile | plugins.smartfile.StreamFile)[] = [];
|
||
|
public addedUrls: string[] = [];
|
||
|
|
||
|
constructor() {}
|
||
|
|
||
|
/**
|
||
|
* gets the original archive stream
|
||
|
*/
|
||
|
public async getArchiveStream() {
|
||
|
if (this.sourceStream) {
|
||
|
return this.sourceStream;
|
||
|
}
|
||
|
if (this.sourceUrl) {
|
||
|
const urlStream = await plugins.smartrequest.getStream(this.sourceUrl);
|
||
|
return urlStream;
|
||
|
}
|
||
|
if (this.sourceFilePath) {
|
||
|
const fileStream = plugins.smartfile.fs.toReadStream(this.sourceFilePath);
|
||
|
return fileStream;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public async exportToTarGzStream() {
|
||
|
const tarPackStream = await this.tarTools.getPackStream();
|
||
|
const gzipStream = await this.gzipTools.getCompressionStream();
|
||
|
// const archiveStream = tarPackStream.pipe(gzipStream);
|
||
|
// return archiveStream;
|
||
|
}
|
||
|
|
||
|
public async exportToFs(targetDir: string): Promise<void> {}
|
||
|
|
||
|
public async exportToStreamOfStreamFiles() {
|
||
|
const streamFileIntake = new plugins.smartstream.StreamIntake<plugins.smartfile.StreamFile>({
|
||
|
objectMode: true,
|
||
|
});
|
||
|
const archiveStream = await this.getArchiveStream();
|
||
|
const createAnalyzedStream = () => this.archiveAnalyzer.getAnalyzedStream();
|
||
|
|
||
|
// lets create a function that can be called multiple times to unpack layers of archives
|
||
|
const createUnpackStream = () =>
|
||
|
plugins.smartstream.createTransformFunction<IAnalyzedResult, any>(
|
||
|
async (analyzedResultChunk) => {
|
||
|
if (analyzedResultChunk.fileType.mime === 'application/x-tar') {
|
||
|
(analyzedResultChunk.decompressionStream as plugins.tarStream.Extract).on(
|
||
|
'entry',
|
||
|
async (header, stream, next) => {
|
||
|
const streamfile = plugins.smartfile.StreamFile.fromStream(stream, header.name);
|
||
|
streamFileIntake.push(streamfile);
|
||
|
stream.on('end', function () {
|
||
|
next(); // ready for next entry
|
||
|
});
|
||
|
}
|
||
|
);
|
||
|
analyzedResultChunk.resultStream.pipe(analyzedResultChunk.decompressionStream);
|
||
|
} else if (analyzedResultChunk.isArchive && analyzedResultChunk.decompressionStream) {
|
||
|
analyzedResultChunk.resultStream
|
||
|
.pipe(analyzedResultChunk.decompressionStream)
|
||
|
.pipe(createAnalyzedStream())
|
||
|
.pipe(createUnpackStream());
|
||
|
} else {
|
||
|
const streamFile = plugins.smartfile.StreamFile.fromStream(
|
||
|
analyzedResultChunk.resultStream,
|
||
|
analyzedResultChunk.fileType.ext
|
||
|
);
|
||
|
streamFileIntake.push(streamFile);
|
||
|
}
|
||
|
}
|
||
|
);
|
||
|
|
||
|
archiveStream.pipe(createAnalyzedStream()).pipe(createUnpackStream());
|
||
|
return streamFileIntake;
|
||
|
}
|
||
|
}
|