comparison app/controllers/pastes_controller.rb @ 211:5e1d728975a4

Refactor
author Edho Arief <edho@myconan.net>
date Sun, 12 May 2013 02:41:08 +0900
parents 4e409ac7082d
children 186b4674bcbe
comparison
equal deleted inserted replaced
210:d59731c3c7bf 211:5e1d728975a4
2 caches_page :show 2 caches_page :show
3 3
4 # GET /1 4 # GET /1
5 # GET /1.txt 5 # GET /1.txt
6 def show 6 def show
7 id = params[:id].to_i
8 if id.to_s != params[:id]
9 redirect_to paste_path(id)
10 return
11 end
12 @paste = Paste.find(params[:id]) 7 @paste = Paste.find(params[:id])
13 8
14 expires_in 1.year, :public => true 9 expires_in 1.year, :public => true
15 respond_to do |format| 10 respond_to do |format|
16 format.html # show.html.erb 11 format.html # show.html.erb
34 29
35 # POST / 30 # POST /
36 # POST /pastes.json 31 # POST /pastes.json
37 # POST /pastes.txt 32 # POST /pastes.txt
38 def create 33 def create
39 @paste = Paste.new 34 if params[:paste] && params[:paste][:paste_gzip_base64]
40 @paste.ip = request.remote_ip 35 # 1. decode from base64
41 if params[:paste] 36 # 2. create StringIO from decoded string
42 if params[:paste][:paste_gzip_base64] 37 # 3. unzip and read the stream
43 # 1. decode from base64 38 params[:paste][:paste] = Zlib::GzipReader.new(StringIO.new(Base64.decode64(params[:paste][:paste_gzip_base64].delete))).read
44 # 2. create StringIO from decoded string
45 # 3. unzip and read the stream
46 params[:paste][:paste] = Zlib::GzipReader.new(StringIO.new(Base64.decode64(params[:paste][:paste_gzip_base64]))).read
47 end
48 unless params[:paste][:paste].blank?
49 @paste.paste = params[:paste][:paste]
50 end
51 if params[:paste][:key]
52 @paste.key = params[:paste][:key]
53 end
54 end 39 end
40 @paste = Paste.new(paste_params.merge(ip: request.remote_ip))
55 41
56 begin 42 begin
57 respond_to do |format| 43 respond_to do |format|
58 if @paste.save 44 if @paste.save
59 @fresh = true 45 @fresh = true
74 end 60 end
75 end 61 end
76 end 62 end
77 63
78 def destroy 64 def destroy
79 @paste = Paste.find(params[:id].to_i) 65 @paste = Paste.find(params[:id])
80 if @paste.key == params[:paste][:key] 66 if @paste.key == params[:paste][:key]
81 @paste.destroy 67 @paste.destroy
82 expire_page :controller => 'pastes', :action => 'show', :id => @paste.id 68 expire_page :controller => 'pastes', :action => 'show', :id => @paste.id
83 flash[:notice] = "Paste ##{params[:id]} deleted" 69 flash[:notice] = "Paste ##{params[:id]} deleted"
84 redirect_to root_path 70 redirect_to root_path
86 flash[:error] = 'Incorrect deletion key' 72 flash[:error] = 'Incorrect deletion key'
87 render :action => :show 73 render :action => :show
88 end 74 end
89 end 75 end
90 76
77 private
78 def paste_params
79 params.require(:paste).permit(:paste, :key)
80 end
91 end 81 end