Skip to content

Commit 3944274

Browse files
committed
Merge pull request #444 from CodeNow/hash
add hash to infracode files
2 parents 85554c6 + 78b0c9a commit 3944274

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/models/mongo/infra-code-version.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var last = require('101/last');
77
var isFunction = require('101/is-function');
88
var debug = require('debug')('runnable-api:infra-code-version:model');
99
var regexpQuote = require('regexp-quote');
10+
var crypto = require('crypto');
1011

1112
var path = require('path');
1213
var join = path.join;
@@ -215,9 +216,13 @@ InfraCodeVersionSchema.methods.createFs = function (data, cb) {
215216
infraCodeVersion.files.push(s3Data);
216217
var fileData = infraCodeVersion.files.pop().toJSON();
217218
var fileKey, dirKey;
219+
var attrs = {
220+
edited: true
221+
};
218222
if (last(fileData.Key) === '/') {
219223
fileKey = fileData.Key.slice(0, -1);
220224
dirKey = fileData.Key;
225+
attrs.hash = hashString(data.body.toString());
221226
}
222227
else {
223228
fileKey = fileData.Key;
@@ -231,9 +236,7 @@ InfraCodeVersionSchema.methods.createFs = function (data, cb) {
231236
$push: {
232237
files: fileData
233238
},
234-
$set: {
235-
edited: true
236-
}
239+
$set: attrs
237240
}, function (err, numUpdated) {
238241
if (err) {
239242
cb(err);
@@ -287,6 +290,7 @@ InfraCodeVersionSchema.methods.updateFile = function (fullpath, body, cb) {
287290
}, {
288291
$set: {
289292
'files.$': file.toJSON(),
293+
'hash': hashString(body.toString()),
290294
edited: true
291295
}
292296
}, function (err) {
@@ -546,4 +550,7 @@ InfraCodeVersionSchema.methods.copyFilesFromSource = function (sourceInfraCodeVe
546550
}
547551
};
548552

553+
function hashString(data) {
554+
return crypto.createHash('md5').update(data.toString().trim()).digest('hex');
555+
}
549556
var InfraCodeVersion = module.exports = mongoose.model('InfraCodeVersion', InfraCodeVersionSchema);

lib/models/mongo/schemas/infra-code-version.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ var InfraCodeVersionSchema = new Schema({
103103
'default': Date.now,
104104
index: true,
105105
validate: validators.beforeNow({model: 'ContextVersion', literal: 'Created'})
106-
}
106+
},
107+
hash: {
108+
type: String
109+
},
107110
});
108111

109112
extend(InfraCodeVersionSchema.methods, BaseSchema.methods);

0 commit comments

Comments
 (0)