diff --git a/lib/fileio.js b/lib/fileio.js index 1a1ad2e..1e869ca 100644 --- a/lib/fileio.js +++ b/lib/fileio.js @@ -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); }); } diff --git a/test/fileio.js b/test/fileio.js index a345737..a503c5f 100644 --- a/test/fileio.js +++ b/test/fileio.js @@ -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'); + }); +});