Source code for ciowarehouse2.handlers.video

"""A file handler for videos."""

from __future__ import annotations
from os.path import splitext

from pyramid.config import Configurator
from pyramid.request import Request

from chrysalio.helpers.literal import Literal
from ..lib.ciotype import CioType
from ..lib.ciopath import CioPath
from ..lib.warehouse import Warehouse
from ..lib.handler import Handler
from ..lib.i18n import _

EXTENSIONS_WEB = (
    '.mp4', '.wmv', '.webm', '.avi', '.flv', '.mkv', '.mts', '.mov')


# =============================================================================
[docs] def includeme(configurator: Configurator): """Function to include a video handler. :type configurator: pyramid.config.Configurator :param configurator: Object used to do configuration declaration within the application. """ Handler.register(configurator, HandlerVideo)
# =============================================================================
[docs] class HandlerVideo(Handler): """Class to manage an video.""" ciotype = CioType('video') label = _('Generic video file handling') viewings = ( # yapf: disable {'name': 'default', 'label': _('Default'), 'template': 'ciowarehouse2:Templates/handler_layout_view.pt', 'css': ('/ciowarehouse2/css/handler_video.css',)},) # -------------------------------------------------------------------------
[docs] def match(self, ciotype: CioType) -> bool: """Check whether this file handler matches with the CioType. See: :meth:`.lib.handler.Handler.match` """ return ciotype.type_ == 'video' \ and f'.{ciotype.subtype}' in EXTENSIONS_WEB
# -------------------------------------------------------------------------
[docs] def view( self, request: Request, warehouse: Warehouse, ciopath: CioPath, ts_factory=None) -> str | None: """Return a string containing HTML to display the file. See: :meth:`.lib.handler.Handler.view` """ if not ciopath or \ splitext(ciopath.path)[1].lower() not in EXTENSIONS_WEB: return None url = request.route_path('file_download', ciopath=ciopath.route()) message = request.localizer.translate( _('Your browser does not support HTML5 video.')) content = '<div class="cioVideo"><video controls="">'\ f'<source src="{url}"/><span>{message}</span></video></div>' return self._chameleon_render( request, warehouse, ciopath, self.viewings[0], ts_factory or _, {'content': Literal(content)})