From 658149240e1f1c6218726885594c1c9b889ac860 Mon Sep 17 00:00:00 2001 From: max Date: Sun, 29 Apr 2012 22:36:26 +0700 Subject: [PATCH 1/2] Added upload file button (HTML5) --- src/__init__.py | 8 +- src/paste.py | 14 +-- static/css/style.css | 224 +++++++++++++++++++++++++++++------------- static/js/behavior.js | 33 ++++++- views/base.tpl | 9 +- views/home.tpl | 5 + views/paste.tpl | 11 +-- 7 files changed, 217 insertions(+), 87 deletions(-) diff --git a/src/__init__.py b/src/__init__.py index 80d200b..6602367 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -16,11 +16,7 @@ def setup_path(): Add the project dir in the python path to the site to run with the source code beeing just copied/pasted and not installed. - Also try to import dependancies. If it fails, fallback on embeded libs. + Add fallback on embeded libs to path. """ sys.path.insert(0, os.path.dirname(settings.ROOT_DIR)) - - try: - import bottle - except ImportError: - sys.path.append(os.path.join(settings.ROOT_DIR, 'libs')) \ No newline at end of file + sys.path.append(os.path.join(settings.ROOT_DIR, 'libs')) \ No newline at end of file diff --git a/src/paste.py b/src/paste.py index f4d6f40..687d8b4 100644 --- a/src/paste.py +++ b/src/paste.py @@ -154,12 +154,14 @@ class Paste(object): self.expiration = self.expiration + '#%s' % datetime.now() # write the paste - with open(self.path, 'w') as f: - f.write(unicode(self.expiration) + '\n') - f.write(self.content + '\n') - if self.comments: - f.write(comments) - + try: + with open(self.path, 'w') as f: + f.write(unicode(self.expiration) + '\n') + f.write(self.content + '\n') + if self.comments: + f.write(comments) + except Exception as e: + import ipdb; ipdb.set_trace() return self diff --git a/static/css/style.css b/static/css/style.css index fc551ba..967a0cd 100644 --- a/static/css/style.css +++ b/static/css/style.css @@ -2,14 +2,13 @@ .brand { -font-size: 38px !important; -padding: 0px 55px 3px !important; - -text-shadow: 0 1px 0 rgba(255, 255, 255, .1), 0 0 30px rgba(255, 255, 255, .125); --webkit-transition: all .2s linear; --moz-transition: all .2s linear; -transition: all .2s linear; + font-size: 38px !important; + padding: 0px 55px 3px !important; + text-shadow: 0 1px 0 rgba(255, 255, 255, .1), 0 0 30px rgba(255, 255, 255, .125); + -webkit-transition: all .2s linear; + -moz-transition: all .2s linear; + transition: all .2s linear; } .brand span { @@ -34,15 +33,6 @@ body { padding: 9px 0; } -.btn-group { - float:left; -} - -.paste-option { - float:right; -} - - select { width: 135px; } @@ -61,7 +51,7 @@ ul, ol { } li { - margin-left: -4px; + margin-left: -9px; } @@ -73,24 +63,169 @@ p { color: #999; } -.items { - text-align: center; -} - .nav-list { padding-right: 0px !important; font-size: 12px; } -.form-options li{ +blockquote { + width: 630px; float: left; - list-style-type: none; +} + +h4 p { + float: left; + font-size: 80px; + text-shadow: 1px 3px 1px #DDD, 0 0 4px #333; + -webkit-transition: all 0.2s linear; + -moz-transition: all .2s linear; + transition: all .2s linear; + margin-right: 7px; + margin-top: 3px; +} + +h4#pixels-total { + position: relative; + width: 166px; + float: right; + margin: 8px 0 0 0; + padding: 0 0 0 54px; + font-size: 1.1em; + line-height: 1.4; + font-weight: normal; + color: #777; + -webkit-border-top-right-radius: 6px; + -webkit-border-top-left-radius: 20px; + -moz-border-top-right-radius: 6px; + -moz-border-top-left-radius: 20px; + border-top-right-radius: 6px; + border-top-left-radius: 20px; +} + +.greetings { + clear: both; + margin: 0 auto; + text-align: center; +} + +/* Home */ + +.btn-group { + float:left; +} + +.file-upload { + float: left; + margin: 22px 0px 0px 21px; + display: none; +} + +input.btn-upload { + position: relative; + left: -6px; + width: 100px; + z-index: 1; + margin-top: -13px; +} + +input.hide-upload { + position: relative; + left: -110px; + -moz-opacity: 0; + filter: alpha(opacity: 0); + opacity: 0 ; + z-index: 2; + width: 100px; + margin-top: -20px; + cursor: pointer; + cursor: hand; + height: 49px; +} + +/* Paste Page */ + +.items { + text-align: center; } #paste-content { background-color: white; } +#copy-success, +#short-url-success, +.submit-form { + display:none; +} + +.paste-option { + float:right; +} + +a#clip-button.hover{ + cursor:pointer; + text-decoration:underline; +} + +li.L0, li.L1, li.L2, li.L3, li.L4, +li.L5, li.L6, li.L7, li.L8, li.L9 +{ + list-style-type: decimal; + background: inherit; +} + +.prettyprint.linenums { + -webkit-box-shadow: inset 40px 0 0 #fbfbfc, inset 41px 0 0 #ececf0; + -moz-box-shadow: inset 40px 0 0 #fbfbfc, inset 41px 0 0 #ececf0; + box-shadow: inset 40px 0 0 #fbfbfc, inset 41px 0 0 #ececf0; +} + +/* Specify class=linenums on a pre to get line numbering */ +ol.linenums { + margin: 0 0 0 55px; /* IE indents via margin-left */ +} +ol.linenums li { + color: #bebec5; + line-height: 18px; + text-shadow: 0 1px 0 #fff; +} + +.prettyprint { + padding: 8px; + background-color: #f7f7f9; + border: 1px solid #e1e1e8; +} + +pre { + font-family: Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, monospace, serif; + line-height: 21px; + font-size: 12px; + +} + +.kwd { +color: #66F; +} + +.pun, .opn, .clo { +color: #0A0; +} + +.lit { +color: #933; +} + +.com { +color: #C0C; +} + + +/* Common css */ + +form { + padding-bottom:3em !important; + padding-right: 17px; +} form textarea { overflow-y:auto; @@ -100,10 +235,6 @@ button.btn, input[type="submit"].btn { margin-left: 5px; } -.submit-form { - display: none; -} - .well { padding-bottom: 40px; padding-right: 17px; @@ -120,48 +251,9 @@ button.btn, input[type="submit"].btn { margin-left: 5px; } -form { - padding-bottom:3em !important; - padding-right: 17px; -} - .btn-primary { position:relative; top:-4px; } -pre { - padding:1em 20px !important; -} - -.linenums { - padding-left: 35px; -} - - -ol.linenums li { - line-height:14px; -} - -ol.linenums span:first-child { - border-left: solid 1px #999; - padding-left:1em; -} - -li.L0, li.L1, li.L2, li.L3, li.L4, -li.L5, li.L6, li.L7, li.L8, li.L9 -{ - list-style-type: decimal; - background: inherit; -} - -a#clip-button.hover{ -cursor:pointer; -text-decoration:underline; -} - -#copy-success, #short-url-success { - display:none; -} - diff --git a/static/js/behavior.js b/static/js/behavior.js index c8d5c52..a709c46 100644 --- a/static/js/behavior.js +++ b/static/js/behavior.js @@ -6,7 +6,6 @@ sjcl.random.startCollectors(); $.ajaxSetup({ cache: true }); - zerobin = { encrypt: function(key, content) { content = sjcl.codec.base64.fromBits(sjcl.codec.utf8String.toBits(content)); @@ -234,6 +233,38 @@ $('.btn-clone').click(function(e){ }); + +/* Upload file using HTML5 File API */ + +if (window.File && window.FileReader && window.FileList && window.Blob) { + $('.file-upload').show(); +} + +var file_upload = function(file) { + var reader = new FileReader(); + reader.onload = function(event) { + var content = event.target.result; + $('#content').val(content); + $('#content').trigger('change'); + }; + + reader.readAsText(file[0]); +} + +try { + $('#file-upload').change(function() { + file_upload(this.files); + }); +} +catch (e) { + alert(e); +} + +$('#file-upload').mouseover(function(){ + $(this).css( 'cursor', 'pointer' ); +}); + + }); diff --git a/views/base.tpl b/views/base.tpl index d6e6d04..b35ee03 100644 --- a/views/base.tpl +++ b/views/base.tpl @@ -75,12 +75,17 @@

«Few persons can be made to believe that it is not quite an easy thing to invent a method of secret writing which shall baffle investigation. Yet it may be roundly asserted that human ingenuity cannot concoct a cipher which human ingenuity cannot resolve...»

Edgar Allan Poe + +

+

ø

+ 41,017,923,819 pastes øbinned +

+
-

+

Based on an original idea from sebsauvage.net

- diff --git a/views/home.tpl b/views/home.tpl index ca70732..90dc804 100644 --- a/views/home.tpl +++ b/views/home.tpl @@ -1,3 +1,8 @@ +

+ + +

+

diff --git a/views/paste.tpl b/views/paste.tpl index 5cc1942..4fb4681 100644 --- a/views/paste.tpl +++ b/views/paste.tpl @@ -21,12 +21,11 @@

-

- Copy To Clipboard - | - Get short url - +

+ Copy To Clipboard + | + Get short url From 08032470c95bdd1622f0e515e7bd64204dc7079b Mon Sep 17 00:00:00 2001 From: max Date: Mon, 30 Apr 2012 01:10:04 +0700 Subject: [PATCH 2/2] 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. +
+ +

-

+

@@ -22,4 +28,4 @@

-%rebase base \ No newline at end of file +%rebase base max_size=max_size \ No newline at end of file