smartfile/ts/classes.streamfile.ts

91 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-11-04 19:07:43 +00:00
import * as plugins from './smartfile.plugins.js';
import * as fsStream from './fsstream.js';
import { Readable } from 'stream';
type StreamSource = () => Promise<Readable>;
/**
* The StreamFile class represents a file as a stream.
* It allows creating streams from a file path, a URL, or a buffer.
*/
export class StreamFile {
// INSTANCE
relativeFilePath?: string;
private streamSource: StreamSource;
private constructor(streamSource: StreamSource, relativeFilePath?: string) {
this.streamSource = streamSource;
this.relativeFilePath = relativeFilePath;
}
// STATIC
public static async fromPath(filePath: string): Promise<StreamFile> {
const streamSource = () => Promise.resolve(fsStream.createReadStream(filePath));
return new StreamFile(streamSource, filePath);
}
public static async fromUrl(url: string): Promise<StreamFile> {
const streamSource = async () => plugins.smartrequest.getStream(url); // Replace with actual plugin method
return new StreamFile(streamSource);
}
public static fromBuffer(buffer: Buffer, relativeFilePath?: string): StreamFile {
const streamSource = () => {
const stream = new Readable();
stream.push(buffer);
stream.push(null); // End of stream
return Promise.resolve(stream);
};
return new StreamFile(streamSource, relativeFilePath);
}
// METHODS
/**
* Creates a new readable stream from the source.
*/
public async createReadStream(): Promise<Readable> {
return this.streamSource();
}
/**
* Writes the stream to the disk at the specified path.
* @param filePathArg The file path where the stream should be written.
*/
public async writeToDisk(filePathArg: string): Promise<void> {
const readStream = await this.createReadStream();
const writeStream = fsStream.createWriteStream(filePathArg);
return new Promise((resolve, reject) => {
readStream.pipe(writeStream);
readStream.on('error', reject);
writeStream.on('error', reject);
writeStream.on('finish', resolve);
});
}
public async writeToDir(dirPathArg: string) {
const filePath = plugins.path.join(dirPathArg, this.relativeFilePath);
return this.writeToDisk(filePath);
}
public async getContentAsBuffer() {
const done = plugins.smartpromise.defer<Buffer>();
const readStream = await this.createReadStream();
const chunks: Buffer[] = [];
readStream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
readStream.on('error', done.reject);
readStream.on('end', () => {
const contentBuffer = Buffer.concat(chunks);
done.resolve(contentBuffer);
});
return done.promise;
}
public async getContentAsString(formatArg: 'utf8' | 'binary' = 'utf8') {
const contentBuffer = await this.getContentAsBuffer();
return contentBuffer.toString(formatArg);
}
}