Skip to content

Commit

Permalink
Merge pull request #944 from girder/better-upload-mimetypes
Browse files Browse the repository at this point in the history
Improve mimetypes on upload
  • Loading branch information
manthey authored Aug 25, 2022
2 parents 9a3c50b + 0d07412 commit 2898685
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Improvements
- Add a general filter control to item lists ([938](../../pull/938), [941](../../pull/941))
- Item list modal dialogs are wider ([939](../../pull/939))
- Improve mimetypes on upload to Girder ([943](../../pull/943))

### Bug Fixes
- Fix iterating tiles where the overlap larger than the tile size ([940](../../pull/940))
Expand Down
31 changes: 31 additions & 0 deletions girder/girder_large_image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
pass


mimetypes = None


# Girder 3 is pinned to use pymongo < 4; its warnings aren't relevant until
# that changes.
warnings.filterwarnings('ignore', category=UserWarning, module='pymongo')
Expand Down Expand Up @@ -217,6 +220,33 @@ def handleRemoveFile(event):
ImageItem().delete(item, [fileObj['_id']])


def handleFinalizeUploadBefore(event):
"""
When a file is uploaded, mark its mime type based on its extension if we
would otherwise just mark it as generic application/octet-stream.
"""
fileObj = event.info['file']
if fileObj.get('mimeType', None) in {None, 'application/octet-stream'}:
global mimetypes

if not mimetypes:
import mimetypes

if not mimetypes.inited:
mimetypes.init()
# Augment the standard mimetypes with some additional values
for mimeType, ext, std in [
('text/yaml', '.yaml', True),
('text/yaml', '.yml', False),
]:
if ext not in mimetypes.types_map:
mimetypes.add_type(mimeType, ext, std)

alt = mimetypes.guess_type(fileObj.get('name', ''))[0]
if alt is not None:
fileObj['mimeType'] = alt


# Validators

@setting_utilities.validator({
Expand Down Expand Up @@ -356,3 +386,4 @@ def load(self, info):
events.bind('model.item.remove', 'large_image.removeThumbnails', removeThumbnails)
events.bind('server_fuse.unmount', 'large_image', large_image.cache_util.cachesClear)
events.bind('model.file.remove', 'large_image', handleRemoveFile)
events.bind('model.file.finalizeUpload.before', 'large_image', handleFinalizeUploadBefore)

0 comments on commit 2898685

Please sign in to comment.