From 4c2c4b87c26ad3b0358ee6be0a6de0011b6e6348 Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Tue, 22 Oct 2013 11:12:06 +0400 Subject: [PATCH] calculating sha1sum of uploaded tarballs --- lib/local-storage.js | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/local-storage.js b/lib/local-storage.js index 230a42403..61ad52c2c 100644 --- a/lib/local-storage.js +++ b/lib/local-storage.js @@ -160,20 +160,23 @@ Storage.prototype.add_version = function(name, version, metadata, tag, callback) } Storage.prototype.add_tarball = function(name, filename) { - var stream = new mystreams.UploadTarballStream(); - var _transform = stream._transform; - var length = 0; + var stream = new mystreams.UploadTarballStream() + , _transform = stream._transform + , length = 0 + , shasum = crypto.createHash('sha1') + stream._transform = function(data) { - length += data.length; - _transform.apply(stream, arguments); - }; + shasum.update(data) + length += data.length + _transform.apply(stream, arguments) + } - var self = this; + var self = this if (name === info_file || name === '__proto__') { stream.emit('error', new UError({ status: 403, msg: 'can\'t use this filename' - })); + })) } var wstream = this.storage.write_stream(name + '/' + filename); @@ -201,14 +204,24 @@ Storage.prototype.add_tarball = function(name, filename) { wstream.on('open', function() { // re-emitting open because it's handled in storage.js stream.emit('open'); - }); + }) wstream.on('success', function() { - // re-emitting open because it's handled in index.js - stream.emit('success'); - }); + self.update_package(name, function updater(data, cb) { + data._attachments[filename] = { + shasum: shasum.digest('hex'), + } + cb() + }, function(err) { + if (err) { + stream.emit('error', err) + } else { + stream.emit('success') + } + }) + }) stream.abort = function() { - wstream.abort(); - }; + wstream.abort() + } stream.done = function() { if (!length) { stream.emit('error', new UError({