Skip to content

Commit

Permalink
api: add get file extension from key
Browse files Browse the repository at this point in the history
  • Loading branch information
jrcastro2 committed Nov 13, 2023
1 parent bc0ad6d commit 4c8ed3c
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions invenio_records_resources/records/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
"""Records API."""

import mimetypes
import os
from contextlib import contextmanager

from invenio_db import db
from invenio_files_rest.models import FileInstance, ObjectVersion
from invenio_files_rest.utils import FALLBACK_MIMETYPE
from invenio_records.api import Record as RecordBase
from invenio_records.dumpers import SearchDumper
from invenio_records.systemfields import DictField, SystemFieldsMixin
Expand Down Expand Up @@ -177,8 +179,20 @@ def dumps(self):
@property
def ext(self):
"""File extension."""
ext = mimetypes.guess_extension(self.object_model.mimetype)
return ext[1:] if ext else None
# The ``ext`` property is used to in search to aggregate file types, and we want e.g. both ``.jpeg`` and
# ``.jpg`` to be aggregated under ``.jpg``
ext_guessed = mimetypes.guess_extension(self.object_model.mimetype)

# Check if a valid extension is guessed and it's not the default mimetype
if ext_guessed is not None and ext_guessed != FALLBACK_MIMETYPE:
return ext_guessed[1:]

# Support non-standard file extensions that cannot be guessed
_, ext = os.path.splitext(self.key)
if ext and len(ext) <= 5:
return ext.lower()

return ext_guessed[1:]

def __getattr__(self, name):
"""Override to get attributes from ObjectVersion and FileInstance."""
Expand Down

0 comments on commit 4c8ed3c

Please sign in to comment.