Skip to content

Commit

Permalink
Merge pull request #113 from bcgov/bug/file-cache
Browse files Browse the repository at this point in the history
Improve template file handling
  • Loading branch information
TimCsaky authored Jul 25, 2024
2 parents 337b1c2 + cbd9d1a commit b9ae3d5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/src/components/carboneCopyApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ const carboneCopyApi = {
carboneRender.carboneSet();
},

findAndRender: (hash, req, res) => {
findAndRender: async (hash, req, res) => {
const template = fileCache.find(hash);
if (!template.success) {
new Problem(template.errorType, { detail: template.errorMsg }).send(res);
} else {
return carboneCopyApi.renderTemplate(template, req, res);
return await carboneCopyApi.renderTemplate(template, req, res);
}
},

Expand Down
7 changes: 5 additions & 2 deletions app/src/components/fileCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ class FileCache {
if (!fs.existsSync(hashPath)) {
result.errorType = 404;
result.errorMsg = `Hash '${hash}' not found.`;
log.error(`Hash '${hash}' not found.`, { function: 'fileCache.find', result });
return result;
}
result.hash = hash;

const files = fs.readdirSync(hashPath);
if (!files || files.length !== 1) {
if (!files || files.length === 0) {
result.errorType = 404;
result.errorMsg = 'Hash found; could not read file from cache.';
log.error('Hash found. could not read file from cache', { function: 'fileCache.find', result });
return result;
} else {
result.name = files[0];
Expand Down Expand Up @@ -121,9 +123,10 @@ class FileCache {
}

const hashPath = this._getHashPath(result.hash);
// if file exists at temp file path
// if template exists
if (fs.existsSync(hashPath)) {
if (options.overwrite) {
// remove template
fs.removeSync(hashPath);
} else {
// Remove temporary file from cache
Expand Down

0 comments on commit b9ae3d5

Please sign in to comment.