add support for patterns
[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 #{@project} / #{@package}"
8     else
9       @page_title = "Install pattern #{@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     render :html, :layout => 'iframe'
20   end
21
22   # /download.json?project=name&package=name
23   # /download.json?project=name&pattern=name
24   def json
25     # needed for rails < 3.0 to support JSONP
26     render_json @data.to_json
27   end
28
29   private
30
31   def prepare
32     required_parameters :project
33     raise "Invalid project name" unless valid_project_name? params[:project]
34     raise "Invalid package name" unless params[:package].nil? or valid_package_name? params[:package]
35     raise "Invalid pattern name" unless params[:pattern].nil? or valid_pattern_name? params[:pattern]
36     @project = params[:project]
37     @package = params[:package]
38     @pattern = params[:pattern]
39     raise "Provide package or pattern parameter" if @package.nil? and @pattern.nil?
40     cache_key = "soo_download_#{@project}_#{@package}_#{@pattern}"
41
42     @data = Rails.cache.fetch(cache_key, :expires_in => 2.hours) do
43       if not @package.nil?
44         api_result = get_from_api("/search/published/binary/id?match=project='#{@project}'+and+package='#{@package}'")
45         xpath = "/collection/binary"
46       else
47 # TODO: fix filtering
48 #        api_result = get_from_api("/search/published/pattern/id?match=project='#{@project}'+and+filename='#{@pattern}.ymp'")
49         api_result = get_from_api("/search/published/pattern/id?match=project='#{@project}'")
50         xpath = "/collection/pattern"
51       end
52       if api_result
53         doc = REXML::Document.new api_result.body
54         data = Hash.new
55         doc.elements.each(xpath) { |e|
56           distro = e.attributes['repository']
57           if not data.has_key?(distro)
58             data[distro] = {
59               :repo => "http://download.opensuse.org/repositories/#{@project}/#{distro}/",
60               :package => Hash.new
61             }
62             case e.attributes['baseproject']
63               when /^(DISCONTINUED:)?openSUSE:/
64                 data[distro][:flavor] = 'openSUSE'
65                 if not @package.nil?
66                   data[distro][:ymp] = "http://software.opensuse.org/ymp/#{@project}/#{distro}/#{@package}.ymp"
67                 else
68                   data[distro][:ymp] = "http://download.opensuse.org/repositories/" + e.attributes['filepath']
69                 end
70               when /^(DISCONTINUED:)?SUSE:SLE-/
71                 data[distro][:flavor] = 'SLE'
72                 if not @package.nil?
73                   data[distro][:ymp] = "http://software.opensuse.org/ymp/#{@project}/#{distro}/#{@package}.ymp"
74                 else
75                   data[distro][:ymp] = "http://download.opensuse.org/repositories/" + e.attributes['filepath']
76                 end
77               when /^(DISCONTINUED:)?Fedora:/
78                 data[distro][:flavor] = 'Fedora'
79               when /^(DISCONTINUED:)?RedHat:RHEL-/
80                 data[distro][:flavor] = 'RHEL'
81               when /^(DISCONTINUED:)?ScientificLinux:/
82                 data[distro][:flavor] = 'SL'
83               when /^(DISCONTINUED:)?CentOS:CentOS-/
84                 data[distro][:flavor] = 'CentOS'
85               when /^(DISCONTINUED:)?Mandriva:/
86                 data[distro][:flavor] = 'Mandriva'
87               when /^(DISCONTINUED:)?Mageia:/
88                 data[distro][:flavor] = 'Mageia'
89               when /^(DISCONTINUED:)?Debian:/
90                 data[distro][:flavor] = 'Debian'
91               when /^(DISCONTINUED:)?Ubuntu:/
92                 data[distro][:flavor] = 'Ubuntu'
93               else
94                 data[distro][:flavor] = 'Unknown'
95             end
96           end
97           if not @package.nil?
98             filename = e.attributes['filename']
99             filepath = e.attributes['filepath']
100             data[distro][:package][filename] = 'http://download.opensuse.org/repositories/' + filepath
101           end
102         }
103         data
104       else
105         nil
106       end
107     end
108
109     if @data.nil?
110       head :forbidden
111     else
112       # collect distro types from @data
113       @flavors = @data.values.collect { |i| i[:flavor] }.uniq.sort{|x,y| x.downcase <=> y.downcase }
114     end
115   end
116
117 end