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]
7 %w(Files Attributes Metadata Provenance_graph Used_by JSON API)
12 when 'persistent', 'cache'
13 persist_links = Link.filter([['owner_uuid', '=', current_user.uuid],
14 ['link_class', '=', 'resources'],
15 ['name', '=', 'wants'],
16 ['tail_uuid', '=', current_user.uuid],
17 ['head_uuid', '=', @object.uuid]])
18 logger.debug persist_links.inspect
20 return unprocessable "Invalid value #{value.inspect}"
22 if params[:value] == 'persistent'
23 if not persist_links.any?
24 Link.create(link_class: 'resources',
26 tail_uuid: current_user.uuid,
27 head_uuid: @object.uuid)
30 persist_links.each do |link|
36 f.json { render json: @object }
41 if params[:search].andand.length.andand > 0
42 tags = Link.where(any: ['contains', params[:search]])
43 @collections = (Collection.where(uuid: tags.collect(&:head_uuid)) |
44 Collection.where(any: ['contains', params[:search]])).
48 limit = params[:limit].to_i
54 offset = params[:offset].to_i
59 @collections = Collection.limit(limit).offset(offset)
61 @links = Link.limit(1000).
62 where(head_uuid: @collections.collect(&:uuid))
64 @collections.each do |c|
65 @collection_info[c.uuid] = {
74 @collection_info[link.head_uuid] ||= {}
75 info = @collection_info[link.head_uuid]
78 info[:tag_links] << link
81 info[:wanted_by_me] ||= link.tail_uuid == current_user.uuid
83 info[:provenance] << link.name
87 @request_url = request.url
91 # We pipe from arv-get to send the file to the user. Before we start it,
92 # we ask the API server if the file actually exists. This serves two
93 # purposes: it lets us return a useful status code for common errors, and
94 # helps us figure out which token to provide to arv-get.
96 usable_token = find_usable_token do
97 coll = Collection.find(params[:uuid])
100 return # Response already rendered.
101 elsif params[:file].nil? or not file_in_collection?(coll, params[:file])
102 return render_not_found
104 opts = params.merge(arvados_api_token: usable_token)
105 ext = File.extname(params[:file])
106 self.response.headers['Content-Type'] =
107 Rack::Mime::MIME_TYPES[ext] || 'application/octet-stream'
108 self.response.headers['Content-Length'] = params[:size] if params[:size]
109 self.response.headers['Content-Disposition'] = params[:disposition] if params[:disposition]
110 self.response_body = file_enumerator opts
114 return super if !@object
117 @output2colorindex = {}
118 @sourcedata = {params[:uuid] => {uuid: params[:uuid]}}
124 any_hope_left = false
125 Job.where(output: @sourcedata.keys).sort_by { |a| a.finished_at || a.created_at }.reverse.each do |job|
126 if !@output2colorindex[job.output]
128 @output2colorindex[job.output] = (colorindex += 1) % 10
129 @provenance << {job: job, output: job.output}
130 @sourcedata.delete job.output
131 @output2job[job.output] = job
132 job.dependencies.each do |new_source_data|
133 unless @output2colorindex[new_source_data]
134 @sourcedata[new_source_data] = {uuid: new_source_data}
141 Link.where(head_uuid: @sourcedata.keys | @output2job.keys).each do |link|
142 if link.link_class == 'resources' and link.name == 'wants'
143 @protected[link.head_uuid] = true
144 if link.tail_uuid == current_user.uuid
145 @is_persistent = true
149 Link.where(tail_uuid: @sourcedata.keys).each do |link|
150 if link.link_class == 'data_origin'
151 @sourcedata[link.tail_uuid][:data_origins] ||= []
152 @sourcedata[link.tail_uuid][:data_origins] << [link.name, link.head_uuid]
155 Collection.where(uuid: @sourcedata.keys).each do |collection|
156 if @sourcedata[collection.uuid]
157 @sourcedata[collection.uuid][:collection] = collection
161 Collection.where(uuid: @object.uuid).each do |u|
162 @prov_svg = ProvenanceHelper::create_provenance_graph(u.provenance, "provenance_svg",
163 {:request => request,
164 :direction => :bottom_up,
165 :combine_jobs => :script_only}) rescue nil
166 @used_by_svg = ProvenanceHelper::create_provenance_graph(u.used_by, "used_by_svg",
167 {:request => request,
168 :direction => :top_down,
169 :combine_jobs => :script_only,
170 :pdata_only => true}) rescue nil
176 def find_usable_token
177 # Iterate over every token available to make it the current token and
178 # yield the given block.
179 # If the block succeeds, return the token it used.
180 # Otherwise, render an error response based on the most specific
181 # error we encounter, and return nil.
182 read_tokens = [Thread.current[:arvados_api_token]].compact
183 if params[:reader_tokens].is_a? Array
184 read_tokens += params[:reader_tokens]
186 most_specific_error = [401]
187 read_tokens.each do |api_token|
188 using_specific_api_token(api_token) do
192 rescue ArvadosApiClient::NotLoggedInException => error
195 status = (error.message =~ /\[API: (\d+)\]$/) ? $1.to_i : nil
196 raise unless [401, 403, 404].include?(status)
198 if status >= most_specific_error.first
199 most_specific_error = [status, error]
203 case most_specific_error.shift
207 render_not_found(*most_specific_error)
212 def file_in_collection?(collection, filename)
213 def normalized_path(part_list)
214 File.join(part_list).sub(%r{^\./}, '')
216 target = normalized_path([filename])
217 collection.files.each do |file_spec|
218 return true if (normalized_path(file_spec[0, 2]) == target)
223 def file_enumerator(opts)
224 FileStreamer.new opts
228 def initialize(opts={})
232 return unless @opts[:uuid] && @opts[:file]
235 'ARVADOS_API_HOST' =>
236 arvados_api_client.arvados_v1_base.
237 sub(/\/arvados\/v1/, '').
238 sub(/^https?:\/\//, ''),
239 'ARVADOS_API_TOKEN' =>
240 @opts[:arvados_api_token],
241 'ARVADOS_API_HOST_INSECURE' =>
242 Rails.configuration.arvados_insecure_https ? 'true' : 'false'
244 IO.popen([env, 'arv-get', "#{@opts[:uuid]}/#{@opts[:file]}"],
246 while buf = io.read(2**20)
250 Rails.logger.warn("#{@opts[:uuid]}/#{@opts[:file]}: #{$?}") if $? != 0