2019-10-14 18:55:07 +00:00
|
|
|
import * as plugins from './smartbucket.plugins';
|
2019-10-16 16:12:18 +00:00
|
|
|
import { Directory } from './smartbucket.classes.directory';
|
2019-10-14 18:55:07 +00:00
|
|
|
|
2019-10-16 16:12:18 +00:00
|
|
|
export interface IFileMetaData {
|
|
|
|
name: string;
|
|
|
|
fileType: string;
|
|
|
|
size: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class File {
|
|
|
|
// STATIC
|
|
|
|
public static async createFileFromString(
|
|
|
|
dirArg: Directory,
|
|
|
|
fileName: string,
|
|
|
|
fileContent: string
|
|
|
|
) {
|
2019-10-16 17:15:48 +00:00
|
|
|
await this.createFileFromBuffer(dirArg, fileName, Buffer.from(fileContent));
|
2019-10-16 16:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static async createFileFromBuffer(
|
|
|
|
directoryRef: Directory,
|
|
|
|
fileName: string,
|
|
|
|
fileContent: Buffer
|
|
|
|
) {
|
2019-10-16 17:11:28 +00:00
|
|
|
const filePath = plugins.path.join(directoryRef.getBasePath(), fileName);
|
2019-10-16 16:12:18 +00:00
|
|
|
const streamIntake = new plugins.streamfunction.Intake();
|
|
|
|
const putPromise = directoryRef.bucketRef.smartbucketRef.minioClient
|
|
|
|
.putObject(this.name, filePath, streamIntake.getReadable())
|
2020-10-12 00:37:50 +00:00
|
|
|
.catch((e) => console.log(e));
|
2019-10-16 16:12:18 +00:00
|
|
|
streamIntake.pushData(fileContent);
|
|
|
|
streamIntake.signalEnd();
|
|
|
|
await putPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// INSTANCE
|
2019-10-18 13:43:06 +00:00
|
|
|
public parentDirectoryRef: Directory;
|
|
|
|
public name: string;
|
2019-10-16 16:12:18 +00:00
|
|
|
|
|
|
|
public path: string;
|
|
|
|
public metaData: IFileMetaData;
|
|
|
|
|
|
|
|
constructor(directoryRefArg: Directory, fileName: string) {
|
2019-10-18 13:43:06 +00:00
|
|
|
this.parentDirectoryRef = directoryRefArg;
|
|
|
|
this.name = fileName;
|
2019-10-16 16:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async getContentAsString() {
|
|
|
|
const fileBuffer = await this.getContentAsBuffer();
|
|
|
|
return fileBuffer.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getContentAsBuffer() {
|
|
|
|
const done = plugins.smartpromise.defer();
|
2019-10-18 13:43:06 +00:00
|
|
|
const fileStream = await this.parentDirectoryRef.bucketRef.smartbucketRef.minioClient
|
|
|
|
.getObject(this.parentDirectoryRef.bucketRef.name, this.path)
|
2020-10-12 00:37:50 +00:00
|
|
|
.catch((e) => console.log(e));
|
2019-10-16 16:12:18 +00:00
|
|
|
let completeFile = new Buffer('');
|
2019-10-16 17:15:48 +00:00
|
|
|
const duplexStream = plugins.streamfunction.createDuplexStream<Buffer, Buffer>(
|
2020-10-12 00:37:50 +00:00
|
|
|
async (chunk) => {
|
2019-10-16 17:15:48 +00:00
|
|
|
completeFile = Buffer.concat([chunk]);
|
|
|
|
return chunk;
|
|
|
|
},
|
2020-10-12 00:37:50 +00:00
|
|
|
async (cb) => {
|
2019-10-16 17:15:48 +00:00
|
|
|
done.resolve();
|
|
|
|
return Buffer.from('');
|
|
|
|
}
|
|
|
|
);
|
2019-10-16 16:12:18 +00:00
|
|
|
|
|
|
|
if (!fileStream) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
fileStream.pipe(duplexStream);
|
|
|
|
await done.promise;
|
|
|
|
return completeFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async streamContent() {
|
|
|
|
// TODO
|
2021-06-02 09:14:24 +00:00
|
|
|
throw new Error('not yet implemented');
|
2019-10-16 16:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* removes this file
|
|
|
|
*/
|
2019-10-16 17:15:48 +00:00
|
|
|
public async remove() {
|
2019-10-18 13:43:06 +00:00
|
|
|
await this.parentDirectoryRef.bucketRef.smartbucketRef.minioClient.removeObject(
|
|
|
|
this.parentDirectoryRef.bucketRef.name,
|
2019-10-16 17:15:48 +00:00
|
|
|
this.path
|
|
|
|
);
|
2019-10-18 13:43:06 +00:00
|
|
|
await this.parentDirectoryRef.listFiles();
|
2019-10-16 16:12:18 +00:00
|
|
|
}
|
|
|
|
}
|