OSDN Git Service

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