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