better temporary file handling

master
Gregory Scheerlinck 8 years ago
parent 70f3482007
commit 3a784dd69a

@ -9,14 +9,17 @@ var fsWriteFile = Promise.promisify(require('fs').writeFile);
var fsUnlink = Promise.promisify(require('fs').unlink);
var path = require('path');
var getArticlePath = require('./util').getArticlePath;
var sanitize = require('sanitize-filename');
var tmpFile = path.resolve('./tmp/tmpfile');
var tmp = require('tmp');
var temp = null;
exports.convert = convert;
exports.getContents = getContents;
exports.tmpSave = tmpSave;
exports.removeTmp = removeTmp;
exports.processMd = processMd;
exports.getTmp = function getTmpLoc() { return temp; };
function convert(contents, options) {
return remark.use(man, options).process(contents);
@ -26,6 +29,7 @@ function getContents(article) {
article.path = getArticlePath(article.path);
return fsReadFile(article.path, 'utf-8').then(function readArticle(contents) {
article.contents = contents;
article.title = sanitize(article.title);
return article;
}).catch(function failReadArticle(err) {
console.log('Can\'t read ' + article.path + '. This should never happen. Try refreshing the database?');
@ -34,19 +38,21 @@ function getContents(article) {
}
function tmpSave(roff) {
return fsWriteFile(tmpFile, roff, 'utf8').then(function writeRoff() {
return path.resolve(tmpFile);
}).catch(function catchWrite(err) {
temp = tmp.fileSync().name;
return fsWriteFile(temp, roff, 'utf8').then(function writeRoff() {
temp = path.resolve(temp);
return temp;
}).catch(function catchWrite(err2) {
console.log('Could not write the temporary roff page');
console.log('Error: ' + err);
console.log('Error: ' + err2);
});
}
function removeTmp() {
return fsUnlink(tmpFile).then(function deleteTmp() {
return fsUnlink(temp).then(function deleteTmp() {
return 'done';
}).catch(function catchUnlink(err) {
console.log('Could not delete the temporary file at ' + tmpFile);
console.log('Could not delete the temporary file at ' + temp);
console.log('Error: ', + err);
});
}

@ -8,7 +8,6 @@ var fileio = require('../lib/fileio');
var Promise = require('bluebird');
var fsReadFile = Promise.promisify(require('fs').readFile);
var fsUnlink = Promise.promisify(require('fs').unlink);
var fsWriteFile = Promise.promisify(require('fs').writeFile);
var path = require('path');
describe('methods', function() {
@ -31,6 +30,10 @@ describe('methods', function() {
it('has a processMd function', function() {
expect(fileio.processMd).to.be.a('function');
});
it('has a getTmp function', function() {
expect(fileio.getTmp).to.be.a('function');
});
});
describe('getContents', function() {
@ -86,7 +89,7 @@ describe('tmpSave', function() {
it('saves a temporary file', function(done) {
fileio.tmpSave('hello world').then(function(tmpFile) {
tmp = tmpFile;
expect(tmpFile).to.equal(path.resolve('./tmp/tmpfile'));
expect(tmpFile).to.equal(path.resolve(fileio.getTmp()));
fsReadFile(tmpFile, 'utf8').then(function(contents) {
expect(contents).to.equal('hello world');
done();
@ -102,10 +105,8 @@ describe('tmpSave', function() {
});
describe('removeTmp', function() {
var tmp = path.resolve('./tmp/tmpfile');
before(function(done) {
fsWriteFile(tmp, 'hello world').then(function() {
fileio.tmpSave('hello world').then(function() {
done();
});
});
@ -129,3 +130,9 @@ describe('processMd', function() {
expect(converted.contents).to.equal('foo\n');
});
});
describe('getTmp', function() {
it('gets the tmpfile location', function() {
expect(fileio.getTmp()).to.be.a('string');
});
});

Loading…
Cancel
Save