little debug helper
[opensuse:software-o-o.git] / app / controllers / main_controller.rb
1 require 'net/http'
2 require 'gettext_rails'
3
4 class MainController < ApplicationController
5
6   verify :only => :ymp, :params => [:project, :repository, :arch, :binary],
7     :redirect_to => :index
8
9   # these pages are completely static:
10   caches_page :release, :download_js
11
12   def ymp_with_arch_and_version
13     path = "/published/#{params[:project]}/#{params[:repository]}/#{params[:arch]}/#{params[:binary]}?view=ymp"
14     res = get_from_api(path)
15     render :text => res.body, :content_type => res.content_type
16   end
17
18   def ymp_without_arch_and_version
19     path = "/published/#{params[:project]}/#{params[:repository]}/#{params[:package]}?view=ymp"
20     res = get_from_api(path)
21     render :text => res.body, :content_type => res.content_type
22   end
23
24   def set_release(release)
25     if release == "111"
26        @isos = {}
27        @directory = "http://download.opensuse.org/distribution/11.1"
28        @isos["lang-32"] = "11.1-Addon-Lang-i586"
29        @isos["lang-64"] = "11.1-Addon-Lang-x86_64"
30        @isos["nonoss"] = "11.1-Addon-NonOss-BiArch-i586-x86_64"
31        @isos["kde-64"] = "11.1-KDE4-LiveCD-x86_64"
32        @isos["kde-32"] = "11.1-KDE4-LiveCD-i686"
33        @isos["gnome-64"] = "11.1-GNOME-LiveCD-x86_64"
34        @isos["gnome-32"] = "11.1-GNOME-LiveCD-i686"
35        @isos["dvd-64"] = "11.1-DVD-x86_64"
36        @isos["dvd-32"] = "11.1-DVD-i586"
37        @isos["net-32"] = "11.1-NET-i586"
38        @isos["net-64"] = "11.1-NET-x86_64"
39
40        @releasenotes = "http://www.suse.de/relnotes/i386/openSUSE/11.1/RELEASE-NOTES.en.html"
41        @releasename = "openSUSE 11.1"
42        @repourl = "http://download.opensuse.org/distribution/11.1"
43        @medium = "dvd"
44     elsif release == "112"
45        @isos = {}
46        @directory = "http://download.opensuse.org/distribution/11.2"
47        @isos["lang-32"] = "11.2-Addon-Lang-i586"
48        @isos["lang-64"] = "11.2-Addon-Lang-x86_64"
49        @isos["nonoss"] = "11.2-Addon-NonOss-BiArch-i586-x86_64"
50        @isos["kde-64"] = "11.2-KDE4-LiveCD-x86_64"
51        @isos["kde-32"] = "11.2-KDE4-LiveCD-i686"
52        @isos["gnome-64"] = "11.2-GNOME-LiveCD-x86_64"
53        @isos["gnome-32"] = "11.2-GNOME-LiveCD-i686"
54        @isos["dvd-64"] = "11.2-DVD-x86_64"
55        @isos["dvd-32"] = "11.2-DVD-i586"
56        @isos["net-32"] = "11.2-NET-i586"
57        @isos["net-64"] = "11.2-NET-x86_64"
58
59        @releasenotes = "http://www.suse.de/relnotes/i386/openSUSE/11.2/RELEASE-NOTES.en.html"
60        @releasename = "openSUSE 11.2"
61        @repourl = "http://download.opensuse.org/distribution/11.2"
62        @medium = "dvd"
63     elsif release == "developer"
64        @isos = {}
65        @directory = "http://download.opensuse.org/distribution/11.2-RC2"
66        @isos["lang-32"] = "Addon-Lang-Build0339-i586"
67        @isos["lang-64"] = "Addon-Lang-Build0339-x86_64"
68        @isos["nonoss"] = "Addon-NonOss-BiArch-Build0339-i586-x86_64"
69        @isos["kde-64"] = "KDE4-LiveCD-Build0339-x86_64"
70        @isos["kde-32"] = "KDE4-LiveCD-Build0339-i686"
71        @isos["gnome-64"] = "GNOME-LiveCD-Build0339-x86_64"
72        @isos["gnome-32"] = "GNOME-LiveCD-Build0339-i686"
73        @isos["dvd-64"] = "DVD-Build0339-x86_64"
74        @isos["dvd-32"] = "DVD-Build0339-i586"
75        @isos["net-32"] = "NET-Build0339-i586"
76        @isos["net-64"] = "NET-Build0339-x86_64"
77
78        @releasenotes = "http://www.suse.de/relnotes/i386/openSUSE/11.2/RELEASE-NOTES.en.html"
79        @releasename = "openSUSE 11.2-RC2"
80        @repourl = "http://download.opensuse.org/distribution/11.2"
81        @medium = "dvd"
82     end
83     @release = release
84   end
85
86   def redirectit(release)
87     if params[:lang].nil?
88       lang = request.compatible_language_from(LANGUAGES) || "en"
89     else
90       lang = params[:lang][0]
91     end
92     notice = nil
93     url = "/%s/%s" % [release, lang]
94     if request.user_agent.index('Mozilla/5.0 (compatible; Konqueror/3')
95         notice = _("Konqueror of KDE 3 is unfortunately unmaintained and its javascript implementation contains bugs that make it impossible to use with this page. Please make sure you have javascript disabled before you <a href='%s'>continue</a>.") % url
96     end
97     if notice
98        render :template => "main/redirect_with_notice", :locals => { :notice => notice }
99     else
100        redirect_to url
101     end
102   end
103
104   def developer
105     redirectit("developer")
106   end
107    
108   def index
109     redirectit("111")
110   end
111
112   def release
113     @lang = params[:lang][0]
114     GetText.locale = @lang
115
116     set_release(params[:release])
117     render :template => "main/release"
118   end
119
120   def change_install
121     set_release(params[:release])
122     @medium = params[:medium]
123     @lang = params[:lang]
124     render :template => "main/release"
125   end
126
127   def download_js
128     set_release(params[:release])
129     render :template => "main/download", :content_type => 'text/javascript', :layout => false
130   end
131
132   def show_request
133     render :template => "main/testrequest", :layout => false
134   end
135
136   def download
137     set_release(params[:release])
138     medium = params[:medium]
139
140     if params[:arch] == "i686"
141       medium += "-32"
142     else
143       medium += "-64"
144     end
145
146     suffix = ".iso"
147     
148     case
149     when params[:protocol] == "torrent"
150       if params[:medium] != "net"
151           suffix = ".iso.torrent"
152       end
153     when params[:protocol] == "mirror"
154       suffix = ".iso?mirrorlist"
155     when params[:protocol] == "metalink"
156       suffix = ".iso.metalink"
157     end
158     redirect_to @directory + "/iso/openSUSE-" + @isos[medium] + suffix
159
160   end
161
162   private
163   
164   def get_from_api(path)
165     req = Net::HTTP::Get.new(path)
166     req['x-username'] = "obs_read_only"
167
168     host, port = API_HOST.split(/:/)
169     port ||= 80
170     res = Net::HTTP.new(host, port).start do |http|
171       http.request(req)
172     end
173   end
174 end