18870: New script to help manage install
[arvados.git] / apps / workbench / app / helpers / collections_helper.rb
index 23933cf599bd88e5a6d4a9b75d0dcd1a19f6e004..0c89ca8783c0c45b523a33c4c8fd495ce17f0c58 100644 (file)
@@ -1,3 +1,7 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
 module CollectionsHelper
   def d3ify_links(links)
     links.collect do |x|
@@ -6,15 +10,27 @@ module CollectionsHelper
   end
 
   ##
-  # Regex match for collection UUIDs, returns a regex match object with the
+  # Regex match for collection portable data hash, returns a regex match object with the
   # hash in group 1, (optional) size in group 2, (optional) subsequent uuid
   # fields in group 3, and (optional) file path within the collection as group
-  # 4; or nil for no match.
+  # 4
+  # returns nil for no match.
+  #
+  # +pdh+ the portable data hash string to match
   #
-  # +uuid+ the uuid string to match
+  def self.match(pdh)
+    /^([a-f0-9]{32})(\+\d+)(\+[^+]+)*?(\/.*)?$/.match(pdh.to_s)
+  end
+
+  ##
+  # Regex match for collection UUIDs, returns a regex match object with the
+  # uuid in group 1, empty groups 2 and 3 (for consistency with the match
+  # method above), and (optional) file path within the collection as group
+  # 4.
+  # returns nil for no match.
   #
-  def self.match(uuid)
-    /^([a-f0-9]{32})(\+[0-9]+)?(\+.*?)?(\/.*)?$/.match(uuid.to_s)
+  def self.match_uuid_with_optional_filepath(uuid_with_optional_file)
+    /^([0-9a-z]{5}-4zz18-[0-9a-z]{15})()()(\/.*)?$/.match(uuid_with_optional_file.to_s)
   end
 
   ##
@@ -39,6 +55,27 @@ module CollectionsHelper
     f0 = '' if f0 == '.'
     f0 = f0[2..-1] if f0[0..1] == './'
     f0 += '/' if not f0.empty?
-    file_path = "#{f0}#{file[1]}"
+    "#{f0}#{file[1]}"
+  end
+
+  ##
+  # Check if collection preview is allowed for the given filename with extension
+  #
+  def preview_allowed_for file_name
+    file_type = MIME::Types.type_for(file_name).first
+    if file_type.nil?
+      if file_name.downcase.end_with?('.cwl') # unknown mime type, but we support preview
+        true
+      else
+        false
+      end
+    elsif (file_type.raw_media_type == "text") || (file_type.raw_media_type == "image")
+      true
+    elsif (file_type.raw_media_type == "application") &&
+          Rails.configuration.Workbench.ApplicationMimetypesWithViewIcon[file_type.sub_type]
+      true
+    else
+      false
+    end
   end
 end