41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import * as plugins from './plugins.js';
|
|
import * as paths from './paths.js';
|
|
|
|
export interface IDockerImageStoreConstructorOptions {
|
|
dirPath: string;
|
|
}
|
|
|
|
export class DockerImageStore {
|
|
public options: IDockerImageStoreConstructorOptions;
|
|
|
|
constructor(optionsArg: IDockerImageStoreConstructorOptions) {
|
|
this.options = optionsArg;
|
|
}
|
|
|
|
// Method to store tar stream
|
|
public async storeImage(imageName: string, tarStream: NodeJS.ReadableStream): Promise<void> {
|
|
const imagePath = plugins.path.join(this.options.dirPath, `${imageName}.tar`);
|
|
|
|
// Create a write stream to store the tar file
|
|
const writeStream = plugins.smartfile.fsStream.createWriteStream(imagePath);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
tarStream.pipe(writeStream);
|
|
|
|
writeStream.on('finish', resolve);
|
|
writeStream.on('error', reject);
|
|
});
|
|
}
|
|
|
|
// Method to retrieve tar stream
|
|
public async getImage(imageName: string): Promise<plugins.smartstream.stream.Readable> {
|
|
const imagePath = plugins.path.join(this.options.dirPath, `${imageName}.tar`);
|
|
|
|
if (!(await plugins.smartfile.fs.fileExists(imagePath))) {
|
|
throw new Error(`Image ${imageName} does not exist.`);
|
|
}
|
|
|
|
return plugins.smartfile.fsStream.createReadStream(imagePath);
|
|
}
|
|
}
|