OSDN Git Service

Merge branch 'master' of git.sourceforge.jp:/gitroot/pettanr/pettanr
[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])
68 #    if params[:subdir] == 'refresh'
69 #      refresh 
70 #      return
71 #    end
72
73     respond_to do |format|
74       if @original_picture.own?(@author)
75         opt = {:type => @original_picture.mime_type, :disposition=>"inline"}
76         format.png { send_data(@original_picture.restore, opt ) }
77         format.gif { send_data(@original_picture.restore, opt ) }
78         format.jpeg { send_data(@original_picture.restore, opt ) }
79         format.html # show.html.erb
80         format.json { render json: @original_picture}
81       else
82         raise ActiveRecord::Forbidden
83       end
84     end
85   end
86
87   def list
88     @original_pictures = OriginalPicture.all
89
90     respond_to do |format|
91       format.html { render layout: 'system' }
92       format.json { render json: @original_pictures }
93     end
94   end
95
96   def browse
97     @original_picture = OriginalPicture.find(params[:id])
98
99     respond_to do |format|
100       format.html { render layout: 'system' }
101       format.json { render json: @original_picture }
102     end
103   end
104
105   def refresh
106     @original_picture = OriginalPicture.find(params[:id])
107     img = Magick::Image.from_blob(@original_picture.restore).shift
108     @original_picture.store img
109     respond_to do |format|
110       format.html { redirect_to original_pictures_url }
111     end
112   end
113   
114   # GET /original_pictures/new
115   # GET /original_pictures/new.json
116   def new
117     @original_picture = OriginalPicture.new
118     @original_picture.supply_default @artist
119
120     respond_to do |format|
121       format.html # new.html.erb
122       format.js
123       format.json { render json: @original_picture }
124     end
125   end
126
127   # GET /original_pictures/1/edit
128   def edit
129     @original_picture = OriginalPicture.show(params[:id])
130     raise ActiveRecord::Forbidden unless @original_picture.own?(@author)
131     respond_to do |format|
132       format.html
133       format.js
134     end
135   end
136
137   # POST /original_pictures
138   # POST /original_pictures.json
139   def create
140     @original_picture = OriginalPicture.new
141     img = set_image params
142     @original_picture.artist_id = @author.artist.id
143     @original_picture.license_id = @author.artist.default_license_id
144
145     respond_to do |format|
146       OriginalPicture.transaction do
147         if @original_picture.save
148           if @original_picture.store(img)
149             format.html { redirect_to @original_picture, notice: 'Original picture was successfully created.' }
150             format.json { render json: @original_picture, status: :created, location: @original_picture }
151           else
152             format.html { redirect_to @original_picture, notice: 'Failed! Original picture was NOT created.' }
153             format.json { render json: @original_picture.errors, status: :unprocessable_entity }
154           end
155         else
156           format.html { render action: "new" }
157           format.json { render json: @original_picture.errors, status: :unprocessable_entity }
158         end
159       end
160     end
161   end
162
163   # PUT /original_pictures/1
164   # PUT /original_pictures/1.json
165   def update
166     @original_picture = OriginalPicture.show(params[:id])
167     raise ActiveRecord::Forbidden unless @original_picture.own?(@author)
168     img = set_image params
169
170     respond_to do |format|
171       OriginalPicture.transaction do
172         if @original_picture.save
173           if @original_picture.store(img)
174             format.html { redirect_to @original_picture, notice: 'Original picture was successfully updated.' }
175             format.json { head :ok }
176           else
177             format.html { redirect_to @original_picture, notice: 'Failed! Original picture was NOT created.' }
178             format.json { render json: @original_picture.errors, status: :unprocessable_entity }
179           end
180         else
181           format.html { render action: "edit" }
182           format.json { render json: @original_picture.errors, status: :unprocessable_entity }
183         end
184       end
185     end
186   end
187
188   # DELETE /original_pictures/1
189   # DELETE /original_pictures/1.json
190   def destroy
191     @original_picture = OriginalPicture.find(params[:id])
192     raise ActiveRecord::Forbidden unless @original_picture.own?(@author)
193     OriginalPicture.transaction do
194       @original_picture.destroy
195     end
196     
197     respond_to do |format|
198       format.html { redirect_to original_pictures_url }
199       format.json { head :ok }
200     end
201   end
202 end