Merge branch 'master' into 5145-combine-collections-repeated-filenames
[arvados.git] / apps / workbench / app / controllers / actions_controller.rb
1 class ActionsController < ApplicationController
2
3   skip_filter :require_thread_api_token, only: [:report_issue_popup, :report_issue]
4   skip_filter :check_user_agreements, only: [:report_issue_popup, :report_issue]
5
6   @@exposed_actions = {}
7   def self.expose_action method, &block
8     @@exposed_actions[method] = true
9     define_method method, block
10   end
11
12   def model_class
13     ArvadosBase::resource_class_for_uuid(params[:uuid])
14   end
15
16   def show
17     @object = model_class.andand.find(params[:uuid])
18     if @object.is_a? Link and
19         @object.link_class == 'name' and
20         ArvadosBase::resource_class_for_uuid(@object.head_uuid) == Collection
21       redirect_to collection_path(id: @object.uuid)
22     elsif @object
23       redirect_to @object
24     else
25       raise ActiveRecord::RecordNotFound
26     end
27   end
28
29   def post
30     params.keys.collect(&:to_sym).each do |param|
31       if @@exposed_actions[param]
32         return self.send(param)
33       end
34     end
35     redirect_to :back
36   end
37
38   expose_action :copy_selections_into_project do
39     move_or_copy :copy
40   end
41
42   expose_action :move_selections_into_project do
43     move_or_copy :move
44   end
45
46   def move_or_copy action
47     uuids_to_add = params["selection"]
48     uuids_to_add = [ uuids_to_add ] unless uuids_to_add.is_a? Array
49     resource_classes = uuids_to_add.
50       collect { |x| ArvadosBase::resource_class_for_uuid(x) }.
51       uniq
52     resource_classes.each do |resource_class|
53       resource_class.filter([['uuid','in',uuids_to_add]]).each do |src|
54         if resource_class == Collection and not Collection.attribute_info.include?(:name)
55           dst = Link.new(owner_uuid: @object.uuid,
56                          tail_uuid: @object.uuid,
57                          head_uuid: src.uuid,
58                          link_class: 'name',
59                          name: src.uuid)
60         else
61           case action
62           when :copy
63             dst = src.dup
64             if dst.respond_to? :'name='
65               if dst.name
66                 dst.name = "Copy of #{dst.name}"
67               else
68                 dst.name = "Copy of unnamed #{dst.class_for_display.downcase}"
69               end
70             end
71             if resource_class == Collection
72               dst.manifest_text = Collection.select([:manifest_text]).where(uuid: src.uuid).first.manifest_text
73             end
74           when :move
75             dst = src
76           else
77             raise ArgumentError.new "Unsupported action #{action}"
78           end
79           dst.owner_uuid = @object.uuid
80           dst.tail_uuid = @object.uuid if dst.class == Link
81         end
82         begin
83           dst.save!
84         rescue
85           dst.name += " (#{Time.now.localtime})" if dst.respond_to? :name=
86           dst.save!
87         end
88       end
89     end
90     if (resource_classes == [Collection] and
91         @object.is_a? Group and
92         @object.group_class == 'project')
93       # In the common case where only collections are copied/moved
94       # into a project, it's polite to land on the collections tab on
95       # the destination project.
96       redirect_to project_url(@object.uuid, anchor: 'Data_collections')
97     else
98       # Otherwise just land on the default (Description) tab.
99       redirect_to @object
100     end
101   end
102
103   def arv_normalize mt, *opts
104     r = ""
105     env = Hash[ENV].
106       merge({'ARVADOS_API_HOST' =>
107               arvados_api_client.arvados_v1_base.
108               sub(/\/arvados\/v1/, '').
109               sub(/^https?:\/\//, ''),
110               'ARVADOS_API_TOKEN' => 'x',
111               'ARVADOS_API_HOST_INSECURE' =>
112               Rails.configuration.arvados_insecure_https ? 'true' : 'false'
113             })
114     IO.popen([env, 'arv-normalize'] + opts, 'w+b') do |io|
115       io.write mt
116       io.close_write
117       while buf = io.read(2**16)
118         r += buf
119       end
120     end
121     r
122   end
123
124   expose_action :combine_selected_files_into_collection do
125     uuids = []
126     pdhs = []
127     files = []
128     params["selection"].each do |s|
129       a = ArvadosBase::resource_class_for_uuid s
130       if a == Link
131         begin
132           if (m = CollectionsHelper.match(Link.find(s).head_uuid))
133             pdhs.append(m[1] + m[2])
134             files.append(m)
135           end
136         rescue
137         end
138       elsif (m = CollectionsHelper.match(s))
139         pdhs.append(m[1] + m[2])
140         files.append(m)
141       elsif (m = CollectionsHelper.match_uuid_with_optional_filepath(s))
142         uuids.append(m[1])
143         files.append(m)
144       end
145     end
146
147     pdhs = pdhs.uniq
148     uuids = uuids.uniq
149     chash = {}
150
151     Collection.select([:uuid, :manifest_text]).where(uuid: uuids).each do |c|
152       chash[c.uuid] = c
153     end
154
155     Collection.select([:portable_data_hash, :manifest_text]).where(portable_data_hash: pdhs).each do |c|
156       chash[c.portable_data_hash] = c
157     end
158
159     combined = ""
160     files_in_dirs = {}
161     files.each do |m|
162       mt = chash[m[1]+m[2]].andand.manifest_text
163       if not m[4].nil? and m[4].size > 1
164         manifest_files = files_in_dirs['.']
165         if !manifest_files
166           manifest_files = []
167           files_in_dirs['.'] = manifest_files
168         end
169         manifest_file = m[4].split('/')[-1]
170         uniq_file = derive_unique_filename(manifest_file, manifest_files)
171         normalized = arv_normalize mt, '--extract', ".#{m[4]}"
172         normalized = normalized.gsub(manifest_file) {|s| uniq_file}
173         combined += normalized
174         manifest_files << uniq_file
175       else
176         manifest_streams = mt.split "\n"
177         adjusted_streams = []
178         manifest_streams.each do |stream|
179           manifest_parts = stream.split
180           adjusted_parts = []
181           manifest_files = files_in_dirs[manifest_parts[0]]
182           if !manifest_files
183             manifest_files = []
184             files_in_dirs[manifest_parts[0]] = manifest_files
185           end
186
187           manifest_parts.each do |part|
188             part_match = /\d*:\d*:(.*)/.match(part)
189             if part_match
190               uniq_file = derive_unique_filename(part_match[1], manifest_files)
191               adjusted_parts << (part.gsub(part_match[1]) {|s| uniq_file})
192               manifest_files << uniq_file
193             else
194               adjusted_parts << part
195             end
196           end
197           adjusted_streams << adjusted_parts.join(' ')
198         end
199         adjusted_streams.each do |stream|
200           combined += (stream + "\n")
201         end
202       end
203     end
204
205     normalized = arv_normalize combined
206     newc = Collection.new({:manifest_text => normalized})
207     newc.name = newc.name || "Collection created at #{Time.now.localtime}"
208
209     # set owner_uuid to current project, provided it is writable
210     current_project_writable = false
211     action_data = JSON.parse(params['action_data']) if params['action_data']
212     if action_data && action_data['current_project_uuid']
213       current_project = Group.find(action_data['current_project_uuid']) rescue nil
214       if (current_project && current_project.writable_by.andand.include?(current_user.uuid))
215         newc.owner_uuid = action_data['current_project_uuid']
216         current_project_writable = true
217       end
218     end
219
220     newc.save!
221
222     chash.each do |k,v|
223       l = Link.new({
224                      tail_uuid: k,
225                      head_uuid: newc.uuid,
226                      link_class: "provenance",
227                      name: "provided"
228                    })
229       l.save!
230     end
231
232     msg = current_project_writable ?
233               "Created new collection in the project #{current_project.name}." :
234               "Created new collection in your Home project."
235
236     redirect_to newc, flash: {'message' => msg}
237   end
238
239   def report_issue_popup
240     respond_to do |format|
241       format.js
242       format.html
243     end
244   end
245
246   def report_issue
247     logger.warn "report_issue: #{params.inspect}"
248
249     respond_to do |format|
250       IssueReporter.send_report(current_user, params).deliver
251       format.js {render nothing: true}
252     end
253   end
254
255   protected
256
257   def derive_unique_filename filename, manifest_files
258     filename_parts = filename.split('.')
259     filename_part = filename_parts[0]
260     counter = 1
261     while true
262       return filename if !manifest_files.include? filename
263       filename_parts[0] = filename_part + "(" + counter.to_s + ")"
264       filename = filename_parts.join('.')
265       counter += 1
266     end
267   end
268
269 end