OSDN Git Service

user check
[pettanr/pettanr.git] / app / controllers / original_pictures_controller.rb
1 class OriginalPicturesController < ApplicationController
2   before_filter :authenticate_user!, :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, :license, :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, :license]
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.js
104       format.json { render json: @original_picture }
105     end
106   end
107
108   # GET /original_pictures/1/edit
109   def edit
110     @original_picture = OriginalPicture.find(params[:id])
111   end
112
113   # POST /original_pictures
114   # POST /original_pictures.json
115   def create
116     unless @artist
117       respond_to do |format|
118         format.html { redirect_to @original_picture, notice: 'Failed! author does not artist.' }
119         format.json { render json: @original_picture.errors, status: :unprocessable_entity }
120       end
121       return
122     end
123     @original_picture = OriginalPicture.new
124     img = set_image params
125     @original_picture.artist_id = @author.artist.id
126     @original_picture.license_id = @author.artist.default_license_id
127
128     respond_to do |format|
129       OriginalPicture.transaction do
130         if @original_picture.save
131           if @original_picture.store(img)
132             format.html { redirect_to @original_picture, notice: 'Original picture was successfully created.' }
133             format.json { render json: @original_picture, status: :created, location: @original_picture }
134           else
135             format.html { redirect_to @original_picture, notice: 'Failed! Original picture was NOT created.' }
136             format.json { render json: @original_picture.errors, status: :unprocessable_entity }
137           end
138         else
139           format.html { render action: "new" }
140           format.json { render json: @original_picture.errors, status: :unprocessable_entity }
141         end
142       end
143     end
144   end
145
146   # PUT /original_pictures/1
147   # PUT /original_pictures/1.json
148   def update
149     unless @artist
150       respond_to do |format|
151         format.html { redirect_to @original_picture, notice: 'Failed! author does not artist.' }
152         format.json { render json: @original_picture.errors, status: :unprocessable_entity }
153       end
154       return
155     end
156     @original_picture = OriginalPicture.find(params[:id])
157     unless @original_picture.own? @author
158       respond_to do |format|
159         format.html { redirect_to @original_picture, notice: 'Failed! ' }
160         format.json { render json: @original_picture.errors, status: :unprocessable_entity }
161       end
162       return
163     end
164     img = set_image params
165
166     respond_to do |format|
167       OriginalPicture.transaction do
168         if @original_picture.save
169           if @original_picture.store(img)
170             format.html { redirect_to @original_picture, notice: 'Original picture was successfully updated.' }
171             format.json { head :ok }
172           else
173             format.html { redirect_to @original_picture, notice: 'Failed! Original picture was NOT created.' }
174             format.json { render json: @original_picture.errors, status: :unprocessable_entity }
175           end
176         else
177           format.html { render action: "edit" }
178           format.json { render json: @original_picture.errors, status: :unprocessable_entity }
179         end
180       end
181     end
182   end
183
184   # DELETE /original_pictures/1
185   # DELETE /original_pictures/1.json
186   def destroy
187     unless @artist
188       respond_to do |format|
189         format.html { redirect_to @original_picture, notice: 'Failed! author does not artist.' }
190         format.json { render json: @original_picture.errors, status: :unprocessable_entity }
191       end
192       return
193     end
194     @original_picture = OriginalPicture.find(params[:id])
195     unless @original_picture.own? @author
196       respond_to do |format|
197         format.html { redirect_to @original_picture, notice: 'Failed! ' }
198         format.json { render json: @original_picture.errors, status: :unprocessable_entity }
199       end
200       return
201     end
202     OriginalPicture.transaction do
203       @original_picture.destroy
204     end
205     
206     respond_to do |format|
207       format.html { redirect_to original_pictures_url }
208       format.json { head :ok }
209     end
210   end
211 end