smartarchive/dist_ts/smartarchive.classes.smartarchive.js

116 lines
9.9 KiB
JavaScript
Raw Normal View History

2022-04-04 14:28:40 +00:00
import * as plugins from './smartarchive.plugins.js';
import * as paths from './smartarchive.paths.js';
export class SmartArchive {
2020-03-13 20:22:56 +00:00
constructor() { }
2022-04-04 14:28:40 +00:00
/**
* extracts an archive from a given url
*/
async extractArchiveFromUrlToFs(urlArg, targetDir) {
const parsedPath = plugins.path.parse(urlArg);
const uniqueFileName = plugins.smartunique.uni() + parsedPath.ext;
plugins.smartfile.fs.ensureDir(paths.nogitDir); // TODO: totally remove caching needs
const downloadPath = plugins.path.join(paths.nogitDir, uniqueFileName);
const downloadedArchive = (await plugins.smartrequest.getBinary(urlArg)).body;
await plugins.smartfile.memory.toFs(downloadedArchive, downloadPath);
await this.extractArchiveFromFilePathToFs(downloadPath, targetDir);
await plugins.smartfile.fs.remove(downloadPath);
}
/**
* extracts an archive from a given filePath on disk
* @param filePathArg
2023-07-26 14:13:33 +00:00
* @param targetDirArg
2022-04-04 14:28:40 +00:00
*/
2023-07-26 14:13:33 +00:00
async extractArchiveFromFilePathToFs(filePathArg, targetDirArg) {
2022-06-07 15:11:13 +00:00
console.log(`extracting ${filePathArg}`);
2023-07-26 14:13:33 +00:00
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();
});
stream.resume();
});
extractPipeStop.on('finish', () => {
console.log(`Sucessfully extracted ${filePathArg}!`);
done.resolve();
});
// lets run the stream
readableStream.pipe(plugins.gunzipMaybe()).pipe(extractPipeStop);
await done.promise;
2020-03-13 20:22:56 +00:00
}
2022-04-04 14:28:40 +00:00
/**
* extracts to Observable
2023-07-26 14:13:33 +00:00
* where the Observable is emitting smartfiles
2022-04-04 14:28:40 +00:00
*/
async extractArchiveFromBufferToObservable(bufferArg) {
2023-07-26 14:13:33 +00:00
const { intake, replaySubject } = this.extractArchiveWithIntakeAndReplaySubject();
intake.pushData(bufferArg);
intake.signalEnd();
return replaySubject;
}
extractArchiveWithIntakeAndReplaySubject() {
2022-06-07 15:11:13 +00:00
const intake = new plugins.smartstream.StreamIntake();
2022-04-04 14:28:40 +00:00
const replaySubject = new plugins.smartrx.rxjs.ReplaySubject();
const readableStream = intake.getReadableStream();
const extractPipeStop = plugins.tarStream.extract();
extractPipeStop.on('entry', (header, stream, next) => {
let fileBuffer;
stream.on('data', (chunkArg) => {
if (!fileBuffer) {
fileBuffer = chunkArg;
}
else {
fileBuffer = Buffer.concat([fileBuffer, chunkArg]);
}
});
stream.on('end', () => {
replaySubject.next(new plugins.smartfile.Smartfile({
base: null,
contentBuffer: fileBuffer,
2023-07-26 14:13:33 +00:00
path: `${header.name}`,
2022-04-04 14:28:40 +00:00
}));
next();
});
stream.resume();
});
extractPipeStop.on('finish', () => {
replaySubject.complete();
});
// lets run the stream
2023-07-26 14:13:33 +00:00
readableStream.pipe(plugins.gunzipMaybe()).pipe(extractPipeStop);
return {
intake,
replaySubject,
};
2022-04-04 14:28:40 +00:00
}
/**
* extracts to Observable
*/
async extractArchiveFromUrlToObservable(urlArg) {
const response = await plugins.smartrequest.getBinary(urlArg);
const replaySubject = this.extractArchiveFromBufferToObservable(response.body);
return replaySubject;
}
2022-06-07 15:11:13 +00:00
// TODO
2023-07-26 14:13:33 +00:00
async extractArchiveFromUrlToStream() { }
2022-06-07 15:11:13 +00:00
// TODO
async extractArchiveFromFilePathToStream() { }
// TODO
async extractArchiveFromStreamToStream() { }
// TODO
async packFromStreamToStream() { }
// TODO
async packFromDirPathToStream() { }
// TODO
async packFromDirPathToFs() { }
2020-03-13 20:22:56 +00:00
}
2023-07-26 14:13:33 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRhcmNoaXZlLmNsYXNzZXMuc21hcnRhcmNoaXZlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRhcmNoaXZlLmNsYXNzZXMuc21hcnRhcmNoaXZlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxPQUFPLE1BQU0sMkJBQTJCLENBQUM7QUFDckQsT0FBTyxLQUFLLEtBQUssTUFBTSx5QkFBeUIsQ0FBQztBQUVqRCxNQUFNLE9BQU8sWUFBWTtJQUN2QixnQkFBZSxDQUFDO0lBRWhCOztPQUVHO0lBQ0ksS0FBSyxDQUFDLHlCQUF5QixDQUFDLE1BQWMsRUFBRSxTQUFpQjtRQUN0RSxNQUFNLFVBQVUsR0FBRyxPQUFPLENBQUMsSUFBSSxDQUFDLEtBQUssQ0FBQyxNQUFNLENBQUMsQ0FBQztRQUM5QyxNQUFNLGNBQWMsR0FBRyxPQUFPLENBQUMsV0FBVyxDQUFDLEdBQUcsRUFBRSxHQUFHLFVBQVUsQ0FBQyxHQUFHLENBQUM7UUFDbEUsT0FBTyxDQUFDLFNBQVMsQ0FBQyxFQUFFLENBQUMsU0FBUyxDQUFDLEtBQUssQ0FBQyxRQUFRLENBQUMsQ0FBQyxDQUFDLHFDQUFxQztRQUNyRixNQUFNLFlBQVksR0FBRyxPQUFPLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxLQUFLLENBQUMsUUFBUSxFQUFFLGNBQWMsQ0FBQyxDQUFDO1FBQ3ZFLE1BQU0saUJBQWlCLEdBQUcsQ0FBQyxNQUFNLE9BQU8sQ0FBQyxZQUFZLENBQUMsU0FBUyxDQU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