Hello everyone, what I am attempting to do is build an attachment from a Readable stream and send it to a channel.
So, everything starts with a createFile method which receives a stream and pipes it into a stream that splits the file into chunks of 25mb each. If the file for example is 26 mb I am going to end up with two chunks, one of 25mb and other one of 1mb. In my code i call them "parts".
public createFile(filePath: string, fileStream: FileStream): Promise<stream.Writable> {
let j = 0;
return pump(
fileStream,
new SizeStream(25e+6, (s) => {
if (this.aesKey != null) {
let iv = crypto.randomBytes(16);
this.createFileImpl(filePath + (j == 0 ? "" : ".part" + j), s.pipe(this.getCipher(iv)), iv);
} else {
this.createFileImpl(filePath + (j == 0 ? "" : ".part" + j), s);
}
j++;
})
);
};
Here's the code for createFileImpl:
private createFileImpl(filePath: string, content: stream.Readable, iv: Buffer = null): FileJournalEntry {
let that = this;
let directoryName = that.normalizePath(path.dirname(filePath));
let fileName = path.basename(filePath);
let directory = that.directories.find(d => d.name == directoryName);
/* There's some unrelated commented out code in between... */
const entry = this.createFileOnDiscord(fileName, directory, content, iv);
return entry;
}
//I am hitting the character limit so i'll continue in a comment.