OSDN Git Service

884d33cb613b8ba28c69aaea9796ecbbdd79bc52
[pettanr/pettanr.git] / app / controllers / system_pictures_controller.rb
1 class SystemPicturesController < ApplicationController
2   before_filter :authenticate_user!, :only => [:index, :show]
3   before_filter :authenticate_admin!, :only => [:list, :browse, :create, :update, :destroy]
4
5   private
6   
7   def set_image(prm)
8     img = nil
9     if (f = prm[:system_picture][:file]).respond_to?(:read)
10       if f.size > 1000000
11         @system_picture.width = 0
12         @system_picture.height = 0
13         @system_picture.ext = 'none'
14         @system_picture.filesize = 1.megabytes
15       else
16         img = Magick::Image.from_blob(f.read).shift
17         @system_picture.width = img.columns
18         @system_picture.height = img.rows
19         @system_picture.ext = img.format.downcase
20         @system_picture.filesize = f.size
21       end
22     else
23       dat = Base64.decode64(prm[:system_picture][:file].to_s.gsub(' ', '+')) #rubyのバグ?+でデコードされるべきキャラがスペースになる
24       img = Magick::Image.from_blob(dat).shift
25       @system_picture.width = img.columns
26       @system_picture.height = img.rows
27       @system_picture.ext = img.format.downcase
28       @system_picture.filesize = 1000
29     end
30     img
31   end
32   
33   public
34   
35   # GET /system_pictures
36   # GET /system_pictures.json
37   def index
38     @system_pictures = SystemPicture.all
39
40     respond_to do |format|
41       format.html # index.html.erb
42       format.json { render json: @system_pictures }
43     end
44   end
45
46   # GET /system_pictures/1
47   # GET /system_pictures/1.json
48   def show
49     @system_picture = SystemPicture.find(params[:id])
50
51     respond_to do |format|
52       opt = {:type => @system_picture.mime_type, :disposition=>"inline"}
53       format.png { send_data(@system_picture.restore, opt ) }
54       format.gif { send_data(@system_picture.restore, opt ) }
55       format.jpeg { send_data(@system_picture.restore, opt ) }
56       format.html # show.html.erb
57       format.json { render json: @system_picture}
58     end
59   end
60
61   def list
62     @system_pictures = SystemPicture.all
63
64     respond_to do |format|
65       format.html { render layout: 'system' }
66       format.json { render json: @system_pictures }
67     end
68   end
69
70   def browse
71     @system_picture = SystemPicture.find(params[:id])
72
73     respond_to do |format|
74       format.html { render layout: 'system' }
75       format.json { render json: @system_picture}
76     end
77   end
78
79
80   # POST /system_pictures
81   # POST /system_pictures.json
82   def create
83     @system_picture = SystemPicture.new
84     img = set_image params
85
86     respond_to do |format|
87       SystemPicture.transaction do
88         if @system_picture.save
89           if @system_picture.store(img)
90             format.html { redirect_to @system_picture, notice: 'system picture was successfully created.' }
91             format.json { render json: @system_picture, status: :created, location: @system_picture }
92           else
93             format.html { redirect_to @system_picture, notice: 'Failed! system picture was NOT created.' }
94             format.json { render json: @system_picture.errors, status: :unprocessable_entity }
95           end
96         else
97           format.html { render action: "new" }
98           format.json { render json: @system_picture.errors, status: :unprocessable_entity }
99         end
100       end
101     end
102   end
103
104   # PUT /system_pictures/1
105   # PUT /system_pictures/1.json
106   def update
107     @system_picture = SystemPicture.find(params[:id])
108     img = set_image params
109
110     respond_to do |format|
111       SystemPicture.transaction do
112         if @system_picture.save
113           if @system_picture.store(img)
114             format.html { redirect_to @system_picture, notice: 'System picture was successfully updated.' }
115             format.json { head :ok }
116           else
117             format.html { redirect_to @system_picture, notice: 'Failed! System picture was NOT created.' }
118             format.json { render json: @system_picture.errors, status: :unprocessable_entity }
119           end
120         else
121           format.html { render action: "edit" }
122           format.json { render json: @system_picture.errors, status: :unprocessable_entity }
123         end
124       end
125     end
126   end
127
128   # DELETE /system_pictures/1
129   # DELETE /system_pictures/1.json
130   def destroy
131     @system_picture = SystemPicture.find(params[:id])
132     SystemPicture.transaction do
133       @system_picture.destroy
134       
135     end
136     
137     respond_to do |format|
138       format.html { redirect_to system_pictures_url }
139       format.json { head :ok }
140     end
141   end
142 end