1 class CollectionsController < ApplicationController
2 skip_around_filter :thread_with_mandatory_api_token, only: [:show_file]
3 skip_before_filter :find_object_by_uuid, only: [:provenance, :show_file]
4 skip_before_filter :check_user_agreements, only: [:show_file]
9 %w(Files Attributes Metadata Provenance_graph Used_by JSON API)
14 when 'persistent', 'cache'
15 persist_links = Link.filter([['owner_uuid', '=', current_user.uuid],
16 ['link_class', '=', 'resources'],
17 ['name', '=', 'wants'],
18 ['tail_uuid', '=', current_user.uuid],
19 ['head_uuid', '=', @object.uuid]])
20 logger.debug persist_links.inspect
22 return unprocessable "Invalid value #{value.inspect}"
24 if params[:value] == 'persistent'
25 if not persist_links.any?
26 Link.create(link_class: 'resources',
28 tail_uuid: current_user.uuid,
29 head_uuid: @object.uuid)
32 persist_links.each do |link|
38 f.json { render json: @object }
43 if params[:search].andand.length.andand > 0
44 tags = Link.where(any: ['contains', params[:search]])
45 @collections = (Collection.where(uuid: tags.collect(&:head_uuid)) |
46 Collection.where(any: ['contains', params[:search]])).
50 limit = params[:limit].to_i
56 offset = params[:offset].to_i
61 @collections = Collection.limit(limit).offset(offset)
63 @links = Link.limit(1000).
64 where(head_uuid: @collections.collect(&:uuid))
66 @collections.each do |c|
67 @collection_info[c.uuid] = {
76 @collection_info[link.head_uuid] ||= {}
77 info = @collection_info[link.head_uuid]
80 info[:tag_links] << link
83 info[:wanted_by_me] ||= link.tail_uuid == current_user.uuid
85 info[:provenance] << link.name
89 @request_url = request.url
93 # We pipe from arv-get to send the file to the user. Before we start it,
94 # we ask the API server if the file actually exists. This serves two
95 # purposes: it lets us return a useful status code for common errors, and
96 # helps us figure out which token to provide to arv-get.
98 usable_token = find_usable_token do
99 coll = Collection.find(params[:uuid])
102 return # Response already rendered.
103 elsif params[:file].nil? or not file_in_collection?(coll, params[:file])
104 return render_not_found
106 opts = params.merge(arvados_api_token: usable_token)
107 ext = File.extname(params[:file])
108 self.response.headers['Content-Type'] =
109 Rack::Mime::MIME_TYPES[ext] || 'application/octet-stream'
110 self.response.headers['Content-Length'] = params[:size] if params[:size]
111 self.response.headers['Content-Disposition'] = params[:disposition] if params[:disposition]
112 self.response_body = file_enumerator opts
116 return super if !@object
118 jobs_with = lambda do |conds|
119 Job.limit(RELATION_LIMIT).where(conds)
120 .results.sort_by { |j| j.finished_at || j.created_at }
122 @output_of = jobs_with.call(output: @object.uuid)
123 @log_of = jobs_with.call(log: @object.uuid)
124 folder_links = Link.limit(RELATION_LIMIT).order("modified_at DESC")
125 .where(head_uuid: @object.uuid, link_class: 'name').results
126 folder_hash = Group.where(uuid: folder_links.map(&:tail_uuid)).to_hash
127 @folders = folder_links.map { |link| folder_hash[link.tail_uuid] }
128 @permissions = Link.limit(RELATION_LIMIT).order("modified_at DESC")
129 .where(head_uuid: @object.uuid, link_class: 'permission',
130 name: 'can_read').results
131 @logs = Log.limit(RELATION_LIMIT).order("created_at DESC")
132 .where(object_uuid: @object.uuid).results
133 @is_persistent = Link.limit(1)
134 .where(head_uuid: @object.uuid, tail_uuid: current_user.uuid,
135 link_class: 'resources', name: 'wants')
138 @prov_svg = ProvenanceHelper::create_provenance_graph(@object.provenance, "provenance_svg",
139 {:request => request,
140 :direction => :bottom_up,
141 :combine_jobs => :script_only}) rescue nil
142 @used_by_svg = ProvenanceHelper::create_provenance_graph(@object.used_by, "used_by_svg",
143 {:request => request,
144 :direction => :top_down,
145 :combine_jobs => :script_only,
146 :pdata_only => true}) rescue nil
151 def find_usable_token
152 # Iterate over every token available to make it the current token and
153 # yield the given block.
154 # If the block succeeds, return the token it used.
155 # Otherwise, render an error response based on the most specific
156 # error we encounter, and return nil.
157 read_tokens = [Thread.current[:arvados_api_token]].compact
158 if params[:reader_tokens].is_a? Array
159 read_tokens += params[:reader_tokens]
161 most_specific_error = [401]
162 read_tokens.each do |api_token|
163 using_specific_api_token(api_token) do
167 rescue ArvadosApiClient::NotLoggedInException => error
170 status = (error.message =~ /\[API: (\d+)\]$/) ? $1.to_i : nil
171 raise unless [401, 403, 404].include?(status)
173 if status >= most_specific_error.first
174 most_specific_error = [status, error]
178 case most_specific_error.shift
182 render_not_found(*most_specific_error)
187 def file_in_collection?(collection, filename)
188 target = CollectionsHelper.file_path(File.split(filename))
189 collection.files.each do |file_spec|
190 return true if (CollectionsHelper.file_path(file_spec) == target)
195 def file_enumerator(opts)
196 FileStreamer.new opts
200 include ArvadosApiClientHelper
201 def initialize(opts={})
205 return unless @opts[:uuid] && @opts[:file]
208 'ARVADOS_API_HOST' =>
209 arvados_api_client.arvados_v1_base.
210 sub(/\/arvados\/v1/, '').
211 sub(/^https?:\/\//, ''),
212 'ARVADOS_API_TOKEN' =>
213 @opts[:arvados_api_token],
214 'ARVADOS_API_HOST_INSECURE' =>
215 Rails.configuration.arvados_insecure_https ? 'true' : 'false'
217 IO.popen([env, 'arv-get', "#{@opts[:uuid]}/#{@opts[:file]}"],
219 while buf = io.read(2**20)
223 Rails.logger.warn("#{@opts[:uuid]}/#{@opts[:file]}: #{$?}") if $? != 0