Commit 581e9c06 authored by Adam Caprez's avatar Adam Caprez
Browse files

Add db function to fetch and save file from GridFS.

parent 3c318e9d
......@@ -7,6 +7,7 @@ import os
import itertools
import chipathlon.conf
from pprint import pprint
import hashlib
class MongoDB(object):
......@@ -228,6 +229,39 @@ class MongoDB(object):
msg = "Experiment with id '%s' does not exist.\n" % (experiment_id,)
return (valid, msg, data)
def fetch_from_gridfs(self, gridfs_id, filename):
"""
:param gridfs_id: GridFS _id of file to get.
:type gridfs_id: bson.objectid.ObjectId
:param filename: Filename to save file to.
:type filename: str
Fetch the file with _id 'gridfs_id' from GridFS and save to the file 'filename'.
"""
gridfs_file = self.gfs.get(gridfs_id)
gridfs_md5 = gridfs_file.md5
try:
output_fh = open(filename,'wb')
except IOError as e:
print "Error creating GridFS output file '%s':" % (filename)
print (e.errno,e.strerror)
sys.exit(1)
hash_md5 = hashlib.md5()
for chunk in gridfs_file:
output_fh.write(chunk)
hash_md5.update(chunk)
output_fh.close()
gridfs_file.close()
if gridfs_md5 == hash_md5.hexdigest():
return True
else:
print "MD5 mismatch saving file from GridFS to '%s'" % (filename)
return False
def get_samples(self, experiment_id):
valid = True
msg = ""
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment