Beispiel #1
0
def record_file_preview(
    record: RecordItem = None,
    pid_value: str = None,
    pid_type: str = "recid",
    file_metadata: FileItem = None,
    is_preview: bool = False,
    **kwargs,
):
    """Render a preview of the specified file."""
    file_previewer = file_metadata.data.get("previewer")
    url = url_for(
        "invenio_records_lom.record_file_download",
        pid_value=pid_value,
        filename=file_metadata.data["key"],
        preview=1 if is_preview else 0,
    )

    # find a suitable previewer
    file_obj = PreviewFile(file_metadata, pid_value, url)
    for plugin in current_previewer.iter_previewers(
        previewers=[file_previewer] if file_previewer else None
    ):
        if plugin.can_preview(file_obj):
            return plugin.preview(file_obj)

    return default.preview(file_obj)
Beispiel #2
0
def record_file_preview(
    record=None,
    pid_value=None,
    pid_type="recid",
    file_metadata=None,
    is_preview=False,
    **kwargs
):
    """Render a preview of the specified file."""
    # Try to see if specific previewer is set
    # TODO: what's the analog of: file_previewer = fileobj.get("previewer") ?
    file_previewer = file_metadata.data.get("previewer")

    url = url_for(
        "invenio_app_rdm_records.record_file_download",
        pid_value=pid_value,
        filename=file_metadata.data["key"],
        preview=1 if is_preview else 0
    )
    # Find a suitable previewer
    fileobj = PreviewFile(file_metadata, pid_value, url)
    for plugin in current_previewer.iter_previewers(
        previewers=[file_previewer] if file_previewer else None
    ):
        if plugin.can_preview(fileobj):
            return plugin.preview(fileobj)

    return default.preview(fileobj)
Beispiel #3
0
def _try_previewers(pid,
                    record,
                    filename,
                    preview_file_class=PreviewFile,
                    previewer=None,
                    **kwargs):
    """Try previewing file with all available previewers."""
    # Get file from record
    fileobj = current_previewer.record_file_factory(pid, record, filename)

    if not fileobj:
        abort(404)

    # Try to see if specific previewer is requested?
    file_previewer = previewer or fileobj.get('previewer')

    fileobj = preview_file_class(pid, record, fileobj)

    # Try out available previewers
    for plugin in current_previewer.iter_previewers(
            previewers=[file_previewer] if file_previewer else None):
        if plugin.can_preview(fileobj):
            try:
                return plugin.preview(fileobj)
            except Exception:
                current_app.logger.warning(
                    ('Preview failed for {key}, in {pid_type}:{pid_value}'.
                     format(key=fileobj.file.key,
                            pid_type=fileobj.pid.pid_type,
                            pid_value=fileobj.pid.pid_value)),
                    exc_info=True)
    return default.preview(fileobj)
Beispiel #4
0
def preview(pid, record, template=None, **kwargs):
    """Preview file for given record.

    Plug this method into your ``RECORDS_UI_ENDPOINTS`` configuration:

    .. code-block:: python

        RECORDS_UI_ENDPOINTS = dict(
            recid=dict(
                # ...
                route='/records/<pid_value/preview/<path:filename>',
                view_imp='invenio_previewer.views.preview',
                record_class='invenio_records_files.api:Record',
            )
        )
    """
    # Get file from record
    fileobj = current_previewer.record_file_factory(
        pid, record,
        request.view_args.get('filename', request.args.get('filename',
                                                           type=str)))
    if not fileobj:
        abort(404)

    # Try to see if specific previewer is requested?
    try:
        file_previewer = fileobj['previewer']
    except KeyError:
        file_previewer = None

    # Find a suitable previewer
    fileobj = PreviewFile(pid, record, fileobj)

    if fileobj.has_extensions('.zip'):
        return zip_preview(fileobj)
    else:
        for plugin in current_previewer.iter_previewers(
                previewers=[file_previewer] if file_previewer else None):
            if plugin.can_preview(fileobj):
                try:
                    return plugin.preview(fileobj)
                except Exception:
                    current_app.logger.warning(
                        ('Preview failed for {key}, in {pid_type}:{pid_value}'.
                         format(key=fileobj.file.key,
                                pid_type=fileobj.pid.pid_type,
                                pid_value=fileobj.pid.pid_value)),
                        exc_info=True)
        return default.preview(fileobj)
def record_file_preview(record=None,
                        pid_value=None,
                        pid_type="recid",
                        file_metadata=None,
                        **kwargs):
    """Render a preview of the specified file."""
    # Try to see if specific previewer is set
    # TODO: what's the analog of: file_previewer = fileobj.get("previewer") ?
    file_previewer = file_metadata.data.get("previewer")

    # Find a suitable previewer
    fileobj = PreviewFile(file_metadata, pid_value)
    for plugin in current_previewer.iter_previewers(
            previewers=[file_previewer] if file_previewer else None):
        if plugin.can_preview(fileobj):
            return plugin.preview(fileobj)

    return default.preview(fileobj)