OSDN Git Service

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