Merge branch 'master' of git.clinicalfuture.com:arvados
[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 common image file extensions, returns a regex match object
22   # with the matched extension in group 1; or nil for no match.
23   #
24   # +file+ the file string to match
25   #
26   def self.is_image file
27     /\.(jpg|jpeg|gif|png|svg)$/i.match(file)
28   end
29
30   ##
31   # Generates a relative file path than can be appended to the URL of a
32   # collection to get a file download link without adding a spurious ./ at the
33   # beginning for files in the default stream.
34   #
35   # +file+ an entry in the Collection.files list in the form [stream, name, size]
36   #
37   def self.file_path file
38     f0 = file[0]
39     f0 = '' if f0 == '.'
40     f0 = f0[2..-1] if f0[0..1] == './'
41     f0 += '/' if not f0.empty?
42     file_path = "#{f0}#{file[1]}"
43   end
44 end