113 lines
3.8 KiB
Python
113 lines
3.8 KiB
Python
import hashlib
|
|
import os
|
|
from datetime import datetime
|
|
from mimetypes import guess_type
|
|
|
|
import aiofiles
|
|
from base58 import b58encode
|
|
from sanic import response
|
|
|
|
from app.core._config import UPLOADS_DIR
|
|
from app.core._utils.resolve_content import resolve_content
|
|
from app.core.logger import make_log
|
|
from app.core.models.node_storage import StoredContent
|
|
|
|
|
|
async def s_api_v1_storage_post(request):
|
|
if not request.files:
|
|
return response.json({"error": "No file provided"}, status=400)
|
|
|
|
file_param = list(request.files.values())[0][0] if request.files else None
|
|
# file_name_json = request.json.get("filename") if request.json else None
|
|
|
|
if file_param:
|
|
file_content = file_param.body
|
|
file_name = file_param.name
|
|
else:
|
|
return response.json({"error": "No file provided"}, status=400)
|
|
|
|
file_meta = {}
|
|
file_mimetype, file_encoding = guess_type(file_name)
|
|
if file_mimetype:
|
|
file_meta["content_type"] = file_mimetype
|
|
|
|
if file_encoding:
|
|
file_meta["extension_encoding"] = file_encoding
|
|
|
|
try:
|
|
file_hash_bin = hashlib.sha256(file_content).digest()
|
|
file_hash = b58encode(file_hash_bin).decode()
|
|
stored_content = request.ctx.db_session.query(StoredContent).filter(StoredContent.hash == file_hash).first()
|
|
if stored_content:
|
|
stored_cid = stored_content.cid.serialize_v1()
|
|
return response.json({
|
|
"content_sha256": file_hash,
|
|
"content_id_v1": stored_cid,
|
|
"content_url": f"dmy://storage?cid={stored_cid}",
|
|
})
|
|
|
|
if request.ctx.user:
|
|
pass
|
|
elif request.ctx.verified_hash:
|
|
assert request.ctx.verified_hash == file_hash_bin, "Invalid service request hash"
|
|
else:
|
|
return response.json({"error": "Unauthorized"}, status=401)
|
|
|
|
new_content = StoredContent(
|
|
type="local/content_bin",
|
|
user_id=request.ctx.user.id,
|
|
hash=file_hash,
|
|
filename=file_name,
|
|
meta=file_meta,
|
|
created=datetime.now(),
|
|
key_id=None,
|
|
)
|
|
request.ctx.db_session.add(new_content)
|
|
request.ctx.db_session.commit()
|
|
|
|
file_path = os.path.join(UPLOADS_DIR, file_hash)
|
|
async with aiofiles.open(file_path, "wb") as file:
|
|
await file.write(file_content)
|
|
|
|
new_content_id = new_content.cid
|
|
new_cid = new_content_id.serialize_v1()
|
|
|
|
return response.json({
|
|
"content_sha256": file_hash,
|
|
"content_id_v1": new_cid,
|
|
"content_url": f"dmy://storage?cid={new_cid}",
|
|
})
|
|
except BaseException as e:
|
|
return response.json({"error": f"Error: {e}"}, status=500)
|
|
|
|
|
|
async def s_api_v1_storage_get(request, file_hash=None):
|
|
content_id = file_hash
|
|
cid, errmsg = resolve_content(content_id)
|
|
if errmsg:
|
|
return response.json({"error": errmsg}, status=400)
|
|
|
|
content_sha256 = b58encode(cid.content_hash).decode()
|
|
content = request.ctx.db_session.query(StoredContent).filter(StoredContent.hash == content_sha256).first()
|
|
if not content:
|
|
return response.json({"error": "File not found"}, status=404)
|
|
|
|
make_log("Storage", f"File {content_sha256} requested by {request.ip}")
|
|
file_path = os.path.join(UPLOADS_DIR, content_sha256)
|
|
if not os.path.exists(file_path):
|
|
return response.json({"error": "File not found"}, status=404)
|
|
|
|
return await response.file(file_path)
|
|
|
|
|
|
async def s_api_v1_storage_decode_cid(request, content_id=None):
|
|
cid, errmsg = resolve_content(content_id)
|
|
if errmsg:
|
|
return response.json({"error": errmsg}, status=400)
|
|
|
|
return response.json({
|
|
"content_hash": b58encode(cid.content_hash).decode(),
|
|
"onchain_index": cid.onchain_index,
|
|
"accept_type": cid.accept_type,
|
|
})
|