OSDN Git Service

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