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

#17 - Add drag and drop file support. On the upload of an image, display it.

This commit is contained in:
Rémy HUBSCHER 2013-01-19 17:05:37 +01:00
parent 0879a8556e
commit 74bfee09c8
2 changed files with 47 additions and 40 deletions

View File

@ -55,13 +55,13 @@ def create_paste():
return {'status': 'error', return {'status': 'error',
'message': u"Encoding error: the paste couldn't be saved."} '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 '' return ''
if content: if content:
# check size of the paste. if more than settings return error without saving paste. # check size of the paste. if more than settings return error
# prevent from unusual use of the system. # without saving paste. prevent from unusual use of the
# need to be improved # system. need to be improved
if len(content) < settings.MAX_SIZE: if len(content) < settings.MAX_SIZE:
expiration = request.forms.get('expiration', u'burn_after_reading') expiration = request.forms.get('expiration', u'burn_after_reading')
paste = Paste(expiration=expiration, content=content) paste = Paste(expiration=expiration, content=content)
@ -81,12 +81,12 @@ def create_paste():
GLOBAL_CONTEXT['pastes_count'] = Paste.get_pastes_count() GLOBAL_CONTEXT['pastes_count'] = Paste.get_pastes_count()
GLOBAL_CONTEXT['refresh_counter'] = now GLOBAL_CONTEXT['refresh_counter'] = now
return {'status': 'ok', return {'status': 'ok',
'paste': paste.uuid} 'paste': paste.uuid}
return {'status': 'error', 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') @app.route('/paste/:paste_id')
@ -105,7 +105,8 @@ def display_paste(paste_id):
# to the paste that happens during the paste creation # to the paste that happens during the paste creation
try: try:
keep_alive = paste.expiration.split('#')[1] 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) keep_alive = now < keep_alive + timedelta(seconds=10)
except IndexError: except IndexError:
keep_alive = False keep_alive = False
@ -136,8 +137,8 @@ def server_static(filename):
def get_app(debug=None, settings_file='', compressed_static=None): def get_app(debug=None, settings_file='', compressed_static=None):
""" """
Return a tuple (settings, app) configured using passed parameters and/or Return a tuple (settings, app) configured using passed
a setting file. parameters and/or a setting file.
""" """
if settings_file: if settings_file:
settings.update_with_file(os.path.abspath(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}) @clize.clize(coerce={'debug': bool, 'compressed_static': bool})
def runserver(host='', port='', debug=None, user='', def runserver(host='', port='', debug=None, user='', group='',
group='', settings_file='', compressed_static=None, version=False): settings_file='', compressed_static=None, version=False):
settings, app = get_app(debug, settings_file, compressed_static) 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, run(app, host=settings.HOST, port=settings.PORT, reloader=True,
server="cherrypy") server="cherrypy")
else: else:
run(app, host=settings.HOST, port=settings.PORT, server="cherrypy") run(app, host=settings.HOST, port=settings.PORT, server="cherrypy")
def main(): def main():

View File

@ -41,8 +41,8 @@ window.zerobin = {
setTimeout (function(){ setTimeout (function(){
content = sjcl.codec.utf8String.toBits(content); content = sjcl.codec.utf8String.toBits(content);
if (toBase64Callback) {toBase64Callback();} if (toBase64Callback) {toBase64Callback();}
setTimeout(function(){ setTimeout(function(){
@ -55,7 +55,12 @@ window.zerobin = {
if (encryptCallback) {encryptCallback();} if (encryptCallback) {encryptCallback();}
setTimeout(function(){ 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);} if (doneCallback) {doneCallback(content);}
}, 250); }, 250);
@ -363,19 +368,17 @@ window.zerobin = {
handleDrop: function(e) { handleDrop: function(e) {
zerobin.ignoreDrag(e); zerobin.ignoreDrag(e);
zerobin.upload(e.originalEvent.dataTransfer.files); zerobin.upload(e.originalEvent.dataTransfer.files);
$("#content").removeClass('hover'); $("#content").removeClass('hover');
}, },
handleDragOver: function(e) { handleDragOver: function(e) {
zerobin.ignoreDrag(e); zerobin.ignoreDrag(e);
$(this).addClass('hover'); $(this).addClass('hover');
console.log('dragover');
}, },
handleDragLeave: function(e) { handleDragLeave: function(e) {
zerobin.ignoreDrag(e); zerobin.ignoreDrag(e);
$(this).removeClass('hover'); $(this).removeClass('hover');
console.log('dragout');
}, },
upload: function(files) { upload: function(files) {
@ -386,7 +389,7 @@ window.zerobin = {
$('#content').val(event.target.result).trigger('change'); $('#content').val(event.target.result).trigger('change');
$('#content').hide(); $('#content').hide();
var img = $('<img/>'); var img = $('<img/>');
$(img).attr('src', event.target.result); $(img).attr('src', event.target.result);
$(img).css('max-width', '742px'); $(img).css('max-width', '742px');
$('#content').after(img); $('#content').after(img);
}; };
@ -395,7 +398,7 @@ window.zerobin = {
reader.onload = function(event) { reader.onload = function(event) {
$('#content').val(event.target.result).trigger('change'); $('#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(); e.preventDefault();
var paste = $('textarea').val(); var paste = $('textarea').val();
var sizebytes = zerobin.count($('#content').val()); if (paste.trim()) {
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 <strong class="file-size">' + readableFsize +
'</strong>KB. You have reached the maximum size limit of ' +
readableMaxsize + 'KB.'),
'Warning!', true);
}
if (!oversized && paste.trim()) {
var $form = $('input, textarea, select, button').prop('disabled', true); var $form = $('input, textarea, select, button').prop('disabled', true);
@ -461,6 +451,20 @@ $('.btn-primary').live("click", function(e){
bar.set('Sending...', '95%'); bar.set('Sending...', '95%');
var data = {content: content, expiration: expiration}; 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 <strong class="file-size">' + readableFsize +
'</strong>KB. You have reached the maximum size limit of 93 KB.'),
'Warning!', true);
return;
}
$.post('/paste/create', data) $.post('/paste/create', data)
.error(function(error) { .error(function(error) {
@ -562,7 +566,7 @@ if (content && key) {
bar.set('Code coloration...', '95%'); bar.set('Code coloration...', '95%');
/* Add a continuation to let the UI redraw */ /* Add a continuation to let the UI redraw */
setTimeout(function(){ setTimeout(function() {
/* Setup flash clipboard button */ /* Setup flash clipboard button */
ZeroClipboard.setMoviePath('/static/js/ZeroClipboard.swf'); ZeroClipboard.setMoviePath('/static/js/ZeroClipboard.swf');
@ -614,11 +618,13 @@ if (content && key) {
$('#paste-content').addClass('linenums'); $('#paste-content').addClass('linenums');
prettyPrint(); prettyPrint();
} else { } else {
zerobin.message('info', if(content.indexOf('data:image') != 0){
"The paste did not seem to be code, so it " + zerobin.message('info',
"was not colorized. " + "The paste did not seem to be code, so it " +
"<a id='force-coloration' href='#'>Force coloration</a>", "was not colorized. " +
'', false, reposition); "<a id='force-coloration' href='#'>Force coloration</a>",
'', false, reposition);
}
} }
/* Class to switch to paste content style with coloration done */ /* Class to switch to paste content style with coloration done */