def copy_apk(self, apk, file_like_obj, **kwargs): ''' See doc of :py:meth:`.ApkCopyInterface.copy_apk`. Inserts the apk from the `file_like_obj` into mongodb's gridfs, but only if not already in db. Returns ------- The id of the apk (in db) ''' file_like_obj.seek(0) try: gridfs = self.__apk_coll # escape keys accoring to mongodb rules apk_meta = escape_keys(apk.meta_dict()) _id = apk.hash # gridfs doesn't have an update method -> delete and insert if not gridfs.exists(**{RESOBJ_ID : _id}): # store file together with metadata filename = os.path.basename(apk_meta[RESOBJ_APK_META][RESOBJ_APK_META_PATH]) gridfs.put(file_like_obj, metadata = apk_meta[RESOBJ_APK_META], filename = filename, _id = _id, chunkSize = MAX_BSON_SIZE) log.info("put %s into %s", apk.short_description(), self) except (PyMongoError, BSONError) as e: raise DatabaseStoreException(self, "apk: %s" % apk.short_description(), caused_by = e), None, sys.exc_info()[2] # return id return _id
def store_result_for_apk(self, apk, script): ''' See doc of :py:meth:`.ResultWritingInterface.store_result_for_apk`. Returns ------- tuple<str, bool> First component is the id of the entry and the second a boolean indication if the result has been stored in gridfs. None If an error occurred. ''' try: # escape keys for mongodb insert res_obj_dict = escape_keys(script.result_dict(gen_id = False)) _id = script.gen_unique_id() # if data is to big or custom result object used -> store with gridfs if script.uses_custom_result_object() or script.is_big_res(): log.debug("storing results for %s, %s in %s (id: %s)", apk.short_description(), script, self.grid_fs, _id) result = self.get_custom_res_obj_representation(script) gridfs = self.grid_fs # gridfs doesn't have an update method -> delete and insert if gridfs.exists(**{RESOBJ_ID : _id}): # delete by _id gridfs.delete(_id) # store file together with metadata from `ResultObject` gridfs.put(result, metadata = res_obj_dict, filename = script.get_file_name(), _id = _id) # return id return _id, True # normal json data else: log.debug("storing results for %s, %s in %s db(id: %s)", apk.short_description(), script, self.res_coll, _id) # set id so we don't have multiple results for same script and apk res_obj_dict[RESOBJ_ID] = _id # update or insert if not existing self.res_coll.update({RESOBJ_ID : _id}, res_obj_dict, upsert = True) # return id return _id, False except (PyMongoError, BSONError) as e: raise DatabaseStoreException(self, "script: %s" % script, caused_by = e), None, sys.exc_info()[2]
def put_gridfs(name, mime_type, gridfs): gridfs.put(get_image_bytes(name), content_type=mime_type)
}) db.questions.insert({ "question_number": 2, "question": "What is BPHC?", "choices": { "a": "Boston Public Health Commission", }, "answer": "a", }) wn_card0 = { "_id": ObjectId(), "background": gridfs.put(get_image_bytes("mongodb.png"), content_type="image/png"), "caption": "This is in mongo", "sub_caption": "This is the sub-caption", "hyperlink": "example.com" } wn_card1 = { "_id": ObjectId(), "background": gridfs.put(get_image_bytes("FlaskLogo.png"), content_type="image/png"), "caption": "This is a flask app", "sub_caption":