@ -1005,12 +1005,12 @@ factory('MtpDcConfigurator', function () {
var dcOptions = window . _testMode
var dcOptions = window . _testMode
? [
? [
{ id : 1 , host : '173.240.5.253' , port : 80 } ,
{ id : 1 , host : '173.240.5.253' , port : 80 } ,
{ id : 2 , host : '109.239.131.195 ' , port : 80 } ,
{ id : 2 , host : '149.154.167.40 ' , port : 80 } ,
{ id : 3 , host : '174.140.142.5' , port : 80 }
{ id : 3 , host : '174.140.142.5' , port : 80 }
]
]
: [
: [
{ id : 1 , host : '173.240.5.1' , port : 80 } ,
{ id : 1 , host : '173.240.5.1' , port : 80 } ,
{ id : 2 , host : '109.239.131.193 ' , port : 80 } ,
{ id : 2 , host : '149.154.167.5 ' , port : 80 } ,
{ id : 3 , host : '174.140.142.6' , port : 80 } ,
{ id : 3 , host : '174.140.142.6' , port : 80 } ,
{ id : 4 , host : '31.210.235.12' , port : 80 } ,
{ id : 4 , host : '31.210.235.12' , port : 80 } ,
{ id : 5 , host : '116.51.22.2' , port : 80 } ,
{ id : 5 , host : '116.51.22.2' , port : 80 } ,
@ -2832,7 +2832,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
deferred . reject ( ) ;
deferred . reject ( ) ;
} ;
} ;
if ( false ) { // is file bytes
if ( bytes instanceof Blob ) { // is file bytes
fileWriter . write ( bytes ) ;
fileWriter . write ( bytes ) ;
} else {
} else {
fileWriter . write ( new Blob ( [ bytesToArrayBuffer ( bytes ) ] ) ) ;
fileWriter . write ( new Blob ( [ bytesToArrayBuffer ( bytes ) ] ) ) ;
@ -2841,6 +2841,23 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
return deferred . promise ;
return deferred . promise ;
}
}
function fileCopyTo ( fromFileEntry , toFileEntry ) {
var deferred = $q . defer ( ) ;
toFileEntry . createWriter ( function ( fileWriter ) {
fileWriteBytes ( fileWriter , fromFileEntry ) . then ( function ( ) {
deferred . resolve ( fileWriter ) ;
} , function ( e ) {
fileWriter . truncate ( 0 ) ;
deferred . reject ( e ) ;
} ) ;
} , function ( e ) {
deferred . reject ( e ) ;
} ) ;
return deferred . promise ;
}
function getFileName ( location ) {
function getFileName ( location ) {
switch ( location . _ ) {
switch ( location . _ ) {
case 'inputVideoFileLocation' :
case 'inputVideoFileLocation' :
@ -2982,14 +2999,15 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
return cachedDownloadPromises [ fileName ] = deferred . promise ;
return cachedDownloadPromises [ fileName ] = deferred . promise ;
}
}
function downloadFile ( dcID , location , size , fileEntry , options ) {
function downloadFile ( dcID , location , size , options ) {
options = options || { } ;
options = options || { } ;
console . log ( dT ( ) , 'Dload file' , dcID , location , size ) ;
console . log ( dT ( ) , 'Dload file' , dcID , location , size ) ;
var fileName = getFileName ( location ) ,
var fileName = getFileName ( location ) ,
toFileEntry = options . toFileEntry || null ,
cachedPromise = cachedSavePromises [ fileName ] || cachedDownloadPromises [ fileName ] ;
cachedPromise = cachedSavePromises [ fileName ] || cachedDownloadPromises [ fileName ] ;
if ( cachedPromise ) {
if ( ! toFileEntry && cachedPromise ) {
return cachedPromise ;
return cachedPromise ;
}
}
@ -3049,7 +3067,11 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
if ( isFinal ) {
if ( isFinal ) {
// console.timeEnd(fileName + ' ' + (size / 1024));
// console.timeEnd(fileName + ' ' + (size / 1024));
resolved = true ;
resolved = true ;
deferred . resolve ( cachedDownloads [ fileName ] = fileEntry . toURL ( options . mime || 'image/jpeg' ) ) ;
if ( toFileEntry ) {
deferred . resolve ( ) ;
} else {
deferred . resolve ( cachedDownloads [ fileName ] = fileEntry . toURL ( options . mime || 'image/jpeg' ) ) ;
}
} else {
} else {
// console.log('notify', {done: offset + limit, total: size});
// console.log('notify', {done: offset + limit, total: size});
deferred . notify ( { done : offset + limit , total : size } ) ;
deferred . notify ( { done : offset + limit , total : size } ) ;
@ -3070,90 +3092,104 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
} ;
} ;
if ( fileEntry ) {
requestFS ( ) . then ( function ( ) {
saveToFileEntry ( fileEntry ) ;
cachedFs . root . getFile ( fileName , { create : false } , function ( fileEntry ) {
} else {
fileEntry . file ( function ( file ) {
requestFS ( ) . then ( function ( ) {
// console.log(dT(), 'Check size', file.size, size);
cachedFs . root . getFile ( fileName , { create : false } , function ( fileEntry ) {
if ( file . size >= size /* && false*/ ) {
fileEntry . file ( function ( file ) {
resolved = true ;
// console.log(dT(), 'Check size', file.size, size);
if ( toFileEntry ) {
if ( file . size >= size /* && false*/ ) {
fileCopyTo ( file , toFileEntry ) . then ( function ( ) {
resolved = true ;
deferred . resolve ( ) ;
} )
} else {
deferred . resolve ( cachedDownloads [ fileName ] = fileEntry . toURL ( ) ) ;
deferred . resolve ( cachedDownloads [ fileName ] = fileEntry . toURL ( ) ) ;
}
} else {
// setTimeout(function () {
console . log ( 'File bad size' , file , size ) ;
if ( toFileEntry ) {
saveToFileEntry ( toFileEntry ) ;
} else {
} else {
// setTimeout(function () {
console . log ( 'File bad size' , file , size ) ;
cachedFs . root . getFile ( fileName , { create : true } , saveToFileEntry , errorHandler )
cachedFs . root . getFile ( fileName , { create : true } , saveToFileEntry , errorHandler )
// }, 10000);
}
}
} , errorHandler ) ;
// }, 10000);
} , function ( ) {
}
cachedFs . root . getFile ( fileName , { create : true } , saveToFileEntry , errorHandler )
} , errorHandler ) ;
} ) ;
} , function ( ) {
} , function ( ) {
if ( toFileEntry ) {
saveToFileEntry ( toFileEntry ) ;
} else {
cachedFs . root . getFile ( fileName , { create : true } , saveToFileEntry , errorHandler )
}
} ) ;
} , function ( ) {
var blobParts = [ ] ;
if ( toFileEntry ) {
var limit = size > 30400 ? 524288 : 4096 ;
return saveToFileEntry ( toFileEntry ) ;
var writeBlobPromise = $q . when ( ) ,
}
writeBlobDeferred ;
for ( var offset = 0 ; offset < size ; offset += limit ) {
var blobParts = [ ] ;
writeBlobDeferred = $q . defer ( ) ;
var limit = size > 30400 ? 524288 : 4096 ;
( function ( isFinal , offset , writeBlobDeferred , writeBlobPromise ) {
var writeBlobPromise = $q . when ( ) ,
return downloadRequest ( dcID , function ( ) {
writeBlobDeferred ;
for ( var offset = 0 ; offset < size ; offset += limit ) {
writeBlobDeferred = $q . defer ( ) ;
( function ( isFinal , offset , writeBlobDeferred , writeBlobPromise ) {
return downloadRequest ( dcID , function ( ) {
if ( canceled ) {
return $q . when ( ) ;
}
return MtpApiManager . invokeApi ( 'upload.getFile' , {
location : location ,
offset : offset ,
limit : limit
} , {
dcID : dcID ,
fileDownload : true ,
createNetworker : true
} ) ;
} , 6 ) . then ( function ( result ) {
writeBlobPromise . then ( function ( ) {
if ( canceled ) {
if ( canceled ) {
return $q . when ( ) ;
return $q . when ( ) ;
}
}
return MtpApiManager . invokeApi ( 'upload.getFile' , {
try {
location : location ,
blobParts . push ( bytesToArrayBuffer ( result . bytes ) ) ;
offset : offset ,
writeBlobDeferred . resolve ( ) ;
limit : limit
} , {
if ( isFinal ) {
dcID : dcID ,
try {
fileDownload : true ,
var blob = new Blob ( blobParts , { type : options . mime || 'image/jpeg' } ) ;
createNetworker : true
} catch ( e ) {
} ) ;
window . BlobBuilder = window . BlobBuilder || window . WebKitBlobBuilder || window . MozBlobBuilder ;
} , 6 ) . then ( function ( result ) {
var bb = new BlobBuilder ;
writeBlobPromise . then ( function ( ) {
angular . forEach ( blobParts , function ( blobPart ) {
if ( canceled ) {
bb . append ( blobPart ) ;
return $q . when ( ) ;
} ) ;
}
var blob = bb . getBlob ( options . mime || 'image/jpeg' ) ;
try {
}
blobParts . push ( bytesToArrayBuffer ( result . bytes ) ) ;
writeBlobDeferred . resolve ( ) ;
if ( isFinal ) {
try {
var blob = new Blob ( blobParts , { type : options . mime || 'image/jpeg' } ) ;
} catch ( e ) {
window . BlobBuilder = window . BlobBuilder || window . WebKitBlobBuilder || window . MozBlobBuilder ;
var bb = new BlobBuilder ;
angular . forEach ( blobParts , function ( blobPart ) {
bb . append ( blobPart ) ;
} ) ;
var blob = bb . getBlob ( options . mime || 'image/jpeg' ) ;
}
window . URL = window . URL || window . webkitURL ;
window . URL = window . URL || window . webkitURL ;
resolved = true ;
resolved = true ;
deferred . resolve ( cachedDownloads [ fileName ] = URL . createObjectURL ( blob ) ) ;
deferred . resolve ( cachedDownloads [ fileName ] = URL . createObjectURL ( blob ) ) ;
} else {
} else {
deferred . notify ( { done : offset + limit , total : size } ) ;
deferred . notify ( { done : offset + limit , total : size } ) ;
} ;
} ;
} catch ( e ) {
} catch ( e ) {
errorHandler ( e ) ;
errorHandler ( e ) ;
}
}
} , errorHandler ) ;
} , errorHandler ) ;
} ) ;
} ) ;
} ) ( offset + limit >= size , offset , writeBlobDeferred , writeBlobPromise ) ;
} ) ( offset + limit >= size , offset , writeBlobDeferred , writeBlobPromise ) ;
writeBlobPromise = writeBlobDeferred . promise ;
writeBlobPromise = writeBlobDeferred . promise ;
}
}
} ) ;
} ) ;
}
deferred . promise . cancel = function ( ) {
deferred . promise . cancel = function ( ) {
if ( ! canceled && ! resolved ) {
if ( ! canceled && ! resolved ) {
@ -3163,37 +3199,12 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
}
}
}
}
return cachedDownloadPromises [ fileName ] = deferred . promise ;
if ( ! toFileEntry ) {
}
cachedDownloadPromises [ fileName ] = deferred . promise ;
}
function writeFile ( file ) {
console . log ( dT ( ) , 'Write file' , file ) ;
var fileName = getTempFileName ( file ) ;
var deferred = $q . defer ( ) ,
cacheFileWriter ,
errorHandler = function ( error ) {
console . log ( 'fail' ) ;
deferred . reject ( error ) ;
if ( cacheFileWriter ) cacheFileWriter . truncate ( 0 ) ;
errorHandler = angular . noop ;
} ;
requestFS ( ) . then ( function ( ) {
return deferred . promise ;
cachedFs . root . getFile ( fileName , { create : false } , function ( fileEntry ) {
}
deferred . resolve ( fileEntry ) ;
} , function ( ) {
cachedFs . root . getFile ( fileName , { create : true } , function ( fileEntry ) {
fileEntry . createWriter ( function ( fileWriter ) {
cacheFileWriter = fileWriter ;
fileWriteBytes ( fileWriter , file ) . then ( function ( ) {
deferred . resolve ( fileEntry ) ;
} , errorHandler ) ;
} , errorHandler ) ;
} ) ;
} ) ;
} ) ;
} ;
function uploadFile ( file ) {
function uploadFile ( file ) {
var fileSize = file . size ,
var fileSize = file . size ,