accept also '012345' values for download page color attributes (only '012' were accep...
[opensuse:software-o-o.git] / app / controllers / download_controller.rb
1 class DownloadController < ApplicationController
2
3   before_filter :prepare
4
5   def index
6     if @package
7       @page_title = _("Install package %s / %s") % [@project, @package]
8     else
9       @page_title = _("Install pattern %s / %s") % [@project, @pattern]
10     end
11     @box_title  = @page_title
12     @hide_search_box = true
13     render :html, :layout => 'download'
14   end
15
16   # /download.html?project=name&package=name
17   # /download.html?project=name&pattern=name
18   def iframe
19     if params[:acolor]
20       raise "Invalid acolor value (has to be 000-fff)" unless params[:acolor] =~ /^[0-9a-f]{3}([0-9a-f]{3})?$/
21       @acolor = '#' + params[:acolor]
22     end
23     if params[:bcolor]
24       raise "Invalid bcolor value (has to be 000-fff)" unless params[:bcolor] =~ /^[0-9a-f]{3}([0-9a-f]{3})?$/
25       @bcolor = '#' + params[:bcolor]
26     end
27     if params[:fcolor]
28       raise "Invalid fcolor value (has to be 000-fff)" unless params[:fcolor] =~ /^[0-9a-f]{3}([0-9a-f]{3})?$/
29       @fcolor = '#' + params[:fcolor]
30     end
31     if params[:hcolor]
32       raise "Invalid hcolor value (has to be 000-fff)" unless params[:hcolor] =~ /^[0-9a-f]{3}([0-9a-f]{3})?$/
33       @hcolor = '#' + params[:hcolor]
34     end
35     render :html, :layout => 'iframe'
36   end
37
38   # /download.json?project=name&package=name
39   # /download.json?project=name&pattern=name
40   def json
41     # needed for rails < 3.0 to support JSONP
42     render_json @data.to_json
43   end
44
45   private
46
47   def prepare
48     required_parameters :project
49     raise "Invalid project name" unless valid_project_name? params[:project]
50     raise "Invalid package name" unless params[:package].nil? or valid_package_name? params[:package]
51     raise "Invalid pattern name" unless params[:pattern].nil? or valid_pattern_name? params[:pattern]
52     @project = params[:project]
53     @package = params[:package]
54     @pattern = params[:pattern]
55     raise "Provide package or pattern parameter" if @package.nil? and @pattern.nil?
56     cache_key = "soo_download_#{@project}_#{@package}_#{@pattern}"
57
58     @data = Rails.cache.fetch(cache_key, :expires_in => 2.hours) do
59       if not @package.nil?
60         api_result = get_from_api("/search/published/binary/id?match=project='#{@project}'+and+package='#{@package}'")
61         xpath = "/collection/binary"
62       else
63 #        api_result = get_from_api("/search/published/pattern/id?match=project='#{@project}'+and+filename='#{@pattern}.ymp'")
64 # TODO: workaround - the line above does not return a thing - see http://lists.opensuse.org/opensuse-buildservice/2011-07/msg00088.html
65         api_result = get_from_api("/search/published/pattern/id?match=project='#{@project}'")
66 # END
67         xpath = "/collection/pattern"
68       end
69       if api_result
70         doc = REXML::Document.new api_result.body
71         data = Hash.new
72         doc.elements.each(xpath) { |e|
73 # TODO: workaround - filter by filename here - see comment few lines above for explanation
74           next if not @pattern.nil? and e.attributes['filename'] != "#{@pattern}.ymp"
75 # END
76           distro = e.attributes['repository']
77           if not data.has_key?(distro)
78             data[distro] = {
79               :repo => "http://download.opensuse.org/repositories/#{@project}/#{distro}/",
80               :package => Hash.new
81             }
82             case e.attributes['baseproject']
83               when /^(DISCONTINUED:)?openSUSE:/
84                 data[distro][:flavor] = 'openSUSE'
85                 if not @package.nil?
86                   data[distro][:ymp] = "http://software.opensuse.org/ymp/#{@project}/#{distro}/#{@package}.ymp"
87                 else
88                   data[distro][:ymp] = "http://download.opensuse.org/repositories/" + e.attributes['filepath']
89                 end
90               when /^(DISCONTINUED:)?SUSE:SLE-/
91                 data[distro][:flavor] = 'SLE'
92                 if not @package.nil?
93                   data[distro][:ymp] = "http://software.opensuse.org/ymp/#{@project}/#{distro}/#{@package}.ymp"
94                 else
95                   data[distro][:ymp] = "http://download.opensuse.org/repositories/" + e.attributes['filepath']
96                 end
97               when /^(DISCONTINUED:)?Fedora:/
98                 data[distro][:flavor] = 'Fedora'
99               when /^(DISCONTINUED:)?RedHat:RHEL-/
100                 data[distro][:flavor] = 'RHEL'
101               when /^(DISCONTINUED:)?ScientificLinux:/
102                 data[distro][:flavor] = 'SL'
103               when /^(DISCONTINUED:)?CentOS:CentOS-/
104                 data[distro][:flavor] = 'CentOS'
105               when /^(DISCONTINUED:)?Mandriva:/
106                 data[distro][:flavor] = 'Mandriva'
107               when /^(DISCONTINUED:)?Mageia:/
108                 data[distro][:flavor] = 'Mageia'
109               when /^(DISCONTINUED:)?Debian:/
110                 data[distro][:flavor] = 'Debian'
111               when /^(DISCONTINUED:)?Ubuntu:/
112                 data[distro][:flavor] = 'Ubuntu'
113               else
114                 data[distro][:flavor] = 'Unknown'
115             end
116           end
117           if not @package.nil?
118             filename = e.attributes['filename']
119             filepath = e.attributes['filepath']
120             data[distro][:package][filename] = 'http://download.opensuse.org/repositories/' + filepath
121           end
122         }
123         data
124       else
125         nil
126       end
127     end
128
129     if @data.nil?
130       head :forbidden
131     else
132       # collect distro types from @data
133       @flavors = @data.values.collect { |i| i[:flavor] }.uniq.sort{|x,y| x.downcase <=> y.downcase }
134     end
135   end
136
137 end