docker/ts/classes.imagestore.ts

49 lines
1.5 KiB
TypeScript
Raw Normal View History

import * as plugins from './plugins.js';
import * as paths from './paths.js';
2024-06-05 21:56:02 +00:00
import type { DockerHost } from './classes.host.js';
export interface IDockerImageStoreConstructorOptions {
2024-06-05 22:32:50 +00:00
/**
* used for preparing images for longer term storage
*/
localDirPath: string;
/**
* a smartbucket dir for longer term storage.
*/
bucketDir: plugins.smartbucket.Directory;
}
export class DockerImageStore {
public options: IDockerImageStoreConstructorOptions;
2024-06-05 21:56:02 +00:00
constructor(dockerHost: DockerHost, optionsArg: IDockerImageStoreConstructorOptions) {
this.options = optionsArg;
}
// Method to store tar stream
2024-06-05 22:25:39 +00:00
public async storeImage(imageName: string, tarStream: plugins.smartstream.stream.Readable): Promise<void> {
2024-06-05 22:32:50 +00:00
const imagePath = plugins.path.join(this.options.localDirPath, `${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> {
2024-06-05 22:32:50 +00:00
const imagePath = plugins.path.join(this.options.localDirPath, `${imageName}.tar`);
if (!(await plugins.smartfile.fs.fileExists(imagePath))) {
throw new Error(`Image ${imageName} does not exist.`);
}
return plugins.smartfile.fsStream.createReadStream(imagePath);
}
}