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])
10 %w(Files Provenance_graph Used_by Advanced)
15 when 'persistent', 'cache'
16 persist_links = Link.filter([['owner_uuid', '=', current_user.uuid],
17 ['link_class', '=', 'resources'],
18 ['name', '=', 'wants'],
19 ['tail_uuid', '=', current_user.uuid],
20 ['head_uuid', '=', @object.uuid]])
21 logger.debug persist_links.inspect
23 return unprocessable "Invalid value #{value.inspect}"
25 if params[:value] == 'persistent'
26 if not persist_links.any?
27 Link.create(link_class: 'resources',
29 tail_uuid: current_user.uuid,
30 head_uuid: @object.uuid)
33 persist_links.each do |link|
39 f.json { render json: @object }
46 filter([['link_class','=','name'],
47 ['head_uuid','is_a','arvados#collection']])
48 find_objects_for_index
49 @next_page_href = (next_page_offset and
50 url_for(offset: next_page_offset, partial: true))
51 @name_links = @objects
52 @objects = Collection.
53 filter([['uuid','in',@name_links.collect(&:head_uuid)]])
58 if params[:search].andand.length.andand > 0
59 tags = Link.where(any: ['contains', params[:search]])
60 @collections = (Collection.where(uuid: tags.collect(&:head_uuid)) |
61 Collection.where(any: ['contains', params[:search]])).
65 limit = params[:limit].to_i
71 offset = params[:offset].to_i
76 @collections = Collection.limit(limit).offset(offset)
78 @links = Link.limit(1000).
79 where(head_uuid: @collections.collect(&:uuid))
81 @collections.each do |c|
82 @collection_info[c.uuid] = {
91 @collection_info[link.head_uuid] ||= {}
92 info = @collection_info[link.head_uuid]
95 info[:tag_links] << link
98 info[:wanted_by_me] ||= link.tail_uuid == current_user.uuid
100 info[:provenance] << link.name
104 @request_url = request.url
108 Thread.current[:reader_tokens] = [params[:reader_token]]
114 # We pipe from arv-get to send the file to the user. Before we start it,
115 # we ask the API server if the file actually exists. This serves two
116 # purposes: it lets us return a useful status code for common errors, and
117 # helps us figure out which token to provide to arv-get.
119 tokens = [Thread.current[:arvados_api_token], params[:reader_token]].compact
120 usable_token = find_usable_token(tokens) do
121 coll = Collection.find(params[:uuid])
124 return # Response already rendered.
125 elsif params[:file].nil? or not file_in_collection?(coll, params[:file])
126 return render_not_found
128 opts = params.merge(arvados_api_token: usable_token)
129 ext = File.extname(params[:file])
130 self.response.headers['Content-Type'] =
131 Rack::Mime::MIME_TYPES[ext] || 'application/octet-stream'
132 self.response.headers['Content-Length'] = params[:size] if params[:size]
133 self.response.headers['Content-Disposition'] = params[:disposition] if params[:disposition]
134 self.response_body = file_enumerator opts
138 ["GET /arvados/v1/collections/#{@object.uuid}", "GET /arvados/v1/collections/#{@object.uuid}/", "GET /arvados/v1/keep_services/accessible"]
143 ApiClientAuthorization.filter([['scopes', '=', sharing_scopes]]).results
144 rescue ArvadosApiClient::AccessForbiddenException
150 return super if !@object
152 jobs_with = lambda do |conds|
153 Job.limit(RELATION_LIMIT).where(conds)
154 .results.sort_by { |j| j.finished_at || j.created_at }
156 @output_of = jobs_with.call(output: @object.uuid)
157 @log_of = jobs_with.call(log: @object.uuid)
158 folder_links = Link.limit(RELATION_LIMIT).order("modified_at DESC")
159 .where(head_uuid: @object.uuid, link_class: 'name').results
160 folder_hash = Group.where(uuid: folder_links.map(&:tail_uuid)).to_hash
161 @folders = folder_links.map { |link| folder_hash[link.tail_uuid] }
162 @permissions = Link.limit(RELATION_LIMIT).order("modified_at DESC")
163 .where(head_uuid: @object.uuid, link_class: 'permission',
164 name: 'can_read').results
165 @logs = Log.limit(RELATION_LIMIT).order("created_at DESC")
166 .where(object_uuid: @object.uuid).results
167 @is_persistent = Link.limit(1)
168 .where(head_uuid: @object.uuid, tail_uuid: current_user.uuid,
169 link_class: 'resources', name: 'wants')
171 @search_sharing = search_scopes
173 @prov_svg = ProvenanceHelper::create_provenance_graph(@object.provenance, "provenance_svg",
174 {:request => request,
175 :direction => :bottom_up,
176 :combine_jobs => :script_only}) rescue nil
177 @used_by_svg = ProvenanceHelper::create_provenance_graph(@object.used_by, "used_by_svg",
178 {:request => request,
179 :direction => :top_down,
180 :combine_jobs => :script_only,
181 :pdata_only => true}) rescue nil
185 @search_sharing = search_scopes
186 respond_to do |format|
192 helper_method :download_link
195 collections_url + "/download/#{@object.uuid}/#{@search_sharing.first.api_token}"
199 a = ApiClientAuthorization.create(scopes: sharing_scopes)
200 @search_sharing = search_scopes
201 render 'sharing_popup'
205 @search_sharing = search_scopes
206 @search_sharing.each do |s|
209 @search_sharing = search_scopes
210 render 'sharing_popup'
215 def find_usable_token(token_list)
216 # Iterate over every given token to make it the current token and
217 # yield the given block.
218 # If the block succeeds, return the token it used.
219 # Otherwise, render an error response based on the most specific
220 # error we encounter, and return nil.
221 most_specific_error = [401]
222 token_list.each do |api_token|
223 using_specific_api_token(api_token) do
227 rescue ArvadosApiClient::NotLoggedInException => error
230 status = (error.message =~ /\[API: (\d+)\]$/) ? $1.to_i : nil
231 raise unless [401, 403, 404].include?(status)
233 if status >= most_specific_error.first
234 most_specific_error = [status, error]
238 case most_specific_error.shift
242 render_not_found(*most_specific_error)
247 def file_in_collection?(collection, filename)
248 target = CollectionsHelper.file_path(File.split(filename))
249 collection.files.each do |file_spec|
250 return true if (CollectionsHelper.file_path(file_spec) == target)
255 def file_enumerator(opts)
256 FileStreamer.new opts
260 include ArvadosApiClientHelper
261 def initialize(opts={})
265 return unless @opts[:uuid] && @opts[:file]
270 u = URI.parse(arvados_api_client.arvados_v1_base)
271 env['ARVADOS_API_HOST'] = "#{u.host}:#{u.port}"
272 env['ARVADOS_API_TOKEN'] = @opts[:arvados_api_token]
273 env['ARVADOS_API_HOST_INSECURE'] = "true" if Rails.configuration.arvados_insecure_https
275 IO.popen([env, 'arv-get', "#{@opts[:uuid]}/#{@opts[:file]}"],
277 while buf = io.read(2**16)
281 Rails.logger.warn("#{@opts[:uuid]}/#{@opts[:file]}: #{$?}") if $? != 0