2022-04-04 12:15:51 +00:00
|
|
|
import * as plugins from './smartarchive.plugins.js';
|
|
|
|
import * as paths from './smartarchive.paths.js';
|
2021-04-19 11:49:25 +00:00
|
|
|
import { extract } from 'tar';
|
2020-03-13 20:22:56 +00:00
|
|
|
|
|
|
|
export class SmartArchive {
|
|
|
|
public archiveDirectory: string;
|
|
|
|
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
|
|
|
|
* @param targetDir
|
|
|
|
*/
|
2021-04-19 11:49:25 +00:00
|
|
|
public async extractArchiveFromFilePathToFs(filePathArg: string, targetDir: string) {
|
2020-03-13 20:22:56 +00:00
|
|
|
const parsedPath = plugins.path.parse(filePathArg);
|
|
|
|
switch (parsedPath.ext) {
|
2020-03-16 14:38:17 +00:00
|
|
|
case '.tgz':
|
|
|
|
console.log(`detected a .tgz archive`);
|
|
|
|
await plugins.tar.extract({
|
|
|
|
file: filePathArg,
|
2021-04-19 11:49:25 +00:00
|
|
|
cwd: targetDir,
|
2020-03-16 14:38:17 +00:00
|
|
|
});
|
|
|
|
break;
|
2020-03-13 20:22:56 +00:00
|
|
|
}
|
2020-03-16 14:38:17 +00:00
|
|
|
}
|
2021-04-19 11:49:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* extracts to Observable
|
|
|
|
*/
|
|
|
|
public async extractArchiveFromBufferToObservable(
|
|
|
|
bufferArg: Buffer
|
|
|
|
): Promise<plugins.smartrx.rxjs.ReplaySubject<plugins.smartfile.Smartfile>> {
|
|
|
|
const intake = new plugins.streamfunction.Intake();
|
|
|
|
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,
|
2022-04-04 14:28:40 +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
|
|
|
|
readableStream
|
|
|
|
.pipe(plugins.gunzipMaybe())
|
|
|
|
.pipe(extractPipeStop);
|
|
|
|
intake.pushData(bufferArg);
|
|
|
|
intake.signalEnd();
|
|
|
|
return replaySubject;
|
|
|
|
}
|
2021-04-19 12:12:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* extracts to Observable
|
|
|
|
*/
|
|
|
|
public async extractArchiveFromUrlToObservable(
|
|
|
|
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;
|
|
|
|
}
|
2020-03-16 14:38:17 +00:00
|
|
|
}
|