1 class ActionsController < ApplicationController
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]
7 def self.expose_action method, &block
8 @@exposed_actions[method] = true
9 define_method method, block
13 ArvadosBase::resource_class_for_uuid(params[:uuid])
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)
25 raise ActiveRecord::RecordNotFound
30 params.keys.collect(&:to_sym).each do |param|
31 if @@exposed_actions[param]
32 return self.send(param)
38 expose_action :copy_selections_into_project do
42 expose_action :move_selections_into_project do
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) }.
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,
64 if dst.respond_to? :'name='
66 dst.name = "Copy of #{dst.name}"
68 dst.name = "Copy of unnamed #{dst.class_for_display.downcase}"
71 if resource_class == Collection
72 dst.manifest_text = Collection.select([:manifest_text]).where(uuid: src.uuid).first.manifest_text
77 raise ArgumentError.new "Unsupported action #{action}"
79 dst.owner_uuid = @object.uuid
80 dst.tail_uuid = @object.uuid if dst.class == Link
85 dst.name += " (#{Time.now.localtime})" if dst.respond_to? :name=
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')
98 # Otherwise just land on the default (Description) tab.
103 def arv_normalize mt, *opts
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'
114 IO.popen([env, 'arv-normalize'] + opts, 'w+b') do |io|
117 while buf = io.read(2**16)
124 expose_action :combine_selected_files_into_collection do
128 params["selection"].each do |s|
129 a = ArvadosBase::resource_class_for_uuid s
132 if (m = CollectionsHelper.match(Link.find(s).head_uuid))
133 pdhs.append(m[1] + m[2])
138 elsif (m = CollectionsHelper.match(s))
139 pdhs.append(m[1] + m[2])
141 elsif (m = CollectionsHelper.match_uuid_with_optional_filepath(s))
151 Collection.select([:uuid, :manifest_text]).where(uuid: uuids).each do |c|
155 Collection.select([:portable_data_hash, :manifest_text]).where(portable_data_hash: pdhs).each do |c|
156 chash[c.portable_data_hash] = c
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['.']
167 files_in_dirs['.'] = manifest_files
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 index = normalized.rindex manifest_file
173 part1 = normalized[0, index]
174 part2 = normalized[index, normalized.length]
175 part2 = part2.gsub(manifest_file) {|s| uniq_file}
176 normalized = part1 + part2
177 combined += normalized
178 manifest_files << uniq_file
180 mt = arv_normalize mt
181 manifest_streams = mt.split "\n"
182 adjusted_streams = []
183 manifest_streams.each do |stream|
184 manifest_parts = stream.split
186 manifest_files = files_in_dirs[manifest_parts[0]]
189 files_in_dirs[manifest_parts[0]] = manifest_files
192 manifest_parts.each do |part|
193 part_match = /\d*:\d*:(\S+)/.match(part)
195 uniq_file = derive_unique_filename(part_match[1], manifest_files)
196 adjusted_parts << (part.gsub(part_match[1]) {|s| uniq_file})
197 manifest_files << uniq_file
199 adjusted_parts << part
202 adjusted_streams << adjusted_parts.join(' ')
204 adjusted_streams.each do |stream|
205 combined += (stream + "\n")
210 normalized = arv_normalize combined
211 newc = Collection.new({:manifest_text => normalized})
212 newc.name = newc.name || "Collection created at #{Time.now.localtime}"
214 # set owner_uuid to current project, provided it is writable
215 current_project_writable = false
216 action_data = JSON.parse(params['action_data']) if params['action_data']
217 if action_data && action_data['current_project_uuid']
218 current_project = Group.find(action_data['current_project_uuid']) rescue nil
219 if (current_project && current_project.writable_by.andand.include?(current_user.uuid))
220 newc.owner_uuid = action_data['current_project_uuid']
221 current_project_writable = true
230 head_uuid: newc.uuid,
231 link_class: "provenance",
237 msg = current_project_writable ?
238 "Created new collection in the project #{current_project.name}." :
239 "Created new collection in your Home project."
241 redirect_to newc, flash: {'message' => msg}
244 def report_issue_popup
245 respond_to do |format|
252 logger.warn "report_issue: #{params.inspect}"
254 respond_to do |format|
255 IssueReporter.send_report(current_user, params).deliver
256 format.js {render nothing: true}
262 def derive_unique_filename filename, manifest_files
263 filename_parts = filename.split('.')
264 filename_part = filename_parts[0]
267 return filename if !manifest_files.include? filename
268 filename_parts[0] = filename_part + "(" + counter.to_s + ")"
269 filename = filename_parts.join('.')