def post(self): """ @api {POST} /api/example 创建Example @apiName CreateExample @apiGroup Example @apiExample 参数 { "title":"123", } @apiExample 返回值 { "code": 0, "message": null, "data": null } """ params = request.get_json() if not params: return APIResponse(code=BAD_REQUEST) status = self.Model.verify_params(**params) if not status: return APIResponse(code=BAD_REQUEST) status = self.Model.validate(**params) if not status: return APIResponse(code=INVALID_DATA) r = self.Model.create_data(**params) if not r: return APIResponse(code=WRITE_DATA_FAILED) return APIResponse(data=r)
def put(self, uid): """ @api {PUT} /api/example/<uid> 更新Example @apiName UpdateExample @apiGroup Example @apiExample 参数 { "uid":"9d404a96cb664023ac0379d87a1a53c9", "title":"123", } @apiExample 返回值 { "code": 0, "message": null, "data": null } """ params = request.get_json() if not params: return APIResponse(code=BAD_REQUEST) status = self.Model.verify_params(**params) if not status: return APIResponse(code=BAD_REQUEST) r = self.Model.update_by_uid(uid, **params) if not r: return APIResponse(code=OBJECT_NOT_FOUND) return APIResponse(data=r)
def post(self): """ @api {POST} /api/signup 注册账号 @apiName SignUp @apiGroup User @apiExample 参数 { "account":"olivetree", "cipher": "123456" } @apiExample 返回值 { "code": 0, "message": null, "data": null } """ params = request.get_json() if not params: return APIResponse(code=BAD_REQUEST) status = User.is_exists(params["account"]) if status: return APIResponse(code=ACCOUNT_DUPLICATE) cipher = User.cipher_encypt(params["cipher"]) user = User.create_data(account=params["account"], cipher=cipher) return APIResponse(data=user)
def get(self): """ 获取 obj """ name = request.args.get("name") bucket = request.args.get("bucket") if not (name and bucket): return APIResponse(code=BAD_REQUEST) obj = Obj.get_by_name(bucket, name) if not obj: return APIResponse(code=OBJECT_NOT_FOUND) host = Host.get_or_none(Host.id == obj.host_id) if host.ip_addr == "localhost": with open(os.path.join(DATA_PATH, bucket, obj.md5_hash), "rb") as f: content = f.read() else: url = "http://{}:8001/api/file?bucket={}&md5={}".format( host.ip_addr, bucket, obj.md5_hash) r = requests.get(url) content = r.content response = make_response(content) mime_type = mimetypes.guess_type(obj.filename)[0] response.headers["Content-Type"] = mime_type response.headers[ "Content-Disposition"] = "attachment; filename={}".format( obj.filename.encode().decode("latin-1")) return response
def post(self): """ @api {POST} /api/login 登陆 @apiName LogIn @apiGroup User @apiExample 参数 { "account":"olivetree", "cipher": "123456" } @apiExample 返回值 { "code": 0, "message": null, "data": null } """ params = request.get_json() if not params: return APIResponse(code=BAD_REQUEST) status = User.is_exists(params["account"]) if not status: return APIResponse(code=ACCOUNT_NOT_FOUND) user = User.cipher_validate(account=params["account"], cipher=params["cipher"]) if not user: return APIResponse(code=LOGIN_FAILED) user = user.json() user.pop("cipher", None) token = uuid.uuid4().hex user["token"] = token cache.set_token(token, user["uid"]) return APIResponse(data=user)
def delete(self): """ 批量删除 obj """ bucket = request.get_json().get("bucket") name_list = request.get_json().get("name_list") if not (bucket and name_list and isinstance(name_list, (tuple, list))): return APIResponse(code=BAD_REQUEST) Obj.remove_objs(bucket, name_list) return APIResponse()
def get(self): """ 获取 bucket 信息 """ name = request.args.get("name") if not name: return APIResponse(code=BAD_REQUEST) b = Bucket.get_by_name(name) b = b.to_json() if b else None return APIResponse(data=b)
def get(self, uid): """ 获取主机信息 """ host = Host.get_by_uid(uid) host = host.to_json() if host else None return APIResponse(code=0, data=host)
def get(self, uid): """ 获取 obj """ r = Obj.get_by_uid(uid) r = r.to_json() if r else None return APIResponse(data=r)
def get(self): """ 获取主机列表 """ host_list = Host.list() host_list = [host.to_json() for host in host_list] return APIResponse(code=0, data=host_list)
def post(self): """ 添加主机 """ params = request.get_json() if not params: return APIResponse(code=BAD_REQUEST) name = params.get("name") path = params.get("path") port = params.get("port") ip_addr = params.get("ip_addr") username = params.get("username") password = params.get("password") host = Host.create_host(name, path, ip_addr, port, username, password) host = host.to_json() if host else None return APIResponse(code=0, data=host)
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 put(self): """ 更新 bucket """ data = request.get_json() name = data.pop("name", None) desc = data.pop("desc", None) public = data.pop("public", 0) extra_info = data if not (name and public in [0, 1, None]): return APIResponse(code=BAD_REQUEST) b = Bucket.renew(name=name, public=public, desc=desc, extra_info=extra_info) r = b.to_json() if b else None return APIResponse(data=r)
def delete(self, uid): """ @api {DELETE} /api/example/<uid> 删除Example @apiName DeleteExample @apiGroup Example @apiExample 返回值 { "code": 0, "message": null, "data":null } """ r = self.Model.remove(uid) if not r: return APIResponse(code=OBJECT_NOT_FOUND) return APIResponse()
def post(self): # 存储文件 md5 = request.form.get("md5") bucket_path = request.form.get("bucket_path") chunk_num = int(request.form.get("chunk_num", 1)) total_chunk = int(request.form.get("total_chunk", 1)) f = request.files["file"] local_save(chunk_num, bucket_path, f.read(), md5, total_chunk) return APIResponse()
def get(self): """ 获取 bucket 列表 """ data = request.args.to_dict() status = data.pop("status", True) bks = Bucket.filter_bucket(status=status, **data) bks = [b.to_json() for b in bks] return APIResponse(data=bks)
def get(self): """ 获取 obj 列表 """ args = request.args.to_dict() bucket = args.pop("bucket") extra_info = args objs = Obj.list_obj(bucket, **extra_info) objs = [obj.to_json() for obj in objs] if objs else None return APIResponse(data=objs)
def post(self): """ 创建 bucket """ data = request.get_json() name = data.pop("name", None) desc = data.pop("desc", None) public = data.pop("public", 0) extra_info = data if extra_info and not isinstance(extra_info, dict): return APIResponse(code=BAD_REQUEST) if not (name and public in [0, 1]): return APIResponse(code=BAD_REQUEST) b = Bucket.add(name=name, public=public, desc=desc, extra_info=extra_info) r = b.to_json() if b else None return APIResponse(data=r)
def get(self, uid): """ @api {GET} /api/example/<uid> 获取Example @apiName GetExample @apiGroup Example @apiExample 返回值 { "code": 0, "message": null, "data": null } """ r = self.Model.get_with_uid(uid) return APIResponse(data=r)
def delete(self, uid): """ 删除 obj """ obj = Obj.remove(uid) return APIResponse()
def get(self): return APIResponse(code=0)
def delete(self, uid): """ 删除 bucket """ Bucket.remove(uid) return APIResponse()