provide view also in software.o.o layout
[opensuse:software-o-o.git] / app / controllers / download_controller.rb
1 class DownloadController < ApplicationController
2
3   before_filter :prepare
4
5
6   def index
7     @page_title = "Install package #{params[:pkg]}"
8     @hide_search_box = true
9     @box_title = "Install package #{params[:pkg]}"
10     render :html
11   end
12
13   # /download.html?prj=name&pkg=name
14   def iframe
15     render :html, :layout => 'iframe'
16   end
17
18   # /download.json?prj=name&pkg=name
19   def json
20     # needed for rails < 3.0 to support JSONP
21     render_json @data.to_json
22   end
23
24
25
26   private
27
28   def prepare
29     required_parameters :prj, :pkg
30     @prj = params[:prj]
31     @pkg = params[:pkg]
32     cache_key = "soo_download_#{@prj}_#{@pkg}"
33
34     @data = Rails.cache.fetch(cache_key, :expires_in => 2.hours) do
35       api_result = get_from_api("/search/published/binary/id?match=project='#{@prj}'+and+package='#{@pkg}'")
36       if api_result
37         doc = REXML::Document.new api_result.body
38         data = Hash.new
39         doc.elements.each("/collection/binary") { |e|
40           distro = e.attributes['repository']
41           if not data.has_key?(distro)
42             data[distro] = {
43               :repo => "http://download.opensuse.org/repositories/#{@prj}/#{distro}/",
44               :pkg => Hash.new
45             }
46             case e.attributes['baseproject']
47               when /^(DISCONTINUED:)?openSUSE:/
48                 data[distro][:flavor] = 'openSUSE'
49                 data[distro][:ymp] = "http://software.opensuse.org/ymp/#{@prj}/#{distro}/#{@pkg}.ymp"
50               when /^(DISCONTINUED:)?SUSE:SLE-/
51                 data[distro][:flavor] = 'SLE'
52                 data[distro][:ymp] = "http://software.opensuse.org/ymp/#{@prj}/#{distro}/#{@pkg}.ymp"
53               when /^(DISCONTINUED:)?Fedora:/
54                 data[distro][:flavor] = 'Fedora'
55               when /^(DISCONTINUED:)?RedHat:RHEL-/
56                 data[distro][:flavor] = 'RHEL'
57               when /^(DISCONTINUED:)?ScientificLinux:/
58                 data[distro][:flavor] = 'SL'
59               when /^(DISCONTINUED:)?CentOS:CentOS-/
60                 data[distro][:flavor] = 'CentOS'
61               when /^(DISCONTINUED:)?Mandriva:/
62                 data[distro][:flavor] = 'Mandriva'
63               when /^(DISCONTINUED:)?Mageia:/
64                 data[distro][:flavor] = 'Mageia'
65               when /^(DISCONTINUED:)?Debian:/
66                 data[distro][:flavor] = 'Debian'
67               when /^(DISCONTINUED:)?Ubuntu:/
68                 data[distro][:flavor] = 'Ubuntu'
69               else
70                 data[distro][:flavor] = 'Unknown'
71             end
72           end
73           filename = e.attributes['filename']
74           filepath = e.attributes['filepath']
75           data[distro][:pkg][filename] = 'http://download.opensuse.org/repositories/' + filepath
76         }
77         data
78       else
79         nil
80       end
81     end
82
83     if @data.nil?
84       head :forbidden
85     else
86       # collect distro types from @data
87       @flavors = @data.values.collect { |i| i[:flavor] }.uniq.sort{|x,y| x.downcase <=> y.downcase }
88     end
89   end
90
91 end