OSDN Git Service

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