@@ -65,6 +65,7 @@ const JSONBOOL = '":true ';
6565const DIRECTORYLENGTH = 9 ;
6666const IMAGES = { gif : 1 , jpg : 1 , jpeg : 1 , png : 1 , svg : 1 } ;
6767const BINARYREADDATA = { start : BINARY_HEADER_LENGTH } ;
68+ const BINARYREADDATABASE64 = { start : BINARY_HEADER_LENGTH , encoding : 'base64' } ;
6869const BINARYREADMETA = { start : 0 , end : BINARY_HEADER_LENGTH - 1 , encoding : 'binary' } ;
6970const CLEANDBTICKS = 86400000 * 2 ; // 48 hours
7071
@@ -3376,7 +3377,7 @@ DatabaseBuilder.prototype.repository = function(key, value) {
33763377DatabaseBuilder . prototype . compile = function ( ) {
33773378 var self = this ;
33783379 var raw = self . $code . join ( '' ) ;
3379- var code = 'var repository=$F.repository;var options=$F.options;var arg=$F.arg;var fn=$F.fn;var $is=false;var $tmp ;' + raw + ( self . $code . length && raw . substring ( raw . length - 7 ) !== 'return;' ? 'if(!$is)return;' : '' ) + 'if(options.fields){var $doc={};for(var $i=0;$i<options.fields.length;$i++){var prop=options.fields[$i];$doc[prop]=doc[prop]}if(options.sort)$doc[options.sort.name]=doc[options.sort.name];return $doc}else if(options.fields2){var $doc={};var $keys=Object.keys(doc);for(var $i=0;$i<$keys.length;$i++){var prop=$keys[$i];!options.fields2[prop]&&($doc[prop]=doc[prop])}return $doc}else{return doc}' ;
3380+ var code = 'var repository=$F.repository, options=$F.options, arg=$F.arg, fn=$F.fn, $is=false,$tmp ;var R=repository ;' + raw + ( self . $code . length && raw . substring ( raw . length - 7 ) !== 'return;' ? 'if(!$is)return;' : '' ) + 'if(options.fields){var $doc={};for(var $i=0;$i<options.fields.length;$i++){var prop=options.fields[$i];$doc[prop]=doc[prop]}if(options.sort)$doc[options.sort.name]=doc[options.sort.name];return $doc}else if(options.fields2){var $doc={};var $keys=Object.keys(doc);for(var $i=0;$i<$keys.length;$i++){var prop=$keys[$i];!options.fields2[prop]&&($doc[prop]=doc[prop])}return $doc}else{return doc}' ;
33803381 var opt = self . $options ;
33813382 self . $inlinesort = ! ! ( opt . take && opt . sort && opt . sort !== null ) ;
33823383 self . $limit = ( opt . take || 0 ) + ( opt . skip || 0 ) ;
@@ -5040,6 +5041,49 @@ Binary.prototype.read = function(id, callback, count) {
50405041 return self ;
50415042} ;
50425043
5044+ Binary . prototype . readbase64 = function ( id , callback , count ) {
5045+
5046+ var self = this ;
5047+
5048+ if ( count > 3 ) {
5049+ callback ( new Error ( 'File not found.' ) ) ;
5050+ return self ;
5051+ }
5052+
5053+ var isnew = false ;
5054+
5055+ if ( id > 0 )
5056+ isnew = true ;
5057+ else if ( id [ 0 ] === 'B' || id [ 0 ] === 'b' ) {
5058+ id = + id . substring ( id . length - DIRECTORYLENGTH ) ;
5059+ isnew = true ;
5060+ } else if ( id . indexOf ( '#' ) === - 1 )
5061+ id = self . db . name + '#' + id ;
5062+
5063+ var filename ;
5064+
5065+ if ( isnew ) {
5066+ filename = Path . join ( self . $directory ( id ) , id . toString ( ) . padLeft ( DIRECTORYLENGTH , '0' ) + EXTENSION_BINARY ) ;
5067+ } else
5068+ filename = framework_utils . join ( self . directory , id + EXTENSION_BINARY ) ;
5069+
5070+ var stream = Fs . createReadStream ( filename , BINARYREADMETA ) ;
5071+ stream . on ( 'error' , err => callback ( err ) ) ;
5072+ stream . on ( 'data' , function ( buffer ) {
5073+ var json = buffer . toString ( 'utf8' ) . replace ( REG_CLEAN , '' ) ;
5074+ if ( json ) {
5075+ var meta = JSON . parse ( json , jsonparser ) ;
5076+ stream = Fs . createReadStream ( filename , BINARYREADDATABASE64 ) ;
5077+ callback ( null , stream , meta ) ;
5078+ CLEANUP ( stream ) ;
5079+ } else
5080+ setTimeout ( readfileattempt , 100 , self , id , callback , count || 1 ) ;
5081+ } ) ;
5082+
5083+ return self ;
5084+ } ;
5085+
5086+
50435087function readfileattempt ( self , id , callback , count ) {
50445088 self . read ( id , callback , count + 1 ) ;
50455089}
0 commit comments