retrieving all docs from s3

This commit is contained in:
Henrique Dias 2015-06-01 19:36:26 -03:00
parent fa7d3ea857
commit 7c7ff64904
4 changed files with 63 additions and 11 deletions

View file

@ -19,13 +19,14 @@ module.exports = DocManager =
callback null, doc
getAllDocs: (project_id, callback = (error, docs) ->) ->
MongoManager.getProjectsDocs project_id, (error, docs) ->
if err?
return callback(error)
else if !docs?
return callback new Errors.NotFoundError("No docs for project #{project_id}")
else
return callback(null, docs)
DocManager.unArchiveAllDocs project_id, (error) ->
MongoManager.getProjectsDocs project_id, (error, docs) ->
if err?
return callback(error)
else if !docs?
return callback new Errors.NotFoundError("No docs for project #{project_id}")
else
return callback(null, docs)
updateDoc: (project_id, doc_id, lines, callback = (error, modified, rev) ->) ->
MongoManager.findDoc doc_id, (err, doc)->
@ -76,11 +77,40 @@ module.exports = DocManager =
options = buildS3Options(doc.lines, project_id+"/"+doc._id)
request.put options, (err, res)->
if err? || res.statusCode != 200
logger.err err:err, res:res, "something went wrong archiving file in aws"
cb(err)
logger.err err:err, res:res, "something went wrong archiving doc in aws"
cb(err)
MongoManager.markDocAsArchived doc._id, (error) ->
return cb(error) if error?
cb()
async.series jobs, callback
unArchiveAllDocs: (project_id, callback = (error) ->) ->
MongoManager.getProjectsDocs project_id, (error, docs) ->
if err?
return callback(error)
else if !docs?
return callback new Errors.NotFoundError("No docs for project #{project_id}")
jobs = for doc in docs
do (doc) =>
if !doc.inS3?
(cb) => cb()
else
(cb) =>
logger.log project_id: project_id, doc_id: doc._id, "getting doc from s3"
options = buildS3Options(true, project_id+"/"+doc._id)
request.get options, (err, res, lines)->
if err? || res.statusCode != 200
logger.err err:err, res:res, "something went wrong unarchiving doc from aws"
cb(err)
MongoManager.upsertIntoDocCollection project_id, doc._id.toString(), lines, (error) ->
return cb(error) if error?
cb()
async.series jobs, callback
buildS3Options = (content, key)->
return {
aws:

View file

@ -13,9 +13,11 @@ module.exports = MongoManager =
update =
$set:{}
$inc:{}
$unset:{}
update.$set["lines"] = lines
update.$set["project_id"] = ObjectId(project_id)
update.$inc["rev"] = 1 #on new docs being created this will set the rev to 1
update.$unset["inS3"] = true
db.docs.update _id: ObjectId(doc_id), update, {upsert: true}, callback
@ -25,3 +27,12 @@ module.exports = MongoManager =
update.$set["deleted"] = true
db.docs.update _id: ObjectId(doc_id), update, (err)->
callback(err)
markDocAsArchived: (doc_id, callback)->
update =
$set: {}
$unset: {}
update.$set["inS3"] = true
update.$unset["lines"] = true
db.docs.update _id: doc_id, update, (err)->
callback(err)

View file

@ -16,4 +16,5 @@ module.exports =
# user_files: ""
# s3:
# key: ""
# secret: ""
# secret: ""

View file

@ -33,4 +33,14 @@ describe "Archiving all docs", ->
it "should archive all the docs", (done) ->
DocstoreClient.archiveAllDoc @project_id, (error, res) =>
res.statusCode.should.equal 204
done()
done()
it "should unarchive all the docs", (done) ->
DocstoreClient.archiveAllDoc @project_id, (error, res) =>
DocstoreClient.getAllDocs @project_id, (error, res, docs) =>
throw error if error?
docs.length.should.equal @docs.length
for doc, i in docs
doc.lines.should.deep.equal @docs[i].lines
done()