OSDN Git Service

list browser created
[pettanr/pettanr.git] / app / controllers / original_pictures_controller.rb
1 class OriginalPicturesController < ApplicationController
2   before_filter :authenticate_author!, :only => [:index, :show, :new, :edit, :create, :update, :destroy]
3   before_filter :authenticate_admin!, :only => [:list, :browse]
4
5   private
6   
7   def set_image(prm)
8     img = nil
9     if (f = prm[:original_picture][:file]).respond_to?(:read)
10       if f.size > 1000000
11         @original_picture.width = 0
12         @original_picture.height = 0
13         @original_picture.ext = 'none'
14         @original_picture.filesize = 1.megabytes
15       else
16         img = Magick::Image.from_blob(f.read).shift
17         @original_picture.width = img.columns
18         @original_picture.height = img.rows
19         @original_picture.ext = img.format.downcase
20         @original_picture.filesize = f.size
21       end
22     else
23       dat = Base64.decode64(prm[:original_picture][:file].to_s.gsub(' ', '+')) #rubyのバグ?+でデコードされるべきキャラがスペースになる
24       img = Magick::Image.from_blob(dat).shift
25       @original_picture.width = img.columns
26       @original_picture.height = img.rows
27       @original_picture.ext = img.format.downcase
28       @original_picture.filesize = 1000
29     end
30     img
31   end
32   
33   public
34   
35   # GET /original_pictures
36   # GET /original_pictures.json
37   def index
38     @original_pictures = OriginalPicture.find(:all, 
39       :include => [:artist, :lisence, :resource_picture], :order => 'updated_at', :limit => 20
40     )
41
42     respond_to do |format|
43       format.html # index.html.erb
44       format.json { render :json => @original_pictures.to_json(
45         :include => [:resource_picture, :artist, :lisence]
46       ) }
47     end
48   end
49
50   # GET /original_pictures/1
51   # GET /original_pictures/1.json
52   def show
53     @original_picture = OriginalPicture.find(params[:id])
54 #    if params[:subdir] == 'refresh'
55 #      refresh 
56 #      return
57 #    end
58
59     respond_to do |format|
60       opt = {:type => @original_picture.mime_type, :disposition=>"inline"}
61       format.png { send_data(@original_picture.restore, opt ) }
62       format.gif { send_data(@original_picture.restore, opt ) }
63       format.jpeg { send_data(@original_picture.restore, opt ) }
64       format.html # show.html.erb
65       format.json { render json: @original_picture}
66     end
67   end
68
69   def list
70     @original_pictures = OriginalPicture.all
71
72     respond_to do |format|
73       format.html { render layout: 'system' }
74       format.json { render json: @original_pictures }
75     end
76   end
77
78   def browse
79     @original_picture = OriginalPicture.find(params[:id])
80
81     respond_to do |format|
82       format.html { render layout: 'system' }
83       format.json { render json: @original_picture }
84     end
85   end
86
87   def refresh
88     @original_picture = OriginalPicture.find(params[:id])
89     img = Magick::Image.from_blob(@original_picture.restore).shift
90     @original_picture.store img
91     respond_to do |format|
92       format.html { redirect_to original_pictures_url }
93     end
94   end
95   
96   # GET /original_pictures/new
97   # GET /original_pictures/new.json
98   def new
99     @original_picture = OriginalPicture.new
100
101     respond_to do |format|
102       format.html # new.html.erb
103       format.json { render json: @original_picture }
104     end
105   end
106
107   # GET /original_pictures/1/edit
108   def edit
109     @original_picture = OriginalPicture.find(params[:id])
110   end
111
112   # POST /original_pictures
113   # POST /original_pictures.json
114   def create
115     unless @artist
116       respond_to do |format|
117         format.html { redirect_to @original_picture, notice: 'Failed! author does not artist.' }
118         format.json { render json: @original_picture.errors, status: :unprocessable_entity }
119       end
120       return
121     end
122     @original_picture = OriginalPicture.new
123     img = set_image params
124     @original_picture.artist_id = current_author.artist.id
125     @original_picture.lisence_id = current_author.artist.default_lisence_id
126
127     respond_to do |format|
128       OriginalPicture.transaction do
129         if @original_picture.save
130           if @original_picture.store(img)
131             format.html { redirect_to @original_picture, notice: 'Original picture was successfully created.' }
132             format.json { render json: @original_picture, status: :created, location: @original_picture }
133           else
134             format.html { redirect_to @original_picture, notice: 'Failed! Original picture was NOT created.' }
135             format.json { render json: @original_picture.errors, status: :unprocessable_entity }
136           end
137         else
138           format.html { render action: "new" }
139           format.json { render json: @original_picture.errors, status: :unprocessable_entity }
140         end
141       end
142     end
143   end
144
145   # PUT /original_pictures/1
146   # PUT /original_pictures/1.json
147   def update
148     unless @artist
149       respond_to do |format|
150         format.html { redirect_to @original_picture, notice: 'Failed! author does not artist.' }
151         format.json { render json: @original_picture.errors, status: :unprocessable_entity }
152       end
153       return
154     end
155     @original_picture = OriginalPicture.find(params[:id])
156     unless @original_picture.own? current_author
157       respond_to do |format|
158         format.html { redirect_to @original_picture, notice: 'Failed! ' }
159         format.json { render json: @original_picture.errors, status: :unprocessable_entity }
160       end
161       return
162     end
163     img = set_image params
164
165     respond_to do |format|
166       OriginalPicture.transaction do
167         if @original_picture.save
168           if @original_picture.store(img)
169             format.html { redirect_to @original_picture, notice: 'Original picture was successfully updated.' }
170             format.json { head :ok }
171           else
172             format.html { redirect_to @original_picture, notice: 'Failed! Original picture was NOT created.' }
173             format.json { render json: @original_picture.errors, status: :unprocessable_entity }
174           end
175         else
176           format.html { render action: "edit" }
177           format.json { render json: @original_picture.errors, status: :unprocessable_entity }
178         end
179       end
180     end
181   end
182
183   # DELETE /original_pictures/1
184   # DELETE /original_pictures/1.json
185   def destroy
186     unless @artist
187       respond_to do |format|
188         format.html { redirect_to @original_picture, notice: 'Failed! author does not artist.' }
189         format.json { render json: @original_picture.errors, status: :unprocessable_entity }
190       end
191       return
192     end
193     @original_picture = OriginalPicture.find(params[:id])
194     unless @original_picture.own? current_author
195       respond_to do |format|
196         format.html { redirect_to @original_picture, notice: 'Failed! ' }
197         format.json { render json: @original_picture.errors, status: :unprocessable_entity }
198       end
199       return
200     end
201     OriginalPicture.transaction do
202       @original_picture.destroy
203     end
204     
205     respond_to do |format|
206       format.html { redirect_to original_pictures_url }
207       format.json { head :ok }
208     end
209   end
210 end