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:
parent
1262d1d696
commit
6355c54114
3 changed files with 15 additions and 13 deletions
|
@ -1,8 +1,8 @@
|
|||
const crypto = require('crypto');
|
||||
const FSE = require('fs-extra');
|
||||
const fse = require('fs-extra');
|
||||
const isArrayBuffer = require('lodash/isArrayBuffer');
|
||||
const isString = require('lodash/isString');
|
||||
const Path = require('path');
|
||||
const path = require('path');
|
||||
|
||||
|
||||
// _writeAttachmentData :: AttachmentsPath ->
|
||||
|
@ -19,10 +19,11 @@ exports.writeAttachmentData = (root) => {
|
|||
}
|
||||
|
||||
const buffer = Buffer.from(arrayBuffer);
|
||||
const path = Path.join(root, exports._getAttachmentPath());
|
||||
await FSE.ensureFile(path);
|
||||
await FSE.writeFile(path, buffer);
|
||||
return path;
|
||||
const relativePath = exports._getAttachmentPath();
|
||||
const absolutePath = path.join(root, relativePath);
|
||||
await fse.ensureFile(absolutePath);
|
||||
await fse.writeFile(absolutePath, buffer);
|
||||
return relativePath;
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -36,5 +37,5 @@ exports._getAttachmentName = () => {
|
|||
exports._getAttachmentPath = () => {
|
||||
const name = exports._getAttachmentName();
|
||||
const prefix = name.slice(0, 2);
|
||||
return Path.join(prefix, name);
|
||||
return path.join(prefix, name);
|
||||
};
|
||||
|
|
|
@ -18,6 +18,7 @@ const PRIVATE = 'private';
|
|||
// - Attachments: Sanitize Unicode order override characters.
|
||||
// Version 3
|
||||
// - Attachments: Write attachment data to disk and store relative path to it.
|
||||
|
||||
const INITIAL_SCHEMA_VERSION = 0;
|
||||
|
||||
// Increment this version number every time we add a message schema upgrade
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
const FSE = require('fs-extra');
|
||||
const fse = require('fs-extra');
|
||||
const isEqual = require('lodash/isEqual');
|
||||
const Path = require('path');
|
||||
const path = require('path');
|
||||
const stringToArrayBuffer = require('string-to-arraybuffer');
|
||||
const tmp = require('tmp');
|
||||
const { assert } = require('chai');
|
||||
|
@ -24,17 +24,17 @@ describe('writeAttachmentData', () => {
|
|||
});
|
||||
|
||||
after(async () => {
|
||||
await FSE.remove(TEMPORARY_DIRECTORY);
|
||||
await fse.remove(TEMPORARY_DIRECTORY);
|
||||
});
|
||||
|
||||
it('should write file to disk and return path', async () => {
|
||||
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 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);
|
||||
assert.isTrue(isEqual(inputBuffer, output));
|
||||
|
|
Loading…
Reference in a new issue