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

File diff suppressed because one or more lines are too long