Merge branch 'master' into 2596-refactor-pipeline-create
[arvados.git] / apps / workbench / app / controllers / actions_controller.rb
1 class ActionsController < ApplicationController
2
3   skip_before_filter :find_object_by_uuid, only: :post
4
5   def combine_selected_files_into_collection
6     lst = []
7     files = []
8     params["selection"].each do |s|
9       m = CollectionsHelper.match(s)
10       if m and m[1] and m[2]
11         lst.append(m[1] + m[2])
12         files.append(m)
13       end
14     end
15
16     collections = Collection.where(uuid: lst)
17
18     chash = {}
19     collections.each do |c|
20       c.reload()
21       chash[c.uuid] = c
22     end
23
24     combined = ""
25     files.each do |m|
26       mt = chash[m[1]+m[2]].manifest_text
27       if m[4]
28         IO.popen(['arv-normalize', '--extract', m[4][1..-1]], 'w+b') do |io|
29           io.write mt
30           io.close_write
31           while buf = io.read(2**20)
32             combined += buf
33           end
34         end
35       else
36         combined += chash[m[1]+m[2]].manifest_text
37       end
38     end
39
40     normalized = ''
41     IO.popen(['arv-normalize'], 'w+b') do |io|
42       io.write combined
43       io.close_write
44       while buf = io.read(2**20)
45         normalized += buf
46       end
47     end
48
49     require 'digest/md5'
50
51     d = Digest::MD5.new()
52     d << normalized
53     newuuid = "#{d.hexdigest}+#{normalized.length}"
54
55     env = Hash[ENV].
56       merge({
57               'ARVADOS_API_HOST' =>
58               $arvados_api_client.arvados_v1_base.
59               sub(/\/arvados\/v1/, '').
60               sub(/^https?:\/\//, ''),
61               'ARVADOS_API_TOKEN' => Thread.current[:arvados_api_token],
62               'ARVADOS_API_HOST_INSECURE' =>
63               Rails.configuration.arvados_insecure_https ? 'true' : 'false'
64             })
65
66     IO.popen([env, 'arv-put', '--raw'], 'w+b') do |io|
67       io.write normalized
68       io.close_write
69       while buf = io.read(2**20)
70
71       end
72     end
73
74     newc = Collection.new({:uuid => newuuid, :manifest_text => normalized})
75     newc.save!
76
77     chash.each do |k,v|
78       l = Link.new({
79                      tail_uuid: k,
80                      head_uuid: newuuid,
81                      link_class: "provenance",
82                      name: "provided"
83                    })
84       l.save!
85     end
86
87     redirect_to controller: 'collections', action: :show, id: newc.uuid
88   end
89
90   def post
91     if params["combine_selected_files_into_collection"]
92       combine_selected_files_into_collection
93     else
94       redirect_to :back
95     end
96   end
97 end