Milestone3
[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     cache_key = "soo_download_#{@prj}_#{@pkg}"
10
11     @data = Rails.cache.fetch(cache_key, :expires_in => 2.hours) do
12       api_result = get_from_api("/search/published/binary/id?match=project='#{@prj}'+and+package='#{@pkg}'")
13       if api_result
14         doc = REXML::Document.new api_result.body
15         data = Hash.new
16         doc.elements.each("/collection/binary") { |e|
17           distro = e.attributes['repository']
18           if not data.has_key?(distro)
19             data[distro] = {
20               :repo => "http://download.opensuse.org/repositories/#{@prj}/#{distro}/",
21               :pkg => Hash.new
22             }
23             case e.attributes['baseproject']
24               when /^(DISCONTINUED:)?openSUSE:/
25                 data[distro][:flavor] = 'openSUSE'
26                 data[distro][:ymp] = "http://software.opensuse.org/ymp/#{@prj}/#{distro}/#{@pkg}.ymp"
27               when /^(DISCONTINUED:)?SUSE:SLE-/
28                 data[distro][:flavor] = 'SLE'
29                 data[distro][:ymp] = "http://software.opensuse.org/ymp/#{@prj}/#{distro}/#{@pkg}.ymp"
30               when /^(DISCONTINUED:)?Fedora:/
31                 data[distro][:flavor] = 'Fedora'
32               when /^(DISCONTINUED:)?RedHat:RHEL-/
33                 data[distro][:flavor] = 'RHEL'
34               when /^(DISCONTINUED:)?ScientificLinux:/
35                 data[distro][:flavor] = 'SL'
36               when /^(DISCONTINUED:)?CentOS:CentOS-/
37                 data[distro][:flavor] = 'CentOS'
38               when /^(DISCONTINUED:)?Mandriva:/
39                 data[distro][:flavor] = 'Mandriva'
40               when /^(DISCONTINUED:)?Mageia:/
41                 data[distro][:flavor] = 'Mageia'
42               when /^(DISCONTINUED:)?Debian:/
43                 data[distro][:flavor] = 'Debian'
44               when /^(DISCONTINUED:)?Ubuntu:/
45                 data[distro][:flavor] = 'Ubuntu'
46               else
47                 data[distro][:flavor] = 'Unknown'
48             end
49           end
50           filename = e.attributes['filename']
51           filepath = e.attributes['filepath']
52           data[distro][:pkg][filename] = 'http://download.opensuse.org/repositories/' + filepath
53         }
54         data
55       else
56         nil
57       end
58     end
59
60     if @data.nil?
61       head :forbidden
62     else
63       # collect distro types from @data
64       @flavors = @data.values.collect { |i| i[:flavor] }.uniq.sort{|x,y| x.downcase <=> y.downcase }
65     end
66   end
67
68   # /download.html?prj=name&pkg=name
69   def html
70     render :html, :layout => false
71   end
72
73   # /download.json?prj=name&pkg=name
74   def json
75     # needed for rails < 3.0 to support JSONP
76     render_json @data.to_json
77   end
78
79 end