async def handle_paa_file(upload_data: UploadData, data: bytes, sid: str): with tempfile.TemporaryDirectory() as tmpdir: with tarfile.open(fileobj=io.BytesIO(data), mode="r:bz2") as tar: files = tarfile.TarInfo("files") files.type = tarfile.DIRTYPE # We need to explicitly list our members for security reasons # this is upload data so people could upload malicious stuff that breaks out of the path etc tar.extractall( path=tmpdir, members=get_safe_members(tar.getmembers(), tmpdir) ) tmp_path = Path(tmpdir) for asset in os.listdir(tmp_path / "files"): if not (ASSETS_DIR / asset).exists(): shutil.move(str(tmp_path / "files" / asset), str(ASSETS_DIR / asset)) with open(tmp_path / "data") as json_data: raw_assets: List[AssetDict] = json.load(json_data) user = asset_state.get_user(sid) parent_map: Dict[int, int] = defaultdict(lambda: upload_data["directory"]) for raw_asset in raw_assets: new_asset = Asset.create( name=raw_asset["name"], file_hash=raw_asset["file_hash"], owner=user, parent=parent_map[raw_asset["parent"]], options=raw_asset["options"], ) parent_map[raw_asset["id"]] = new_asset.id await sio.emit( "Asset.Import.Finish", upload_data["name"], room=sid, namespace=ASSET_NS )
async def create_folder(sid: str, data): user = asset_state.get_user(sid) parent = data.get("parent", None) if parent is None: parent = Asset.get_root_folder(user) asset = Asset.create(name=data["name"], owner=user, parent=parent) await sio.emit("Folder.Create", asset.as_dict(), room=sid, namespace=ASSET_NS)
async def create_folder(sid, data): user = state.sid_map[sid]["user"] parent = data.get("parent", None) if parent is None: parent = Asset.get_root_folder(user) asset = Asset.create(name=data["name"], owner=user, parent=parent) await sio.emit("Folder.Create", asset.as_dict(), room=sid, namespace="/pa_assetmgmt")
async def set_asset_options(sid: str, asset_options: AssetOptions): pr: PlayerRoom = game_state.get(sid) if pr.role != Role.DM: logger.warning(f"{pr.player.name} attempted to set asset options") return asset = Asset.get_or_none(id=asset_options["asset"]) if asset is None: asset = Asset.create(name="T", owner=game_state.get_user(sid),) asset.options = asset_options["options"] asset.save()
def add_asset(): asset_info = request.form _is_ok, _errors = Asset.validate(asset_info) if _is_ok: if Asset.create(asset_info): _is_ok = True else: _is_ok = False _errors = {"add": "添加不成功"} return json.dumps({ "is_ok": _is_ok, "success": "添加成功", "errors": _errors, "asset_info": asset_info })
async def handle_regular_file(upload_data: UploadData, data: bytes, sid: str): sh = hashlib.sha1(data) hashname = sh.hexdigest() if not (ASSETS_DIR / hashname).exists(): with open(ASSETS_DIR / hashname, "wb") as f: f.write(data) user = asset_state.get_user(sid) asset = Asset.create( name=upload_data["name"], file_hash=hashname, owner=user, parent=upload_data["directory"], ) await sio.emit("Asset.Upload.Finish", asset.as_dict(), room=sid, namespace=ASSET_NS)
async def assetmgmt_upload(sid, file_data): uuid = file_data["uuid"] if uuid not in state.pending_file_upload_cache: state.pending_file_upload_cache[uuid] = {} state.pending_file_upload_cache[uuid][file_data["slice"]] = file_data if len(state.pending_file_upload_cache[uuid]) != file_data["totalSlices"]: # wait for the rest of the slices return # All slices are present data = b"" for slice_ in range(file_data["totalSlices"]): data += state.pending_file_upload_cache[uuid][slice_]["data"] sh = hashlib.sha1(data) hashname = sh.hexdigest() if not (ASSETS_DIR / hashname).exists(): with open(ASSETS_DIR / hashname, "wb") as f: f.write(data) del state.pending_file_upload_cache[uuid] sid_data = state.sid_map[sid] user = sid_data["user"] asset = Asset.create(name=file_data["name"], file_hash=hashname, owner=user, parent=file_data["directory"]) await sio.emit( "Asset.Upload.Finish", asset.as_dict(), room=sid, namespace="/pa_assetmgmt", )
async def handle_ddraft_file(upload_data: UploadData, data: bytes, sid: str): ddraft_file: DDraftData = json.loads(data) image = base64.b64decode(ddraft_file["image"]) sh = hashlib.sha1(image) hashname = sh.hexdigest() if not (ASSETS_DIR / hashname).exists(): with open(ASSETS_DIR / hashname, "wb") as f: f.write(image) template = { "version": "0", "shape": "assetrect", "templates": { "default": { "options": json.dumps([[f"ddraft_{k}", v] for k, v in ddraft_file.items() if k != "image"]) } }, } user = asset_state.get_user(sid) asset = Asset.create( name=upload_data["name"], file_hash=hashname, owner=user, parent=upload_data["directory"], options=json.dumps(template), ) await sio.emit("Asset.Upload.Finish", asset.as_dict(), room=sid, namespace=ASSET_NS)
def asset_add(): lists = [ 'sn', 'ip', 'hostname', 'idc_id', 'purchase_date', 'warranty', 'vendor', 'model', 'admin', 'business', 'os', 'cpu', 'ram', 'disk' ] asset_dict = {} for i in lists: asset_dict['_' + i] = request.form.get(i, '') # 检查资产信息 _is_ok, _error = Asset.validate_create(asset_dict) _status = None _info = None if _is_ok: if Asset.create(asset_dict): _status, _info = True, '添加资产成功!' else: _status, _info = False, '添加资产失败!' return json.dumps({ 'is_ok': _is_ok, 'status': _status, 'info': _info, 'error': _error })