1 require "arvados/collection"
3 class ActionsController < ApplicationController
5 skip_around_filter :require_thread_api_token, if: proc { |ctrl|
6 Rails.configuration.anonymous_user_token and
7 'show' == ctrl.action_name and
9 (model_class == Collection or
10 model_class == Group or
12 model_class == PipelineInstance or
13 model_class == PipelineTemplate)
15 skip_filter :require_thread_api_token, only: [:report_issue_popup, :report_issue]
16 skip_filter :check_user_agreements, only: [:report_issue_popup, :report_issue]
18 @@exposed_actions = {}
19 def self.expose_action method, &block
20 @@exposed_actions[method] = true
21 define_method method, block
25 ArvadosBase::resource_class_for_uuid(params[:uuid])
29 @object = model_class.andand.find(params[:uuid])
30 if @object.is_a? Link and
31 @object.link_class == 'name' and
32 ArvadosBase::resource_class_for_uuid(@object.head_uuid) == Collection
33 redirect_to collection_path(id: @object.uuid)
34 elsif @object.is_a?(Group) and @object.group_class == 'project'
35 redirect_to project_path(id: @object.uuid)
39 raise ActiveRecord::RecordNotFound
44 params.keys.collect(&:to_sym).each do |param|
45 if @@exposed_actions[param]
46 return self.send(param)
52 expose_action :copy_selections_into_project do
56 expose_action :move_selections_into_project do
60 def move_or_copy action
61 uuids_to_add = params["selection"]
62 uuids_to_add = [ uuids_to_add ] unless uuids_to_add.is_a? Array
63 resource_classes = uuids_to_add.
64 collect { |x| ArvadosBase::resource_class_for_uuid(x) }.
66 resource_classes.each do |resource_class|
67 resource_class.filter([['uuid','in',uuids_to_add]]).each do |src|
68 if resource_class == Collection and not Collection.attribute_info.include?(:name)
69 dst = Link.new(owner_uuid: @object.uuid,
70 tail_uuid: @object.uuid,
78 if dst.respond_to? :'name='
80 dst.name = "Copy of #{dst.name}"
82 dst.name = "Copy of unnamed #{dst.class_for_display.downcase}"
85 if resource_class == Collection
86 dst.manifest_text = Collection.select([:manifest_text]).where(uuid: src.uuid).first.manifest_text
91 raise ArgumentError.new "Unsupported action #{action}"
93 dst.owner_uuid = @object.uuid
94 dst.tail_uuid = @object.uuid if dst.class == Link
99 dst.name += " (#{Time.now.localtime})" if dst.respond_to? :name=
104 if (resource_classes == [Collection] and
105 @object.is_a? Group and
106 @object.group_class == 'project') or
108 # In the common case where only collections are copied/moved
109 # into a project, it's polite to land on the collections tab on
110 # the destination project.
111 redirect_to project_url(@object.uuid, anchor: 'Data_collections')
113 # Otherwise just land on the default (Description) tab.
118 expose_action :combine_selected_files_into_collection do
119 link_uuids, coll_ids = params["selection"].partition do |sel_s|
120 ArvadosBase::resource_class_for_uuid(sel_s) == Link
123 unless link_uuids.empty?
124 Link.select([:head_uuid]).where(uuid: link_uuids).each do |link|
125 if ArvadosBase::resource_class_for_uuid(link.head_uuid) == Collection
126 coll_ids << link.head_uuid
133 source_paths = Hash.new { |hash, key| hash[key] = [] }
134 coll_ids.each do |coll_id|
135 if m = CollectionsHelper.match(coll_id)
138 source_paths[key] << m[4]
139 elsif m = CollectionsHelper.match_uuid_with_optional_filepath(coll_id)
142 source_paths[key] << m[4]
147 Collection.where(portable_data_hash: pdhs.uniq).
148 select([:uuid, :portable_data_hash]).each do |coll|
149 unless source_paths[coll.portable_data_hash].empty?
151 source_paths[coll.uuid] = source_paths.delete(coll.portable_data_hash)
156 new_coll = Arv::Collection.new
157 Collection.where(uuid: uuids.uniq).
158 select([:uuid, :manifest_text]).each do |coll|
159 src_coll = Arv::Collection.new(coll.manifest_text)
160 src_pathlist = source_paths[coll.uuid]
161 if src_pathlist.any?(&:blank?)
162 src_pathlist = src_coll.each_file_path
167 src_pathlist.each do |src_path|
168 src_path = src_path.sub(/^(\.\/|\/|)/, "./")
169 src_stream, _, basename = src_path.rpartition("/")
170 dst_stream = destdir || src_stream
171 # Generate a unique name by adding (1), (2), etc. to it.
172 # If the filename has a dot that's not at the beginning, insert the
173 # number just before that. Otherwise, append the number to the name.
174 if match = basename.match(/[^\.]\./)
175 suffix_start = match.begin(0) + 1
177 suffix_start = basename.size
181 loop.each_with_index do |_, try_count|
182 dst_path = "#{dst_stream}/#{basename}"
183 break unless new_coll.exist?(dst_path)
184 uniq_suffix = "(#{try_count + 1})"
185 basename[suffix_start, suffix_size] = uniq_suffix
186 suffix_size = uniq_suffix.size
188 new_coll.cp_r(src_path, dst_path, src_coll)
193 manifest_text: new_coll.manifest_text,
194 name: "Collection created at #{Time.now.localtime}",
198 # set owner_uuid to current project, provided it is writable
199 action_data = Oj.load(params['action_data'] || "{}")
200 if action_data['current_project_uuid'] and
201 current_project = Group.find?(action_data['current_project_uuid']) and
202 current_project.writable_by.andand.include?(current_user.uuid)
203 coll_attrs[:owner_uuid] = current_project.uuid
205 "Created new collection in the project #{current_project.name}."
207 flash[:message] = "Created new collection in your Home project."
210 newc = Collection.create!(coll_attrs)
211 source_paths.each_key do |src_uuid|
214 head_uuid: newc.uuid,
215 link_class: "provenance",
219 An error occurred when saving provenance information for this collection.
220 You can try recreating the collection to get a copy with full provenance data."
224 redirect_to(newc, flash: flash)
227 def report_issue_popup
228 respond_to do |format|
235 logger.warn "report_issue: #{params.inspect}"
237 respond_to do |format|
238 IssueReporter.send_report(current_user, params).deliver
239 format.js {render nothing: true}
245 def derive_unique_filename filename, manifest_files
246 filename_parts = filename.split('.')
247 filename_part = filename_parts[0]
250 return filename if !manifest_files.include? filename
251 filename_parts[0] = filename_part + "(" + counter.to_s + ")"
252 filename = filename_parts.join('.')