1 class CollectionsController < ApplicationController
2 skip_around_filter(:thread_with_mandatory_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]
12 %w(Files Provenance_graph Used_by Advanced)
17 when 'persistent', 'cache'
18 persist_links = Link.filter([['owner_uuid', '=', current_user.uuid],
19 ['link_class', '=', 'resources'],
20 ['name', '=', 'wants'],
21 ['tail_uuid', '=', current_user.uuid],
22 ['head_uuid', '=', @object.uuid]])
23 logger.debug persist_links.inspect
25 return unprocessable "Invalid value #{value.inspect}"
27 if params[:value] == 'persistent'
28 if not persist_links.any?
29 Link.create(link_class: 'resources',
31 tail_uuid: current_user.uuid,
32 head_uuid: @object.uuid)
35 persist_links.each do |link|
41 f.json { render json: @object }
48 filter([['link_class','=','name'],
49 ['head_uuid','is_a','arvados#collection']])
50 find_objects_for_index
51 @next_page_href = (next_page_offset and
52 url_for(offset: next_page_offset, partial: true))
53 @name_links = @objects
54 @objects = Collection.
55 filter([['uuid','in',@name_links.collect(&:head_uuid)]])
60 if params[:search].andand.length.andand > 0
61 tags = Link.where(any: ['contains', params[:search]])
62 @collections = (Collection.where(uuid: tags.collect(&:head_uuid)) |
63 Collection.where(any: ['contains', params[:search]])).
67 limit = params[:limit].to_i
73 offset = params[:offset].to_i
78 @collections = Collection.limit(limit).offset(offset)
80 @links = Link.limit(1000).
81 where(head_uuid: @collections.collect(&:uuid))
83 @collections.each do |c|
84 @collection_info[c.uuid] = {
93 @collection_info[link.head_uuid] ||= {}
94 info = @collection_info[link.head_uuid]
97 info[:tag_links] << link
100 info[:wanted_by_me] ||= link.tail_uuid == current_user.uuid
102 info[:provenance] << link.name
106 @request_url = request.url
112 Thread.current[:reader_tokens] = [params[:reader_token]]
118 # We pipe from arv-get to send the file to the user. Before we start it,
119 # we ask the API server if the file actually exists. This serves two
120 # purposes: it lets us return a useful status code for common errors, and
121 # helps us figure out which token to provide to arv-get.
123 tokens = [Thread.current[:arvados_api_token], params[:reader_token]].compact
124 usable_token = find_usable_token(tokens) do
125 coll = Collection.find(params[:uuid])
128 return # Response already rendered.
129 elsif params[:file].nil? or not file_in_collection?(coll, params[:file])
130 return render_not_found
132 opts = params.merge(arvados_api_token: usable_token)
133 ext = File.extname(params[:file])
134 self.response.headers['Content-Type'] =
135 Rack::Mime::MIME_TYPES[ext] || 'application/octet-stream'
136 self.response.headers['Content-Length'] = params[:size] if params[:size]
137 self.response.headers['Content-Disposition'] = params[:disposition] if params[:disposition]
138 self.response_body = file_enumerator opts
142 ["GET /arvados/v1/collections/#{@object.uuid}", "GET /arvados/v1/collections/#{@object.uuid}/", "GET /arvados/v1/keep_services/accessible"]
147 ApiClientAuthorization.filter([['scopes', '=', sharing_scopes]]).results
148 rescue ArvadosApiClient::AccessForbiddenException
154 return super if !@object
156 jobs_with = lambda do |conds|
157 Job.limit(RELATION_LIMIT).where(conds)
158 .results.sort_by { |j| j.finished_at || j.created_at }
160 @output_of = jobs_with.call(output: @object.uuid)
161 @log_of = jobs_with.call(log: @object.uuid)
162 @project_links = Link.limit(RELATION_LIMIT).order("modified_at DESC")
163 .where(head_uuid: @object.uuid, link_class: 'name').results
164 project_hash = Group.where(uuid: @project_links.map(&:tail_uuid)).to_hash
165 @projects = @project_links.map { |link| project_hash[link.tail_uuid] }
166 @permissions = Link.limit(RELATION_LIMIT).order("modified_at DESC")
167 .where(head_uuid: @object.uuid, link_class: 'permission',
168 name: 'can_read').results
169 @logs = Log.limit(RELATION_LIMIT).order("created_at DESC")
170 .where(object_uuid: @object.uuid).results
171 @is_persistent = Link.limit(1)
172 .where(head_uuid: @object.uuid, tail_uuid: current_user.uuid,
173 link_class: 'resources', name: 'wants')
175 @search_sharing = search_scopes
177 @prov_svg = ProvenanceHelper::create_provenance_graph(@object.provenance, "provenance_svg",
178 {:request => request,
179 :direction => :bottom_up,
180 :combine_jobs => :script_only}) rescue nil
181 @used_by_svg = ProvenanceHelper::create_provenance_graph(@object.used_by, "used_by_svg",
182 {:request => request,
183 :direction => :top_down,
184 :combine_jobs => :script_only,
185 :pdata_only => true}) rescue nil
191 @search_sharing = search_scopes
192 respond_to do |format|
198 helper_method :download_link
201 collections_url + "/download/#{@object.uuid}/#{@search_sharing.first.api_token}"
205 a = ApiClientAuthorization.create(scopes: sharing_scopes)
206 @search_sharing = search_scopes
207 render 'sharing_popup'
211 @search_sharing = search_scopes
212 @search_sharing.each do |s|
215 @search_sharing = search_scopes
216 render 'sharing_popup'
221 def find_usable_token(token_list)
222 # Iterate over every given token to make it the current token and
223 # yield the given block.
224 # If the block succeeds, return the token it used.
225 # Otherwise, render an error response based on the most specific
226 # error we encounter, and return nil.
227 most_specific_error = [401]
228 token_list.each do |api_token|
229 using_specific_api_token(api_token) do
233 rescue ArvadosApiClient::NotLoggedInException => error
236 status = (error.message =~ /\[API: (\d+)\]$/) ? $1.to_i : nil
237 raise unless [401, 403, 404].include?(status)
239 if status >= most_specific_error.first
240 most_specific_error = [status, error]
244 case most_specific_error.shift
248 render_not_found(*most_specific_error)
253 def file_in_collection?(collection, filename)
254 target = CollectionsHelper.file_path(File.split(filename))
255 collection.files.each do |file_spec|
256 return true if (CollectionsHelper.file_path(file_spec) == target)
261 def file_enumerator(opts)
262 FileStreamer.new opts
266 include ArvadosApiClientHelper
267 def initialize(opts={})
271 return unless @opts[:uuid] && @opts[:file]
276 u = URI.parse(arvados_api_client.arvados_v1_base)
277 env['ARVADOS_API_HOST'] = "#{u.host}:#{u.port}"
278 env['ARVADOS_API_TOKEN'] = @opts[:arvados_api_token]
279 env['ARVADOS_API_HOST_INSECURE'] = "true" if Rails.configuration.arvados_insecure_https
281 IO.popen([env, 'arv-get', "#{@opts[:uuid]}/#{@opts[:file]}"],
283 while buf = io.read(2**16)
287 Rails.logger.warn("#{@opts[:uuid]}/#{@opts[:file]}: #{$?}") if $? != 0