apply workaround for pattern search
[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 #        api_result = get_from_api("/search/published/pattern/id?match=project='#{@project}'+and+filename='#{@pattern}.ymp'")
48 # TODO: workaround - the line above does not return a thing - see http://lists.opensuse.org/opensuse-buildservice/2011-07/msg00088.html
49         api_result = get_from_api("/search/published/pattern/id?match=project='#{@project}'")
50 # END
51         xpath = "/collection/pattern"
52       end
53       if api_result
54         doc = REXML::Document.new api_result.body
55         data = Hash.new
56         doc.elements.each(xpath) { |e|
57 # TODO: workaround - filter by filename here - see comment few lines above for explanation
58           next if not @pattern.nil? and e.attributes['filename'] != "#{@pattern}.ymp"
59 # END
60           distro = e.attributes['repository']
61           if not data.has_key?(distro)
62             data[distro] = {
63               :repo => "http://download.opensuse.org/repositories/#{@project}/#{distro}/",
64               :package => Hash.new
65             }
66             case e.attributes['baseproject']
67               when /^(DISCONTINUED:)?openSUSE:/
68                 data[distro][:flavor] = 'openSUSE'
69                 if not @package.nil?
70                   data[distro][:ymp] = "http://software.opensuse.org/ymp/#{@project}/#{distro}/#{@package}.ymp"
71                 else
72                   data[distro][:ymp] = "http://download.opensuse.org/repositories/" + e.attributes['filepath']
73                 end
74               when /^(DISCONTINUED:)?SUSE:SLE-/
75                 data[distro][:flavor] = 'SLE'
76                 if not @package.nil?
77                   data[distro][:ymp] = "http://software.opensuse.org/ymp/#{@project}/#{distro}/#{@package}.ymp"
78                 else
79                   data[distro][:ymp] = "http://download.opensuse.org/repositories/" + e.attributes['filepath']
80                 end
81               when /^(DISCONTINUED:)?Fedora:/
82                 data[distro][:flavor] = 'Fedora'
83               when /^(DISCONTINUED:)?RedHat:RHEL-/
84                 data[distro][:flavor] = 'RHEL'
85               when /^(DISCONTINUED:)?ScientificLinux:/
86                 data[distro][:flavor] = 'SL'
87               when /^(DISCONTINUED:)?CentOS:CentOS-/
88                 data[distro][:flavor] = 'CentOS'
89               when /^(DISCONTINUED:)?Mandriva:/
90                 data[distro][:flavor] = 'Mandriva'
91               when /^(DISCONTINUED:)?Mageia:/
92                 data[distro][:flavor] = 'Mageia'
93               when /^(DISCONTINUED:)?Debian:/
94                 data[distro][:flavor] = 'Debian'
95               when /^(DISCONTINUED:)?Ubuntu:/
96                 data[distro][:flavor] = 'Ubuntu'
97               else
98                 data[distro][:flavor] = 'Unknown'
99             end
100           end
101           if not @package.nil?
102             filename = e.attributes['filename']
103             filepath = e.attributes['filepath']
104             data[distro][:package][filename] = 'http://download.opensuse.org/repositories/' + filepath
105           end
106         }
107         data
108       else
109         nil
110       end
111     end
112
113     if @data.nil?
114       head :forbidden
115     else
116       # collect distro types from @data
117       @flavors = @data.values.collect { |i| i[:flavor] }.uniq.sort{|x,y| x.downcase <=> y.downcase }
118     end
119   end
120
121 end