1 class CollectionsController < ApplicationController
2 skip_around_filter(:require_thread_api_token,
3 only: [:show_file, :show_file_links])
4 skip_before_filter(:find_object_by_uuid,
5 only: [:provenance, :show_file, :show_file_links])
6 # We depend on show_file to display the user agreement:
7 skip_before_filter :check_user_agreements, only: :show_file
8 skip_before_filter :check_user_profile, only: :show_file
13 %w(Files Provenance_graph Used_by Advanced)
18 when 'persistent', 'cache'
19 persist_links = Link.filter([['owner_uuid', '=', current_user.uuid],
20 ['link_class', '=', 'resources'],
21 ['name', '=', 'wants'],
22 ['tail_uuid', '=', current_user.uuid],
23 ['head_uuid', '=', @object.uuid]])
24 logger.debug persist_links.inspect
26 return unprocessable "Invalid value #{value.inspect}"
28 if params[:value] == 'persistent'
29 if not persist_links.any?
30 Link.create(link_class: 'resources',
32 tail_uuid: current_user.uuid,
33 head_uuid: @object.uuid)
36 persist_links.each do |link|
42 f.json { render json: @object }
47 # Find collections using default find_objects logic, then search for name
48 # links, and preload any other links connected to the collections that are
50 # Name links will be obsolete when issue #3036 is merged,
51 # at which point this entire custom #choose function can probably be
56 find_objects_for_index
57 @collections = @objects
59 @filters += [['link_class','=','name'],
60 ['head_uuid','is_a','arvados#collection']]
63 find_objects_for_index
65 @name_links = @objects
67 @objects = Collection.
68 filter([['uuid','in',@name_links.collect(&:head_uuid)]])
70 preload_links_for_objects (@collections.to_a + @objects.to_a)
75 if params[:search].andand.length.andand > 0
76 tags = Link.where(any: ['contains', params[:search]])
77 @collections = (Collection.where(uuid: tags.collect(&:head_uuid)) |
78 Collection.where(any: ['contains', params[:search]])).
82 limit = params[:limit].to_i
88 offset = params[:offset].to_i
93 @collections = Collection.limit(limit).offset(offset)
95 @links = Link.limit(1000).
96 where(head_uuid: @collections.collect(&:uuid))
98 @collections.each do |c|
99 @collection_info[c.uuid] = {
107 @links.each do |link|
108 @collection_info[link.head_uuid] ||= {}
109 info = @collection_info[link.head_uuid]
112 info[:tag_links] << link
115 info[:wanted_by_me] ||= link.tail_uuid == current_user.uuid
117 info[:provenance] << link.name
121 @request_url = request.url
127 Thread.current[:reader_tokens] = [params[:reader_token]]
128 return if false.equal?(find_object_by_uuid)
133 # We pipe from arv-get to send the file to the user. Before we start it,
134 # we ask the API server if the file actually exists. This serves two
135 # purposes: it lets us return a useful status code for common errors, and
136 # helps us figure out which token to provide to arv-get.
138 tokens = [Thread.current[:arvados_api_token], params[:reader_token]].compact
139 usable_token = find_usable_token(tokens) do
140 coll = Collection.find(params[:uuid])
143 return # Response already rendered.
144 elsif params[:file].nil? or not file_in_collection?(coll, params[:file])
145 return render_not_found
147 opts = params.merge(arvados_api_token: usable_token)
148 ext = File.extname(params[:file])
149 self.response.headers['Content-Type'] =
150 Rack::Mime::MIME_TYPES[ext] || 'application/octet-stream'
151 self.response.headers['Content-Length'] = params[:size] if params[:size]
152 self.response.headers['Content-Disposition'] = params[:disposition] if params[:disposition]
153 self.response_body = file_enumerator opts
157 ["GET /arvados/v1/collections/#{@object.uuid}", "GET /arvados/v1/collections/#{@object.uuid}/", "GET /arvados/v1/keep_services/accessible"]
162 ApiClientAuthorization.filter([['scopes', '=', sharing_scopes]]).results
163 rescue ArvadosApiClient::AccessForbiddenException
169 return super if !@object
171 jobs_with = lambda do |conds|
172 Job.limit(RELATION_LIMIT).where(conds)
173 .results.sort_by { |j| j.finished_at || j.created_at }
175 @output_of = jobs_with.call(output: @object.portable_data_hash)
176 @log_of = jobs_with.call(log: @object.portable_data_hash)
177 @project_links = Link.limit(RELATION_LIMIT).order("modified_at DESC")
178 .where(head_uuid: @object.uuid, link_class: 'name').results
179 project_hash = Group.where(uuid: @project_links.map(&:tail_uuid)).to_hash
180 @projects = project_hash.values
182 if @object.uuid.match /[0-9a-f]{32}/
183 @same_pdh = Collection.filter([["portable_data_hash", "=", @object.portable_data_hash]])
184 owners = @same_pdh.map {|s| s.owner_uuid}.to_a
185 preload_objects_for_dataclass Group, owners
186 preload_objects_for_dataclass User, owners
189 @permissions = Link.limit(RELATION_LIMIT).order("modified_at DESC")
190 .where(head_uuid: @object.uuid, link_class: 'permission',
191 name: 'can_read').results
192 @logs = Log.limit(RELATION_LIMIT).order("created_at DESC")
193 .where(object_uuid: @object.uuid).results
194 @is_persistent = Link.limit(1)
195 .where(head_uuid: @object.uuid, tail_uuid: current_user.uuid,
196 link_class: 'resources', name: 'wants')
198 @search_sharing = search_scopes
201 if params["tab_pane"] == "Provenance_graph"
202 @prov_svg = ProvenanceHelper::create_provenance_graph(@object.provenance, "provenance_svg",
203 {:request => request,
204 :direction => :bottom_up,
205 :combine_jobs => :script_only}) rescue nil
207 if params["tab_pane"] == "Used_by"
208 @used_by_svg = ProvenanceHelper::create_provenance_graph(@object.used_by, "used_by_svg",
209 {:request => request,
210 :direction => :top_down,
211 :combine_jobs => :script_only,
212 :pdata_only => true}) rescue nil
218 @search_sharing = search_scopes
219 respond_to do |format|
225 helper_method :download_link
228 collections_url + "/download/#{@object.uuid}/#{@search_sharing.first.api_token}/"
232 a = ApiClientAuthorization.create(scopes: sharing_scopes)
233 @search_sharing = search_scopes
234 render 'sharing_popup'
238 @search_sharing = search_scopes
239 @search_sharing.each do |s|
242 @search_sharing = search_scopes
243 render 'sharing_popup'
248 def find_usable_token(token_list)
249 # Iterate over every given token to make it the current token and
250 # yield the given block.
251 # If the block succeeds, return the token it used.
252 # Otherwise, render an error response based on the most specific
253 # error we encounter, and return nil.
254 most_specific_error = [401]
255 token_list.each do |api_token|
257 using_specific_api_token(api_token) do
261 rescue ArvadosApiClient::ApiError => error
262 if error.api_status >= most_specific_error.first
263 most_specific_error = [error.api_status, error]
267 case most_specific_error.shift
271 render_not_found(*most_specific_error)
276 def file_in_collection?(collection, filename)
277 target = CollectionsHelper.file_path(File.split(filename))
278 collection.files.each do |file_spec|
279 return true if (CollectionsHelper.file_path(file_spec) == target)
284 def file_enumerator(opts)
285 FileStreamer.new opts
289 include ArvadosApiClientHelper
290 def initialize(opts={})
294 return unless @opts[:uuid] && @opts[:file]
299 u = URI.parse(arvados_api_client.arvados_v1_base)
300 env['ARVADOS_API_HOST'] = "#{u.host}:#{u.port}"
301 env['ARVADOS_API_TOKEN'] = @opts[:arvados_api_token]
302 env['ARVADOS_API_HOST_INSECURE'] = "true" if Rails.configuration.arvados_insecure_https
304 IO.popen([env, 'arv-get', "#{@opts[:uuid]}/#{@opts[:file]}"],
306 while buf = io.read(2**16)
310 Rails.logger.warn("#{@opts[:uuid]}/#{@opts[:file]}: #{$?}") if $? != 0