Source code for invenio_records_files.utils
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2016 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.
"""Implementention of various utility functions."""
from __future__ import absolute_import, print_function
from flask import abort
from invenio_files_rest.models import ObjectVersion
from invenio_files_rest.views import ObjectResource
from invenio_records.errors import MissingModelError
[docs]def sorted_files_from_bucket(bucket, keys=None):
"""Return files from bucket sorted by given keys.
:param bucket: :class:`~invenio_files_rest.models.Bucket` containing the
files.
:param keys: Keys order to be used.
:returns: Sorted list of bucket items.
"""
keys = keys or []
total = len(keys)
sortby = dict(zip(keys, range(total)))
values = ObjectVersion.get_by_bucket(bucket).all()
return sorted(values, key=lambda x: sortby.get(x.key, total))
[docs]def record_file_factory(pid, record, filename):
"""Get file from a record.
:param pid: Not used. It keeps the function signature.
:param record: Record which contains the files.
:param filename: Name of the file to be returned.
:returns: File object or ``None`` if not found.
"""
try:
if not (hasattr(record, 'files') and record.files):
return None
except MissingModelError:
return None
try:
return record.files[filename]
except KeyError:
return None
[docs]def file_download_ui(pid, record, _record_file_factory=None, **kwargs):
"""File download view for a given record.
Plug this method into your ``RECORDS_UI_ENDPOINTS`` configuration:
.. code-block:: python
RECORDS_UI_ENDPOINTS = dict(
recid=dict(
# ...
route='/records/<pid_value/files/<filename>',
view_imp='invenio_records_files.utils:file_download_ui',
record_class='invenio_records_files.api:Record',
)
)
:param pid: The :class:`invenio_pidstore.models.PersistentIdentifier`
instance.
:param record: The record metadata.
"""
_record_file_factory = _record_file_factory or record_file_factory
# Extract file from record.
fileobj = _record_file_factory(
pid, record, kwargs.get('filename')
)
if not fileobj:
abort(404)
obj = fileobj.obj
# Check permissions
ObjectResource.check_object_permission(obj)
# Send file.
return ObjectResource.send_object(
obj.bucket, obj,
expected_chksum=fileobj.get('checksum'),
logger_data={
'bucket_id': obj.bucket_id,
'pid_type': pid.pid_type,
'pid_value': pid.pid_value,
},
)