def add(self, request_data): data = dict() file_upload = '' if request_data.files: file_upload = request_data.files['file'] if file_upload: file_name = file_upload.filename curr_app = current_app._get_current_object() path = curr_app.config.get("DOC_PATH_ROOT") if not os.path.exists(path): os.makedirs(path) path = os.path.join(path, file_name) if os.path.exists(path): current_app.logger.error('文件已经存在! %s' % file_name) return False, u'文件已经存在!' file_upload.save(path) # 保存上传的文件 path = Utillity.convert_url( curr_app.config.get('FILE_SRV_URL'), path) data["doc_type"] = DOC_TYPE_FILE data["doc_title"] = request_data.form.get('doc_title') # data["doc_type"] = request_data.form.get('doc_type') data["ver"] = request_data.form.get('ver') data["author"] = request_data.form.get('author') # 作者', data["committer"] = request_data.form.get('committer') # 提交者' data["tags"] = request_data.form.get('tags') if data["tags"]: data["tags"] = data["tags"].split(',') data["summary"] = request_data.form.get("summary") data["keywords"] = request_data.form.get("keywords") else: return False, u'缺少文件!' else: data = request_data.get_json() data["doc_type"] = DOC_TYPE_URL if 'Tags' in data: data["tags"] = data.get("Tags") path = data.get("path") if not path: data["doc_type"] = DOC_TYPE_TEXT content = data.get("content") if not content: return False, u'请指定Url或上传文件或输入文本!' path = content try: if 'path' in data: data.pop("path") if 'file' in data: data.pop('file') data["content"] = path data["update_time"] = self.get_current_time() data["create_time"] = data["update_time"] self._common_add(data, data["update_time"]) return True, 'OK' except Exception as e: # current_app.logger.info('DownLoad file path=%s, %s' % (path_info, file_name)) db.session.rollback() current_app.logger.error('%s' % e) if file_upload: os.remove(path) # 删除文件 return False, "服务异常!请联系管理员!"
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 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 update(self, request_data): data = dict() need_del_old_file = False if request_data.files: file_upload = request_data.files['file'] if file_upload: data["doc_id"] = request_data.form.get('doc_id') old_doc_info = self.get_by_key_id(data.get('doc_id')) if not old_doc_info: return False, u'没有这个文件!' file_name = file_upload.filename curr_app = current_app._get_current_object() path = curr_app.config.get("DOC_PATH_ROOT") if not os.path.exists(path): os.makedirs(path) path = os.path.join(path, file_name) if old_doc_info.get("content") != path and old_doc_info.get( "doc_type") == "file": # 文件变化了, 旧的要删除 need_del_old_file = True file_upload.save(path) # 保存上传的文件 path = Utillity.convert_url( curr_app.config.get('FILE_SRV_URL'), path) data["doc_type"] = DOC_TYPE_FILE data["doc_title"] = request_data.form.get('doc_title') # data["doc_type"] = request_data.form.get('doc_type') data["ver"] = request_data.form.get('ver') data["author"] = request_data.form.get('author') # 作者', data["committer"] = request_data.form.get('committer') # 提交者' data["tags"] = request_data.form.get('tags') if data["tags"]: data["tags"] = data["tags"].split(',') data["summary"] = request_data.form.get("summary") data["keywords"] = request_data.form.get("keywords") else: return False, u'缺少文件!' else: data = request_data.get_json() if 'file_name' in data: data.pop("file_name") old_doc_info = self.get_by_key_id(data.get('doc_id')) if not old_doc_info: return False, u'没有这个文件!' # data["doc_type"] = DOC_TYPE_URL if 'Tags' in data: data["tags"] = data.get("Tags") path = data.get("path") if not path: # data["doc_type"] = DOC_TYPE_TEXT content = data.get("content") # if not content: # return False, u'请指定Url或上传文件或输入文本!' path = content try: if need_del_old_file: # 删除旧的文件 self._delete_file(old_doc_info.get("doc_type"), old_doc_info.get("content")) if 'path' in data: data.pop("path") if 'file' in data: data.pop('file') if path: data["content"] = path else: if 'content' in data: data.pop('content') data["update_time"] = self.get_current_time() # data["create_time"] = data["update_time"] self._common_add(data, data["update_time"], old_doc_info) return True, 'OK' except Exception as e: db.session.rollback() current_app.logger.error('%s' % e) if file_upload: os.remove(path) # 删除文件 return False, "服务异常!请联系管理员!"
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