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

#21 - Resize too big images before upload

This commit is contained in:
Rémy HUBSCHER 2013-01-19 19:40:44 +01:00
parent d57595297e
commit b729a678ea
2 changed files with 687 additions and 624 deletions

View File

@ -1,11 +1,14 @@
/*global sjcl:true, jQuery:true, $:true, lzw:true, zerobin:true, ZeroClipboard:true, vizhash:true, prettyPrint:true, confirm:true */ /*global sjcl:true, jQuery:true, $:true, lzw:true, zerobin:true, ZeroClipboard:true, vizhash:true, prettyPrint:true, confirm:true */
;(function(){ ;
(function () {
"use strict"; "use strict";
/* Start random number generator seeding ASAP */ /* Start random number generator seeding ASAP */
sjcl.random.startCollectors(); sjcl.random.startCollectors();
/* Ensure jquery use cache for ajax requests */ /* Ensure jquery use cache for ajax requests */
$.ajaxSetup({ cache: true }); $.ajaxSetup({
cache: true
});
/** Create a function that create inline callbacks. /** Create a function that create inline callbacks.
We use it to create callbacks for onliners with static arguments We use it to create callbacks for onliners with static arguments
@ -27,7 +30,7 @@ function mkcb(func){
/*************************** /***************************
**** 0bin utilities *** **** 0bin utilities ***
****************************/ ***************************/
window.zerobin = { window.zerobin = {
@ -42,26 +45,37 @@ 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 () {
content = sjcl.codec.base64.fromBits(content); content = sjcl.codec.base64.fromBits(content);
if (compressCallback) {compressCallback();} if (compressCallback) {
compressCallback();
}
setTimeout(function () { setTimeout(function () {
content = lzw.compress(content); // content = lzw.compress(content); // Create a bug with JPG
if (encryptCallback) {encryptCallback();} if (encryptCallback) {
encryptCallback();
}
setTimeout(function () { setTimeout(function () {
try { try {
content = sjcl.encrypt(key, content); content = sjcl.encrypt(key, content);
} catch (e) { } catch (e) {
$('input, textarea, select, button').prop('disabled', false);
zerobin.progressBar('form.well .progress').container.hide();
zerobin.message('error', 'Paste could not be encrypted. Aborting.', zerobin.message('error', 'Paste could not be encrypted. Aborting.',
'Error'); 'Error');
} }
if (doneCallback) {doneCallback(content);} if (doneCallback) {
doneCallback(content);
}
}, 250); }, 250);
}, 250); }, 250);
@ -84,34 +98,35 @@ window.zerobin = {
/* Decrypt */ /* Decrypt */
setTimeout(function () { setTimeout(function () {
try { try {
content = sjcl.decrypt(key, content); content = sjcl.decrypt(key, content);
if (uncompressCallback) {uncompressCallback();} if (uncompressCallback) {
uncompressCallback();
}
/* Decompress */ /* Decompress */
setTimeout(function () { setTimeout(function () {
try { try {
content = lzw.decompress(content); content = lzw.decompress(content);
if (fromBase64Callback) {fromBase64Callback();} if (fromBase64Callback) {
fromBase64Callback();
}
/* From base 64 to bits */ /* From base 64 to bits */
setTimeout(function () { setTimeout(function () {
try { try {
content = sjcl.codec.base64.toBits(content); content = sjcl.codec.base64.toBits(content);
if (toStringCallback) {toStringCallback();} if (toStringCallback) {
toStringCallback();
}
/* From bits to string */ /* From bits to string */
setTimeout(function () { setTimeout(function () {
try { try {
content = sjcl.codec.utf8String.fromBits(content); content = sjcl.codec.utf8String.fromBits(content);
if (doneCallback) {doneCallback(content);} if (doneCallback) {
doneCallback(content);
}
} catch (err) { } catch (err) {
errorCallback(err); errorCallback(err);
} }
@ -145,8 +160,7 @@ window.zerobin = {
getFormatedDate: function (date) { getFormatedDate: function (date) {
date = date || new Date(); date = date || new Date();
return ((date.getMonth() +1 ) + '-' + return ((date.getMonth() + 1) + '-' + date.getDate() + '-' + date.getFullYear());
date.getDate() + '-' + date.getFullYear());
}, },
getFormatedTime: function (date) { getFormatedTime: function (date) {
@ -154,9 +168,15 @@ window.zerobin = {
var h = date.getHours(), var h = date.getHours(),
m = date.getMinutes(), m = date.getMinutes(),
s = date.getSeconds(); s = date.getSeconds();
if (h < 10) {h = "0" + h;} if (h < 10) {
if (m < 10) {m = "0" + m;} h = "0" + h;
if (s < 10) {s = "0" + s;} }
if (m < 10) {
m = "0" + m;
}
if (s < 10) {
s = "0" + s;
}
return h + ":" + m + ":" + s; return h + ":" + m + ":" + s;
}, },
@ -216,8 +236,7 @@ window.zerobin = {
storePaste: function (url, date) { storePaste: function (url, date) {
date = date || new Date(); date = date || new Date();
date = (date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date = (date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + zerobin.getFormatedTime(date));
date.getDate() + ' ' + zerobin.getFormatedTime(date));
var keys = zerobin.getLocalStorageKeys(); var keys = zerobin.getLocalStorageKeys();
@ -246,8 +265,11 @@ window.zerobin = {
displayDate = pasteDateTime.split(' ')[1]; displayDate = pasteDateTime.split(' ')[1];
prefix = 'at '; prefix = 'at ';
} }
pastes.push({displayDate: displayDate, prefix: prefix, pastes.push({
link: localStorage.getItem(key)}); displayDate: displayDate,
prefix: prefix,
link: localStorage.getItem(key)
});
}); });
return pastes; return pastes;
@ -309,14 +331,19 @@ window.zerobin = {
message: function (type, message, title, flush, callback) { message: function (type, message, title, flush, callback) {
$(window).scrollTop(0); $(window).scrollTop(0);
if (flush) {$('.alert-'+type).remove();} if (flush) {
$('.alert-' + type).remove();
}
var $message = $('#alert-template').clone().attr('id', null) var $message = $('#alert-template').clone().attr('id', null)
.addClass('alert alert-' + type); .addClass('alert alert-' + type);
$('.message', $message).html(message); $('.message', $message).html(message);
if (title) {$('.title', $message).html(title);} if (title) {
else {$('.title', $message).remove();} $('.title', $message).html(title);
} else {
$('.title', $message).remove();
}
$message.prependTo($('#main')).show('fadeUp', callback); $message.prependTo($('#main')).show('fadeUp', callback);
}, },
@ -324,8 +351,13 @@ window.zerobin = {
/** Return a progress bar object */ /** Return a progress bar object */
progressBar: function (selector) { progressBar: function (selector) {
var $container = $(selector); var $container = $(selector);
var bar = {container: $container, elem: $container.find('.bar')}; var bar = {
bar.set = function(text, rate){bar.elem.text(text).css('width', rate);}; container: $container,
elem: $container.find('.bar')
};
bar.set = function (text, rate) {
bar.elem.text(text).css('width', rate);
};
return bar; return bar;
}, },
@ -386,13 +418,44 @@ window.zerobin = {
var reader = new FileReader(); var reader = new FileReader();
if (current_file.type.indexOf('image') == 0) { if (current_file.type.indexOf('image') == 0) {
reader.onload = function (event) { reader.onload = function (event) {
$('#content').val(event.target.result).trigger('change'); var image = new Image();
image.src = event.target.result;
image.onload = function() {
var maxWidth = 1024,
maxHeight = 1024,
imageWidth = image.width,
imageHeight = image.height;
if (imageWidth > imageHeight) {
if (imageWidth > maxWidth) {
imageHeight *= maxWidth / imageWidth;
imageWidth = maxWidth;
}
}
else {
if (imageHeight > maxHeight) {
imageWidth *= maxHeight / imageHeight;
imageHeight = maxHeight;
}
}
var canvas = document.createElement('canvas');
canvas.width = imageWidth;
canvas.height = imageHeight;
image.width = imageWidth;
image.height = imageHeight;
var ctx = canvas.getContext("2d");
ctx.drawImage(this, 0, 0, imageWidth, imageHeight);
var paste = canvas.toDataURL(current_file.type);
$('#content').val(paste).trigger('change');
$('#content').hide(); $('#content').hide();
var img = $('<img/>'); $(image).css('max-width', '742px');
$(img).attr('src', event.target.result); $('#content').after(image);
$(img).css('max-width', '742px'); }
$('#content').after(img); }
};
reader.readAsDataURL(current_file); reader.readAsDataURL(current_file);
} else { } else {
reader.onload = function (event) { reader.onload = function (event) {
@ -450,7 +513,10 @@ $('.btn-primary').live("click", function(e){
function (content) { function (content) {
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 sizebytes = zerobin.count(JSON.stringify(data));
var oversized = sizebytes > zerobin.max_size; // 100kb - the others header information var oversized = sizebytes > zerobin.max_size; // 100kb - the others header information
var readableFsize = Math.round(sizebytes / 1024); var readableFsize = Math.round(sizebytes / 1024);
@ -459,8 +525,7 @@ $('.btn-primary').live("click", function(e){
if (oversized) { if (oversized) {
bar.container.hide(); bar.container.hide();
$form.prop('disabled', false); $form.prop('disabled', false);
zerobin.message('error', zerobin.message('error', ('The encrypted file was <strong class="file-size">' + readableFsize +
('The encrypted file was <strong class="file-size">' + readableFsize +
'</strong>KB. You have reached the maximum size limit of ' + readableMaxsize + 'KB.'), '</strong>KB. You have reached the maximum size limit of ' + readableMaxsize + 'KB.'),
'Warning!', true); 'Warning!', true);
return; return;
@ -473,8 +538,7 @@ $('.btn-primary').live("click", function(e){
zerobin.message( zerobin.message(
'error', 'error',
'Paste could not be saved. Please try again later.', 'Paste could not be saved. Please try again later.',
'Error' 'Error');
);
}) })
.success(function (data) { .success(function (data) {
@ -492,8 +556,7 @@ $('.btn-primary').live("click", function(e){
window.location = (paste_url); window.location = (paste_url);
} }
}); });
} });
);
} catch (err) { } catch (err) {
$form.prop('disabled', false); $form.prop('disabled', false);
bar.container.hide(); bar.container.hide();
@ -584,7 +647,9 @@ if (content && key) {
var clip = new ZeroClipboard.Client(); var clip = new ZeroClipboard.Client();
// Callback to reposition the clibpboad flash animation overlay // Callback to reposition the clibpboad flash animation overlay
var reposition = function(){clip.reposition();}; var reposition = function () {
clip.reposition();
};
clip.addEventListener('mouseup', function () { clip.addEventListener('mouseup', function () {
$('#clip-button').text('Copying paste...'); $('#clip-button').text('Copying paste...');
@ -609,8 +674,7 @@ if (content && key) {
$('#copy-success').hide(); $('#copy-success').hide();
zerobin.message('success', zerobin.message('success',
'<a href="' + tinyurl + '">' + tinyurl + '</a>', '<a href="' + tinyurl + '">' + tinyurl + '</a>',
'Short url', true, reposition 'Short url', true, reposition);
);
$('#short-url').text('Get short url'); $('#short-url').text('Get short url');
}); });
}); });
@ -649,8 +713,7 @@ if (content && key) {
}, 250); }, 250);
} });
);
} /* End of "DECRYPTION" */ } /* End of "DECRYPTION" */
@ -668,8 +731,7 @@ $('#content').elastic();
$('#content').live('keyup change', function () { $('#content').live('keyup change', function () {
if ($('#content').height() < 400) { if ($('#content').height() < 400) {
$('.paste-option.down').remove(); $('.paste-option.down').remove();
} } else {
else {
if ($('.paste-option').length === 1) { if ($('.paste-option').length === 1) {
$('.paste-option').clone().addClass('down').appendTo('form.well'); $('.paste-option').clone().addClass('down').appendTo('form.well');
@ -747,9 +809,10 @@ if (zerobin.support.fileUpload) {
try { try {
$button.val('Uploading...'); $button.val('Uploading...');
$button.prop('disabled', true); $button.prop('disabled', true);
$buttonOverlay.change(function(){zerobin.upload(this.files);}); $buttonOverlay.change(function () {
} zerobin.upload(this.files);
catch (e) { });
} catch (e) {
zerobin.message('error', 'Could no upload the file', 'Error'); zerobin.message('error', 'Could no upload the file', 'Error');
$button.val('Upload File'); $button.val('Upload File');
$button.prop('disabled', false); $button.prop('disabled', false);

File diff suppressed because one or more lines are too long