1 class ImageResourceController < BaseController
2 before_filter :set_mobile_spec
7 @res = ImageResource.find_by_name(params[:filename])
10 @res ||= ImageResource.find_by_id(params[:id])
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]
17 width, height = [$1.to_i, $2.to_i]
19 send_file @res, @res.scaled_image(width, height)
21 send_file @res, @res.view
30 def send_file(res, data)
31 raise ActiveRecord::RecordNotFound unless res
32 raise ActiveRecord::RecordNotFound unless data
33 content_type = res.content_type
35 content_type.gsub!(/pjpeg/, "jpeg")
37 send_data(data, :type => content_type, :disposition => 'inline')
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]
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-]+)}
55 @mobile_spec = "#{@mobile_device.width}x#{@mobile_device.height}"
57 @mobile_spec ||= "240x320"
60 # テストのときは user_agent メソッドがない
62 @mobile_spec ||= "240x320" if is_mobile?