dev@locazia: add content list

This commit is contained in:
user 2024-03-05 02:02:57 +03:00
parent 5d486544ca
commit 9e60503262
3 changed files with 40 additions and 0 deletions

View File

@ -18,6 +18,7 @@ from app.api.routes.node_storage import s_api_v1_storage_post, s_api_v1_storage_
from app.api.routes.account import s_api_v1_account_get from app.api.routes.account import s_api_v1_account_get
from app.api.routes._blockchain import s_api_v1_blockchain_send_new_content_message, \ from app.api.routes._blockchain import s_api_v1_blockchain_send_new_content_message, \
s_api_v1_blockchain_send_purchase_content_message s_api_v1_blockchain_send_purchase_content_message
from app.api.routes.content import s_api_v1_content_list
app.add_route(s_index, "/", methods=["GET", "OPTIONS"]) app.add_route(s_index, "/", methods=["GET", "OPTIONS"])
app.add_route(s_favicon, "/favicon.ico", methods=["GET", "OPTIONS"]) app.add_route(s_favicon, "/favicon.ico", methods=["GET", "OPTIONS"])
@ -39,6 +40,8 @@ app.add_route(s_api_v1_account_get, "/api/v1/account", methods=["GET", "OPTIONS"
app.add_route(s_api_v1_blockchain_send_new_content_message, "/api/v1/blockchain.sendNewContentMessage", methods=["POST", "OPTIONS"]) app.add_route(s_api_v1_blockchain_send_new_content_message, "/api/v1/blockchain.sendNewContentMessage", methods=["POST", "OPTIONS"])
app.add_route(s_api_v1_blockchain_send_purchase_content_message, "/api/v1/blockchain.sendPurchaseContentMessage", methods=["POST", "OPTIONS"]) app.add_route(s_api_v1_blockchain_send_purchase_content_message, "/api/v1/blockchain.sendPurchaseContentMessage", methods=["POST", "OPTIONS"])
app.add_route(s_api_v1_content_list, "/api/v1/content.list", methods=["GET", "OPTIONS"])
@app.exception(BaseException) @app.exception(BaseException)
async def s_handle_exception(request, exception): async def s_handle_exception(request, exception):

20
app/api/routes/content.py Normal file
View File

@ -0,0 +1,20 @@
from sanic import response
from datetime import datetime, timedelta
from app.core.models.node_storage import StoredContent
async def s_api_v1_content_list(request):
offset = int(request.args.get('offset', 0))
limit = int(request.args.get('limit', 100))
assert 0 <= offset, "Invalid offset"
assert 0 < limit <= 1000, "Invalid limit"
content_list = request.ctx.db_session.query(StoredContent).filter(
StoredContent.type == "local/bin"
).order_by(StoredContent.created.desc()).offset(offset).limit(limit).all()
result = {}
for content in content_list:
content_json = content.json_format()
result[content_json["cid"]] = content_json
return response.json(result)

View File

@ -41,3 +41,20 @@ class StoredContent(AlchemyBase):
onchain_index=self.onchain_index, onchain_index=self.onchain_index,
accept_type=self.meta.get('content_type', 'image/jpeg') accept_type=self.meta.get('content_type', 'image/jpeg')
) )
def json_format(self):
extra_fields = {}
if self.btfs_cid:
extra_fields['btfs_cid'] = self.btfs_cid
if self.ipfs_cid:
extra_fields['ipfs_cid'] = self.ipfs_cid
if self.type.startswith('local'):
pass
return {
**extra_fields,
"hash": self.hash,
"cid": self.cid.serialize_v1(),
"status": self.status,
"meta": self.meta,
}