def process_download_request(self, json_data): """ 下载文件 :param json_data: 参看api.image#download_image 参数说明 :return: """ try: id = self._get_json_param(json_data, "id") url = self._get_json_param(json_data, "url") priority = self._get_json_param(json_data, "priority", nullable=True, default=5) client_name = self._get_json_param(json_data, "client_name") source_name = self._get_json_param(json_data, "source_name") filename = self._get_json_param(json_data, "filename", nullable=True) callback = self._get_json_param(json_data, "callback") log.info("处理下载图像数据请求: 参数解析完成. client_name: %s , id: %s ", client_name, id) except: raise # 添加到下载任务 log.info("处理下载图像数据请求: 添加【下载图像】任务到队列. client_name: %s , id: %s ", client_name, id) download_file.delay(DownloadTaskType.IMAGE.value, id, url, client_name, source_name, callback, filename)
def get_token_handler(): """ 获取新 token :return: """ from vanaspyhelper.util.request import vanas_get_token try: client_id = current_app.conifg['CLIENT_ID'] secrect_key = current_app.conifg['CLIENT_SECRET_KEY'] try: res = vanas_get_token(client_id, secrect_key) if res['success']: current_app.config['access_token'] = res['data'][ 'access_token'] log.info("获取 token。 access_token: %s", current_app.config['access_token']) else: log.error("获取 token。 失败! result : %s", res) except: log.exception("获取 token,获取 vanas_get_token 错误!", exc_info=True) except: log.exception("获取 token,获取 Client_id, secrect_key 错误!", exc_info=True)
def download_file_as_stream(stream_data , filepath:str , filename:str, retry=3, timeout=600): """ 下载文件按进度,按流处理 :param stream_data: request 请求回来的 content 及 下载 url 【元祖】 :param filepath: 下载路径,包含文件名 :param retry: 重试次数 默认 3 :parma timeout: 超时时间 默认 60 秒 :return: """ from vanaspyhelper.util.file import removeFile stream,url = stream_data chunk_size = 1024 # 单次请求最大值 total_size = int(stream.headers['Content-Length']) # 内容体总大小 processed = 0 start_time = time.time() with open(filepath, 'wb') as file: try: for data in stream.iter_content(chunk_size): # 设置 300 秒超时 if time.time() - start_time > timeout: # 设置为超时 raise FileDownloadSaveTimeOutError(url) processed += chunk_size file.write(data) file.flush() end_time = time.time() - start_time log.info("下载文件 {} 完成,大小:[{}] kb, 耗时:[{}] s".format(filename,str(total_size/1024), str(end_time))) except FileDownloadSaveTimeOutError: # 超时异常,到需要重新下载 # 删除文件 removeFile(filepath) if retry > 0: retry -= retry log.warning("重新根据文件流下载文件到本地 ! URL:[{}] , Local_save:[{}]".format(url,filepath)) download_file_as_stream(stream_data, filepath , filename, retry, timeout) else: # 3次重试失败 raise FileDownloadSaveTimeOutError(url) finally: # 关闭文件流 file.close()
def _jwks(): """ 返回签名 公钥 :return: json """ log.info("处理请求:获取公钥") key = { "alg": current_app.config['JWT_ALGORITHM'], # 算法 "e": "AQAB", "n": get_pub_key(), # 公钥 "kty": "RSA", "use": "Signature" # 用途 } log.info("处理请求:获取公钥处理完成") return render_json(key)
def verify_token_data(): """ 验证 token :return: """ try: # 报文要用 json报文,使用 双引号 " data = request.json log.info("处理请求:验证 token . Data: %s ", data) token = data['access_token'] audience = data.get('client_id', '') res_json = verify_token(token,audience) log.info("处理请求:验证 token . Result: %s ", res_json) return render_json(res_json) except Exception as e: log.error("处理请求:验证 token . Error: %s ", str(e)) return E400(str(e))
def generate_token(): """ 生成 token :return: json """ try: data = request.json log.info("处理请求:创建 token . Data: %s ",data) grant_type = data.get('grant_type', '') client_id = data.get('client_id', '') signature = data.get('signature', '') timestamp = int(data.get('timestamp', '0')) # 封装 token res_json = create_token(client_id ,signature, timestamp, grant_type) log.info("处理请求:创建 token . Result: %s ", res_json) return render_json(res_json) except Exception as e3: log.error("处理请求:创建 token . Error: %s ", str(e3)) return E400(str(e3))