Mercurial > zeropaste
annotate app/controllers/pastes_controller.rb @ 158:2b0a6d692588
No need to explicitly require json.
author | Edho Arief <edho@myconan.net> |
---|---|
date | Wed, 28 Nov 2012 09:43:36 -0800 |
parents | db5164ff73a9 |
children | 500c09718fd7 |
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 | |
10 | 24 begin |
25 @paste.paste = Paste.find(params[:base]).paste | |
26 rescue | |
27 end | |
2 | 28 |
29 respond_to do |format| | |
30 format.html # new.html.erb | |
31 end | |
32 end | |
33 | |
74
48598fc65c20
Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents:
73
diff
changeset
|
34 # POST / |
2 | 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 | 37 def create |
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 | 40 if params[:paste] |
41 if params[:paste][:paste_gzip_base64] | |
42 # 1. decode from base64 | |
43 # 2. create StringIO from decoded string | |
44 # 3. unzip and read the stream | |
45 params[:paste][:paste] = Zlib::GzipReader.new(StringIO.new(Base64.decode64(params[:paste][:paste_gzip_base64]))).read | |
46 end | |
47 unless params[:paste][:paste].blank? | |
48 @paste.paste = params[:paste][:paste] | |
49 end | |
69
be561c3967ee
Safety net to ignore invalid post requests.
Edho Arief <edho@myconan.net>
parents:
67
diff
changeset
|
50 end |
2 | 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 |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
55 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
|
56 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
|
57 else |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
58 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
|
59 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
|
60 end |
67
f7b7a39fb48c
Allow post in txt format pt. 2 (view and controller).
Edho Arief <edho@myconan.net>
parents:
53
diff
changeset
|
61 format.txt |
2 | 62 end |
32
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
63 rescue ActiveRecord::RecordNotUnique |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
64 redirect_to paste_path(Paste.where(:ip => @paste.ip, :paste_hash => @paste.paste_hash).first) |
2 | 65 end |
66 end | |
67 | |
68 end |