8784: Fix test for latest firefox.
[arvados.git] / apps / workbench / app / helpers / collections_helper.rb
1 module CollectionsHelper
2   def d3ify_links(links)
3     links.collect do |x|
4       {source: x.tail_uuid, target: x.head_uuid, type: x.name}
5     end
6   end
7
8   ##
9   # Regex match for collection portable data hash, returns a regex match object with the
10   # hash in group 1, (optional) size in group 2, (optional) subsequent uuid
11   # fields in group 3, and (optional) file path within the collection as group
12   # 4
13   # returns nil for no match.
14   #
15   # +pdh+ the portable data hash string to match
16   #
17   def self.match(pdh)
18     /^([a-f0-9]{32})(\+\d+)(\+[^+]+)*?(\/.*)?$/.match(pdh.to_s)
19   end
20
21   ##
22   # Regex match for collection UUIDs, returns a regex match object with the
23   # uuid in group 1, empty groups 2 and 3 (for consistency with the match
24   # method above), and (optional) file path within the collection as group
25   # 4.
26   # returns nil for no match.
27   #
28   def self.match_uuid_with_optional_filepath(uuid_with_optional_file)
29     /^([0-9a-z]{5}-4zz18-[0-9a-z]{15})()()(\/.*)?$/.match(uuid_with_optional_file.to_s)
30   end
31
32   ##
33   # Regex match for common image file extensions, returns a regex match object
34   # with the matched extension in group 1; or nil for no match.
35   #
36   # +file+ the file string to match
37   #
38   def self.is_image file
39     /\.(jpg|jpeg|gif|png|svg)$/i.match(file)
40   end
41
42   ##
43   # Generates a relative file path than can be appended to the URL of a
44   # collection to get a file download link without adding a spurious ./ at the
45   # beginning for files in the default stream.
46   #
47   # +file+ an entry in the Collection.files list in the form [stream, name, size]
48   #
49   def self.file_path file
50     f0 = file[0]
51     f0 = '' if f0 == '.'
52     f0 = f0[2..-1] if f0[0..1] == './'
53     f0 += '/' if not f0.empty?
54     file_path = "#{f0}#{file[1]}"
55   end
56
57   ##
58   # Check if collection preview is allowed for the given filename with extension
59   #
60   def preview_allowed_for file_name
61     file_type = MIME::Types.type_for(file_name).first
62     if file_type.nil?
63       false
64     elsif (file_type.raw_media_type == "text") || (file_type.raw_media_type == "image")
65       true
66     elsif (file_type.raw_media_type == "application") &&
67           (Rails.configuration.application_mimetypes_with_view_icon.include? (file_type.sub_type))
68       true
69     else
70       false
71     end
72   end
73 end