1
0
mirror of https://github.com/Tygs/0bin.git synced 2023-08-10 21:13:00 +03:00

Added paste size limitation

This commit is contained in:
max 2012-04-30 01:10:04 +07:00
parent 658149240e
commit 08032470c9
6 changed files with 77 additions and 22 deletions

View File

@ -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
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

View File

@ -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')

View File

@ -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 {

View File

@ -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();
});
});

View File

@ -22,6 +22,9 @@
<script src="/static/js/sjcl.js"></script>
<script src="/static/js/jquery-1.7.2.min.js"></script>
<script src="/static/js/behavior.js"></script>
<script type="text/javascript">
zerobin.max_size = {{ max_size }};
</script>
</head>
@ -81,10 +84,11 @@
<strong>41,017,923,819</strong> pastes øbinned
</h4>
</br>
</br>
<p class="greetings span12">
Based on an original idea from
<a href="http://sebsauvage.net/paste/">sebsauvage.net</a>
<a href="http://sebsauvage.net/paste/">sebsauvage.net</a><br>
<a href="http://sametmax.com">Sam &amp; Max</a>
</p>
</footer>

View File

@ -1,7 +1,13 @@
<p class="file-upload">
<div class="alert alert-error max-size-reached">
<a class="close" data-dismiss="alert" href="#">×</a>
<strong>Warning!</strong><br>
Your file is <strong class="file-size"></strong>KB You have reached the maximum size limit of {{ max_size_kb }}KB.
</div>
<p class="file-upload">
<input type="button" class="btn btn-upload" value="Upload File">
<input type="file" class="hide-upload" id="file-upload" >
</p>
</p>
<form class="well" method="post" action="/paste/create">
<p class="paste-option">
@ -22,4 +28,4 @@
</form>
%rebase base
%rebase base max_size=max_size