Merge branch '2753-collection-metadata'
[arvados.git] / apps / workbench / app / controllers / collections_controller.rb
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]
5
6   RELATION_LIMIT = 5
7
8   def show_pane_list
9     %w(Files Attributes Metadata Provenance_graph Used_by JSON API)
10   end
11
12   def set_persistent
13     case params[:value]
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
21     else
22       return unprocessable "Invalid value #{value.inspect}"
23     end
24     if params[:value] == 'persistent'
25       if not persist_links.any?
26         Link.create(link_class: 'resources',
27                     name: 'wants',
28                     tail_uuid: current_user.uuid,
29                     head_uuid: @object.uuid)
30       end
31     else
32       persist_links.each do |link|
33         link.destroy || raise
34       end
35     end
36
37     respond_to do |f|
38       f.json { render json: @object }
39     end
40   end
41
42   def index
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]])).
47         uniq { |c| c.uuid }
48     else
49       if params[:limit]
50         limit = params[:limit].to_i
51       else
52         limit = 100
53       end
54
55       if params[:offset]
56         offset = params[:offset].to_i
57       else
58         offset = 0
59       end
60
61       @collections = Collection.limit(limit).offset(offset)
62     end
63     @links = Link.limit(1000).
64       where(head_uuid: @collections.collect(&:uuid))
65     @collection_info = {}
66     @collections.each do |c|
67       @collection_info[c.uuid] = {
68         tag_links: [],
69         wanted: false,
70         wanted_by_me: false,
71         provenance: [],
72         links: []
73       }
74     end
75     @links.each do |link|
76       @collection_info[link.head_uuid] ||= {}
77       info = @collection_info[link.head_uuid]
78       case link.link_class
79       when 'tag'
80         info[:tag_links] << link
81       when 'resources'
82         info[:wanted] = true
83         info[:wanted_by_me] ||= link.tail_uuid == current_user.uuid
84       when 'provenance'
85         info[:provenance] << link.name
86       end
87       info[:links] << link
88     end
89     @request_url = request.url
90   end
91
92   def show_file
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.
97     coll = nil
98     usable_token = find_usable_token do
99       coll = Collection.find(params[:uuid])
100     end
101     if usable_token.nil?
102       return  # Response already rendered.
103     elsif params[:file].nil? or not file_in_collection?(coll, params[:file])
104       return render_not_found
105     end
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
113   end
114
115   def show
116     return super if !@object
117     if current_user
118       jobs_with = lambda do |conds|
119         Job.limit(RELATION_LIMIT).where(conds)
120           .results.sort_by { |j| j.finished_at || j.created_at }
121       end
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')
136         .results.any?
137     end
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
147   end
148
149   protected
150
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]
160     end
161     most_specific_error = [401]
162     read_tokens.each do |api_token|
163       using_specific_api_token(api_token) do
164         begin
165           yield
166           return api_token
167         rescue ArvadosApiClient::NotLoggedInException => error
168           status = 401
169         rescue => error
170           status = (error.message =~ /\[API: (\d+)\]$/) ? $1.to_i : nil
171           raise unless [401, 403, 404].include?(status)
172         end
173         if status >= most_specific_error.first
174           most_specific_error = [status, error]
175         end
176       end
177     end
178     case most_specific_error.shift
179     when 401, 403
180       redirect_to_login
181     when 404
182       render_not_found(*most_specific_error)
183     end
184     return nil
185   end
186
187   def file_in_collection?(collection, filename)
188     def normalized_path(part_list)
189       File.join(part_list).sub(%r{^\./}, '')
190     end
191     target = normalized_path([filename])
192     collection.files.each do |file_spec|
193       return true if (normalized_path(file_spec[0, 2]) == target)
194     end
195     false
196   end
197
198   def file_enumerator(opts)
199     FileStreamer.new opts
200   end
201
202   class FileStreamer
203     include ArvadosApiClientHelper
204     def initialize(opts={})
205       @opts = opts
206     end
207     def each
208       return unless @opts[:uuid] && @opts[:file]
209       env = Hash[ENV].
210         merge({
211                 'ARVADOS_API_HOST' =>
212                 arvados_api_client.arvados_v1_base.
213                 sub(/\/arvados\/v1/, '').
214                 sub(/^https?:\/\//, ''),
215                 'ARVADOS_API_TOKEN' =>
216                 @opts[:arvados_api_token],
217                 'ARVADOS_API_HOST_INSECURE' =>
218                 Rails.configuration.arvados_insecure_https ? 'true' : 'false'
219               })
220       IO.popen([env, 'arv-get', "#{@opts[:uuid]}/#{@opts[:file]}"],
221                'rb') do |io|
222         while buf = io.read(2**20)
223           yield buf
224         end
225       end
226       Rails.logger.warn("#{@opts[:uuid]}/#{@opts[:file]}: #{$?}") if $? != 0
227     end
228   end
229 end