|
import pymongo |
|
import gridfs |
|
|
|
# MongoDB constants |
|
__DATABASE_NAME__ = 'sharexserver' |
|
__UPLOAD_COLLECTION_NAME__ = 'uploads' |
|
__GRID_FS_PREFIX_NAME__ = 'uploads' |
|
# System file constants |
|
__UPLOAD_FILE_DIRECTORY__ = './files/' |
|
|
|
|
|
class EntryConverter(object): |
|
def __init__(self, old_db_mongo_client, new_db_mongo_client): |
|
self.old_db_mongo__client = old_db_mongo_client |
|
self.new_db_mongo_client = new_db_mongo_client |
|
self.grid_fs = gridfs.GridFS(self.new_db_mongo_client[__DATABASE_NAME__], __GRID_FS_PREFIX_NAME__) |
|
|
|
def convert_entry(self, entry): |
|
with self.grid_fs.new_file( |
|
content_type=entry['content_type'], filename=entry['filename'], uploadDate=entry['upload_date'], |
|
metadata=dict(author=entry['author'], call_reference=entry['call_reference']), chunkSize=255000, |
|
) as grid_fs_file, open(__UPLOAD_FILE_DIRECTORY__ + str(entry['_id']), 'rb', 255000) as upload_file: |
|
for line in upload_file: |
|
grid_fs_file.write(line) |
|
|
|
def start_conversion(self): |
|
upload_collection = self.old_db_mongo__client[__DATABASE_NAME__][__UPLOAD_COLLECTION_NAME__] |
|
for entry in upload_collection.find(): |
|
self.convert_entry(entry) |
|
|
|
|
|
if __name__ == '__main__': |
|
old_db_mongo_client = pymongo.MongoClient('localhost', 27017) |
|
new_db_mongo_client = pymongo.MongoClient('localhost', 27018) |
|
entry_converter = EntryConverter(old_db_mongo_client, new_db_mongo_client) |
|
entry_converter.start_conversion() |
|
print "done" |