2015-08-05 02:48:46 +02:00
upload . load . need ( 'js/dragresize.js' , function ( ) { return window . dragresize } )
2015-06-11 06:02:46 +02:00
upload . modules . addmodule ( {
name : 'download' ,
2015-07-10 21:20:36 +02:00
delkeys : { } ,
2015-06-11 06:02:46 +02:00
// Dear santa, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings
template : ' \
2015-06-11 11:55:38 +02:00
< div class = "modulecontent" id = "dlarea" > \
2015-06-11 23:08:54 +02:00
< div class = "topbar" > \
< h1 id = "downloaded_filename" > < / h 1 > \
2015-06-11 11:55:38 +02:00
< div class = "viewswitcher" > \
2015-06-11 23:08:54 +02:00
< a id = "editpaste" class = "btn" > Edit Paste < / a \
> < a class = "btn" id = "newupload" href = "#" > New Upload < / a > \
< / d i v > \
2015-06-11 11:55:38 +02:00
< / d i v > \
2015-06-11 06:02:46 +02:00
< div id = "downloaddetails" > < / d i v > \
< div id = "btnarea" > \
2015-06-11 23:08:54 +02:00
< a class = "btn" id = "dlbtn" href = "#" > Download < / a \
> < a class = "btn" id = "inbrowserbtn" target = "_blank" href = "#" > View In Browser < / a \
> < a class = "btn" id = "deletebtn" href = "#" > Delete < / a > \
< / d i v > \
2015-06-11 11:55:38 +02:00
< / d i v > \
2015-06-11 06:02:46 +02:00
' ,
init : function ( ) {
2015-06-11 11:55:38 +02:00
$ ( document ) . on ( 'click' , '#editpaste' , this . editpaste . bind ( this ) )
2015-06-11 06:02:46 +02:00
} ,
route : function ( route , content ) {
2015-06-25 20:27:35 +02:00
if ( content != 'noref' ) {
return this
}
2015-06-11 06:02:46 +02:00
} ,
render : function ( view ) {
view . html ( this . template )
this . _ = { }
2015-06-11 11:55:38 +02:00
this . _ . view = view
2015-06-11 06:02:46 +02:00
this . _ . detailsarea = view . find ( '#downloaddetails' )
this . _ . filename = view . find ( '#downloaded_filename' )
this . _ . btns = view . find ( '#btnarea' )
this . _ . deletebtn = view . find ( '#deletebtn' )
this . _ . dlbtn = view . find ( '#dlbtn' )
this . _ . viewbtn = view . find ( '#inbrowserbtn' )
2015-06-11 11:55:38 +02:00
this . _ . viewswitcher = view . find ( '.viewswitcher' )
2015-06-11 12:20:15 +02:00
this . _ . newupload = view . find ( '#newupload' )
this . _ . editpaste = view . find ( '#editpaste' )
2015-06-11 11:55:38 +02:00
this . _ . dlarea = view . find ( '#dlarea' )
2015-06-12 04:00:30 +02:00
this . _ . title = $ ( 'title' )
2015-06-11 06:02:46 +02:00
$ ( '#footer' ) . hide ( )
} ,
initroute : function ( content ) {
2015-06-11 11:55:38 +02:00
delete this . _ [ 'text' ]
2015-06-11 06:02:46 +02:00
this . _ . filename . hide ( )
2015-06-12 04:00:30 +02:00
this . _ . title . text ( "Up1" )
2015-06-11 06:02:46 +02:00
this . _ . btns . hide ( )
2015-06-11 12:20:15 +02:00
this . _ . editpaste . hide ( )
this . _ . newupload . hide ( )
2015-06-11 06:02:46 +02:00
this . _ . content = { }
this . _ . content . main = this . _ . content . loading = $ ( '<h1>' ) . prop ( 'id' , 'downloadprogress' ) . text ( 'Downloading' )
this . _ . detailsarea . empty ( ) . append ( this . _ . content . main )
this . _ . deletebtn . hide ( )
upload . updown . download ( content , this . progress . bind ( this ) , this . downloaded . bind ( this ) )
} ,
unrender : function ( ) {
2015-06-12 04:00:30 +02:00
this . _ . title . text ( 'Up1' )
2015-06-11 06:02:46 +02:00
delete this [ '_' ]
} ,
2016-02-02 03:28:35 +01:00
/ * T h e s e m i m e s a r e t r u s t e d , a n y t h i n g n o t o n t h i s l i s t w i l l n o t e m b e d
nor provide view in browser links . Some embed exceptions apply
like svg will embed but will not directly link and pdf vice versa .
ALl text mime types support view in browser and translate to text / plain * /
2015-06-11 11:55:38 +02:00
assocations : {
'application/javascript' : 'text' ,
'application/x-javascript' : 'text' ,
'application/xml' : 'text' ,
2016-02-02 00:44:42 +01:00
'image/svg+xml' : 'svg' ,
2015-06-25 05:06:11 +02:00
// PDF for now only offers 'view in browser'
'application/pdf' : 'pdf' ,
'application/x-pdf' : 'pdf' ,
2016-02-02 03:28:35 +01:00
'text/plain' : 'text' ,
'audio/aac' : 'audio' ,
'audio/mp4' : 'audio' ,
'audio/mpeg' : 'audio' ,
'audio/ogg' : 'audio' ,
'audio/wav' : 'audio' ,
'audio/webm' : 'audio' ,
'video/mp4' : 'video' ,
'video/ogg' : 'video' ,
'video/webm' : 'video' ,
'audio/wave' : 'audio' ,
'audio/wav' : 'audio' ,
'audio/x-wav' : 'audio' ,
'audio/x-pn-wav' : 'audio' ,
'audio/vnd.wave' : 'audio' ,
'image/tiff' : 'image' ,
'image/x-tiff' : 'image' ,
'image/bmp' : 'image' ,
'image/x-windows-bmp' : 'image' ,
'image/gif' : 'image' ,
'image/x-icon' : 'image' ,
'image/jpeg' : 'image' ,
'image/pjpeg' : 'image' ,
'image/png' : 'image' ,
'image/webp' : 'image' ,
'text/' : 'text'
2015-06-25 05:06:11 +02:00
} ,
// Mime types to use for "View in browser" for safety reasons such as html we use text/plain
// Other display types such as PDF and images you want native viewing so we leave those
2016-02-02 00:44:42 +01:00
// SVG can be unsafe for viewing in a browser directly
2015-06-25 05:06:11 +02:00
safeassocations : {
2016-02-02 00:44:42 +01:00
'text' : 'text/plain' ,
'svg' : 'text/plain'
2015-06-25 05:06:11 +02:00
} ,
getassociation : function ( mime ) {
for ( var key in this . assocations ) {
if ( mime . startsWith ( key ) ) {
return this . assocations [ key ]
}
}
2015-06-11 11:55:38 +02:00
} ,
2016-02-05 03:46:07 +01:00
setupLineNumbers : function ( ele ) {
var markup = ele . html ( )
ele . html ( '<div class="line">' + markup . replace ( /\n/g , '</div><div class="line">' ) + '</div>' )
ele . find ( '.line' ) . each ( function ( i , e ) {
$ ( e ) . prepend ( $ ( '<span>' ) . addClass ( 'linenum' ) . text ( i + 1 ) )
} )
} ,
2015-06-11 06:02:46 +02:00
downloaded : function ( data ) {
this . _ . filename . text ( data . header . name )
2015-06-12 04:00:30 +02:00
this . _ . title . text ( data . header . name + ' - Up1' )
2015-06-11 06:02:46 +02:00
2015-07-10 21:20:36 +02:00
var stored = this . delkeys [ data . ident ]
if ( ! stored ) {
try {
stored = localStorage . getItem ( 'delete-' + data . ident )
2015-07-10 21:28:01 +02:00
} catch ( e ) {
console . log ( e )
}
2015-07-10 21:20:36 +02:00
}
2015-06-11 06:02:46 +02:00
2015-07-05 19:34:06 +02:00
if ( stored && ! isiframed ( ) ) {
2015-06-11 06:02:46 +02:00
this . _ . deletebtn . show ( ) . prop ( 'href' , ( upload . config . server ? upload . config . server : '' ) + 'del?delkey=' + stored + '&ident=' + data . ident )
}
2015-06-11 12:20:15 +02:00
this . _ . newupload . show ( )
2015-06-25 05:06:11 +02:00
var association = this . getassociation ( data . header . mime )
var safemime = this . safeassocations [ association ]
2015-06-11 06:02:46 +02:00
2016-02-02 00:44:42 +01:00
var decrypted = new Blob ( [ data . decrypted ] , { type : data . header . mime } )
var safedecrypted = new Blob ( [ decrypted ] , { type : safemime ? safemime : data . header . mime } )
2015-06-25 05:06:11 +02:00
var url = URL . createObjectURL ( decrypted )
2016-02-02 00:44:42 +01:00
var safeurl = URL . createObjectURL ( safedecrypted )
this . _ . viewbtn . prop ( 'href' , safeurl ) . hide ( )
2015-06-11 06:02:46 +02:00
this . _ . dlbtn . prop ( 'href' , url )
this . _ . dlbtn . prop ( 'download' , data . header . name )
delete this . _ [ 'content' ]
this . _ . detailsarea . empty ( )
2015-06-25 05:06:11 +02:00
if ( ! ! association ) {
this . _ . viewbtn . show ( )
}
2015-06-11 06:02:46 +02:00
2016-02-02 00:44:42 +01:00
if ( association == 'image' || association == 'svg' ) {
2015-06-11 06:02:46 +02:00
var imgcontent = $ ( '<div>' ) . prop ( 'id' , 'previewimg' ) . addClass ( 'preview' ) . appendTo ( this . _ . detailsarea )
var previewimg = $ ( '<img>' ) . addClass ( 'dragresize' ) . appendTo ( imgcontent ) . prop ( 'src' , url )
2015-06-25 05:06:11 +02:00
} else if ( association == 'text' ) {
2015-06-11 06:02:46 +02:00
var textcontent = $ ( '<div>' ) . prop ( 'id' , 'downloaded_text' ) . addClass ( 'preview' ) . addClass ( 'previewtext' ) . appendTo ( this . _ . detailsarea )
var pre = $ ( '<pre>' ) . appendTo ( textcontent )
var code = $ ( '<code>' ) . appendTo ( pre )
var fr = new FileReader ( )
fr . onload = function ( ) {
var text = fr . result
2015-06-11 11:55:38 +02:00
this . _ . text = { }
this . _ . text . header = data . header
this . _ . text . data = text
2015-06-11 06:02:46 +02:00
code . text ( text )
hljs . highlightBlock ( code [ 0 ] )
2016-02-05 03:46:07 +01:00
this . setupLineNumbers ( code )
2015-06-11 06:02:46 +02:00
2015-06-11 11:55:38 +02:00
} . bind ( this )
2015-06-11 06:02:46 +02:00
fr . readAsText ( data . decrypted )
2015-06-11 11:55:38 +02:00
2015-06-11 12:20:15 +02:00
this . _ . editpaste . show ( )
2015-06-25 05:06:11 +02:00
} else if ( association == 'video' ) {
2015-06-11 06:02:46 +02:00
$ ( '<video>' ) . addClass ( 'preview' ) . prop ( 'controls' , true ) . prop ( 'autoplay' , true ) . appendTo ( this . _ . detailsarea ) . prop ( 'src' , url )
2015-06-25 05:06:11 +02:00
} else if ( association == 'audio' ) {
2015-06-11 06:02:46 +02:00
$ ( '<audio>' ) . addClass ( 'preview' ) . prop ( 'controls' , true ) . prop ( 'autoplay' , true ) . appendTo ( this . _ . detailsarea ) . prop ( 'src' , url )
} else {
2015-06-24 05:33:25 +02:00
$ ( '<div>' ) . addClass ( 'preview' ) . addClass ( 'downloadexplain' ) . text ( "Click the Download link in the bottom-left to download this file." ) . appendTo ( this . _ . detailsarea )
2015-06-11 06:02:46 +02:00
}
this . _ . filename . show ( )
this . _ . btns . show ( )
} ,
2015-06-11 11:55:38 +02:00
closepaste : function ( ) {
this . _ . dlarea . show ( )
} ,
editpaste : function ( ) {
this . _ . dlarea . hide ( )
upload . textpaste . render ( this . _ . view , this . _ . text . header . name , this . _ . text . data , this . _ . text . header . mime , this . closepaste . bind ( this ) )
} ,
2015-06-11 06:02:46 +02:00
progress : function ( e ) {
if ( e == 'decrypting' ) {
this . _ . content . loading . text ( 'Decrypting' )
2015-06-11 08:47:11 +02:00
} else if ( e == 'error' ) {
2015-06-11 09:16:51 +02:00
this . _ . content . loading . text ( 'File not found or corrupt' )
2015-06-11 20:34:15 +02:00
this . _ . newupload . show ( )
2015-06-11 06:02:46 +02:00
} else {
2015-06-24 02:55:58 +02:00
var text = ''
if ( e . eventsource != 'encrypt' ) {
text = 'Downloading'
} else {
text = 'Decrypting'
}
2015-06-11 06:02:46 +02:00
var percent = ( e . loaded / e . total ) * 100
2015-06-24 02:55:58 +02:00
this . _ . content . loading . text ( text + ' ' + Math . floor ( percent ) + '%' )
2015-06-11 06:02:46 +02:00
}
}
} )