2961: Don't call #render_index from #index on folders_controller because #index
[arvados.git] / apps / workbench / app / controllers / actions_controller.rb
1 class ActionsController < ApplicationController
2
3   @@exposed_actions = {}
4   def self.expose_action method, &block
5     @@exposed_actions[method] = true
6     define_method method, block
7   end
8
9   def model_class
10     ArvadosBase::resource_class_for_uuid(params[:uuid])
11   end
12
13   def post
14     params.keys.collect(&:to_sym).each do |param|
15       if @@exposed_actions[param]
16         return self.send(param)
17       end
18     end
19     redirect_to :back
20   end
21
22   expose_action :copy_selections_into_folder do
23     already_named = Link.
24       filter([['tail_uuid','=',@object.uuid],
25               ['head_uuid','in',params["selection"]]]).
26       collect(&:head_uuid)
27     (params["selection"] - already_named).each do |s|
28       Link.create(tail_uuid: @object.uuid,
29                   head_uuid: s,
30                   link_class: 'name',
31                   name: "#{s} added #{Time.now}")
32     end
33     redirect_to @object
34   end
35
36   expose_action :combine_selected_files_into_collection do
37     lst = []
38     files = []
39     params["selection"].each do |s|
40       m = CollectionsHelper.match(s)
41       if m and m[1] and m[2]
42         lst.append(m[1] + m[2])
43         files.append(m)
44       end
45     end
46
47     collections = Collection.where(uuid: lst)
48
49     chash = {}
50     collections.each do |c|
51       c.reload()
52       chash[c.uuid] = c
53     end
54
55     combined = ""
56     files.each do |m|
57       mt = chash[m[1]+m[2]].manifest_text
58       if m[4]
59         IO.popen(['arv-normalize', '--extract', m[4][1..-1]], 'w+b') do |io|
60           io.write mt
61           io.close_write
62           while buf = io.read(2**20)
63             combined += buf
64           end
65         end
66       else
67         combined += chash[m[1]+m[2]].manifest_text
68       end
69     end
70
71     normalized = ''
72     IO.popen(['arv-normalize'], 'w+b') do |io|
73       io.write combined
74       io.close_write
75       while buf = io.read(2**20)
76         normalized += buf
77       end
78     end
79
80     require 'digest/md5'
81
82     d = Digest::MD5.new()
83     d << normalized
84     newuuid = "#{d.hexdigest}+#{normalized.length}"
85
86     env = Hash[ENV].
87       merge({
88               'ARVADOS_API_HOST' =>
89               arvados_api_client.arvados_v1_base.
90               sub(/\/arvados\/v1/, '').
91               sub(/^https?:\/\//, ''),
92               'ARVADOS_API_TOKEN' => Thread.current[:arvados_api_token],
93               'ARVADOS_API_HOST_INSECURE' =>
94               Rails.configuration.arvados_insecure_https ? 'true' : 'false'
95             })
96
97     IO.popen([env, 'arv-put', '--raw'], 'w+b') do |io|
98       io.write normalized
99       io.close_write
100       while buf = io.read(2**20)
101
102       end
103     end
104
105     newc = Collection.new({:uuid => newuuid, :manifest_text => normalized})
106     newc.save!
107
108     chash.each do |k,v|
109       l = Link.new({
110                      tail_uuid: k,
111                      head_uuid: newuuid,
112                      link_class: "provenance",
113                      name: "provided"
114                    })
115       l.save!
116     end
117
118     redirect_to controller: 'collections', action: :show, id: newc.uuid
119   end
120
121 end