docker/ts/classes.imagestore.ts
2024-06-05 23:56:02 +02:00

42 lines
1.3 KiB
TypeScript

import * as plugins from './plugins.js';
import * as paths from './paths.js';
import type { DockerHost } from './classes.host.js';
export interface IDockerImageStoreConstructorOptions {
dirPath: string;
}
export class DockerImageStore {
public options: IDockerImageStoreConstructorOptions;
constructor(dockerHost: DockerHost, 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);
}
}