Add Attachments.deleteData
This commit is contained in:
parent
9b636a1885
commit
10afcf1bb0
2 changed files with 43 additions and 0 deletions
|
@ -67,6 +67,22 @@ exports.writeData = (root) => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// deleteData :: AttachmentsPath -> IO Unit
|
||||||
|
exports.deleteData = (root) => {
|
||||||
|
if (!isString(root)) {
|
||||||
|
throw new TypeError('`root` must be a path');
|
||||||
|
}
|
||||||
|
|
||||||
|
return async (relativePath) => {
|
||||||
|
if (!isString(relativePath)) {
|
||||||
|
throw new TypeError('`relativePath` must be a string');
|
||||||
|
}
|
||||||
|
|
||||||
|
const absolutePath = path.join(root, relativePath);
|
||||||
|
await fse.remove(absolutePath);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
// createName :: Unit -> IO String
|
// createName :: Unit -> IO String
|
||||||
exports.createName = () => {
|
exports.createName = () => {
|
||||||
const buffer = crypto.randomBytes(32);
|
const buffer = crypto.randomBytes(32);
|
||||||
|
|
|
@ -63,6 +63,33 @@ describe('Attachments', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('deleteData', () => {
|
||||||
|
let tempRootDirectory = null;
|
||||||
|
before(() => {
|
||||||
|
tempRootDirectory = tmp.dirSync().name;
|
||||||
|
});
|
||||||
|
|
||||||
|
after(async () => {
|
||||||
|
await fse.remove(tempRootDirectory);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should delete file from disk', async () => {
|
||||||
|
const tempDirectory = path.join(tempRootDirectory, 'Attachments_deleteData');
|
||||||
|
|
||||||
|
const relativePath = Attachments.getRelativePath(Attachments.createName());
|
||||||
|
const fullPath = path.join(tempDirectory, relativePath);
|
||||||
|
const input = stringToArrayBuffer('test string');
|
||||||
|
|
||||||
|
const inputBuffer = Buffer.from(input);
|
||||||
|
await fse.ensureFile(fullPath);
|
||||||
|
await fse.writeFile(fullPath, inputBuffer);
|
||||||
|
await Attachments.deleteData(tempDirectory)(relativePath);
|
||||||
|
|
||||||
|
const existsFile = await fse.exists(fullPath);
|
||||||
|
assert.isFalse(existsFile);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('createName', () => {
|
describe('createName', () => {
|
||||||
it('should return random file name with correct length', () => {
|
it('should return random file name with correct length', () => {
|
||||||
assert.lengthOf(Attachments.createName(), NAME_LENGTH);
|
assert.lengthOf(Attachments.createName(), NAME_LENGTH);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue