Skip to content

Commit

Permalink
Improve template file handling
Browse files Browse the repository at this point in the history
when a high frequency of requests hit the app,
template files are not managed consistently.
fileCache.find should handle when more than one template is found.
  • Loading branch information
TimCsaky committed Jul 25, 2024
1 parent 337b1c2 commit cbd9d1a
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 cbd9d1a

Please sign in to comment.