Mercurial > zeropaste
annotate app/controllers/pastes_controller.rb @ 193:d09bdb5904f5
Update gems
author | Edho Arief <edho@myconan.net> |
---|---|
date | Thu, 04 Apr 2013 11:26:02 +0900 |
parents | 4e409ac7082d |
children | 5e1d728975a4 |
rev | line source |
---|---|
2 | 1 class PastesController < ApplicationController |
48
f649b46fca4f
Cache all the things! Or just the show page.
Edho Arief <edho@myconan.net>
parents:
32
diff
changeset
|
2 caches_page :show |
73 | 3 |
74
48598fc65c20
Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents:
73
diff
changeset
|
4 # GET /1 |
48598fc65c20
Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents:
73
diff
changeset
|
5 # GET /1.txt |
2 | 6 def show |
53 | 7 id = params[:id].to_i |
8 if id.to_s != params[:id] | |
9 redirect_to paste_path(id) | |
10 return | |
11 end | |
2 | 12 @paste = Paste.find(params[:id]) |
13 | |
77
2fb80ca710e0
Added cache control so it's cached by browser the first time loaded.
Edho Arief <edho@myconan.net>
parents:
75
diff
changeset
|
14 expires_in 1.year, :public => true |
2 | 15 respond_to do |format| |
16 format.html # show.html.erb | |
74
48598fc65c20
Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents:
73
diff
changeset
|
17 format.txt # show.txt.erb |
2 | 18 end |
19 end | |
20 | |
74
48598fc65c20
Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents:
73
diff
changeset
|
21 # GET / |
2 | 22 def new |
23 @paste = Paste.new | |
189
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
24 @paste.set_paste_key |
10 | 25 begin |
26 @paste.paste = Paste.find(params[:base]).paste | |
27 rescue | |
28 end | |
2 | 29 |
30 respond_to do |format| | |
31 format.html # new.html.erb | |
32 end | |
33 end | |
34 | |
74
48598fc65c20
Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents:
73
diff
changeset
|
35 # POST / |
2 | 36 # POST /pastes.json |
74
48598fc65c20
Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents:
73
diff
changeset
|
37 # POST /pastes.txt |
2 | 38 def create |
39 @paste = Paste.new | |
151
db5164ff73a9
Always fill in @paste.ip when creating new one.
Edho Arief <edho@myconan.net>
parents:
88
diff
changeset
|
40 @paste.ip = request.remote_ip |
88 | 41 if params[:paste] |
42 if params[:paste][:paste_gzip_base64] | |
43 # 1. decode from base64 | |
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 | |
189
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
51 if params[:paste][:key] |
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
52 @paste.key = params[:paste][:key] |
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
53 end |
69
be561c3967ee
Safety net to ignore invalid post requests.
Edho Arief <edho@myconan.net>
parents:
67
diff
changeset
|
54 end |
2 | 55 |
32
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
56 begin |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
57 respond_to do |format| |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
58 if @paste.save |
181 | 59 @fresh = true |
32
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
60 format.html { redirect_to @paste, :notice => 'Paste was successfully created.' } |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
61 format.json { render :json => @paste, :status => :created, :location => @paste } |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
62 else |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
63 format.html { render :action => "new" } |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
64 format.json { render :json => @paste.errors, :status => :unprocessable_entity } |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
65 end |
67
f7b7a39fb48c
Allow post in txt format pt. 2 (view and controller).
Edho Arief <edho@myconan.net>
parents:
53
diff
changeset
|
66 format.txt |
2 | 67 end |
32
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
68 rescue ActiveRecord::RecordNotUnique |
183
fc234f8cf3d9
Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents:
181
diff
changeset
|
69 @paste = Paste.where(:ip => @paste.ip, :paste_hash => @paste.paste_hash).first |
fc234f8cf3d9
Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents:
181
diff
changeset
|
70 respond_to do |format| |
fc234f8cf3d9
Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents:
181
diff
changeset
|
71 format.html { redirect_to paste_path(@paste) } |
fc234f8cf3d9
Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents:
181
diff
changeset
|
72 format.json { render :json => @paste } |
fc234f8cf3d9
Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents:
181
diff
changeset
|
73 format.txt |
fc234f8cf3d9
Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents:
181
diff
changeset
|
74 end |
2 | 75 end |
76 end | |
77 | |
189
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
78 def destroy |
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
79 @paste = Paste.find(params[:id].to_i) |
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
80 if @paste.key == params[:paste][:key] |
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
81 @paste.destroy |
191
4e409ac7082d
Also expires the page upon deletion.
Edho Arief <edho@myconan.net>
parents:
190
diff
changeset
|
82 expire_page :controller => 'pastes', :action => 'show', :id => @paste.id |
189
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
83 flash[:notice] = "Paste ##{params[:id]} deleted" |
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
84 redirect_to root_path |
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
85 else |
190 | 86 flash[:error] = 'Incorrect deletion key' |
189
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
87 render :action => :show |
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
88 end |
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
89 end |
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
90 |
2 | 91 end |