From bcad8f7ef3cf4294f93eb0b7d77d6c69cb3f9b1e Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Sat, 23 May 2020 23:30:26 -0700 Subject: [PATCH] uploads --- examples/basic/upload.html | 22 ++++++++++++---------- lib/upload.js | 13 +++++++++---- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/examples/basic/upload.html b/examples/basic/upload.html index 3c1e85ce..39d41677 100644 --- a/examples/basic/upload.html +++ b/examples/basic/upload.html @@ -1,10 +1,12 @@ -
+ +
+ Drag & drop videos, songs, or images!
@@ -19,16 +21,16 @@ $('html').upload(function resize(eve, up){ return up.shrink(eve, resize, 1024); } var b64 = (eve.base64 || ((eve.event || eve).target || eve).result || eve); - gun.get('test').get('paste').put(b64); -}) + gun.get('test').get(eve.id).put(b64); +}); -gun.get('test').get('paste').on(function(data){ - $('video, audio, img').css({display: 'none'}) +gun.get('test').map().on(function(data){ if(!data){ return } var type = data.split(';')[0], ui; - if(type.indexOf('image') + 1){ ui = $("img") } - if(type.indexOf('video') + 1){ ui = $('video') } - if(type.indexOf('audio') + 1){ ui = $('audio') } - ($(ui).css({display: 'block'}).get(0)||{}).src = data; + if(type.indexOf('image') + 1){ ui = $("img").get(0) } + if(type.indexOf('video') + 1){ ui = $('video').get(0) } + if(type.indexOf('audio') + 1){ ui = $('audio').get(0) } + if(!ui){ return } + $(ui).clone().prependTo('center').get(0).src = data; }) \ No newline at end of file diff --git a/lib/upload.js b/lib/upload.js index aaacba29..8d748ffa 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -1,11 +1,15 @@ ;(function(){ function upload(cb, opt){ - var el = $(this); cb = cb || function(){}; opt = opt || {}; + var el = $(this); cb = cb || function(){}; + opt = $.isPlainObject(opt)? opt : {input: opt}; el.on('drop', function(e){ e.preventDefault(); - upload.drop(((e.originalEvent||{}).dataTransfer||{}).files, 0); + upload.drop(((e.originalEvent||{}).dataTransfer||{}).files||[], 0); }).on('dragover', function(e){ - e.preventDefault(); + e.preventDefault(); + }); + $(opt.input||el).on('change', function(e){ + upload.drop((e.target||this||{}).files, 0); }); upload.drop = function(files,i){ if(opt.max && (files[i].fileSize > opt.max || files[i].size > opt.max)){ @@ -20,6 +24,7 @@ }; if(files[i]){ reader.readAsDataURL(files[i]) } } + return this; } upload.shrink = function(e, cb, w, h){ // via stackoverflow if(!e){ return cb && cb({err: "No file!"}) } @@ -37,7 +42,7 @@ canvas.width = w; canvas.height = h; ctx.drawImage(img, 0, 0, w, h); // draw source image to canvas. - var b64 = e.base64 = canvas.toDataURL(); // base64 the compressed image. + var b64 = e.base64 = canvas.toDataURL(); // base64 the shrunk image. cb((e.base64 && e) || b64); }; }