def post(self): """ 创建/更新 obj """ data = request.form.to_dict() name = data.pop("name", None) desc = data.pop("desc", None) md5 = data.pop("md5", None) bucket = data.pop("bucket", None) content = data.pop("content", None) chunk_num = int(data.pop("chunk_num", 1)) total_chunk = int(data.pop("total_chunk", 1)) file_objs = request.files.get("file_name") extra_info = data b = Bucket.get_by_name(bucket) if not b: return APIResponse(code=BUCKET_NOT_FOUND) file_objs = file_objs if isinstance(file_objs, (list, tuple)) else [file_objs] result = [] for file_obj in file_objs: filename = file_obj.filename if file_obj else request.form.get( "filename") if not (content or file_obj): return APIResponse(code=BAD_REQUEST) content = content if content else str(file_obj.read(), encoding="latin-1") if not md5: md5 = content_md5(content.encode("latin-1")) # 需要一个异步 worker 来处理 io. r = hista_save(chunk_num, b.path, content, md5, total_chunk) if not r: return APIResponse(code=OBJECT_SAVE_FAILED) if chunk_num == total_chunk: host = Host.get_host_by_md5(md5) obj = Obj.create_or_update(name=name or md5, bucket=bucket, filename=filename or name, md5_hash=md5, host_id=host.id, desc=desc, extra_info=extra_info) obj = obj.to_json() if obj else obj result.append(obj) else: obj = None return APIResponse(data=result)
def hista_save(chunk_num, bucket_path, content, md5, total_chunk): # 根据文件的哈希值对主机数量取余,选择存放的主机。 host = Host.get_host_by_md5(md5) if host.name == "localhost": return local_save(chunk_num, bucket_path, content, md5, total_chunk) # 使用http发送文件内容,让其他服务器保存 files = {"file": content} wy = winney_api(host.ip_addr) wy.save_file(files=files, data={ "md5": md5, "bucket_path": bucket_path, "chunk_num": chunk_num, "total_chunk": total_chunk }) if wy.ok: return True return False