From bf821506ea6708eb4c7cb4415cd0c5538dfbe5a2 Mon Sep 17 00:00:00 2001 From: David Manthey Date: Fri, 18 Nov 2022 17:00:02 -0500 Subject: [PATCH] Set mime type for imported files Add text/plain inline display and editor. --- CHANGELOG.md | 1 + girder/girder_large_image/__init__.py | 12 +++++++----- .../web_client/templates/itemViewCodemirror.pug | 5 +++-- .../web_client/views/itemViewCodemirror.js | 7 +++++++ 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d7eee2b7..98e87b411 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Improvements - Better control dtype on multi sources ([#993](../../pull/993)) - Don't use dask threads when using nd2 to fetch tiles ([#994](../../pull/994)) +- Set mime type for imported girder files ([#995](../../pull/995)) ### Bug Fixes - Use open.read rather than download to access files in Girder ([#989](../../pull/989)) diff --git a/girder/girder_large_image/__init__.py b/girder/girder_large_image/__init__.py index ae779bb61..2e04084e6 100644 --- a/girder/girder_large_image/__init__.py +++ b/girder/girder_large_image/__init__.py @@ -221,13 +221,15 @@ def handleRemoveFile(event): ImageItem().delete(item, [fileObj['_id']]) -def handleFinalizeUploadBefore(event): +def handleFileSave(event): """ - When a file is uploaded, mark its mime type based on its extension if we + When a file is first saved, 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'}: + fileObj = event.info + if fileObj.get('mimeType', None) in {None, ''} or ( + '_id' not in fileObj and + fileObj.get('mimeType', None) in {'application/octet-stream'}): global mimetypes if not mimetypes: @@ -483,7 +485,7 @@ 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) + events.bind('model.file.save', 'large_image', handleFileSave) search._allowedSearchMode.pop('li_metadata', None) search.addSearchMode('li_metadata', metadataSearchHandler) diff --git a/girder/girder_large_image/web_client/templates/itemViewCodemirror.pug b/girder/girder_large_image/web_client/templates/itemViewCodemirror.pug index d6b1db554..80086d58c 100644 --- a/girder/girder_large_image/web_client/templates/itemViewCodemirror.pug +++ b/girder/girder_large_image/web_client/templates/itemViewCodemirror.pug @@ -7,8 +7,9 @@ = ' ' button.g-view-codemirror-revert-button.btn.btn-sm.btn-default Revert = ' ' - button.g-view-codemirror-format-button.btn.btn-sm.btn-default(title='This may remove comments') Format - = ' ' + if formatRecord.format + button.g-view-codemirror-format-button.btn.btn-sm.btn-default(title='This may remove comments') Format + = ' ' button.g-view-codemirror-save-button.btn.btn-sm.btn-primary Save i.icon-edit span diff --git a/girder/girder_large_image/web_client/views/itemViewCodemirror.js b/girder/girder_large_image/web_client/views/itemViewCodemirror.js index 884b3ff52..0a7280163 100644 --- a/girder/girder_large_image/web_client/views/itemViewCodemirror.js +++ b/girder/girder_large_image/web_client/views/itemViewCodemirror.js @@ -52,6 +52,12 @@ const Formats = { * more work. */ format: (val) => jsyaml.dump(val, {lineWidth: -1, noRefs: true}) }, + 'text/plain': { + name: 'Text', + mode: 'text', + validator: (val) => val, + format: null + }, 'application/x-girder-ini': { name: 'Configuration', mode: 'properties', @@ -198,6 +204,7 @@ var CodemirrorEditWidget = View.extend({ formatName: Formats[this.mimeType].name, accessLevel: this.accessLevel, buttonList: Formats[this.mimeType].buttons || [], + formatRecord: Formats[this.mimeType], AccessType: AccessType })); this.code = CodeMirror(this.$el.find('.editor')[0], {