From 08032470c95bdd1622f0e515e7bd64204dc7079b Mon Sep 17 00:00:00 2001
From: max
Date: Mon, 30 Apr 2012 01:10:04 +0700
Subject: [PATCH] Added paste size limitation
---
settings.py | 6 ++++-
start.py | 6 ++++-
static/css/style.css | 7 ++++-
static/js/behavior.js | 60 +++++++++++++++++++++++++++++++++----------
views/base.tpl | 8 ++++--
views/home.tpl | 12 ++++++---
6 files changed, 77 insertions(+), 22 deletions(-)
diff --git a/settings.py b/settings.py
index eef79a5..e5149cb 100644
--- a/settings.py
+++ b/settings.py
@@ -25,4 +25,8 @@ DEV_PORT= "8000"
# User and group the server should run as. Set to None if it should be the
# current user
USER = None
-GROUP = None
\ No newline at end of file
+GROUP = None
+
+# limit size of pasted text in bytes. Be carefull allowing too much size can slow down user's
+# browser
+MAX_SIZE = 500 * 1
\ No newline at end of file
diff --git a/start.py b/start.py
index 6da0d85..794683d 100644
--- a/start.py
+++ b/start.py
@@ -12,6 +12,7 @@ import thread
import time
import tempfile
import glob
+import math
from datetime import datetime, timedelta
@@ -30,11 +31,14 @@ from bottle import (Bottle, route, run, abort,
app = Bottle()
+import settings
+
@app.route('/')
@view('home')
def index():
- return {}
+ max_size_kb = int(math.ceil(settings.MAX_SIZE/1024.0))
+ return {'max_size': settings.MAX_SIZE, 'max_size_kb': max_size_kb}
@app.route('/paste/create', method='POST')
diff --git a/static/css/style.css b/static/css/style.css
index 967a0cd..690e339 100644
--- a/static/css/style.css
+++ b/static/css/style.css
@@ -106,6 +106,7 @@ h4#pixels-total {
clear: both;
margin: 0 auto;
text-align: center;
+ margin-top: 40px;
}
/* Home */
@@ -141,7 +142,11 @@ input.hide-upload {
cursor: hand;
height: 49px;
}
-
+
+.max-size-reached {
+ display: none;
+}
+
/* Paste Page */
.items {
diff --git a/static/js/behavior.js b/static/js/behavior.js
index a709c46..d9ceb77 100644
--- a/static/js/behavior.js
+++ b/static/js/behavior.js
@@ -108,6 +108,19 @@ zerobin = {
content_clone = content_clone + $(this).text() + '\n';
});
return content_clone;
+ },
+ count: function(text, options) {
+ // Set option defaults
+ var crlf = /(\r?\n|\r)/g;
+ var whitespace = /(\r?\n|\r|\s+)/g;
+ options = options || {};
+ options.lineBreaks = options.lineBreaks || 1;
+
+ var length = text.length,
+ nonAscii = length - text.replace(/[\u0100-\uFFFF]/g, '').length,
+ lineBreaks = length - text.replace(crlf, '').length;
+
+ return length + nonAscii + Math.max(0, options.lineBreaks * (lineBreaks - 1));
}
};
@@ -125,22 +138,32 @@ $('button[type=submit]').live("click", function(e){
e.preventDefault();
var paste = $('textarea').val();
- if (paste.trim()) {
- var expiration = $('#expiration').val();
- var key = zerobin.make_key();
- var data = {content: zerobin.encrypt(key, paste), expiration: expiration}
+ var sizebytes = zerobin.count($('#content').val(), { });
+
+ if (sizebytes > zerobin.max_size ){
+
+ $('.max-size-reached').show();
+ $('.file-size').text(Math.round(sizebytes/1024));
+
+ }else{
+
+ if (paste.trim()) {
+ var expiration = $('#expiration').val();
+ var key = zerobin.make_key();
+ var data = {content: zerobin.encrypt(key, paste), expiration: expiration}
+
+ $.post('/paste/create', data)
+ .error(function(error) {
+ alert('Paste could not be saved. Please try again later.');
+ })
+ .success(function(data) {
+ var paste_url = '/paste/' + data['paste'] + '#' + key;
+ window.location = (paste_url);
+ zerobin.store_paste(paste_url);
+ });
+ }
- $.post('/paste/create', data)
- .error(function(error) {
- alert('Paste could not be saved. Please try again later.');
- })
- .success(function(data) {
- var paste_url = '/paste/' + data['paste'] + '#' + key;
- window.location = (paste_url);
- zerobin.store_paste(paste_url);
- });
}
-
});
/** On the display paste page.
@@ -215,6 +238,7 @@ $('#content').live('keyup change', function(){
$('.paste-option').clone().addClass('down').appendTo('form.well');
}
}
+
});
/* Display previous pastes */
@@ -265,6 +289,14 @@ $('#file-upload').mouseover(function(){
});
+/* Alerts */
+
+$(".close").click(function(){
+ $(this).parent().fadeOut();
+});
+
+
+
});
diff --git a/views/base.tpl b/views/base.tpl
index b35ee03..793e31a 100644
--- a/views/base.tpl
+++ b/views/base.tpl
@@ -22,6 +22,9 @@
+
@@ -81,10 +84,11 @@
41,017,923,819 pastes øbinned
-
+
Based on an original idea from
- sebsauvage.net
+ sebsauvage.net
+ Sam & Max
diff --git a/views/home.tpl b/views/home.tpl
index 90dc804..d5d40dd 100644
--- a/views/home.tpl
+++ b/views/home.tpl
@@ -1,7 +1,13 @@
-
+
+
×
+
Warning!
+ Your file is
KB You have reached the maximum size limit of {{ max_size_kb }}KB.
+
+
+
-
+
-%rebase base
\ No newline at end of file
+%rebase base max_size=max_size
\ No newline at end of file