OSDN Git Service

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