Source code for ciowarehouse2.managers.video

"""A file manager 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.manager import Manager
from ..lib.i18n import _

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


# =============================================================================
[docs] def includeme(configurator: Configurator): """Function to include a video manager. :type configurator: pyramid.config.Configurator :param configurator: Object used to do configuration declaration within the application. """ Manager.register(configurator, ManagerVideo)
# =============================================================================
[docs] class ManagerVideo(Manager): """Class to manage an video.""" ciotype = CioType('video') label = _('Generic video file handling') viewings = ( # yapf: disable {'name': 'default', 'label': _('Default'), 'template': 'ciowarehouse2:Templates/manager_layout_view.pt', 'css': ('/ciowarehouse2/css/manager_video.css',)},) # -------------------------------------------------------------------------
[docs] def match(self, ciotype: CioType) -> bool: """Check whether this file manager matches with the CioType. See: :meth:`.lib.manager.Manager.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.manager.Manager.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)})