3710: update the combine_selected_files_into_collection method to also handle the...
[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 UUIDs, 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; or nil for no match.
13   #
14   # +uuid+ the uuid string to match
15   #
16   def self.match(uuid)
17     /^([a-f0-9]{32})(\+[0-9]+)?(\+.*?)?(\/.*)?$/.match(uuid.to_s)
18   end
19
20   ##
21   # Regex match for collection UUID or "UUID/file" path.
22   # Returns a regex match object with the
23   # UUID in group 1, (optional) file path in group 2; or nil for no match.
24   #
25   def self.match_uuid_with_optional_filepath(uuid_with_optional_file)
26     /^([0-9a-z]{5}-[0-9a-z]{5}-[0-9a-z]{15})(\/*.*)$/.match(uuid_with_optional_file.to_s)
27   end
28
29   ##
30   # Regex match for common image file extensions, returns a regex match object
31   # with the matched extension in group 1; or nil for no match.
32   #
33   # +file+ the file string to match
34   #
35   def self.is_image file
36     /\.(jpg|jpeg|gif|png|svg)$/i.match(file)
37   end
38
39   ##
40   # Generates a relative file path than can be appended to the URL of a
41   # collection to get a file download link without adding a spurious ./ at the
42   # beginning for files in the default stream.
43   #
44   # +file+ an entry in the Collection.files list in the form [stream, name, size]
45   #
46   def self.file_path file
47     f0 = file[0]
48     f0 = '' if f0 == '.'
49     f0 = f0[2..-1] if f0[0..1] == './'
50     f0 += '/' if not f0.empty?
51     file_path = "#{f0}#{file[1]}"
52   end
53 end