1 require "arvados/collection"
3 class ActionsController < ApplicationController
5 # Skip require_thread_api_token if this is a show action
6 # for an object uuid that supports anonymous access.
7 skip_around_filter :require_thread_api_token, if: proc { |ctrl|
8 Rails.configuration.anonymous_user_token and
9 'show' == ctrl.action_name and
11 model_class.in?([Collection, Group, Job, PipelineInstance, PipelineTemplate])
13 skip_filter :require_thread_api_token, only: [:report_issue_popup, :report_issue]
14 skip_filter :check_user_agreements, only: [:report_issue_popup, :report_issue]
16 @@exposed_actions = {}
17 def self.expose_action method, &block
18 @@exposed_actions[method] = true
19 define_method method, block
23 ArvadosBase::resource_class_for_uuid(params[:uuid])
27 @object = model_class.andand.find(params[:uuid])
28 if @object.is_a? Link and
29 @object.link_class == 'name' and
30 ArvadosBase::resource_class_for_uuid(@object.head_uuid) == Collection
31 redirect_to collection_path(id: @object.uuid)
32 elsif @object.is_a?(Group) and @object.group_class == 'project'
33 redirect_to project_path(id: @object.uuid)
37 raise ActiveRecord::RecordNotFound
42 params.keys.collect(&:to_sym).each do |param|
43 if @@exposed_actions[param]
44 return self.send(param)
50 expose_action :copy_selections_into_project do
54 expose_action :move_selections_into_project do
58 def move_or_copy action
59 uuids_to_add = params["selection"]
60 uuids_to_add = [ uuids_to_add ] unless uuids_to_add.is_a? Array
61 resource_classes = uuids_to_add.
62 collect { |x| ArvadosBase::resource_class_for_uuid(x) }.
64 resource_classes.each do |resource_class|
65 resource_class.filter([['uuid','in',uuids_to_add]]).each do |src|
66 if resource_class == Collection and not Collection.attribute_info.include?(:name)
67 dst = Link.new(owner_uuid: @object.uuid,
68 tail_uuid: @object.uuid,
76 if dst.respond_to? :'name='
78 dst.name = "Copy of #{dst.name}"
80 dst.name = "Copy of unnamed #{dst.class_for_display.downcase}"
83 if resource_class == Collection
84 dst.manifest_text = Collection.select([:manifest_text]).where(uuid: src.uuid).first.manifest_text
89 raise ArgumentError.new "Unsupported action #{action}"
91 dst.owner_uuid = @object.uuid
92 dst.tail_uuid = @object.uuid if dst.class == Link
97 dst.name += " (#{Time.now.localtime})" if dst.respond_to? :name=
102 if (resource_classes == [Collection] and
103 @object.is_a? Group and
104 @object.group_class == 'project') or
106 # In the common case where only collections are copied/moved
107 # into a project, it's polite to land on the collections tab on
108 # the destination project.
109 redirect_to project_url(@object.uuid, anchor: 'Data_collections')
111 # Otherwise just land on the default (Description) tab.
116 expose_action :combine_selected_files_into_collection do
117 uuids, source_paths = selected_collection_files params
119 new_coll = Arv::Collection.new
120 Collection.where(uuid: uuids.uniq).
121 select([:uuid, :manifest_text]).each do |coll|
122 src_coll = Arv::Collection.new(coll.manifest_text)
123 src_pathlist = source_paths[coll.uuid]
124 if src_pathlist.any?(&:blank?)
125 src_pathlist = src_coll.each_file_path
130 src_pathlist.each do |src_path|
131 src_path = src_path.sub(/^(\.\/|\/|)/, "./")
132 src_stream, _, basename = src_path.rpartition("/")
133 dst_stream = destdir || src_stream
134 # Generate a unique name by adding (1), (2), etc. to it.
135 # If the filename has a dot that's not at the beginning, insert the
136 # number just before that. Otherwise, append the number to the name.
137 if match = basename.match(/[^\.]\./)
138 suffix_start = match.begin(0) + 1
140 suffix_start = basename.size
144 loop.each_with_index do |_, try_count|
145 dst_path = "#{dst_stream}/#{basename}"
146 break unless new_coll.exist?(dst_path)
147 uniq_suffix = "(#{try_count + 1})"
148 basename[suffix_start, suffix_size] = uniq_suffix
149 suffix_size = uniq_suffix.size
151 new_coll.cp_r(src_path, dst_path, src_coll)
156 manifest_text: new_coll.manifest_text,
157 name: "Collection created at #{Time.now.localtime}",
161 # set owner_uuid to current project, provided it is writable
162 action_data = Oj.load(params['action_data'] || "{}")
163 if action_data['current_project_uuid'] and
164 current_project = Group.find?(action_data['current_project_uuid']) and
165 current_project.writable_by.andand.include?(current_user.uuid)
166 coll_attrs[:owner_uuid] = current_project.uuid
168 "Created new collection in the project #{current_project.name}."
170 flash[:message] = "Created new collection in your Home project."
173 newc = Collection.create!(coll_attrs)
174 source_paths.each_key do |src_uuid|
177 head_uuid: newc.uuid,
178 link_class: "provenance",
182 An error occurred when saving provenance information for this collection.
183 You can try recreating the collection to get a copy with full provenance data."
187 redirect_to(newc, flash: flash)
190 def report_issue_popup
191 respond_to do |format|
198 logger.warn "report_issue: #{params.inspect}"
200 respond_to do |format|
201 IssueReporter.send_report(current_user, params).deliver
202 format.js {render nothing: true}
206 # star / unstar the current project
208 links = Link.where(tail_uuid: current_user.uuid,
209 head_uuid: @object.uuid,
212 if params['status'] == 'create'
213 # create 'star' link if one does not already exist
214 if !links.andand.any?
215 dst = Link.new(owner_uuid: current_user.uuid,
216 tail_uuid: current_user.uuid,
217 head_uuid: @object.uuid,
222 else # delete any existing 'star' links
230 respond_to do |format|
237 def derive_unique_filename filename, manifest_files
238 filename_parts = filename.split('.')
239 filename_part = filename_parts[0]
242 return filename if !manifest_files.include? filename
243 filename_parts[0] = filename_part + "(" + counter.to_s + ")"
244 filename = filename_parts.join('.')