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 }
49 @filters += [['link_class','=','name'],
50 ['head_uuid','is_a','arvados#collection']]
53 find_objects_for_index
55 @name_links = @objects
57 @objects = Collection.
58 filter([['uuid','in',@name_links.collect(&:head_uuid)]])
59 preload_links_for_objects @objects.to_a
64 if params[:search].andand.length.andand > 0
65 tags = Link.where(any: ['contains', params[:search]])
66 @collections = (Collection.where(uuid: tags.collect(&:head_uuid)) |
67 Collection.where(any: ['contains', params[:search]])).
71 limit = params[:limit].to_i
77 offset = params[:offset].to_i
82 @collections = Collection.limit(limit).offset(offset)
84 @links = Link.limit(1000).
85 where(head_uuid: @collections.collect(&:uuid))
87 @collections.each do |c|
88 @collection_info[c.uuid] = {
97 @collection_info[link.head_uuid] ||= {}
98 info = @collection_info[link.head_uuid]
101 info[:tag_links] << link
104 info[:wanted_by_me] ||= link.tail_uuid == current_user.uuid
106 info[:provenance] << link.name
110 @request_url = request.url
116 Thread.current[:reader_tokens] = [params[:reader_token]]
117 return if false.equal?(find_object_by_uuid)
122 # We pipe from arv-get to send the file to the user. Before we start it,
123 # we ask the API server if the file actually exists. This serves two
124 # purposes: it lets us return a useful status code for common errors, and
125 # helps us figure out which token to provide to arv-get.
127 tokens = [Thread.current[:arvados_api_token], params[:reader_token]].compact
128 usable_token = find_usable_token(tokens) do
129 coll = Collection.find(params[:uuid])
132 return # Response already rendered.
133 elsif params[:file].nil? or not file_in_collection?(coll, params[:file])
134 return render_not_found
136 opts = params.merge(arvados_api_token: usable_token)
137 ext = File.extname(params[:file])
138 self.response.headers['Content-Type'] =
139 Rack::Mime::MIME_TYPES[ext] || 'application/octet-stream'
140 self.response.headers['Content-Length'] = params[:size] if params[:size]
141 self.response.headers['Content-Disposition'] = params[:disposition] if params[:disposition]
142 self.response_body = file_enumerator opts
146 ["GET /arvados/v1/collections/#{@object.uuid}", "GET /arvados/v1/collections/#{@object.uuid}/", "GET /arvados/v1/keep_services/accessible"]
151 ApiClientAuthorization.filter([['scopes', '=', sharing_scopes]]).results
152 rescue ArvadosApiClient::AccessForbiddenException
158 return super if !@object
160 jobs_with = lambda do |conds|
161 Job.limit(RELATION_LIMIT).where(conds)
162 .results.sort_by { |j| j.finished_at || j.created_at }
164 @output_of = jobs_with.call(output: @object.uuid)
165 @log_of = jobs_with.call(log: @object.uuid)
166 @project_links = Link.limit(RELATION_LIMIT).order("modified_at DESC")
167 .where(head_uuid: @object.uuid, link_class: 'name').results
168 project_hash = Group.where(uuid: @project_links.map(&:tail_uuid)).to_hash
169 @projects = project_hash.values
170 @permissions = Link.limit(RELATION_LIMIT).order("modified_at DESC")
171 .where(head_uuid: @object.uuid, link_class: 'permission',
172 name: 'can_read').results
173 @logs = Log.limit(RELATION_LIMIT).order("created_at DESC")
174 .where(object_uuid: @object.uuid).results
175 @is_persistent = Link.limit(1)
176 .where(head_uuid: @object.uuid, tail_uuid: current_user.uuid,
177 link_class: 'resources', name: 'wants')
179 @search_sharing = search_scopes
182 if params["tab_pane"] == "Provenance_graph"
183 @prov_svg = ProvenanceHelper::create_provenance_graph(@object.provenance, "provenance_svg",
184 {:request => request,
185 :direction => :bottom_up,
186 :combine_jobs => :script_only}) rescue nil
188 if params["tab_pane"] == "Used_by"
189 @used_by_svg = ProvenanceHelper::create_provenance_graph(@object.used_by, "used_by_svg",
190 {:request => request,
191 :direction => :top_down,
192 :combine_jobs => :script_only,
193 :pdata_only => true}) rescue nil
199 @search_sharing = search_scopes
200 respond_to do |format|
206 helper_method :download_link
209 collections_url + "/download/#{@object.uuid}/#{@search_sharing.first.api_token}/"
213 a = ApiClientAuthorization.create(scopes: sharing_scopes)
214 @search_sharing = search_scopes
215 render 'sharing_popup'
219 @search_sharing = search_scopes
220 @search_sharing.each do |s|
223 @search_sharing = search_scopes
224 render 'sharing_popup'
229 def find_usable_token(token_list)
230 # Iterate over every given token to make it the current token and
231 # yield the given block.
232 # If the block succeeds, return the token it used.
233 # Otherwise, render an error response based on the most specific
234 # error we encounter, and return nil.
235 most_specific_error = [401]
236 token_list.each do |api_token|
238 using_specific_api_token(api_token) do
242 rescue ArvadosApiClient::ApiError => error
243 if error.api_status >= most_specific_error.first
244 most_specific_error = [error.api_status, error]
248 case most_specific_error.shift
252 render_not_found(*most_specific_error)
257 def file_in_collection?(collection, filename)
258 target = CollectionsHelper.file_path(File.split(filename))
259 collection.files.each do |file_spec|
260 return true if (CollectionsHelper.file_path(file_spec) == target)
265 def file_enumerator(opts)
266 FileStreamer.new opts
270 include ArvadosApiClientHelper
271 def initialize(opts={})
275 return unless @opts[:uuid] && @opts[:file]
280 u = URI.parse(arvados_api_client.arvados_v1_base)
281 env['ARVADOS_API_HOST'] = "#{u.host}:#{u.port}"
282 env['ARVADOS_API_TOKEN'] = @opts[:arvados_api_token]
283 env['ARVADOS_API_HOST_INSECURE'] = "true" if Rails.configuration.arvados_insecure_https
285 IO.popen([env, 'arv-get', "#{@opts[:uuid]}/#{@opts[:file]}"],
287 while buf = io.read(2**16)
291 Rails.logger.warn("#{@opts[:uuid]}/#{@opts[:file]}: #{$?}") if $? != 0