Merge branch 'master' into 2798-go-keep-client
[arvados.git] / apps / workbench / app / models / collection.rb
1 class Collection < ArvadosBase
2   include ApplicationHelper
3
4   MD5_EMPTY = 'd41d8cd98f00b204e9800998ecf8427e'
5
6   # Return true if the given string is the locator of a zero-length blob
7   def self.is_empty_blob_locator? locator
8     !!locator.to_s.match("^#{MD5_EMPTY}(\\+.*)?\$")
9   end
10
11   def content_summary
12     human_readable_bytes_html(total_bytes) + " " + super
13   end
14
15   def total_bytes
16     if files
17       tot = 0
18       files.each do |file|
19         tot += file[2]
20       end
21       tot
22     end
23   end
24
25   def files_tree
26     tree = files.group_by { |file_spec| File.split(file_spec.first) }
27     # Fill in entries for empty directories.
28     tree.keys.map { |basedir, _| File.split(basedir) }.each do |splitdir|
29       until tree.include?(splitdir)
30         tree[splitdir] = []
31         splitdir = File.split(splitdir.first)
32       end
33     end
34     dir_to_tree = lambda do |dirname|
35       # First list subdirectories, with their files inside.
36       subnodes = tree.keys.select { |bd, td| (bd == dirname) and (td != '.') }
37         .sort.flat_map do |parts|
38         [parts + [nil]] + dir_to_tree.call(File.join(parts))
39       end
40       # Then extend that list with files in this directory.
41       subnodes + tree[File.split(dirname)]
42     end
43     dir_to_tree.call('.')
44   end
45
46   def attribute_editable?(attr)
47     false
48   end
49
50   def self.creatable?
51     false
52   end
53
54   def provenance
55     arvados_api_client.api "collections/#{self.uuid}/", "provenance"
56   end
57
58   def used_by
59     arvados_api_client.api "collections/#{self.uuid}/", "used_by"
60   end
61
62 end