diff --git a/app/api/__init__.py b/app/api/__init__.py index decbab6..cae27b0 100644 --- a/app/api/__init__.py +++ b/app/api/__init__.py @@ -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._blockchain import s_api_v1_blockchain_send_new_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_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_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) async def s_handle_exception(request, exception): diff --git a/app/api/routes/content.py b/app/api/routes/content.py new file mode 100644 index 0000000..8a71aec --- /dev/null +++ b/app/api/routes/content.py @@ -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) diff --git a/app/core/models/node_storage.py b/app/core/models/node_storage.py index 84f04be..51cd6c3 100644 --- a/app/core/models/node_storage.py +++ b/app/core/models/node_storage.py @@ -41,3 +41,20 @@ class StoredContent(AlchemyBase): onchain_index=self.onchain_index, 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, + }