From 480f54dbdeeba5142aeb444c06c533c1aba76529 Mon Sep 17 00:00:00 2001 From: user Date: Tue, 12 Mar 2024 00:18:03 +0300 Subject: [PATCH] dev@locazia: edit file delivery method to response.raw --- app/api/routes/node_storage.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/api/routes/node_storage.py b/app/api/routes/node_storage.py index 920fe9c..56898ee 100644 --- a/app/api/routes/node_storage.py +++ b/app/api/routes/node_storage.py @@ -7,6 +7,7 @@ import aiofiles import traceback from base58 import b58encode from sanic import response +import json from app.core._config import UPLOADS_DIR from app.core._utils.resolve_content import resolve_content @@ -104,7 +105,18 @@ async def s_api_v1_storage_get(request, file_hash=None): make_log("Storage", f"File {content_sha256} not found locally", level="error") return response.json({"error": "File not found"}, status=404) - return await response.file(file_path) + async with aiofiles.open(file_path, "rb") as file: + content_file_bin = await file.read() + + accept_type = cid.accept_type or request.headers.get("Accept") + accept_type = accept_type or "application/octet-stream" + if accept_type: + if accept_type == "application/json": + return response.json( + json.loads(content_file_bin.decode()) + ) + + return response.raw(body=content_file_bin, content_type=accept_type) async def s_api_v1_storage_decode_cid(request, content_id=None):