annotate app/controllers/pastes_controller.rb @ 226:fb9909885d63

Also support plain gzip
author Edho Arief <edho@myconan.net>
date Mon, 13 May 2013 13:19:33 +0900
parents 9998d8b6c524
children 388504e43bcf
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
212
186b4674bcbe Add controller support for is_private flag
Edho Arief <edho@myconan.net>
parents: 211
diff changeset
7 @paste = Paste.safe_find(params[:id])
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
8
77
2fb80ca710e0 Added cache control so it's cached by browser the first time loaded.
Edho Arief <edho@myconan.net>
parents: 75
diff changeset
9 expires_in 1.year, :public => true
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
10 respond_to do |format|
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
11 format.html # show.html.erb
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
12 format.txt # show.txt.erb
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
13 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
14 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
15
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
16 # GET /
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
17 def new
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
18 @paste = Paste.new
189
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
19 @paste.set_paste_key
10
56c17679105b Moar pastes.
Edho Arief <edho@myconan.net>
parents: 7
diff changeset
20 begin
56c17679105b Moar pastes.
Edho Arief <edho@myconan.net>
parents: 7
diff changeset
21 @paste.paste = Paste.find(params[:base]).paste
56c17679105b Moar pastes.
Edho Arief <edho@myconan.net>
parents: 7
diff changeset
22 rescue
56c17679105b Moar pastes.
Edho Arief <edho@myconan.net>
parents: 7
diff changeset
23 end
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
24
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
25 respond_to do |format|
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
26 format.html # new.html.erb
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
27 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
28 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
29
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
30 # POST /
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
31 # POST /pastes.json
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
32 # POST /pastes.txt
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
33 def create
226
fb9909885d63 Also support plain gzip
Edho Arief <edho@myconan.net>
parents: 225
diff changeset
34 if params[:paste].is_a? Hash
fb9909885d63 Also support plain gzip
Edho Arief <edho@myconan.net>
parents: 225
diff changeset
35 if params[:paste][:paste_gzip_base64]
fb9909885d63 Also support plain gzip
Edho Arief <edho@myconan.net>
parents: 225
diff changeset
36 # 1. decode from base64
fb9909885d63 Also support plain gzip
Edho Arief <edho@myconan.net>
parents: 225
diff changeset
37 # 2. create StringIO from decoded string
fb9909885d63 Also support plain gzip
Edho Arief <edho@myconan.net>
parents: 225
diff changeset
38 # 3. unzip and read the stream
fb9909885d63 Also support plain gzip
Edho Arief <edho@myconan.net>
parents: 225
diff changeset
39 params[:paste][:paste] = Zlib::GzipReader.new(StringIO.new(Base64.decode64(params[:paste].delete(:paste_gzip_base64)))).read
fb9909885d63 Also support plain gzip
Edho Arief <edho@myconan.net>
parents: 225
diff changeset
40 elsif params[:paste][:paste_gzip]
fb9909885d63 Also support plain gzip
Edho Arief <edho@myconan.net>
parents: 225
diff changeset
41 params[:paste][:paste] = Zlib::GzipReader.new(StringIO.new(params[:paste].delete(:paste_gzip))).read
fb9909885d63 Also support plain gzip
Edho Arief <edho@myconan.net>
parents: 225
diff changeset
42 end
69
be561c3967ee Safety net to ignore invalid post requests.
Edho Arief <edho@myconan.net>
parents: 67
diff changeset
43 end
211
5e1d728975a4 Refactor
Edho Arief <edho@myconan.net>
parents: 191
diff changeset
44 @paste = Paste.new(paste_params.merge(ip: request.remote_ip))
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
45
32
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
46 begin
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
47 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
48 if @paste.save
181
500c09718fd7 Added fresh paste marker.
Edho Arief <edho@myconan.net>
parents: 151
diff changeset
49 @fresh = true
32
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
50 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
51 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
52 else
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
53 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
54 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
55 end
67
f7b7a39fb48c Allow post in txt format pt. 2 (view and controller).
Edho Arief <edho@myconan.net>
parents: 53
diff changeset
56 format.txt
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
57 end
32
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
58 rescue ActiveRecord::RecordNotUnique
183
fc234f8cf3d9 Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents: 181
diff changeset
59 @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
60 respond_to do |format|
fc234f8cf3d9 Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents: 181
diff changeset
61 format.html { redirect_to paste_path(@paste) }
fc234f8cf3d9 Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents: 181
diff changeset
62 format.json { render :json => @paste }
fc234f8cf3d9 Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents: 181
diff changeset
63 format.txt
fc234f8cf3d9 Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents: 181
diff changeset
64 end
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
65 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
66 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
67
189
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
68 def destroy
212
186b4674bcbe Add controller support for is_private flag
Edho Arief <edho@myconan.net>
parents: 211
diff changeset
69 @paste = Paste.safe_find(params[:id])
189
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
70 if @paste.key == params[:paste][:key]
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
71 @paste.destroy
191
4e409ac7082d Also expires the page upon deletion.
Edho Arief <edho@myconan.net>
parents: 190
diff changeset
72 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
73 flash[:notice] = "Paste ##{params[:id]} deleted"
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
74 redirect_to root_path
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
75 else
190
d4682cea8e58 Fun with alerts.
Edho Arief <edho@myconan.net>
parents: 189
diff changeset
76 flash[:error] = 'Incorrect deletion key'
189
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
77 render :action => :show
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
78 end
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
79 end
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
80
211
5e1d728975a4 Refactor
Edho Arief <edho@myconan.net>
parents: 191
diff changeset
81 private
5e1d728975a4 Refactor
Edho Arief <edho@myconan.net>
parents: 191
diff changeset
82 def paste_params
212
186b4674bcbe Add controller support for is_private flag
Edho Arief <edho@myconan.net>
parents: 211
diff changeset
83 params.require(:paste).permit(:paste, :is_private, :key)
211
5e1d728975a4 Refactor
Edho Arief <edho@myconan.net>
parents: 191
diff changeset
84 end
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
85 end