remember search queries - and their stats
[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     DownloadHistory.create :query => params[:query], :base => params[:base],
15                            :ymp => path
16     res = get_from_api(path)
17     render :text => res.body, :content_type => res.content_type
18   end
19
20   def ymp_without_arch_and_version
21     path = "/published/#{params[:project]}/#{params[:repository]}/#{params[:package]}?view=ymp"
22     DownloadHistory.create :query => params[:query], :base => params[:base],
23                            :ymp => path
24     res = get_from_api(path)
25     render :text => res.body, :content_type => res.content_type
26   end
27
28   def set_release(release)
29     if release == "111"
30        @isos = {}
31        @directory = "http://download.opensuse.org/distribution/11.1"
32        @isos["lang-32"] = "11.1-Addon-Lang-i586"
33        @isos["lang-64"] = "11.1-Addon-Lang-x86_64"
34        @isos["nonoss"] = "11.1-Addon-NonOss-BiArch-i586-x86_64"
35        @isos["kde-64"] = "11.1-KDE4-LiveCD-x86_64"
36        @isos["kde-32"] = "11.1-KDE4-LiveCD-i686"
37        @isos["gnome-64"] = "11.1-GNOME-LiveCD-x86_64"
38        @isos["gnome-32"] = "11.1-GNOME-LiveCD-i686"
39        @isos["dvd-64"] = "11.1-DVD-x86_64"
40        @isos["dvd-32"] = "11.1-DVD-i586"
41        @isos["net-32"] = "11.1-NET-i586"
42        @isos["net-64"] = "11.1-NET-x86_64"
43
44        @releasenotes = "http://www.suse.de/relnotes/i386/openSUSE/11.1/RELEASE-NOTES.en.html"
45        @releasename = "openSUSE 11.1"
46        @repourl = "http://download.opensuse.org/distribution/11.1"
47        @medium = "dvd"
48     elsif release == "112"
49        @isos = {}
50        @directory = "http://download.opensuse.org/distribution/11.2"
51        @isos["lang-32"] = "11.2-Addon-Lang-i586"
52        @isos["lang-64"] = "11.2-Addon-Lang-x86_64"
53        @isos["nonoss"] = "11.2-Addon-NonOss-BiArch-i586-x86_64"
54        @isos["kde-64"] = "11.2-KDE4-LiveCD-x86_64"
55        @isos["kde-32"] = "11.2-KDE4-LiveCD-i686"
56        @isos["gnome-64"] = "11.2-GNOME-LiveCD-x86_64"
57        @isos["gnome-32"] = "11.2-GNOME-LiveCD-i686"
58        @isos["dvd-64"] = "11.2-DVD-x86_64"
59        @isos["dvd-32"] = "11.2-DVD-i586"
60        @isos["net-32"] = "11.2-NET-i586"
61        @isos["net-64"] = "11.2-NET-x86_64"
62
63        @releasenotes = "http://www.suse.de/relnotes/i386/openSUSE/11.2/RELEASE-NOTES.en.html"
64        @releasename = "openSUSE 11.2"
65        @repourl = "http://download.opensuse.org/distribution/11.2"
66        @medium = "dvd"
67     elsif release == "developer"
68        @isos = {}
69        @directory = "http://download.opensuse.org/distribution/11.3-Milestone5"
70        @isos["lang-32"] = "Addon-Lang-Build0556-i586"
71        @isos["lang-64"] = "Addon-Lang-Build0556-x86_64"
72        @isos["nonoss"] = "Addon-NonOss-BiArch-Build0556-i586-x86_64"
73        @isos["kde-64"] = "KDE-LiveCD-Build0556-x86_64"
74        @isos["kde-32"] = "KDE-LiveCD-Build0556-i686"
75        @isos["gnome-64"] = "GNOME-LiveCD-Build0556-x86_64"
76        @isos["gnome-32"] = "GNOME-LiveCD-Build0556-i686"
77        @isos["dvd-64"] = "DVD-Build0556-x86_64"
78        @isos["dvd-32"] = "DVD-Build0556-i586"
79        @isos["net-32"] = "NET-Build0556-i586"
80        @isos["net-64"] = "NET-Build0556-x86_64"
81
82        @releasenotes = "http://www.suse.de/relnotes/i386/openSUSE/11.3/RELEASE-NOTES.en.html"
83        @releasename = "openSUSE 11.3-Milestone5"
84        @repourl = "http://download.opensuse.org/distribution/11.3"
85        @medium = "dvd"
86     end
87     @release = release
88   end
89
90   def redirectit(release)
91     if params[:lang].nil?
92       lang = request.compatible_language_from(LANGUAGES) || "en"
93     else
94       lang = params[:lang][0]
95     end
96     notice = nil
97     url = "/%s/%s" % [release, lang]
98     if request.user_agent && request.user_agent.index('Mozilla/5.0 (compatible; Konqueror/3')
99         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
100     end
101     if notice
102        render :template => "main/redirect_with_notice", :locals => { :notice => notice }
103     else
104        redirect_to url
105     end
106   end
107
108   def developer
109     redirectit("developer")
110     #redirect_to "http://en.opensuse.org/Factory"
111   end
112    
113   def index
114     redirectit("112")
115   end
116
117   def release
118     @lang = params[:lang][0].gsub(/\-/, '_')
119     GetText.locale = @lang
120
121     set_release(params[:release])
122     render :template => "main/release"
123   end
124
125   def change_install
126     set_release(params[:release])
127     @medium = params[:medium]
128     @lang = params[:lang]
129     render :template => "main/release"
130   end
131
132   def download_js
133     set_release(params[:release])
134     render :template => "main/download", :content_type => 'text/javascript', :layout => false
135   end
136
137   def show_request
138     render :template => "main/testrequest", :layout => false
139   end
140
141   def download
142     set_release(params[:release])
143     medium = params[:medium]
144
145     if params[:arch] == "i686"
146       medium += "-32"
147     else
148       medium += "-64"
149     end
150
151     suffix = ".iso"
152     
153     case
154     when params[:protocol] == "torrent"
155       if params[:medium] != "net"
156           suffix = ".iso.torrent"
157       end
158     when params[:protocol] == "mirror"
159       suffix = ".iso?mirrorlist"
160     when params[:protocol] == "metalink"
161       suffix = ".iso.metalink"
162     end
163     redirect_to @directory + "/iso/openSUSE-" + @isos[medium] + suffix
164
165   end
166
167   private
168   
169   def get_from_api(path)
170     req = Net::HTTP::Get.new(path)
171     req['x-username'] = "obs_read_only"
172
173     host, port = API_HOST.split(/:/)
174     port ||= 80
175     res = Net::HTTP.new(host, port).start do |http|
176       http.request(req)
177     end
178   end
179 end