Merge branch 'master' into 2919-provenance-graph-cutoff
[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 self.goes_in_folders?
12     true
13   end
14
15   def content_summary
16     human_readable_bytes_html(total_bytes) + " " + super
17   end
18
19   def total_bytes
20     if files
21       tot = 0
22       files.each do |file|
23         tot += file[2]
24       end
25       tot
26     end
27   end
28
29   def files_tree
30     tree = files.group_by { |file_spec| File.split(file_spec.first) }
31     # Fill in entries for empty directories.
32     tree.keys.map { |basedir, _| File.split(basedir) }.each do |splitdir|
33       until tree.include?(splitdir)
34         tree[splitdir] = []
35         splitdir = File.split(splitdir.first)
36       end
37     end
38     dir_to_tree = lambda do |dirname|
39       # First list subdirectories, with their files inside.
40       subnodes = tree.keys.select { |bd, td| (bd == dirname) and (td != '.') }
41         .sort.flat_map do |parts|
42         [parts + [nil]] + dir_to_tree.call(File.join(parts))
43       end
44       # Then extend that list with files in this directory.
45       subnodes + tree[File.split(dirname)]
46     end
47     dir_to_tree.call('.')
48   end
49
50   def attribute_editable? attr, *args
51     false
52   end
53
54   def self.creatable?
55     false
56   end
57
58   def provenance
59     arvados_api_client.api "collections/#{self.uuid}/", "provenance"
60   end
61
62   def used_by
63     arvados_api_client.api "collections/#{self.uuid}/", "used_by"
64   end
65
66 end