OSDN Git Service

Merge branch 'v04' 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(file)
9     if file.respond_to?(:read)
10       file.read
11     else
12       Base64.decode64(file.to_s.gsub(' ', '+')) #rubyのバグ?+でデコードされるべきキャラがスペースになる
13     end
14   end
15   
16   def authenticate_artist
17     if @author.artist?
18       true
19     else
20       respond_to do |format|
21         format.html { redirect_to new_artist_path, :status => :found }
22         format.js { render "artists/new" }
23         format.json { 
24           raise ActiveRecord::Forbidden
25         }
26       end
27       false
28     end
29   end
30   
31   public
32   
33   # GET /original_pictures
34   # GET /original_pictures.json
35   def index
36     @page = OriginalPicture.page params[:page]
37     @page_size = OriginalPicture.page_size params[:page_size]
38     @original_pictures = OriginalPicture.list(@artist.id, {}, @page, @page_size)
39
40     respond_to do |format|
41       format.html # index.html.erb
42       format.json { render json: @original_pictures.to_json(OriginalPicture.list_json_opt) }
43     end
44   end
45
46   # GET /original_pictures/1
47   # GET /original_pictures/1.json
48   def show
49     @original_picture = OriginalPicture.show(params[:id], @artist)
50
51     respond_to do |format|
52       opt = {:type => @original_picture.mime_type, :disposition=>"inline"}
53       format.png { send_data(@original_picture.restore, opt ) }
54       format.gif { send_data(@original_picture.restore, opt ) }
55       format.jpeg { send_data(@original_picture.restore, opt ) }
56       format.html # show.html.erb
57       format.json { render json: @original_picture.to_json(OriginalPicture.show_json_include_opt)}
58     end
59   end
60
61   def list
62     @original_pictures = OriginalPicture.all
63
64     respond_to do |format|
65       format.html { render layout: 'system' }
66       format.json { render json: @original_pictures }
67     end
68   end
69
70   def browse
71     @original_picture = OriginalPicture.find(params[:id])
72
73     respond_to do |format|
74       format.html { render layout: 'system' }
75       format.json { render json: @original_picture }
76     end
77   end
78
79   def refresh
80     @original_picture = OriginalPicture.find(params[:id])
81     img = Magick::Image.from_blob(@original_picture.restore).shift
82     @original_picture.store img
83     respond_to do |format|
84       format.html { redirect_to original_pictures_url }
85     end
86   end
87   
88   # GET /original_pictures/new
89   # GET /original_pictures/new.json
90   def new
91     @original_picture = OriginalPicture.new
92     @original_picture.supply_default @artist
93
94     respond_to do |format|
95       format.html # new.html.erb
96       format.js
97       format.json { render json: @original_picture }
98     end
99   end
100
101   # GET /original_pictures/1/edit
102   def edit
103     @original_picture = OriginalPicture.show(params[:id], @author)
104     respond_to do |format|
105       format.html
106       format.js
107     end
108   end
109
110   # POST /original_pictures
111   # POST /original_pictures.json
112   def create
113     @picture_data = set_image params[:original_picture][:file]
114     @original_picture = OriginalPicture.new
115     @original_picture.supply_default @artist
116
117     respond_to do |format|
118       if @original_picture.store(@picture_data, @artist, params[:original_picture][:license_id])
119         format.html { redirect_to @original_picture, notice: 'Original picture was successfully created.' }
120         format.json { render json: @original_picture, status: :created, location: @original_picture }
121       else
122         format.html { render action: "new" }
123         format.json { render json: @original_picture.errors, status: :unprocessable_entity }
124       end
125     end
126   end
127
128   # PUT /original_pictures/1
129   # PUT /original_pictures/1.json
130   def update
131     @picture_data = set_image params[:original_picture][:file]
132     @original_picture = OriginalPicture.edit(params[:id], @author)
133     @original_picture.supply_default @artist
134
135     respond_to do |format|
136       if @original_picture.store(@picture_data, @artist, params[:original_picture][:license_id])
137         format.html { redirect_to @original_picture, notice: 'Original picture was successfully created.' }
138         format.json { render json: @original_picture, status: :created, location: @original_picture }
139       else
140         format.html { render action: "edit" }
141         format.json { render json: @original_picture.errors, status: :unprocessable_entity }
142       end
143     end
144   end
145
146   # DELETE /original_pictures/1
147   # DELETE /original_pictures/1.json
148   def destroy
149     @original_picture = OriginalPicture.edit(params[:id], @author)
150     OriginalPicture.transaction do
151       @original_picture.destroy
152     end
153     
154     respond_to do |format|
155       format.html { redirect_to original_pictures_url }
156       format.json { head :ok }
157     end
158   end
159 end