Mercurial > zeropaste
annotate app/controllers/pastes_controller.rb @ 321:fec39456dcbe
Better request format checker.
Didn't quite work when using curl.
author | edogawaconan <me@myconan.net> |
---|---|
date | Sun, 06 Apr 2014 03:20:10 +0900 |
parents | 612bc7c32324 |
children | 430dadffd91e |
rev | line source |
---|---|
2 | 1 class PastesController < ApplicationController |
320 | 2 before_action :lowercase_path, :only => :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 |
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]) |
229
388504e43bcf
Properly return 404 when requesting invalid paste.
Edho Arief <edho@myconan.net>
parents:
226
diff
changeset
|
8 head :not_found and return unless @paste |
2 | 9 |
10 respond_to do |format| | |
320 | 11 format.html { cache } |
12 format.txt { cache } | |
2 | 13 end |
14 end | |
15 | |
74
48598fc65c20
Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents:
73
diff
changeset
|
16 # GET / |
2 | 17 def new |
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 |
240
16251b94eb6c
Whoops, forgot to secure the "new paste based on ##"
edogawaconan <me@myconan.net>
parents:
229
diff
changeset
|
20 @paste.paste = Paste.safe_find(params[:base]).try(:paste) |
2 | 21 |
22 respond_to do |format| | |
23 format.html # new.html.erb | |
24 end | |
25 end | |
26 | |
74
48598fc65c20
Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents:
73
diff
changeset
|
27 # POST / |
2 | 28 # POST /pastes.json |
74
48598fc65c20
Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents:
73
diff
changeset
|
29 # POST /pastes.txt |
2 | 30 def create |
211 | 31 @paste = Paste.new(paste_params.merge(ip: request.remote_ip)) |
2 | 32 |
32
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
33 begin |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
34 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
|
35 if @paste.save |
181 | 36 @fresh = true |
32
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
37 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
|
38 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
|
39 else |
283 | 40 flash.now[:alert] = @paste.errors.full_messages.to_sentence |
32
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
41 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
|
42 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
|
43 end |
67
f7b7a39fb48c
Allow post in txt format pt. 2 (view and controller).
Edho Arief <edho@myconan.net>
parents:
53
diff
changeset
|
44 format.txt |
2 | 45 end |
32
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
46 rescue ActiveRecord::RecordNotUnique |
183
fc234f8cf3d9
Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents:
181
diff
changeset
|
47 @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
|
48 respond_to do |format| |
fc234f8cf3d9
Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents:
181
diff
changeset
|
49 format.html { redirect_to paste_path(@paste) } |
fc234f8cf3d9
Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents:
181
diff
changeset
|
50 format.json { render :json => @paste } |
fc234f8cf3d9
Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents:
181
diff
changeset
|
51 format.txt |
fc234f8cf3d9
Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents:
181
diff
changeset
|
52 end |
2 | 53 end |
54 end | |
55 | |
189
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
56 def destroy |
212
186b4674bcbe
Add controller support for is_private flag
Edho Arief <edho@myconan.net>
parents:
211
diff
changeset
|
57 @paste = Paste.safe_find(params[:id]) |
265
6cca1ab53337
Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents:
240
diff
changeset
|
58 if @paste.safe_destroy(params[:paste][:key]) |
320 | 59 uncache |
265
6cca1ab53337
Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents:
240
diff
changeset
|
60 redirect_to root_path, :notice => "Paste ##{params[:id]} deleted" |
189
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
61 else |
283 | 62 flash.now[:alert] = @paste.errors.full_messages.to_sentence |
265
6cca1ab53337
Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents:
240
diff
changeset
|
63 render :show |
189
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
64 end |
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
65 end |
b4b7a29b70f6
Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents:
183
diff
changeset
|
66 |
211 | 67 private |
320 | 68 |
69 def lowercase_path | |
70 correct_path = request.fullpath.downcase | |
71 redirect_to correct_path, :status => :moved_permanently unless | |
72 correct_path == request.fullpath | |
73 end | |
74 | |
321
fec39456dcbe
Better request format checker.
edogawaconan <me@myconan.net>
parents:
320
diff
changeset
|
75 def cache_key(format = request.format.symbol) |
320 | 76 ext = case format |
321
fec39456dcbe
Better request format checker.
edogawaconan <me@myconan.net>
parents:
320
diff
changeset
|
77 when :txt |
320 | 78 ".txt" |
321
fec39456dcbe
Better request format checker.
edogawaconan <me@myconan.net>
parents:
320
diff
changeset
|
79 when :html, nil |
320 | 80 "" |
81 else | |
82 ".unknown" | |
83 end | |
84 "pastes:#{@paste.to_param}#{ext}" | |
85 end | |
86 | |
87 def cache | |
88 Rails.cache.fetch cache_key, :raw => true do | |
89 render_to_string :show | |
90 end | |
91 end | |
92 | |
93 def uncache | |
321
fec39456dcbe
Better request format checker.
edogawaconan <me@myconan.net>
parents:
320
diff
changeset
|
94 [:txt, :html, :unknown].each do |format| |
320 | 95 Rails.cache.delete cache_key(format) |
96 end | |
97 end | |
98 | |
211 | 99 def paste_params |
316
61f7f258a6fb
Move from-gzip paste parsing to model.
edogawaconan <me@myconan.net>
parents:
283
diff
changeset
|
100 params.require(:paste).permit(:paste, :paste_gzip, :paste_gzip_base64, :is_private, :key) |
211 | 101 end |
2 | 102 end |