2020-11-24 22:37:45 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-04 18:34:43 +08:00
|
|
|
module FileIconsHelper
|
|
|
|
def wopi_file?(asset)
|
2021-03-08 19:20:10 +08:00
|
|
|
file_ext = asset.file_name.split('.').last&.downcase
|
2022-03-28 22:32:18 +08:00
|
|
|
%w(ods xls xlsb xlsm xlsx odp pot potm potx pps ppsm
|
2019-07-02 05:30:20 +08:00
|
|
|
ppsx ppt pptm pptx doc docm docx dot dotm dotx odt rtf).include?(file_ext)
|
2016-10-04 18:34:43 +08:00
|
|
|
end
|
|
|
|
|
2018-03-30 17:50:28 +08:00
|
|
|
def file_fa_icon_class(asset)
|
2021-03-08 19:20:10 +08:00
|
|
|
file_ext = asset.file_name.split('.').last&.downcase
|
2019-04-26 14:58:01 +08:00
|
|
|
|
|
|
|
if Extends::FILE_FA_ICON_MAPPINGS[file_ext] # Check for custom mappings or possible overrides
|
2020-11-24 22:37:45 +08:00
|
|
|
Extends::FILE_FA_ICON_MAPPINGS[file_ext]
|
2019-04-26 14:58:01 +08:00
|
|
|
elsif Constants::FILE_TEXT_FORMATS.include?(file_ext)
|
2023-09-14 18:12:15 +08:00
|
|
|
'sn-icon-file-word'
|
2019-01-01 01:10:12 +08:00
|
|
|
elsif Constants::FILE_TABLE_FORMATS.include?(file_ext)
|
2023-09-14 18:12:15 +08:00
|
|
|
'sn-icon-file-excel'
|
2019-01-01 01:10:12 +08:00
|
|
|
elsif Constants::FILE_PRESENTATION_FORMATS.include?(file_ext)
|
2023-09-14 18:12:15 +08:00
|
|
|
'sn-icon-file-powerpoint'
|
2018-04-13 15:43:14 +08:00
|
|
|
elsif %w(pdf).include?(file_ext)
|
2023-10-04 22:16:55 +08:00
|
|
|
'sn-icon-file-pdf'
|
2018-04-13 15:43:14 +08:00
|
|
|
elsif %w(txt csv tab tex).include?(file_ext)
|
2023-09-14 18:12:15 +08:00
|
|
|
'sn-icon-result-text'
|
2019-04-23 17:13:16 +08:00
|
|
|
elsif Constants::WHITELISTED_IMAGE_TYPES.include?(file_ext)
|
2023-09-14 18:12:15 +08:00
|
|
|
'sn-icon-result-image'
|
2023-10-04 22:16:55 +08:00
|
|
|
elsif asset.file.attached? && asset.file.metadata['asset_type'] == 'marvinjs'
|
|
|
|
'sn-icon-marvinjs'
|
|
|
|
elsif asset.file.attached? && asset.file.metadata['asset_type'] == 'gene_sequence'
|
|
|
|
'sn-icon-sequence-editor'
|
2019-04-23 17:13:16 +08:00
|
|
|
else
|
2023-10-04 22:16:55 +08:00
|
|
|
'sn-icon-attachment'
|
2019-04-23 17:13:16 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-04 18:34:43 +08:00
|
|
|
# For showing next to file
|
2021-01-19 19:53:49 +08:00
|
|
|
def file_extension_icon(asset, report = false)
|
2021-03-08 19:20:10 +08:00
|
|
|
file_ext = asset.file_name.split('.').last&.downcase
|
2019-01-01 01:10:12 +08:00
|
|
|
if Constants::FILE_TEXT_FORMATS.include?(file_ext)
|
2020-11-24 22:37:45 +08:00
|
|
|
image_link = 'icon_small/docx_file.svg'
|
2019-01-01 01:10:12 +08:00
|
|
|
elsif Constants::FILE_TABLE_FORMATS.include?(file_ext)
|
2020-11-24 22:37:45 +08:00
|
|
|
image_link = 'icon_small/xslx_file.svg'
|
2019-01-01 01:10:12 +08:00
|
|
|
elsif Constants::FILE_PRESENTATION_FORMATS.include?(file_ext)
|
2020-11-24 22:37:45 +08:00
|
|
|
image_link = 'icon_small/pptx_file.svg'
|
|
|
|
elsif asset.file.attached? && asset.file.metadata['asset_type'] == 'marvinjs'
|
|
|
|
image_link = 'icon_small/marvinjs_file.svg'
|
2023-08-16 19:09:45 +08:00
|
|
|
elsif asset.file.attached? && asset.file.metadata['asset_type'] == 'gene_sequence'
|
|
|
|
image_link = 'icon_small/sequence-editor.svg'
|
2016-10-04 18:34:43 +08:00
|
|
|
end
|
|
|
|
|
2018-02-26 21:23:02 +08:00
|
|
|
# Now check for custom mappings or possible overrides
|
2019-07-02 05:30:20 +08:00
|
|
|
image_link = Extends::FILE_ICON_MAPPINGS[file_ext] if Extends::FILE_ICON_MAPPINGS[file_ext]
|
2018-02-26 21:23:02 +08:00
|
|
|
|
2016-10-04 18:34:43 +08:00
|
|
|
if image_link
|
2023-09-14 22:03:03 +08:00
|
|
|
image_link = wicked_pdf_asset_base64(image_link) if report
|
|
|
|
|
|
|
|
ActionController::Base.helpers.image_tag(image_link, class: 'image-icon')
|
2016-10-04 18:34:43 +08:00
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-17 16:39:17 +08:00
|
|
|
# For showing in view/edit icon url (WOPI)
|
|
|
|
def file_application_url(asset)
|
2019-07-09 16:28:15 +08:00
|
|
|
file_ext = asset.file_name.split('.').last
|
2019-01-01 01:10:12 +08:00
|
|
|
if Constants::FILE_TEXT_FORMATS.include?(file_ext)
|
2022-05-17 16:39:17 +08:00
|
|
|
'icon_small/docx_file.svg'
|
2019-01-01 01:10:12 +08:00
|
|
|
elsif Constants::FILE_TABLE_FORMATS.include?(file_ext)
|
2022-05-17 16:39:17 +08:00
|
|
|
'icon_small/xslx_file.svg'
|
2019-01-01 01:10:12 +08:00
|
|
|
elsif Constants::FILE_PRESENTATION_FORMATS.include?(file_ext)
|
2022-05-17 16:39:17 +08:00
|
|
|
'icon_small/pptx_file.svg'
|
2016-10-04 18:34:43 +08:00
|
|
|
end
|
2022-05-17 16:39:17 +08:00
|
|
|
end
|
2016-10-04 18:34:43 +08:00
|
|
|
|
2023-07-25 15:52:31 +08:00
|
|
|
def sn_icon_for(asset)
|
|
|
|
file_ext = asset.file_name.split('.').last
|
|
|
|
if Constants::FILE_TEXT_FORMATS.include?(file_ext)
|
|
|
|
'file-word'
|
|
|
|
elsif Constants::FILE_TABLE_FORMATS.include?(file_ext)
|
|
|
|
'file-excel'
|
|
|
|
elsif Constants::FILE_PRESENTATION_FORMATS.include?(file_ext)
|
|
|
|
'file-powerpoint'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-17 16:39:17 +08:00
|
|
|
# For showing in view/edit buttons (WOPI)
|
|
|
|
def file_application_icon(asset)
|
|
|
|
image_link = file_application_url(asset)
|
2016-10-04 18:34:43 +08:00
|
|
|
if image_link
|
|
|
|
image_tag image_link
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Shows correct WOPI application text (Word Online/Excel ..)
|
|
|
|
def wopi_button_text(asset, action)
|
2019-07-09 16:28:15 +08:00
|
|
|
file_ext = asset.file_name.split('.').last
|
2019-01-01 01:10:12 +08:00
|
|
|
if Constants::FILE_TEXT_FORMATS.include?(file_ext)
|
2022-05-17 16:39:17 +08:00
|
|
|
app = I18n.t('result_assets.wopi_word')
|
2019-01-01 01:10:12 +08:00
|
|
|
elsif Constants::FILE_TABLE_FORMATS.include?(file_ext)
|
2022-05-17 16:39:17 +08:00
|
|
|
app = I18n.t('result_assets.wopi_excel')
|
2019-01-01 01:10:12 +08:00
|
|
|
elsif Constants::FILE_PRESENTATION_FORMATS.include?(file_ext)
|
2022-05-17 16:39:17 +08:00
|
|
|
app = I18n.t('result_assets.wopi_powerpoint')
|
2016-10-04 18:34:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if action == 'view'
|
2022-05-17 16:39:17 +08:00
|
|
|
I18n.t('result_assets.wopi_open_file', app: app)
|
2016-10-04 18:34:43 +08:00
|
|
|
elsif action == 'edit'
|
2022-05-17 16:39:17 +08:00
|
|
|
I18n.t('result_assets.wopi_edit_file', app: app)
|
2016-10-04 18:34:43 +08:00
|
|
|
end
|
|
|
|
end
|
2019-03-18 02:23:17 +08:00
|
|
|
|
|
|
|
# Returns correct content type for given extension
|
|
|
|
def wopi_content_type(extension)
|
|
|
|
case extension
|
|
|
|
when 'docx'
|
|
|
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
|
|
|
when 'xlsx'
|
|
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
|
|
|
when 'pptx'
|
|
|
|
'application/vnd.openxmlformats-officedocument.presentationml.presentation'
|
|
|
|
end
|
|
|
|
end
|
2019-11-11 21:05:59 +08:00
|
|
|
|
2021-01-19 19:53:49 +08:00
|
|
|
def file_extension_icon_html(asset, report = false)
|
|
|
|
html = file_extension_icon(asset, report)
|
2020-11-24 22:37:45 +08:00
|
|
|
if html.blank?
|
|
|
|
html = ActionController::Base.helpers.content_tag(
|
|
|
|
:i,
|
|
|
|
'',
|
2023-09-14 18:12:15 +08:00
|
|
|
class: ['sn-icon', 'asset-icon', file_fa_icon_class(asset)]
|
2020-11-24 22:37:45 +08:00
|
|
|
)
|
|
|
|
end
|
2019-11-11 21:05:59 +08:00
|
|
|
html
|
|
|
|
end
|
|
|
|
|
|
|
|
module_function :file_extension_icon_html, :file_extension_icon, :file_fa_icon_class
|
2016-10-04 18:34:43 +08:00
|
|
|
end
|