2022-04-04 12:15:51 +00:00
|
|
|
import * as plugins from './smartarchive.plugins.js';
|
|
|
|
import * as paths from './smartarchive.paths.js';
|
2020-03-13 20:22:56 +00:00
|
|
|
|
|
|
|
export class SmartArchive {
|
|
|
|
constructor() {}
|
|
|
|
|
2020-03-16 14:38:17 +00:00
|
|
|
/**
|
|
|
|
* extracts an archive from a given url
|
|
|
|
*/
|
2021-04-19 11:49:25 +00:00
|
|
|
public async extractArchiveFromUrlToFs(urlArg: string, targetDir: string) {
|
2020-03-16 14:38:17 +00:00
|
|
|
const parsedPath = plugins.path.parse(urlArg);
|
|
|
|
const uniqueFileName = plugins.smartunique.uni() + parsedPath.ext;
|
2021-04-19 14:14:24 +00:00
|
|
|
plugins.smartfile.fs.ensureDir(paths.nogitDir); // TODO: totally remove caching needs
|
2020-03-16 14:38:17 +00:00
|
|
|
const downloadPath = plugins.path.join(paths.nogitDir, uniqueFileName);
|
2020-03-16 17:04:17 +00:00
|
|
|
const downloadedArchive = (await plugins.smartrequest.getBinary(urlArg)).body;
|
2020-03-16 14:38:17 +00:00
|
|
|
await plugins.smartfile.memory.toFs(downloadedArchive, downloadPath);
|
2021-04-19 11:49:25 +00:00
|
|
|
await this.extractArchiveFromFilePathToFs(downloadPath, targetDir);
|
2020-03-16 17:04:17 +00:00
|
|
|
await plugins.smartfile.fs.remove(downloadPath);
|
2020-03-16 14:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* extracts an archive from a given filePath on disk
|
|
|
|
* @param filePathArg
|
2022-06-07 15:11:13 +00:00
|
|
|
* @param targetDirArg
|
2020-03-16 14:38:17 +00:00
|
|
|
*/
|
2022-06-07 15:11:13 +00:00
|
|
|
public async extractArchiveFromFilePathToFs(filePathArg: string, targetDirArg: string) {
|
|
|
|
console.log(`extracting ${filePathArg}`);
|
|
|
|
const done = plugins.smartpromise.defer();
|
|
|
|
filePathArg = plugins.smartpath.transform.makeAbsolute(filePathArg);
|
|
|
|
targetDirArg = plugins.smartpath.transform.makeAbsolute(targetDirArg);
|
|
|
|
const readableStream = plugins.smartfile.fsStream.createReadStream(filePathArg);
|
|
|
|
const extractPipeStop = plugins.tarStream.extract();
|
|
|
|
extractPipeStop.on('entry', async (header, stream, next) => {
|
|
|
|
const targetFilePath = plugins.path.join(targetDirArg, header.name);
|
|
|
|
const parsedPath = plugins.path.parse(targetFilePath);
|
|
|
|
await plugins.smartfile.fs.ensureDir(parsedPath.dir);
|
|
|
|
const writeStream = plugins.smartfile.fsStream.createWriteStream(targetFilePath);
|
|
|
|
stream.pipe(writeStream);
|
|
|
|
stream.on('end', () => {
|
|
|
|
console.log(`extracted ${header.name}`);
|
|
|
|
next();
|
2023-07-26 14:13:33 +00:00
|
|
|
});
|
2022-06-07 15:11:13 +00:00
|
|
|
stream.resume();
|
|
|
|
});
|
|
|
|
extractPipeStop.on('finish', () => {
|
|
|
|
console.log(`Sucessfully extracted ${filePathArg}!`);
|
|
|
|
done.resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
// lets run the stream
|
2023-07-26 14:13:33 +00:00
|
|
|
readableStream.pipe(plugins.gunzipMaybe()).pipe(extractPipeStop);
|
2022-06-07 15:11:13 +00:00
|
|
|
await done.promise;
|
2020-03-16 14:38:17 +00:00
|
|
|
}
|
2021-04-19 11:49:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* extracts to Observable
|
2022-06-07 15:11:13 +00:00
|
|
|
* where the Observable is emitting smartfiles
|
2021-04-19 11:49:25 +00:00
|
|
|
*/
|
|
|
|
public async extractArchiveFromBufferToObservable(
|
|
|
|
bufferArg: Buffer
|
|
|
|
): Promise<plugins.smartrx.rxjs.ReplaySubject<plugins.smartfile.Smartfile>> {
|
2022-06-07 15:11:13 +00:00
|
|
|
const { intake, replaySubject } = this.extractArchiveWithIntakeAndReplaySubject();
|
|
|
|
intake.pushData(bufferArg);
|
|
|
|
intake.signalEnd();
|
|
|
|
return replaySubject;
|
|
|
|
}
|
|
|
|
|
|
|
|
extractArchiveWithIntakeAndReplaySubject() {
|
|
|
|
const intake = new plugins.smartstream.StreamIntake<Buffer>();
|
2021-04-19 11:49:25 +00:00
|
|
|
const replaySubject = new plugins.smartrx.rxjs.ReplaySubject<plugins.smartfile.Smartfile>();
|
|
|
|
const readableStream = intake.getReadableStream();
|
|
|
|
const extractPipeStop = plugins.tarStream.extract();
|
|
|
|
extractPipeStop.on('entry', (header, stream, next) => {
|
|
|
|
let fileBuffer: Buffer;
|
|
|
|
stream.on('data', (chunkArg) => {
|
|
|
|
if (!fileBuffer) {
|
|
|
|
fileBuffer = chunkArg;
|
|
|
|
} else {
|
|
|
|
fileBuffer = Buffer.concat([fileBuffer, chunkArg]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
stream.on('end', () => {
|
|
|
|
replaySubject.next(
|
|
|
|
new plugins.smartfile.Smartfile({
|
2022-04-04 14:28:40 +00:00
|
|
|
base: null, // no working directory for this one
|
2021-04-19 11:49:25 +00:00
|
|
|
contentBuffer: fileBuffer,
|
2023-07-26 14:13:33 +00:00
|
|
|
path: `${header.name}`,
|
2021-04-19 11:49:25 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
stream.resume();
|
|
|
|
});
|
|
|
|
extractPipeStop.on('finish', () => {
|
2021-04-19 12:15:23 +00:00
|
|
|
replaySubject.complete();
|
2021-04-19 11:49:25 +00:00
|
|
|
});
|
|
|
|
// lets run the stream
|
2023-07-26 14:13:33 +00:00
|
|
|
readableStream.pipe(plugins.gunzipMaybe()).pipe(extractPipeStop);
|
2022-06-07 15:11:13 +00:00
|
|
|
return {
|
|
|
|
intake,
|
2023-07-26 14:13:33 +00:00
|
|
|
replaySubject,
|
2022-06-07 15:11:13 +00:00
|
|
|
};
|
2021-04-19 11:49:25 +00:00
|
|
|
}
|
2021-04-19 12:12:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* extracts to Observable
|
|
|
|
*/
|
2023-07-26 14:13:33 +00:00
|
|
|
public async extractArchiveFromUrlToObservable(
|
2021-04-19 12:12:48 +00:00
|
|
|
urlArg: string
|
|
|
|
): Promise<plugins.smartrx.rxjs.ReplaySubject<plugins.smartfile.Smartfile>> {
|
|
|
|
const response = await plugins.smartrequest.getBinary(urlArg);
|
|
|
|
const replaySubject = this.extractArchiveFromBufferToObservable(response.body);
|
|
|
|
return replaySubject;
|
|
|
|
}
|
2022-06-06 09:29:13 +00:00
|
|
|
|
|
|
|
// TODO
|
2023-07-26 14:13:33 +00:00
|
|
|
public async extractArchiveFromUrlToStream() {}
|
2022-06-06 09:29:13 +00:00
|
|
|
|
|
|
|
// TODO
|
2022-06-07 15:11:13 +00:00
|
|
|
public async extractArchiveFromFilePathToStream() {}
|
2022-06-06 09:29:13 +00:00
|
|
|
|
|
|
|
// TODO
|
|
|
|
public async extractArchiveFromStreamToStream() {}
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
public async packFromStreamToStream() {}
|
|
|
|
|
|
|
|
// TODO
|
2022-06-07 15:11:13 +00:00
|
|
|
public async packFromDirPathToStream() {}
|
2022-06-06 09:29:13 +00:00
|
|
|
|
|
|
|
// TODO
|
2022-06-07 15:11:13 +00:00
|
|
|
public async packFromDirPathToFs() {}
|
2020-03-16 14:38:17 +00:00
|
|
|
}
|