forked from nylas/nylas-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'private/for-merging' into merge-private
- Loading branch information
Showing
9 changed files
with
175 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
const fs = require('fs') | ||
const mkdirp = require('mkdirp') | ||
const path = require('path') | ||
const zlib = require('zlib') | ||
|
||
const MAX_PATH_DIRS = 5; | ||
const FILE_EXTENSION = 'nylasmail' | ||
|
||
function baseMessagePath() { | ||
return path.join(process.env.NYLAS_HOME, 'messages'); | ||
} | ||
|
||
export function tryReadBody(val) { | ||
try { | ||
const parsed = JSON.parse(val); | ||
if (parsed && parsed.path && parsed.path.startsWith(baseMessagePath())) { | ||
if (parsed.compressed) { | ||
return zlib.gunzipSync(fs.readFileSync(parsed.path)).toString(); | ||
} | ||
return fs.readFileSync(parsed.path, {encoding: 'utf8'}); | ||
} | ||
} catch (err) { | ||
console.warn('Got error while trying to parse body path, assuming we need to migrate', err); | ||
} | ||
return null; | ||
} | ||
|
||
export function pathForBodyFile(msgId) { | ||
const pathGroups = []; | ||
let remainingId = msgId; | ||
while (pathGroups.length < MAX_PATH_DIRS) { | ||
pathGroups.push(remainingId.substring(0, 2)); | ||
remainingId = remainingId.substring(2); | ||
} | ||
const bodyPath = path.join(...pathGroups); | ||
return path.join(baseMessagePath, bodyPath, `${remainingId}.${FILE_EXTENSION}`); | ||
} | ||
|
||
// NB: The return value of this function is what gets written into the database. | ||
export function writeBody({msgId, body, forceWrite = true} = {}) { | ||
const bodyPath = pathForBodyFile(msgId); | ||
const bodyDir = path.dirname(bodyPath); | ||
|
||
const compressedBody = zlib.gzipSync(body); | ||
const dbEntry = { | ||
path: bodyPath, | ||
compressed: true, | ||
}; | ||
|
||
// It's possible that gzipping actually makes the body larger. If that's the | ||
// case then just write the uncompressed body instead. | ||
let bodyToWrite = compressedBody; | ||
if (compressedBody.length >= body.length) { | ||
dbEntry.compressed = false; | ||
bodyToWrite = body; | ||
} | ||
|
||
const result = JSON.stringify(dbEntry); | ||
// If the JSON db entry would be longer than the body itself then just write | ||
// the body directly into the database. | ||
if (result.length > body.length) { | ||
return body; | ||
} | ||
|
||
try { | ||
let exists; | ||
|
||
// If we don't have to write to the file and it already exists then don't. | ||
if (!forceWrite) { | ||
exists = fs.existsSync(bodyPath); | ||
if (exists) { | ||
return result; | ||
} | ||
} | ||
|
||
// We want to minimize the number of times we interact with the filesystem | ||
// since it can be slow. | ||
if (exists === undefined) { | ||
exists = fs.existsSync(bodyPath); | ||
} | ||
if (!exists) { | ||
mkdirp.sync(bodyDir); | ||
} | ||
|
||
fs.writeFileSync(bodyPath, bodyToWrite); | ||
return result; | ||
} catch (err) { | ||
// If anything bad happens while trying to write to disk just store the | ||
// body in the database. | ||
return body; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters