annotate app/controllers/pastes_controller.rb @ 188:45ec309a3fa8

Fix indentation.
author Edho Arief <edho@myconan.net>
date Sat, 23 Feb 2013 13:25:09 +0900
parents fc234f8cf3d9
children b4b7a29b70f6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
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
f480cdf5f3df Misc whitespace.
Edho Arief <edho@myconan.net>
parents: 69
diff changeset
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
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
6 def show
53
817c54a72b58 Minor sanitation.
Edho Arief <edho@myconan.net>
parents: 49
diff changeset
7 id = params[:id].to_i
817c54a72b58 Minor sanitation.
Edho Arief <edho@myconan.net>
parents: 49
diff changeset
8 if id.to_s != params[:id]
817c54a72b58 Minor sanitation.
Edho Arief <edho@myconan.net>
parents: 49
diff changeset
9 redirect_to paste_path(id)
817c54a72b58 Minor sanitation.
Edho Arief <edho@myconan.net>
parents: 49
diff changeset
10 return
817c54a72b58 Minor sanitation.
Edho Arief <edho@myconan.net>
parents: 49
diff changeset
11 end
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
12 @paste = Paste.find(params[:id])
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
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
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
15 respond_to do |format|
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
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
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
18 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
19 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
20
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
21 # GET /
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
22 def new
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
23 @paste = Paste.new
10
56c17679105b Moar pastes.
Edho Arief <edho@myconan.net>
parents: 7
diff changeset
24 begin
56c17679105b Moar pastes.
Edho Arief <edho@myconan.net>
parents: 7
diff changeset
25 @paste.paste = Paste.find(params[:base]).paste
56c17679105b Moar pastes.
Edho Arief <edho@myconan.net>
parents: 7
diff changeset
26 rescue
56c17679105b Moar pastes.
Edho Arief <edho@myconan.net>
parents: 7
diff changeset
27 end
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
28
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
29 respond_to do |format|
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
30 format.html # new.html.erb
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
31 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
32 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
33
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
34 # POST /
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
35 # POST /pastes.json
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
36 # POST /pastes.txt
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
37 def create
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
38 @paste = Paste.new
151
db5164ff73a9 Always fill in @paste.ip when creating new one.
Edho Arief <edho@myconan.net>
parents: 88
diff changeset
39 @paste.ip = request.remote_ip
88
0f206bd4924f Allows posting gzipped text.
Edho Arief <edho@myconan.net>
parents: 77
diff changeset
40 if params[:paste]
0f206bd4924f Allows posting gzipped text.
Edho Arief <edho@myconan.net>
parents: 77
diff changeset
41 if params[:paste][:paste_gzip_base64]
0f206bd4924f Allows posting gzipped text.
Edho Arief <edho@myconan.net>
parents: 77
diff changeset
42 # 1. decode from base64
0f206bd4924f Allows posting gzipped text.
Edho Arief <edho@myconan.net>
parents: 77
diff changeset
43 # 2. create StringIO from decoded string
0f206bd4924f Allows posting gzipped text.
Edho Arief <edho@myconan.net>
parents: 77
diff changeset
44 # 3. unzip and read the stream
0f206bd4924f Allows posting gzipped text.
Edho Arief <edho@myconan.net>
parents: 77
diff changeset
45 params[:paste][:paste] = Zlib::GzipReader.new(StringIO.new(Base64.decode64(params[:paste][:paste_gzip_base64]))).read
0f206bd4924f Allows posting gzipped text.
Edho Arief <edho@myconan.net>
parents: 77
diff changeset
46 end
0f206bd4924f Allows posting gzipped text.
Edho Arief <edho@myconan.net>
parents: 77
diff changeset
47 unless params[:paste][:paste].blank?
0f206bd4924f Allows posting gzipped text.
Edho Arief <edho@myconan.net>
parents: 77
diff changeset
48 @paste.paste = params[:paste][:paste]
0f206bd4924f Allows posting gzipped text.
Edho Arief <edho@myconan.net>
parents: 77
diff changeset
49 end
69
be561c3967ee Safety net to ignore invalid post requests.
Edho Arief <edho@myconan.net>
parents: 67
diff changeset
50 end
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
51
32
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
52 begin
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
53 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
54 if @paste.save
181
500c09718fd7 Added fresh paste marker.
Edho Arief <edho@myconan.net>
parents: 151
diff changeset
55 @fresh = true
32
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
56 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
57 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
58 else
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
59 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
60 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
61 end
67
f7b7a39fb48c Allow post in txt format pt. 2 (view and controller).
Edho Arief <edho@myconan.net>
parents: 53
diff changeset
62 format.txt
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
63 end
32
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
64 rescue ActiveRecord::RecordNotUnique
183
fc234f8cf3d9 Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents: 181
diff changeset
65 @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
66 respond_to do |format|
fc234f8cf3d9 Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents: 181
diff changeset
67 format.html { redirect_to paste_path(@paste) }
fc234f8cf3d9 Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents: 181
diff changeset
68 format.json { render :json => @paste }
fc234f8cf3d9 Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents: 181
diff changeset
69 format.txt
fc234f8cf3d9 Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents: 181
diff changeset
70 end
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
71 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
72 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
73
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
74 end