diff --git a/app/cacheCleaner.js b/app/cacheCleaner.js index e6e5207..40b5aad 100644 --- a/app/cacheCleaner.js +++ b/app/cacheCleaner.js @@ -5,7 +5,7 @@ const { join } = require('path'); const log = require('./src/components/log')(module.filename); -const RATIO = 0.8; // Best practice is to keep the cache no more than 80% full +const RATIO = 0.7; // Best practice is to keep the cache no more than 70% full const osTempDir = realpathSync(tmpdir()); const cacheDir = (() => { diff --git a/app/src/components/fileCache.js b/app/src/components/fileCache.js index 5b962c2..b240672 100644 --- a/app/src/components/fileCache.js +++ b/app/src/components/fileCache.js @@ -107,11 +107,12 @@ class FileCache { } if (!name) { result.errorType = 400; - result.errorMsg = 'Cannot move file; name parameter is required.'; + result.errorMsg = 'Cannot move file; file name parameter is required.'; return result; } try { + // get a hash of the file from contents result.hash = await this._getHash(source); } catch (e) { result.errorType = 500; @@ -120,6 +121,7 @@ class FileCache { } const hashPath = this._getHashPath(result.hash); + // if file exists at temp file path if (fs.existsSync(hashPath)) { if (options.overwrite) { fs.removeSync(hashPath); @@ -158,7 +160,7 @@ class FileCache { return result; } - async write(content, name, contentEncodingType = 'base64', options = { overwrite: false }) { + async write(content, fileType, contentEncodingType = 'base64', options = { overwrite: false }) { let result = { success: false, errorType: null, errorMsg: null, hash: null }; if (!content) { @@ -166,20 +168,22 @@ class FileCache { result.errorMsg = 'Cannot write file; content parameter is required.'; return result; } - if (!name) { + if (!fileType) { result.errorType = 400; - result.errorMsg = 'Cannot write file; name parameter is required.'; + result.errorMsg = 'Cannot write file; fileType parameter is required.'; return result; } const tmpFile = this._getTempFilePath(); - fs.outputFileSync(tmpFile, content, { encoding: contentEncodingType }); + // save template to temp directory + await fs.outputFileSync(tmpFile, content, { encoding: contentEncodingType }); - // name may only be an extension, if that is the case, let's generate a name - let destFilename = path.extname(name) === '' ? path.format({ + // move temp file to file cache + let destFilename = path.format({ name: uuidv4(), - ext: (name.startsWith('.') ? name : `.${name}`) - }) : name; + ext: fileType.replace(/\./g, '') + }); result = await this.move(tmpFile, destFilename, options); + log.info('Template cached', { function: 'fileCache.write' }); if (!result.success) { result.errorMsg = `Error writing content to cache. ${result.errorMsg}`; } diff --git a/app/src/routes/v2/template.js b/app/src/routes/v2/template.js index c48e082..ec9f3c9 100644 --- a/app/src/routes/v2/template.js +++ b/app/src/routes/v2/template.js @@ -10,7 +10,9 @@ const log = require('../../components/log')(module.filename); const fileCache = new FileCache(); -/** Returns the rendered report from cache */ +/** + * Upload a template to cache + */ templateRouter.post('/', upload, async (req, res) => { log.verbose('Template upload'); @@ -28,6 +30,9 @@ templateRouter.post('/', upload, async (req, res) => { } }); +/** + * Render a document from a template provided in JSON body + */ templateRouter.post('/render', middleware.validateTemplate, async (req, res) => { log.verbose('Template upload and render'); @@ -52,12 +57,18 @@ templateRouter.post('/render', middleware.validateTemplate, async (req, res) => return await findAndRender(content.hash, req, res); }); +/** + * Render a document from a cached template + */ templateRouter.post('/:uid/render', middleware.validateCarbone, async (req, res) => { const hash = req.params.uid; log.verbose('Template render', { hash: hash }); return await findAndRender(hash, req, res); }); +/** + * get a template from cache + */ templateRouter.get('/:uid', async (req, res) => { const hash = req.params.uid; const download = req.query.download !== undefined; @@ -66,6 +77,9 @@ templateRouter.get('/:uid', async (req, res) => { return getFromCache(hash, hashHeaderName, download, false, res); }); +/** + * delete a template from cache + */ templateRouter.delete('/:uid', async (req, res) => { const hash = req.params.uid; const download = req.query.download !== undefined;