def file_path(self): """ 图片文件路径 :return: """ from models.file import FileModel file_model = FileModel().dao_get(self.file_id) # type: FileModel return file_model.file_path if is_not_empty(file_model) else None
def file_md5(self): """ 图片文件md5值 :return: """ from models.file import FileModel file_model = FileModel().dao_get(self.file_id) # type: FileModel return file_model.md5_id if is_not_empty(file_model) else None
def file_data(self): """ 图片文件的base64字符 :return: """ from models.file import FileModel from utils.encodes import file_to_base64 file_model = FileModel().dao_get(self.file_id) # type: FileModel file_path = file_model.file_path return file_to_base64(file_path) if os.path.isfile(file_path) else None
def dao_get_source_files(self, id): """ 查询流水源文件 :param id: :return: """ return FileModel().query.\ join(ImgDetailModel, ImgDetailModel.parent_file_id == FileModel.id).\ join(ImgDataModel, ImgDataModel.id == ImgDetailModel.img_data_id).\ filter(ImgDataModel.id == id).\ distinct().all()
def post(cls): '''Upload user submission''' try: user_mail = get_jwt_identity() # check current submission counts daily_counts = FileModel.get_interval_upload_count_by_mail( email=user_mail, AOEtime=get_AOE_today(to_str=False)) weekly_counts = FileModel.get_interval_upload_count_by_mail( email=user_mail, AOEtime=get_AOE_week(to_str=False)) if (daily_counts >= configs["DAILY_SUBMIT_LIMIT"]) or (weekly_counts >= configs["WEEKLY_SUBMIT_LIMIT"]): return {"message": f"Exceed Submission Limit: You have submitted {daily_counts} times today and {weekly_counts} times this week."}, HTTPStatus.FORBIDDEN # file validation file = request.files['file'] if file.filename == "": return {"message": "No file selected."}, HTTPStatus.FORBIDDEN if not file_upload.zipfile_check(file): return {"message": "Wrong file format."}, HTTPStatus.FORBIDDEN # load form data formData = publicFormSchema.load(request.form) # get file path upload_count = FileModel.get_upload_count_by_mail( email=user_mail) + 1 folder = file_upload.create_folder(user_mail, str(upload_count)) file_path = file_upload.get_full_path(folder, file.filename) # add column for db formData.update({"email": user_mail, "filePath": file_path}) fileObj = FileModel(**formData) scoreObj = ScoreModel() fileObj.scores.append(scoreObj) fileObj.save_to_db() try: file.save(file_path) # start processing thread = Thread(target=metric_calculate_pipeline, kwargs={"file_path": file_path, "submitUUID": formData["submitUUID"]}) thread.start() return {"message": "Upload successfully!"}, HTTPStatus.OK except Exception as e: fileObj.delete_from_db() # Rollback return {"message": "Internal Server Error!"}, HTTPStatus.INTERNAL_SERVER_ERROR except ValidationError as e: return {"message": "There's something worng with your input!"}, HTTPStatus.BAD_REQUEST except Exception as e: print(e) return {"message": "Internal Server Error!"}, HTTPStatus.INTERNAL_SERVER_ERROR
def post(cls): data = _datafile_parser.parse_args() current_user_id = get_jwt_identity() user = UserModel.find_by_id(current_user_id) links = list() total_size = 0 # "files in data[] contains a list/array of files object/dict which contains file name and size for file_obj in json.loads(data["files"]): file = FileModel.find_by_name(name=file_obj["name"], user_id=current_user_id) if file: return {"msg": "A file with that name already exists!"}, 403 if user.files_size + file_obj["size"] >= configs.allowed_size[ user.account_type]: return {"msg": "Not enough space to upload!"}, 403 # links is a list of upload link, so it is appended with link for each file links.append( s3_storage.get_upload_url(file_obj["name"], current_user_id, data["file_path"])) file = FileModel(file_obj["name"], current_user_id, data["file_path"], file_obj["size"]) file.save_to_db() total_size += file_obj["size"] # updating the total size of files the user currently has user.increment_files_size(total_size) return {"links": links}, 200
def dao_add_info(self, loan_dir, subtransactions=False, nested=False): """ 信息入库 :param nested: :param subtransactions: :param loan_dir: 资料目录 :return: """ with db.auto_commit_db(subtransactions, nested, error_call=FileUtil.del_dir, dir_path=loan_dir) as s: s.add(self) # 写入磁盘 file_models = [] for file in self.file_data: file_path = base64_to_file(file.file_base64, loan_dir, file.file_name, file.file_format) # 文件入库 file_model = FileModel() file_model.dao_init_file(file_path, nested=True) file_models.append(file_model) # 图片处理明细信息 details = [] for file_model in file_models: if file_model.file_format.strip().upper( ) == FileFormat.PDF.value: # 文件为pdf, 分割 pdf, 更新资料信息 img_path = FileUtil.path_join( loan_dir, current_app.config.get('DATA_DIR_IMG'), file_model.id) page_num, img_num, fail_num, detail_info = PDFUtil.pdf_to_pic( file_model.file_path, img_path) images = detail_info.get('images', None) if img_num > 0 and is_not_empty(images): for image in images: if is_empty(image.get('error_msg', None)): # 图片文件入库 img_model = FileModel() img_model.dao_init_file(image.get( 'img_path', ''), nested=True) # 图片明细入库 img_detail = ImgDetailModel() img_detail.dao_add(self.id, file_model.id, img_model.id, nested=True) self.page_num += page_num self.success_num += img_num self.fail_num += fail_num details.append( dict(id=file_model.id, md5=file_model.md5_id, page_num=page_num, success_num=img_num, fail_num=fail_num)) else: # 文件备份, 迁移处理文件 new_img_path = FileUtil.copy_file( file_model.file_path, FileUtil.path_join( loan_dir, current_app.config.get('DATA_DIR_IMG'))) # 图片文件入库 img_model = FileModel() img_model.dao_init_file(new_img_path, nested=True) # 文件为图片, 图片明细入库 img_detail = ImgDetailModel() img_detail.dao_add(self.id, file_model.id, img_model.id, nested=True) self.page_num += 1 self.success_num += 1 self.fail_num += 0 details.append( dict(id=file_model.id, md5=file_model.md5_id, page_num=1, success_num=1, fail_num=0)) return dict(id=self.id, details=details)