content.view return converted content url

This commit is contained in:
user 2025-02-27 22:36:31 +03:00
parent 9da4894a0b
commit 8647352d0f
2 changed files with 30 additions and 8 deletions

View File

@ -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) r_content = StoredContent.from_cid(request.ctx.db_session, content_address)
content = r_content.open_content(request.ctx.db_session) 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: if content['encrypted_content'].key_id:
known_key = request.ctx.db_session.query(KnownKey).filter( known_key = request.ctx.db_session.query(KnownKey).filter(
KnownKey.id == content['encrypted_content'].key_id KnownKey.id == content['encrypted_content'].key_id
).first() ).first()
if known_key: if known_key:
opts['key_hash'] = known_key.seed_hash opts['key_hash'] = known_key.seed_hash # нахер не нужно на данный момент
# чисто болванки, заполнение дальше
opts['have_licenses'] = [] opts['have_licenses'] = []
opts['invoice'] = None opts['invoice'] = None
@ -56,10 +60,17 @@ async def s_api_v1_content_view(request, content_address: str):
have_access = ( have_access = (
(content['encrypted_content'].owner_address == user_wallet_address) (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', 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: if not have_access:
stars_cost = 1 stars_cost = 1 # TODO: как считать стоимость в звездах?
exist_invoice = request.ctx.db_session.query(StarsInvoice).filter( exist_invoice = request.ctx.db_session.query(StarsInvoice).filter(
and_( and_(
@ -101,13 +112,24 @@ async def s_api_v1_content_view(request, content_address: str):
} }
display_options = { display_options = {
'content_url': content['decrypted_content'].web_url + ( 'content_url': None,
'?seconds_limit=30' if not have_access else ''
)
} }
if have_access: if have_access:
opts['have_licenses'].append('listen') 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_meta = content['encrypted_content'].json_format()
content_metadata = StoredContent.from_cid(request.ctx.db_session, content_meta.get('metadata_cid') or None) content_metadata = StoredContent.from_cid(request.ctx.db_session, content_meta.get('metadata_cid') or None)
with open(content_metadata.filepath, 'r') as f: with open(content_metadata.filepath, 'r') as f:

View File

@ -62,7 +62,7 @@ class StoredContent(AlchemyBase, AudioContentMixin):
@property @property
def web_url(self) -> str: 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 @property
def decrypt_possible(self) -> bool: def decrypt_possible(self) -> bool: