validate pkg and prj parameters
[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     raise "Invalid package name" unless valid_package_name? params[:pkg]
31     raise "Invalid project name" unless valid_project_name? params[:prj]
32     @prj = params[:prj]
33     @pkg = params[:pkg]
34     cache_key = "soo_download_#{@prj}_#{@pkg}"
35
36     @data = Rails.cache.fetch(cache_key, :expires_in => 2.hours) do
37       api_result = get_from_api("/search/published/binary/id?match=project='#{@prj}'+and+package='#{@pkg}'")
38       if api_result
39         doc = REXML::Document.new api_result.body
40         data = Hash.new
41         doc.elements.each("/collection/binary") { |e|
42           distro = e.attributes['repository']
43           if not data.has_key?(distro)
44             data[distro] = {
45               :repo => "http://download.opensuse.org/repositories/#{@prj}/#{distro}/",
46               :pkg => Hash.new
47             }
48             case e.attributes['baseproject']
49               when /^(DISCONTINUED:)?openSUSE:/
50                 data[distro][:flavor] = 'openSUSE'
51                 data[distro][:ymp] = "http://software.opensuse.org/ymp/#{@prj}/#{distro}/#{@pkg}.ymp"
52               when /^(DISCONTINUED:)?SUSE:SLE-/
53                 data[distro][:flavor] = 'SLE'
54                 data[distro][:ymp] = "http://software.opensuse.org/ymp/#{@prj}/#{distro}/#{@pkg}.ymp"
55               when /^(DISCONTINUED:)?Fedora:/
56                 data[distro][:flavor] = 'Fedora'
57               when /^(DISCONTINUED:)?RedHat:RHEL-/
58                 data[distro][:flavor] = 'RHEL'
59               when /^(DISCONTINUED:)?ScientificLinux:/
60                 data[distro][:flavor] = 'SL'
61               when /^(DISCONTINUED:)?CentOS:CentOS-/
62                 data[distro][:flavor] = 'CentOS'
63               when /^(DISCONTINUED:)?Mandriva:/
64                 data[distro][:flavor] = 'Mandriva'
65               when /^(DISCONTINUED:)?Mageia:/
66                 data[distro][:flavor] = 'Mageia'
67               when /^(DISCONTINUED:)?Debian:/
68                 data[distro][:flavor] = 'Debian'
69               when /^(DISCONTINUED:)?Ubuntu:/
70                 data[distro][:flavor] = 'Ubuntu'
71               else
72                 data[distro][:flavor] = 'Unknown'
73             end
74           end
75           filename = e.attributes['filename']
76           filepath = e.attributes['filepath']
77           data[distro][:pkg][filename] = 'http://download.opensuse.org/repositories/' + filepath
78         }
79         data
80       else
81         nil
82       end
83     end
84
85     if @data.nil?
86       head :forbidden
87     else
88       # collect distro types from @data
89       @flavors = @data.values.collect { |i| i[:flavor] }.uniq.sort{|x,y| x.downcase <=> y.downcase }
90     end
91   end
92
93 end