def post(self): """上传图片 :return: """ result = {"result": "OK", "content": '', 'error': ''} try: file_upload = request.files['file'] file_name = file_upload.filename # file_path = os.path.join('data', 'Image') curr_app = current_app._get_current_object() file_path = curr_app.config.get("IMG_PATH_ROOT") if not os.path.exists(file_path): os.mkdir(file_path) uti = Utillity() only_id = uti.get_nextval() new_file_name = Utillity.get_new_file_name(file_name, only_id) # new_file_name = '%s-%s' % (str(only_id), file_name) file_upload.save(os.path.join(file_path, new_file_name)) file_url = os.path.join(file_path, new_file_name) file_url = Utillity.convert_url( curr_app.config.get('FILE_SRV_URL'), file_url) result["content"] = file_url except Exception as e: current_app.logger.error('%s' % e) result["error"] = str(e) result['result'] = "NG" finally: return result
def replace_image_url(self, image_path): curr_app = current_app._get_current_object() if ".emf" in image_path: image_path = self.emf_to_jpeg(image_path) image_name = os.path.basename(image_path) uti = Utillity() only_id = uti.get_nextval() new_image_name = Utillity.get_new_file_name(image_name, only_id) new_image_url = os.path.join(curr_app.config.get("IMG_PATH_ROOT"), new_image_name) shutil.copyfile(image_path, new_image_url) image_url = Utillity.convert_url(curr_app.config.get('FILE_SRV_URL'), new_image_url) return image_url
def do_import(self, request_data): if not request_data.files: return False, '没有上传文件!' file_upload = request_data.files['file'] if not file_upload: return False, '没有指定上传的文件!' file_name = file_upload.filename # if "&" in file_name: # return False, '文件名不能含有&符号!' only_id = Utillity.get_nextval() new_file_name = Utillity.get_new_file_name(file_name, only_id) only_file_name = os.path.splitext(new_file_name)[0] path = os.path.join("import_root", only_file_name) if not os.path.exists(path): os.makedirs(path) file_url = os.path.join(path, file_name) file_upload.save(file_url) proj_id = request_data.form.get('proj_id') model_id = request_data.form.get('model_id') creator = request_data.form.get('creator') doc_type = request_data.form.get('doc_type') import_obj = ImportDocBase() try: json_url, error = import_obj.excel_to_json(file_url) if error: return False, error commit_list, error = import_obj.read_json(json_url, model_id, creator, doc_type, proj_id) if error: db.session.rollback() return False, error self.commit_log(commit_list, creator, self.get_current_time()) db.session.commit() from app.db import cache cache.delete('get_model_tree') # 删除缓存 return True, None except Exception as e: db.session.rollback() current_app.logger.error('%s' % e) return False, "服务异常!请联系管理员!"
def add(self, request_data, commit=True): if not request_data.files: return False, '没有上传文件!' file_upload = request_data.files['file'] if not file_upload: return False, '没有指定上传的文件!' file_name = file_upload.filename curr_app = current_app._get_current_object() file_path = curr_app.config.get("ATTACH_PATH_ROOT") # file_path = os.path.join('data', 'Astah') if not os.path.exists(file_path): os.mkdir(file_path) only_id = Utillity().get_nextval() new_file_name = Utillity.get_new_file_name(file_name, only_id) fname, ext_name = Utillity.split_ext_name(file_name) fname = fname.rstrip('.') file_upload.save(os.path.join(file_path, new_file_name)) file_url = os.path.join(file_path, new_file_name) file_url = Utillity.convert_url(curr_app.config.get('FILE_SRV_URL'), file_url) update_time = datetime.datetime.now() commit_time = update_time attach_info = { DSAttach.file_name.name: file_name, DSAttach.file_type.name: ext_name, DSAttach.file_url.name: file_url, DSAttach.update_time.name: update_time, DSAttach.commit_time.name: commit_time } attach = DSAttach(**attach_info) db.session.add(attach) if commit: db.session.commit() else: db.session.flush() return True, attach.to_dict()
def add(self, request_data): from app.ctrl.ctrl_ds_doc import CtrlDsDoc if not request_data.files: return False, '没有上传文件!' file_upload = request_data.files['file'] if not file_upload: return False, '没有指定上传的文件!' doc_id = request_data.form.get('doc_id') doc_data, error = CtrlDsDoc().ds_doc_exist(doc_id) if error: return False, error file_name = file_upload.filename curr_app = current_app._get_current_object() file_path = curr_app.config.get("ATTACH_PATH_ROOT") # file_path = os.path.join('data', 'Astah') if not os.path.exists(file_path): os.mkdir(file_path) fname, ext_name = Utillity.split_ext_name(file_name) # TODO@hcz: 所有都可以 # if not ext_name or ext_name.lower() not in (".asta", ".jude"): # return False, '文件扩展名不对,请上传".asta"或".jude文件"' fname = fname.rstrip('.') # 获取新的文件名称 only_id = Utillity().get_nextval() new_file_name = Utillity.get_new_file_name(file_name, only_id) file_upload.save(os.path.join(file_path, new_file_name)) file_url = os.path.join(file_path, new_file_name) file_url = Utillity.convert_url(curr_app.config.get('FILE_SRV_URL'), file_url) attach_id = request_data.form.get("attach_id") committer = request_data.form.get("committer") # 提交者' ver = request_data.form.get("ver") update_time = datetime.datetime.now() commit_time = update_time attach_info = { DSAttach.attach_id.name: attach_id, DSAttach.file_name.name: file_name, DSAttach.file_type.name: ext_name, DSAttach.file_url.name: file_url, DSAttach.update_time.name: update_time, DSAttach.committer.name: committer, DSAttach.ver.name: ver } commit_list = [] if attach_id: # 更新 log_dict, attach_info = CtrlDSAttach().update(attach_id, attach_info, commit=False) if log_dict: commit_list.append(log_dict) else: # 新增 # 添加附件 attach_info[DSAttach.commit_time.name] = commit_time attach_log = CtrlDSAttach().add2(attach_info, commit=False) attach_id = attach_log.get('key_id') attach_info[DSAttach.attach_id.name] = attach_id attach_info[DSAttach.commit_time.name] = self.time_to_str( commit_time) attach_info[DSAttach.update_time.name] = self.time_to_str( commit_time) astah_rel_info = { DSDocAstahRel.doc_id.name: doc_id, DSDocAstahRel.attach_id.name: attach_id } astah_rel_log = self.common_add(DSDocAstahRel, astah_rel_info, None, [], DSDocAstahRel.gid) commit_list.append(attach_log) commit_list.append(astah_rel_log) if commit_list: log_doc = CtrlDsDoc().update_ver(doc_id, committer) commit_list.append(log_doc) self.commit_log(commit_list, committer, update_time) db.session.commit() return True, attach_info