fix(send): Impose stricter attachment size limits on send

Summary: See title

Test Plan: Manual

Reviewers: jackie, halla, evan

Reviewed By: halla, evan

Differential Revision: https://phab.nylas.com/D3670
This commit is contained in:
Juan Tejada 2017-01-13 19:01:57 -08:00
parent 5ab0173e78
commit 99f4cdcdad
2 changed files with 24 additions and 14 deletions

2
src/K2

@ -1 +1 @@
Subproject commit f141b7253a66d966c043bf0c33539ad2c1e8d308 Subproject commit 239aa612095facabd0386762459da45b09e749c2

View file

@ -68,8 +68,15 @@ class FileUploadStore extends NylasStore {
.catch(() => Promise.reject(new Error(`${filePath} could not be found, or has invalid file permissions.`))); .catch(() => Promise.reject(new Error(`${filePath} could not be found, or has invalid file permissions.`)));
} }
_prepareTargetDir(upload) { async _getTotalDirSize(dirpath) {
return mkdirpAsync(upload.targetDir).thenReturn(upload); const items = await fs.readdirAsync(dirpath)
let total = 0
for (const filename of items) {
const filepath = path.join(dirpath, filename)
const stats = await this._getFileStats(filepath)
total += stats.size
}
return total
} }
_copyUpload(upload) { _copyUpload(upload) {
@ -148,20 +155,23 @@ class FileUploadStore extends NylasStore {
this._assertIdPresent(messageClientId); this._assertIdPresent(messageClientId);
return this._getFileStats(filePath) return this._getFileStats(filePath)
.then((stats) => { .then(async (stats) => {
const upload = new Upload({messageClientId, filePath, stats, inline}); const upload = new Upload({messageClientId, filePath, stats, inline});
if (stats.isDirectory()) { if (stats.isDirectory()) {
return Promise.reject(new Error(`${upload.filename} is a directory. Try compressing it and attaching it again.`)); throw new Error(`${upload.filename} is a directory. Try compressing it and attaching it again.`);
} else if (stats.size > 25 * 1000000) { } else if (stats.size > 15 * 1000000) {
return Promise.reject(new Error(`${upload.filename} cannot be attached because it is larger than 25MB.`)); throw new Error(`${upload.filename} cannot be attached because it is larger than 5MB.`);
} }
return Promise.resolve(upload); await mkdirpAsync(upload.targetDir)
})
.then((upload) => this._prepareTargetDir(upload)) const totalSize = await this._getTotalDirSize(upload.targetDir)
.then((upload) => this._copyUpload(upload)) if (totalSize >= 15 * 1000000) {
.then((upload) => { throw new Error(`Can't upload more than 15MB of attachments`);
return this._applySessionChanges(upload.messageClientId, (uploads) => uploads.concat([upload])) }
.then(() => onUploadCreated(upload));
await this._copyUpload(upload)
await this._applySessionChanges(upload.messageClientId, (uploads) => uploads.concat([upload]))
onUploadCreated(upload)
}) })
.catch(this._onAttachFileError); .catch(this._onAttachFileError);
} }