OSDN Git Service

Init Project
[elecoma/elecoma.git] / app / controllers / image_resource_controller.rb
1 class ImageResourceController < BaseController
2   before_filter :set_mobile_spec
3
4   #caches_page :show
5   def show
6     if params[:filename] 
7       @res = ImageResource.find_by_name(params[:filename])
8     end
9     if params[:id]
10       @res ||= ImageResource.find_by_id(params[:id])
11     end
12     raise ActiveRecord::RecordNotFound unless @res
13     if @mobile_spec && @mobile_spec  =~ /^(\d+)x(\d+)$/
14       if params[:width].present? || params[:height].present?
15         width, height = [params[:width].to_i, params[:height].to_i]
16       else
17         width, height = [$1.to_i, $2.to_i]
18       end
19       send_file @res, @res.scaled_image(width, height)
20     else
21       send_file @res, @res.view
22     end
23   end
24
25   def thumbnail
26   end
27
28   private
29   
30   def send_file(res, data)
31     raise ActiveRecord::RecordNotFound unless res
32     raise ActiveRecord::RecordNotFound unless data
33     content_type = res.content_type
34     if request.mobile?
35       content_type.gsub!(/pjpeg/, "jpeg")
36     end
37     send_data(data, :type => content_type, :disposition => 'inline')
38   end
39
40   MOBILE_SPEC = Hash[*File.read("#{RAILS_ROOT}/db/migrate/fixed_data/mobile_specs.txt").split(/\n/).compact.map{|a|a.split(/\t/)[0...2]}.flatten]
41
42   def set_mobile_spec
43     begin
44       if request.mobile?
45         ua = request.user_agent || ''
46         @mobile_spec = case request.mobile
47                        when Jpmobile::Mobile::Docomo
48                          MOBILE_SPEC["i/#{$1}"] if ua =~ %r{^DoCoMo/(?:1.0/|2.0 )(\w+)}
49                        when Jpmobile::Mobile::Au
50                          MOBILE_SPEC["a/#{$1}"] if ua =~ %r{^(?:KDDI-|UP\.Browser/.*?-)(\w+)}
51                        when Jpmobile::Mobile::Softbank
52                          MOBILE_SPEC["s/#{$1}"] if ua =~ %r{^((.*?)/(.*?)/[\w-]+)}
53                        end
54         if @mobile_device
55           @mobile_spec = "#{@mobile_device.width}x#{@mobile_device.height}"
56         end
57         @mobile_spec ||= "240x320" 
58       end
59     rescue NoMethodError
60       # テストのときは user_agent メソッドがない
61       if request.mobile?
62         @mobile_spec ||= "240x320" if is_mobile?
63       end
64     end
65   end
66 end