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