OSDN Git Service

Merge branch 'v03_test' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v03_test
[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], @author)
50 #    if params[:subdir] == 'refresh'
51 #      refresh 
52 #      return
53 #    end
54
55     respond_to do |format|
56       opt = {:type => @original_picture.mime_type, :disposition=>"inline"}
57       format.png { send_data(@original_picture.restore, opt ) }
58       format.gif { send_data(@original_picture.restore, opt ) }
59       format.jpeg { send_data(@original_picture.restore, opt ) }
60       format.html # show.html.erb
61       format.json { render json: @original_picture}
62     end
63   end
64
65   def list
66     @original_pictures = OriginalPicture.all
67
68     respond_to do |format|
69       format.html { render layout: 'system' }
70       format.json { render json: @original_pictures }
71     end
72   end
73
74   def browse
75     @original_picture = OriginalPicture.find(params[:id])
76
77     respond_to do |format|
78       format.html { render layout: 'system' }
79       format.json { render json: @original_picture }
80     end
81   end
82
83   def refresh
84     @original_picture = OriginalPicture.find(params[:id])
85     img = Magick::Image.from_blob(@original_picture.restore).shift
86     @original_picture.store img
87     respond_to do |format|
88       format.html { redirect_to original_pictures_url }
89     end
90   end
91   
92   # GET /original_pictures/new
93   # GET /original_pictures/new.json
94   def new
95     @original_picture = OriginalPicture.new
96     @original_picture.supply_default @artist
97
98     respond_to do |format|
99       format.html # new.html.erb
100       format.js
101       format.json { render json: @original_picture }
102     end
103   end
104
105   # GET /original_pictures/1/edit
106   def edit
107     @original_picture = OriginalPicture.show(params[:id], @author)
108     respond_to do |format|
109       format.html
110       format.js
111     end
112   end
113
114   # POST /original_pictures
115   # POST /original_pictures.json
116   def create
117     @picture_data = set_image params[:original_picture][:file]
118     @original_picture = OriginalPicture.new
119     @original_picture.supply_default @artist
120
121     respond_to do |format|
122       if @original_picture.store(@picture_data, @artist, params[:original_picture][:license_id])
123         format.html { redirect_to @original_picture, notice: 'Original picture was successfully created.' }
124         format.json { render json: @original_picture, status: :created, location: @original_picture }
125       else
126         format.html { render action: "new" }
127         format.json { render json: @original_picture.errors, status: :unprocessable_entity }
128       end
129     end
130   end
131
132   # PUT /original_pictures/1
133   # PUT /original_pictures/1.json
134   def update
135     @picture_data = set_image params[:original_picture][:file]
136     @original_picture = OriginalPicture.show(params[:id], @author)
137     @original_picture.supply_default @artist
138
139     respond_to do |format|
140       if @original_picture.store(@picture_data, @artist, params[:original_picture][:license_id])
141         format.html { redirect_to @original_picture, notice: 'Original picture was successfully created.' }
142         format.json { render json: @original_picture, status: :created, location: @original_picture }
143       else
144         format.html { render action: "edit" }
145         format.json { render json: @original_picture.errors, status: :unprocessable_entity }
146       end
147     end
148   end
149
150   # DELETE /original_pictures/1
151   # DELETE /original_pictures/1.json
152   def destroy
153     @original_picture = OriginalPicture.find(params[:id], @author)
154     OriginalPicture.transaction do
155       @original_picture.destroy
156     end
157     
158     respond_to do |format|
159       format.html { redirect_to original_pictures_url }
160       format.json { head :ok }
161     end
162   end
163 end