def update_image_hash(self, variable_name, variable_value, ipfs_hash_list): # 测试参数是否合法 if variable_value == '': # 如果没有相应参数则返回错误,JSON 格式 request_error = RequestError( variable_name).required_parameter_not_found() return jsonify({'msg': request_error}), 400 elif ipfs_hash_list is None: request_error = RequestError( 'ipfs_hash_list').required_parameter_not_found() return jsonify({'msg': request_error}), 400 elif not all(isinstance(elem, str) for elem in ipfs_hash_list): request_error = RequestError( 'ipfs_hash_list').required_parameter_not_found() return jsonify({'msg': request_error}), 400 if isinstance(variable_value, int): variable_value = str(variable_value) result = self.connection.Gallery.update_one( {variable_name: variable_value}, {"$set": { "ipfs_image_list": ipfs_hash_list }}) if result.matched_count == 0: # 如果没有找到相应记录则返回错误,JSON 格式 request_error = RequestError().record_not_found() return jsonify({'msg': request_error}), 400 return jsonify({'success': True})
def upload_directly(folder_id, order_number): print(request.headers) if not current_user_is_admin(): return jsonify({'msg': RequestError.admin_permission_required()}), 400 if 'file' not in request.files: return jsonify({'msg': RequestError.no_file_uploaded()}), 400 file = request.files['file'] if file.filename == '' or not file: return jsonify({'msg': RequestError.no_file_uploaded()}), 400 expected_sha_1, file_extension = None, None if request.form: expected_sha_1 = request.form.get('sha1', None) # Optionally check sha1 of the image. file_extension = request.form.get('extension', None) return save_img.save_img(file, folder_id, order_number, expected_sha_1, file_extension)
def test_get_detail_no_gid(self): # 获取作品详细信息 # 测试不提供Gid invalid_gid = '' rv = self.app.get('/v1/view/getDetail?gid=' + invalid_gid) json_response = rv.json assert self.assertRaises(HTTPException) assert json_response['msg'] == RequestError().no_unique_parameter() # 必须返回"参数未找到"
def set_IPFS_image_hash_by_gid(): request_json = request.get_json(force=True, silent=True) or {} ipfs_hash_list = request_json.get('ipfs_hash_list', None) #: 需要添加的 IPFS Hash 列表, list 格式 gid = request_json.get('gid', '') #: 作品的Ex Gid if gid: result = IPFSHash().update_image_hash('ex.gid', gid, ipfs_hash_list) return result hash_id = request_json.get('id', '') #: 作品的数据库Hash id if hash_id: try: hash_id = ObjectId(hash_id) result = IPFSHash().update_image_hash('_id', hash_id, ipfs_hash_list) return result except InvalidId: return jsonify({'msg': RequestError().invalid_hash_id()}), 400 return jsonify({'msg': RequestError().no_unique_parameter()}), 400
def test_update_ipfs_folder_hash_no_gid(self): # 上传 IPFS 资料夹 Hash # 测试不提供 Gid ipfs_hash = hex(random.getrandbits(128)) rv = self.app.post('/v1/upload/setIPFSFolderHash', json={'ipfs_hash': ipfs_hash}) json_response = rv.json assert self.assertRaises(HTTPException) assert json_response['msg'] == RequestError().no_unique_parameter( ) # 必须返回"参数未找到"
def test_update_ipfs_folder_hash_no_hash(self): # 上传 IPFS 图片 Hash 列表 # 测试不提供 IPFS Hash invalid_gid = '99999999999' rv = self.app.post('/v1/upload/setIPFSFolderHash', json={'gid': invalid_gid}) json_response = rv.json assert self.assertRaises(HTTPException) assert json_response['msg'] == RequestError( 'ipfs_hash').required_parameter_not_found() # 必须返回"参数未找到"
def save_img(img, folder: str, order: str, expected_sha_1: str =None, file_extension: str = None): hash_id = ObjectId(folder) result: dict = get_detail_common.get_detail_raw('_id', hash_id) if not result: # 如果Object ID不合法或不存在 return jsonify({'msg': RequestError.invalid_hash_id()}), 400 expected_file_count: int = int(result['file_count']) order_int = int(order) if order_int is None: return jsonify({'msg': RequestError('Order number').parameter_invalid()}), 400 dir_path = Config.UPLOAD_FOLDER + "/" + folder if not os.path.exists(dir_path): os.makedirs(dir_path) if file_extension and file_extension.lower() in ['jpg', 'png', 'gif']: filename = order + file_extension else: filename = order + '.jpg' img_path = os.path.join(dir_path, filename) img.save(img_path) if expected_sha_1: found_sha1 = sha1(img_path) if found_sha1 != expected_sha_1: return jsonify({'msg': 'Sha1 does not match'}), 400 # print('SHA1 check passed.') if len(glob.glob1(dir_path, '*')) == expected_file_count: result_update: UpdateResult = Connect.get_connection().Gallery.update_one( {'_id': hash_id}, {"$set": {"file_count_matches": True}}) else: result_update: UpdateResult = Connect.get_connection().Gallery.update_one( {'_id': hash_id}, {"$set": {"file_count_matches": False}}) Connect.get_connection().Gallery.update_one( {'_id': hash_id}, {"$set": {"uploaded": True}}) print(result_update.modified_count) if order_int >= expected_file_count: return jsonify({'msg': RequestError.file_number_too_big()}), 200 return jsonify({'msg': 'Img successfully uploaded'}), 200
def test_update_ipfs_folder_hash_non_exist_gid(self): # 上传 IPFS 资料夹 Hash # 测试一个不存在的Gid invalid_gid = '99999999999' ipfs_hash = hex(random.getrandbits(128)) rv = self.app.post('/v1/upload/setIPFSFolderHash', json={ 'gid': invalid_gid, 'ipfs_hash': ipfs_hash }) json_response = rv.json assert self.assertRaises(HTTPException) assert json_response['msg'] == RequestError().record_not_found()
def test_update_ipfs_folder_hash_via_illegal_hash_id(self): # 上传 IPFS 资料夹 Hash # 测试一个非法的Hash ID valid_hash_id = '2333' ipfs_hash = hex(random.getrandbits(128)) rv = self.app.post('/v1/upload/setIPFSFolderHash', json={ 'id': valid_hash_id, 'ipfs_hash': ipfs_hash }) json_response = rv.json assert self.assertRaises(HTTPException) assert json_response['msg'] == RequestError().invalid_hash_id()
def test_update_ipfs_folder_hash_via_invalid_hash_id(self): # 上传 IPFS 资料夹 Hash # 测试一个不存在的Hash ID valid_hash_id = '9d43fcd769ada8455ce26774' ipfs_hash = hex(random.getrandbits(128)) rv = self.app.post('/v1/upload/setIPFSFolderHash', json={ 'id': valid_hash_id, 'ipfs_hash': ipfs_hash }) json_response = rv.json assert self.assertRaises(HTTPException) assert json_response['msg'] == RequestError().record_not_found()
def get_detail(): gid = request.args.get('gid', '') if gid: return get_detail_common.get_detail_common('ex.gid', gid) hash_id = request.args.get('id', '') if hash_id: hash_id = ObjectId(hash_id) return get_detail_common.get_detail_common('_id', hash_id) title = request.args.get('title', '') if title: return get_detail_common.get_detail_common('title', title) japan_title = request.args.get('japanTitle', '') if japan_title: return get_detail_common.get_detail_common('japan_title', japan_title) return jsonify({'msg': RequestError().no_unique_parameter()})
def upload_records(): request_json: dict = request.get_json(force=True, silent=True) or {} if len(request_json) == 0: return jsonify({'msg': RequestError('json').required_parameter_not_found()}) return 'Method not implemented!'