From 74bfee09c825d6735da58f100e452debf4ed924f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20HUBSCHER?= Date: Sat, 19 Jan 2013 17:05:37 +0100 Subject: [PATCH] #17 - Add drag and drop file support. On the upload of an image, display it. --- zerobin/routes.py | 25 +++++++------- zerobin/static/js/behavior.js | 62 +++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 40 deletions(-) diff --git a/zerobin/routes.py b/zerobin/routes.py index 5d4d038..8b44836 100644 --- a/zerobin/routes.py +++ b/zerobin/routes.py @@ -55,13 +55,13 @@ def create_paste(): return {'status': 'error', 'message': u"Encoding error: the paste couldn't be saved."} - if '{"iv":' not in content: # reject silently non encrypted content + if '{"iv":' not in content: # reject silently non encrypted content return '' if content: - # check size of the paste. if more than settings return error without saving paste. - # prevent from unusual use of the system. - # need to be improved + # check size of the paste. if more than settings return error + # without saving paste. prevent from unusual use of the + # system. need to be improved if len(content) < settings.MAX_SIZE: expiration = request.forms.get('expiration', u'burn_after_reading') paste = Paste(expiration=expiration, content=content) @@ -81,12 +81,12 @@ def create_paste(): GLOBAL_CONTEXT['pastes_count'] = Paste.get_pastes_count() GLOBAL_CONTEXT['refresh_counter'] = now - return {'status': 'ok', 'paste': paste.uuid} return {'status': 'error', - 'message': u"Serveur error: the paste couldn't be saved. Please try later."} + 'message': u"Serveur error: the paste couldn't be saved. " + u"Please try later."} @app.route('/paste/:paste_id') @@ -105,7 +105,8 @@ def display_paste(paste_id): # to the paste that happens during the paste creation try: keep_alive = paste.expiration.split('#')[1] - keep_alive = datetime.strptime(keep_alive, '%Y-%m-%d %H:%M:%S.%f') + keep_alive = datetime.strptime(keep_alive, + '%Y-%m-%d %H:%M:%S.%f') keep_alive = now < keep_alive + timedelta(seconds=10) except IndexError: keep_alive = False @@ -136,8 +137,8 @@ def server_static(filename): def get_app(debug=None, settings_file='', compressed_static=None): """ - Return a tuple (settings, app) configured using passed parameters and/or - a setting file. + Return a tuple (settings, app) configured using passed + parameters and/or a setting file. """ if settings_file: settings.update_with_file(os.path.abspath(settings_file)) @@ -159,8 +160,8 @@ def get_app(debug=None, settings_file='', compressed_static=None): @clize.clize(coerce={'debug': bool, 'compressed_static': bool}) -def runserver(host='', port='', debug=None, user='', - group='', settings_file='', compressed_static=None, version=False): +def runserver(host='', port='', debug=None, user='', group='', + settings_file='', compressed_static=None, version=False): settings, app = get_app(debug, settings_file, compressed_static) @@ -179,7 +180,7 @@ def runserver(host='', port='', debug=None, user='', run(app, host=settings.HOST, port=settings.PORT, reloader=True, server="cherrypy") else: - run(app, host=settings.HOST, port=settings.PORT, server="cherrypy") + run(app, host=settings.HOST, port=settings.PORT, server="cherrypy") def main(): diff --git a/zerobin/static/js/behavior.js b/zerobin/static/js/behavior.js index fbfc620..a004954 100644 --- a/zerobin/static/js/behavior.js +++ b/zerobin/static/js/behavior.js @@ -41,8 +41,8 @@ window.zerobin = { setTimeout (function(){ - content = sjcl.codec.utf8String.toBits(content); - if (toBase64Callback) {toBase64Callback();} + content = sjcl.codec.utf8String.toBits(content); + if (toBase64Callback) {toBase64Callback();} setTimeout(function(){ @@ -55,7 +55,12 @@ window.zerobin = { if (encryptCallback) {encryptCallback();} setTimeout(function(){ - content = sjcl.encrypt(key, content); + try { + content = sjcl.encrypt(key, content); + } catch (e) { + zerobin.message('error', 'Paste could not be encrypted. Aborting.', + 'Error'); + } if (doneCallback) {doneCallback(content);} }, 250); @@ -363,19 +368,17 @@ window.zerobin = { handleDrop: function(e) { zerobin.ignoreDrag(e); zerobin.upload(e.originalEvent.dataTransfer.files); - $("#content").removeClass('hover'); + $("#content").removeClass('hover'); }, handleDragOver: function(e) { zerobin.ignoreDrag(e); $(this).addClass('hover'); - console.log('dragover'); }, handleDragLeave: function(e) { zerobin.ignoreDrag(e); $(this).removeClass('hover'); - console.log('dragout'); }, upload: function(files) { @@ -386,7 +389,7 @@ window.zerobin = { $('#content').val(event.target.result).trigger('change'); $('#content').hide(); var img = $(''); - $(img).attr('src', event.target.result); + $(img).attr('src', event.target.result); $(img).css('max-width', '742px'); $('#content').after(img); }; @@ -395,7 +398,7 @@ window.zerobin = { reader.onload = function(event) { $('#content').val(event.target.result).trigger('change'); }; - reader.readAsText(current_file); + reader.readAsText(current_file); } } }; @@ -419,20 +422,7 @@ $('.btn-primary').live("click", function(e){ e.preventDefault(); var paste = $('textarea').val(); - var sizebytes = zerobin.count($('#content').val()); - var oversized = sizebytes > zerobin.max_size; - var readableFsize = Math.round(sizebytes / 1024); - var readableMaxsize = Math.round(zerobin.max_size / 1024); - - if (oversized){ - zerobin.message('error', - ('Your file is ' + readableFsize + - 'KB. You have reached the maximum size limit of ' + - readableMaxsize + 'KB.'), - 'Warning!', true); - } - - if (!oversized && paste.trim()) { + if (paste.trim()) { var $form = $('input, textarea, select, button').prop('disabled', true); @@ -461,6 +451,20 @@ $('.btn-primary').live("click", function(e){ bar.set('Sending...', '95%'); var data = {content: content, expiration: expiration}; + var sizebytes = zerobin.count(JSON.stringify(data)); + var oversized = sizebytes > 95000; // 100kb - the others header information + var readableFsize = Math.round(sizebytes / 1024); + var readableMaxsize = Math.round(zerobin.max_size / 1024); + + if(oversized) { + bar.container.hide(); + $form.prop('disabled', false); + zerobin.message('error', + ('The encrypted file was ' + readableFsize + + 'KB. You have reached the maximum size limit of 93 KB.'), + 'Warning!', true); + return; + } $.post('/paste/create', data) .error(function(error) { @@ -562,7 +566,7 @@ if (content && key) { bar.set('Code coloration...', '95%'); /* Add a continuation to let the UI redraw */ - setTimeout(function(){ + setTimeout(function() { /* Setup flash clipboard button */ ZeroClipboard.setMoviePath('/static/js/ZeroClipboard.swf'); @@ -614,11 +618,13 @@ if (content && key) { $('#paste-content').addClass('linenums'); prettyPrint(); } else { - zerobin.message('info', - "The paste did not seem to be code, so it " + - "was not colorized. " + - "Force coloration", - '', false, reposition); + if(content.indexOf('data:image') != 0){ + zerobin.message('info', + "The paste did not seem to be code, so it " + + "was not colorized. " + + "Force coloration", + '', false, reposition); + } } /* Class to switch to paste content style with coloration done */