Skip to content

Commit

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

import mimetypes
import os
from contextlib import contextmanager

from invenio_db import db
Expand Down Expand Up @@ -177,8 +178,16 @@ def dumps(self):
@property
def ext(self):
"""File extension."""
# 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 = mimetypes.guess_extension(self.object_model.mimetype)
return ext[1:] if ext else None
if ext is not None:
return ext[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()

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

0 comments on commit dd3ee77

Please sign in to comment.