From c6de54d13833d19dddf2ec938ecf69fa5749a562 Mon Sep 17 00:00:00 2001 From: Jared Noble Date: Wed, 11 Oct 2017 12:04:20 -0700 Subject: [PATCH] assets on file but not in index will now save to index on submission resolves #586 --- lib/cluster/storage/assets.js | 26 +++++++++++++++++-- .../storage/backends/elasticsearch_store.js | 6 ++++- lib/utils/file_utils.js | 3 ++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/cluster/storage/assets.js b/lib/cluster/storage/assets.js index 40d56b0300d..e4b2a5fbdb5 100644 --- a/lib/cluster/storage/assets.js +++ b/lib/cluster/storage/assets.js @@ -6,6 +6,7 @@ var crypto = require('crypto'); var _ = require('lodash'); var parseError = require('../../utils/error_utils').parseError; var saveAsset = require('../../utils/file_utils').saveAsset; +var normalizeZipFile = require('../../utils/file_utils').normalizeZipFile; var fse = require('fs-extra'); // Module to manager job states in Elasticsearch. @@ -24,10 +25,31 @@ module.exports = function module(context) { var esData = data.toString('base64'); var id = crypto.createHash('sha1').update(esData).digest("hex"); return fs.openAsync(`${assets_path}/${id}`, 'r') - .then(function(alreadyExists) { - return id; + .then(function() { + // directory exists, need to check if the index has it, if not then save it + return backend.get(id, null, ['name']) + // it exists, just return the id + .then(() => id) + .catch((err) => { + if (err === 'Not Found') { + logger.info(`asset: ${id} exists on disk but not in index, saving asset to index`); + return normalizeZipFile(id, `${assets_path}/${id}`, logger) + .then((_metaData) => { + metaData = _metaData; + const fileData = { blob: esData, _created: new Date() }; + const file = _.assign(fileData, _metaData); + return backend.indexWithId(id, file); + }) + .then(() => { + logger.info(`assets: ${metaData.name}, id: ${id} has been saved to elasticsearch`); + return id; + }); + } + return Promise.reject(`error checking asset index, could not get asset with id: ${id}`); + }); }) .catch(function(err) { + // directory does not exist, need to save it if (err.code === 'ENOENT') { return Promise.resolve(saveAsset(logger, assets_path, id, data, _metaIsUnqiue)) .then(function(_metaData) { diff --git a/lib/cluster/storage/backends/elasticsearch_store.js b/lib/cluster/storage/backends/elasticsearch_store.js index 5af056698f1..d4dc1661048 100644 --- a/lib/cluster/storage/backends/elasticsearch_store.js +++ b/lib/cluster/storage/backends/elasticsearch_store.js @@ -19,7 +19,7 @@ module.exports = function module(context, indexName, recordType, idField, _bulkS let bulkSize = 500; if (_bulkSize) bulkSize = _bulkSize; - function getRecord(recordId, indexArg) { + function getRecord(recordId, indexArg, fields) { logger.trace(`getting record id: ${recordId}`); const query = { index: indexArg || indexName, @@ -27,6 +27,10 @@ module.exports = function module(context, indexName, recordType, idField, _bulkS id: recordId }; + if (fields) { + query._source = fields; + } + return elasticsearch.get(query); } diff --git a/lib/utils/file_utils.js b/lib/utils/file_utils.js index 10d202fc8e8..48efbaa1cc6 100644 --- a/lib/utils/file_utils.js +++ b/lib/utils/file_utils.js @@ -128,5 +128,6 @@ function saveAsset(logger, assets_path, id, binaryData, metaCheck) { module.exports = { existsSync: existsSync, saveAsset: saveAsset, - deleteDir: deleteDir + deleteDir: deleteDir, + normalizeZipFile: normalizeZipFile }; \ No newline at end of file