scinote-web/app/helpers/file_icons_helper.rb

136 lines
4.4 KiB
Ruby
Raw Normal View History

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
%w(ods xls xlsb xlsm xlsx odp pot potm potx pps ppsm
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'
elsif Constants::FILE_TABLE_FORMATS.include?(file_ext)
2023-09-14 18:12:15 +08:00
'sn-icon-file-excel'
elsif Constants::FILE_PRESENTATION_FORMATS.include?(file_ext)
2023-09-14 18:12:15 +08:00
'sn-icon-file-powerpoint'
elsif %w(pdf).include?(file_ext)
2023-09-14 18:12:15 +08:00
'sn-icon-pdf'
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'
2019-04-23 17:13:16 +08:00
else
2020-11-24 22:37:45 +08:00
'fa-paperclip'
2019-04-23 17:13:16 +08:00
end
end
2016-10-04 18:34:43 +08:00
# For showing next to file
def file_extension_icon(asset, report = false)
2021-03-08 19:20:10 +08:00
file_ext = asset.file_name.split('.').last&.downcase
if Constants::FILE_TEXT_FORMATS.include?(file_ext)
2020-11-24 22:37:45 +08:00
image_link = 'icon_small/docx_file.svg'
elsif Constants::FILE_TABLE_FORMATS.include?(file_ext)
2020-11-24 22:37:45 +08:00
image_link = 'icon_small/xslx_file.svg'
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'
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
# Now check for custom mappings or possible overrides
image_link = Extends::FILE_ICON_MAPPINGS[file_ext] if Extends::FILE_ICON_MAPPINGS[file_ext]
2016-10-04 18:34:43 +08:00
if image_link
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
# For showing in view/edit icon url (WOPI)
def file_application_url(asset)
file_ext = asset.file_name.split('.').last
if Constants::FILE_TEXT_FORMATS.include?(file_ext)
'icon_small/docx_file.svg'
elsif Constants::FILE_TABLE_FORMATS.include?(file_ext)
'icon_small/xslx_file.svg'
elsif Constants::FILE_PRESENTATION_FORMATS.include?(file_ext)
'icon_small/pptx_file.svg'
2016-10-04 18:34:43 +08:00
end
end
2016-10-04 18:34:43 +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
# 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)
file_ext = asset.file_name.split('.').last
if Constants::FILE_TEXT_FORMATS.include?(file_ext)
app = I18n.t('result_assets.wopi_word')
elsif Constants::FILE_TABLE_FORMATS.include?(file_ext)
app = I18n.t('result_assets.wopi_excel')
elsif Constants::FILE_PRESENTATION_FORMATS.include?(file_ext)
app = I18n.t('result_assets.wopi_powerpoint')
2016-10-04 18:34:43 +08:00
end
if action == 'view'
I18n.t('result_assets.wopi_open_file', app: app)
2016-10-04 18:34:43 +08:00
elsif action == 'edit'
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
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