From 8647352d0fe29917d61e619d37db6b149a0b2729 Mon Sep 17 00:00:00 2001 From: user Date: Thu, 27 Feb 2025 22:36:31 +0300 Subject: [PATCH] content.view return converted content url --- app/api/routes/content.py | 36 ++++++++++++++++++++++++++------- app/core/models/node_storage.py | 2 +- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/app/api/routes/content.py b/app/api/routes/content.py index df99c8c..2dc33a5 100644 --- a/app/api/routes/content.py +++ b/app/api/routes/content.py @@ -39,14 +39,18 @@ async def s_api_v1_content_view(request, content_address: str): r_content = StoredContent.from_cid(request.ctx.db_session, content_address) content = r_content.open_content(request.ctx.db_session) - opts = {'content_type': content['content_type'], 'content_address': content['encrypted_content'].meta.get('item_address', '')} + opts = { + 'content_type': content['content_type'], # возможно с ошибками, нужно переделать на ffprobe + 'content_address': content['encrypted_content'].meta.get('item_address', '') + } if content['encrypted_content'].key_id: known_key = request.ctx.db_session.query(KnownKey).filter( KnownKey.id == content['encrypted_content'].key_id ).first() if known_key: - opts['key_hash'] = known_key.seed_hash + opts['key_hash'] = known_key.seed_hash # нахер не нужно на данный момент + # чисто болванки, заполнение дальше opts['have_licenses'] = [] opts['invoice'] = None @@ -56,10 +60,17 @@ async def s_api_v1_content_view(request, content_address: str): have_access = ( (content['encrypted_content'].owner_address == user_wallet_address) or bool(request.ctx.db_session.query(UserContent).filter_by(owner_address=user_wallet_address, status='active', - content_id=content['encrypted_content'].id).first()) + content_id=content['encrypted_content'].id).first()) \ + or bool(request.ctx.db_session.query(UserContent).filter( + and_( + StarsInvoice.user_id == request.ctx.user.id, + StarsInvoice.content_hash == content['encrypted_content'].hash, + StarsInvoice.paid == True + ) + )) ) if not have_access: - stars_cost = 1 + stars_cost = 1 # TODO: как считать стоимость в звездах? exist_invoice = request.ctx.db_session.query(StarsInvoice).filter( and_( @@ -101,13 +112,24 @@ async def s_api_v1_content_view(request, content_address: str): } display_options = { - 'content_url': content['decrypted_content'].web_url + ( - '?seconds_limit=30' if not have_access else '' - ) + 'content_url': None, } + if have_access: opts['have_licenses'].append('listen') + converted_content = content['encrypted_content'].meta.get('converted_content') + if converted_content: + user_content_option = 'low_preview' + if have_access: + user_content_option = 'low' # TODO: подключать high если человек внезапно меломан + + converted_content = request.ctx.db_session.query(StoredContent).filter( + StoredContent.hash == converted_content[user_content_option] + ).first() + if converted_content: + display_options['content_url'] = converted_content.web_url + content_meta = content['encrypted_content'].json_format() content_metadata = StoredContent.from_cid(request.ctx.db_session, content_meta.get('metadata_cid') or None) with open(content_metadata.filepath, 'r') as f: diff --git a/app/core/models/node_storage.py b/app/core/models/node_storage.py index 32824c1..aafe1c6 100644 --- a/app/core/models/node_storage.py +++ b/app/core/models/node_storage.py @@ -62,7 +62,7 @@ class StoredContent(AlchemyBase, AudioContentMixin): @property def web_url(self) -> str: - return f"{PROJECT_HOST}/api/v1/storage/{self.cid.serialize_v2(include_accept_type=True)}" + return f"{PROJECT_HOST}/api/v1.5/storage/{self.cid.serialize_v2(include_accept_type=True)}" @property def decrypt_possible(self) -> bool: