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