def get_rec(file_hash, file_name, version): """Download route.""" s3_client = boto3.client('s3') input_file = io.BytesIO() parts = path_components(file_hash) + [file_hash + '.mgc'] s3_client.download_fileobj(S3_BUCKET, os.path.join(*parts), input_file) input_file.seek(0) mgz_bytes = decompress(input_file, version=version) return mgz_bytes
def get_zip(file_hash, file_name, version): """Download route.""" s3_client = boto3.client('s3') input_file = io.BytesIO() parts = path_components(file_hash) + [file_hash + '.mgc'] s3_client.download_fileobj(S3_BUCKET, os.path.join(*parts), input_file) input_file.seek(0) mgz_bytes = decompress(input_file, version=version) output_file = io.BytesIO() with ZipFile(output_file, 'w', ZIP_DEFLATED) as zip_file: zip_file.writestr(file_name, mgz_bytes) return output_file.getvalue()
def add_rec(filename, contents, database_url, voobly_username, voobly_password): """Upload recorded game files.""" s3_client = boto3.client('s3') sessions = platforms.factory(voobly_username=voobly_username, voobly_password=voobly_password) with tempfile.TemporaryDirectory() as rec_path: path = os.path.join(rec_path, filename) with open(path, 'wb') as rec: rec.write(contents) with tempfile.TemporaryDirectory() as store_path: file_hash, payload = API(database_url, store_path, sessions).add_file(path, 'upload') if file_hash is False: with open(path, 'rb') as rec: s3_client.upload_fileobj(rec, S3_BUCKET_ERRORS, filename) return dict(success=False, message=payload) if file_hash is not None: object_path = os.path.join(*path_components(file_hash) + [file_hash + '.mgc']) with open(os.path.join(store_path, object_path), 'rb') as data: s3_client.upload_fileobj(data, S3_BUCKET, object_path) return dict(success=True, match_id=payload)