Return relative path from writeAttachmentData

This will make our app more robust to changes in location on the file system.
This commit is contained in:
Daniel Gasienica 2018-03-16 14:58:21 -04:00
parent 1262d1d696
commit 6355c54114
3 changed files with 15 additions and 13 deletions

View file

@ -1,8 +1,8 @@
const crypto = require('crypto'); const crypto = require('crypto');
const FSE = require('fs-extra'); const fse = require('fs-extra');
const isArrayBuffer = require('lodash/isArrayBuffer'); const isArrayBuffer = require('lodash/isArrayBuffer');
const isString = require('lodash/isString'); const isString = require('lodash/isString');
const Path = require('path'); const path = require('path');
// _writeAttachmentData :: AttachmentsPath -> // _writeAttachmentData :: AttachmentsPath ->
@ -19,10 +19,11 @@ exports.writeAttachmentData = (root) => {
} }
const buffer = Buffer.from(arrayBuffer); const buffer = Buffer.from(arrayBuffer);
const path = Path.join(root, exports._getAttachmentPath()); const relativePath = exports._getAttachmentPath();
await FSE.ensureFile(path); const absolutePath = path.join(root, relativePath);
await FSE.writeFile(path, buffer); await fse.ensureFile(absolutePath);
return path; await fse.writeFile(absolutePath, buffer);
return relativePath;
}; };
}; };
@ -36,5 +37,5 @@ exports._getAttachmentName = () => {
exports._getAttachmentPath = () => { exports._getAttachmentPath = () => {
const name = exports._getAttachmentName(); const name = exports._getAttachmentName();
const prefix = name.slice(0, 2); const prefix = name.slice(0, 2);
return Path.join(prefix, name); return path.join(prefix, name);
}; };

View file

@ -18,6 +18,7 @@ const PRIVATE = 'private';
// - Attachments: Sanitize Unicode order override characters. // - Attachments: Sanitize Unicode order override characters.
// Version 3 // Version 3
// - Attachments: Write attachment data to disk and store relative path to it. // - Attachments: Write attachment data to disk and store relative path to it.
const INITIAL_SCHEMA_VERSION = 0; const INITIAL_SCHEMA_VERSION = 0;
// Increment this version number every time we add a message schema upgrade // Increment this version number every time we add a message schema upgrade

View file

@ -1,6 +1,6 @@
const FSE = require('fs-extra'); const fse = require('fs-extra');
const isEqual = require('lodash/isEqual'); const isEqual = require('lodash/isEqual');
const Path = require('path'); const path = require('path');
const stringToArrayBuffer = require('string-to-arraybuffer'); const stringToArrayBuffer = require('string-to-arraybuffer');
const tmp = require('tmp'); const tmp = require('tmp');
const { assert } = require('chai'); const { assert } = require('chai');
@ -24,17 +24,17 @@ describe('writeAttachmentData', () => {
}); });
after(async () => { after(async () => {
await FSE.remove(TEMPORARY_DIRECTORY); await fse.remove(TEMPORARY_DIRECTORY);
}); });
it('should write file to disk and return path', async () => { it('should write file to disk and return path', async () => {
const input = stringToArrayBuffer('test string'); const input = stringToArrayBuffer('test string');
const tempDirectory = Path.join(TEMPORARY_DIRECTORY, 'writeAttachmentData'); const tempDirectory = path.join(TEMPORARY_DIRECTORY, 'writeAttachmentData');
const outputPath = await writeAttachmentData(tempDirectory)(input); const outputPath = await writeAttachmentData(tempDirectory)(input);
const output = await FSE.readFile(outputPath); const output = await fse.readFile(path.join(tempDirectory, outputPath));
assert.lengthOf(Path.relative(tempDirectory, outputPath), PATH_LENGTH); assert.lengthOf(outputPath, PATH_LENGTH);
const inputBuffer = Buffer.from(input); const inputBuffer = Buffer.from(input);
assert.isTrue(isEqual(inputBuffer, output)); assert.isTrue(isEqual(inputBuffer, output));