Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow large uploads via Blob #76

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 67 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const path = require('path');
const pathSep = require('path').sep;
const crypto = require("crypto");
const algorithm = 'aes-256-gcm';
const { Readable, Transform } = require('stream')

function FileSystemAdapter(options) {
options = options || {};
Expand All @@ -25,41 +26,81 @@ function FileSystemAdapter(options) {
}
}

FileSystemAdapter.prototype.createFile = function(filename, data) {
FileSystemAdapter.prototype.createFile = function (filename, data) {
const filepath = this._getLocalFilePath(filename);
const stream = fs.createWriteStream(filepath);
return new Promise((resolve, reject) => {
try {
if (this._encryptionKey !== null) {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(
algorithm,
this._encryptionKey,
iv
);
const encryptedResult = Buffer.concat([
cipher.update(data),
cipher.final(),
iv,
cipher.getAuthTag(),
]);
stream.write(encryptedResult);
stream.end();
stream.on('finish', function() {
resolve(data);
});
const iv = this._encryptionKey ? crypto.randomBytes(16) : null;

const cipher =
this._encryptionKey && iv
? crypto.createCipheriv(algorithm, this._encryptionKey, iv)
: null;

// when working with a Blob, it could be over the max size of a buffer, so we need to stream it
if (data instanceof Blob) {
let readableStream = data.stream();

// may come in as a web stream, so we need to convert it to a node stream
if (readableStream instanceof ReadableStream) {
readableStream = Readable.fromWeb(readableStream);
}

if (cipher && iv) {
// we need to stream the data through the cipher
const cipherTransform = new Transform({
transform(chunk, encoding, callback) {
try {
const encryptedChunk = cipher.update(chunk);
callback(null, encryptedChunk);
} catch (err) {
callback(err);
}
},
// at the end we need to push the final cipher text, iv, and auth tag
flush(callback) {
try {
this.push(cipher.final());
this.push(iv);
this.push(cipher.getAuthTag());
callback();
} catch (err) {
callback(err);
}
},
});
// pipe the stream through the cipher and then to the main stream
readableStream
.pipe(cipherTransform)
.on("error", reject)
.pipe(stream)
.on("error", reject);
} else {
// if we don't have a cipher, we can just pipe the stream to the main stream
readableStream.pipe(stream).on("error", reject);
}
} else {
stream.write(data);
if (cipher && iv) {
const encryptedResult = Buffer.concat([
cipher.update(data),
cipher.final(),
iv,
cipher.getAuthTag(),
]);
stream.write(encryptedResult);
} else {
stream.write(data);
}
stream.end();
stream.on('finish', function() {
resolve(data);
});
}
} catch(err) {
return reject(err);
stream.on("finish", resolve);
stream.on("error", reject);
} catch (e) {
reject(e);
}
});
}
};

FileSystemAdapter.prototype.deleteFile = function(filename) {
const filepath = this._getLocalFilePath(filename);
Expand Down
16 changes: 16 additions & 0 deletions spec/secureFiles.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,20 @@ describe('File encryption tests', () => {
const encryptedData2 = fs.readFileSync(filePath2);
expect(encryptedData2.toString('utf-8')).not.toEqual(oldEncryptedData2);
}, 5000);

it('should handle blobs on creation', async function() {
const adapter = new FileSystemAdapter({
encryptionKey: '89E4AFF1-DFE4-4603-9574-BFA16BB446FD'
});
const filename = 'file2.txt';
const filePath = 'files/' + filename;
const data = new Blob(['hello world']);
await adapter.createFile(filename, data);
const result = await adapter.getFileData(filename);
expect(result instanceof Buffer).toBe(true);
expect(result.toString('utf-8')).toEqual('hello world');
const fileData = fs.readFileSync(filePath);
expect(fileData.toString('utf-8')).not.toEqual('hello world');
}, 5000);

})