From 6655d7bbc351e47b7f6500676758a05c8f02cd3b Mon Sep 17 00:00:00 2001 From: Pratham Goenka Date: Sun, 8 Dec 2024 05:45:50 +0530 Subject: [PATCH] feat: Walrus Storage --- .gitignore | 2 + walrus/main.py | 144 ++++++++++++++++++++++++++++++++++++++++ walrus/requirements.txt | Bin 0 -> 1442 bytes 3 files changed, 146 insertions(+) create mode 100644 walrus/main.py create mode 100644 walrus/requirements.txt diff --git a/.gitignore b/.gitignore index 7ec360a..d9c0e54 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ node_modules dist .husky + +env diff --git a/walrus/main.py b/walrus/main.py new file mode 100644 index 0000000..ac0a389 --- /dev/null +++ b/walrus/main.py @@ -0,0 +1,144 @@ +from fastapi import FastAPI, HTTPException, UploadFile, Form +from fastapi.responses import FileResponse, Response, RedirectResponse +import httpx +import subprocess +import random +from pathlib import Path + + +app = FastAPI() + + +# Environment variables for public aggregator and publisher +AGGREGATOR = "https://aggregator.walrus-testnet.walrus.space" +PUBLISHER = "https://publisher.walrus-testnet.walrus.space" + +@app.post("/store") +async def store_image(file: UploadFile, epochs: int = Form(1)): + """ + Store an image in the public publisher. + + Args: + file: The image file to upload. + epochs: The number of epochs to store the blob. + + Returns: + JSON response from the publisher. + """ + try: + # Ensure the file is an image + if not file.content_type.startswith("image/"): + raise HTTPException(status_code=400, detail="Only image files are supported.") + + # Upload the file using a PUT request + url = f"{PUBLISHER}/v1/store?epochs={epochs}" + async with httpx.AsyncClient() as client: + response = await client.put(url, content=await file.read()) + + if response.status_code != 200: + raise HTTPException(status_code=response.status_code, detail=response.text) + + return response.json() + + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) + + +BLOB_IDS = [ + "Up3B5WtHG06f6bb8Qrf2zNNmixKQcEPFM-uJI1EoFzE", + "5I1DYglEocvcA0nX0uPClHEBFFxz4a7YspbtoeihcvI", + "KQ7VGN23gif1in3I0wVETV6tiqtwBmLF1vla8GNauH8", + "KRv8f6lfvd9FxYlpAZ5DhnYdEMfu3yjUl-PtvqBYYSw", + "PAnhlbndMMhGpbGYbJn24mbllXwYKXNzICe5oWACaYQ" +] + + +@app.get("/retrieve/{blob_id}") +async def retrieve_image(blob_id: str): + """ + Retrieve an image from the public aggregator and display it in the browser. + + Args: + blob_id: The ID of the blob to retrieve. + + Returns: + The retrieved image content as a response. + """ + try: + # Define local file path + output_path = Path("./output_image.png") + + # Build the curl command + url = f"{AGGREGATOR}/v1/{blob_id}" + result = subprocess.run( + ["curl", url, "-o", str(output_path)], + capture_output=True, + text=True + ) + + # Check if curl executed successfully + if result.returncode != 0: + raise HTTPException(status_code=500, detail=f"Error with curl: {result.stderr}") + + # Check if the file exists and read its content + if output_path.exists(): + image_content = output_path.read_bytes() + return Response(content=image_content, media_type="image/png") + else: + raise HTTPException(status_code=404, detail="File could not be saved or retrieved.") + + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) + + +@app.get("/retrieverandom") +async def retrieve_random_blob(): + """ + Retrieve a random image from the public aggregator and display it in the browser. + + Returns: + The retrieved random image content as a response. + """ + # try: + # for _ in range(len(BLOB_IDS)): + # # Select a random blob ID from the list + # random_blob_id = random.choice(BLOB_IDS) + + # # Try to retrieve the image using the retrieve_image function + # response = await retrieve_image(blob_id=random_blob_id) + + # # Check if the response has valid content + # if response.body: + # return response + + # # If all blob IDs fail, raise an exception + # raise HTTPException(status_code=404, detail="No valid image found in the random blob IDs.") + + # except Exception as e: + # raise HTTPException(status_code=500, detail=str(e)) + try: + # Define local file path + output_path = Path("./output_image.png") + + # Build the curl command + blob_id = random.choice(BLOB_IDS) + url = f"{AGGREGATOR}/v1/{blob_id}" + result = subprocess.run( + ["curl", url, "-o", str(output_path)], + capture_output=True, + text=True + ) + + # Check if curl executed successfully + if result.returncode != 0: + raise HTTPException(status_code=500, detail=f"Error with curl: {result.stderr}") + + # Check if the file exists and read its content + if output_path.exists(): + image_content = output_path.read_bytes() + return Response(content=image_content, media_type="image/png") + else: + raise HTTPException(status_code=404, detail="File could not be saved or retrieved.") + + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) \ No newline at end of file diff --git a/walrus/requirements.txt b/walrus/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..afc93143e6570c959e370618e3193b1fe8b04965 GIT binary patch literal 1442 zcmY+EL2uJg5QO)P#7}WlJE5fq4qOqHkT`LQQpbr+6Svq7Y54KLH@kkREz3&eot>SX z-S__e-B`=>Y=dohE8AMe@5NsD-`gji8(Y{N?~T2;cc5CL4H=59X0ei-j_k&EyowiO z6^{A{ayfF8R>RZ5UxCiNerHF%9mxCREXrPzqY6>E^k`{M&Mdy1_s&(7=sECqaEK@A zR`#tUwp8iq>O#dDWb2s18B&GHa~qh}_Ip&b<#%xA=x0Gc3BM!O6WzcY{mQGH=fkz` z9Ftjek@N{4`u280Q$t)`Bz1B?m~fzW?v$HPys1$!pdzv>?@jK+>@>H8P%rZ8ZML`I zR_;;4yk7k)Og=~ThTUlCRrRGSAHRP=b!$I~6q8uu)HFI~wx#MBTn1V**CZ0tTZa!L z`b7LY|99-d6jiv+BWx#F4<1vtdX3l%S=y0}?OoU2F|mUQaUoxOtW7P)c=V$km0MCb z#jIz}Y6A*C6SFrFJ**h zs&RkS@WVdagMIb5rYcPI+)?WnJUY|EID08v5>GvM?ndp|xlj0>SZqd$>28SqdGa|X zCAss&p7iWmg<_rM7GHGMk0a~CPeyR|M0Hc>u87-2F2rVO263`9W!(~TD^**$Uook4 zS9RBDoW5`}qM>6A!>jY?CQwcNic_~lbP#UEZb|