|
|
@ -248,12 +248,12 @@ export class ApiFileManager { |
|
|
|
return fileStorage.getFile(fileName, size); |
|
|
|
return fileStorage.getFile(fileName, size); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public async downloadFile(dcID: number, location: any, size: number, options: { |
|
|
|
public downloadFile(dcID: number, location: any, size: number, options: { |
|
|
|
mimeType?: string, |
|
|
|
mimeType?: string, |
|
|
|
dcID?: number, |
|
|
|
dcID?: number, |
|
|
|
toFileEntry?: any, |
|
|
|
toFileEntry?: any, |
|
|
|
limitPart?: number |
|
|
|
limitPart?: number |
|
|
|
} = {}): Promise<Blob> { |
|
|
|
} = {}): CancellablePromise<Blob> { |
|
|
|
if(!FileManager.isAvailable()) { |
|
|
|
if(!FileManager.isAvailable()) { |
|
|
|
return Promise.reject({type: 'BROWSER_BLOB_NOT_SUPPORTED'}); |
|
|
|
return Promise.reject({type: 'BROWSER_BLOB_NOT_SUPPORTED'}); |
|
|
|
} |
|
|
|
} |
|
|
@ -288,19 +288,33 @@ export class ApiFileManager { |
|
|
|
//this.log('downloadFile cachedPromise');
|
|
|
|
//this.log('downloadFile cachedPromise');
|
|
|
|
|
|
|
|
|
|
|
|
if(size) { |
|
|
|
if(size) { |
|
|
|
let blob = await cachedPromise; |
|
|
|
/* let blob = await cachedPromise; |
|
|
|
if(blob.size < size) { |
|
|
|
if(blob.size < size) { |
|
|
|
this.log('downloadFile need to deleteFile, wrong size:', blob.size, size); |
|
|
|
this.log('downloadFile need to deleteFile, wrong size:', blob.size, size); |
|
|
|
await this.deleteFile(location); |
|
|
|
await this.deleteFile(location); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return cachedPromise; |
|
|
|
return cachedPromise; |
|
|
|
|
|
|
|
} */ |
|
|
|
|
|
|
|
return cachedPromise.then((blob: Blob) => { |
|
|
|
|
|
|
|
if(blob.size < size) { |
|
|
|
|
|
|
|
this.log('downloadFile need to deleteFile, wrong size:', blob.size, size); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return this.deleteFile(location).then(() => { |
|
|
|
|
|
|
|
return this.downloadFile(dcID, location, size, options); |
|
|
|
|
|
|
|
}).catch(() => { |
|
|
|
|
|
|
|
return this.downloadFile(dcID, location, size, options); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
//return cachedPromise;
|
|
|
|
|
|
|
|
return blob; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return cachedPromise; |
|
|
|
return cachedPromise; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//this.log('arriba');
|
|
|
|
this.log('arriba'); |
|
|
|
|
|
|
|
|
|
|
|
//var deferred = $q.defer()
|
|
|
|
//var deferred = $q.defer()
|
|
|
|
let deferredHelper: any = {notify: () => {}}; |
|
|
|
let deferredHelper: any = {notify: () => {}}; |
|
|
@ -310,16 +324,17 @@ export class ApiFileManager { |
|
|
|
}); |
|
|
|
}); |
|
|
|
Object.assign(deferred, deferredHelper); |
|
|
|
Object.assign(deferred, deferredHelper); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//return;
|
|
|
|
|
|
|
|
|
|
|
|
var canceled = false; |
|
|
|
var canceled = false; |
|
|
|
var resolved = false; |
|
|
|
var resolved = false; |
|
|
|
var mimeType = options.mimeType || 'image/jpeg', |
|
|
|
var mimeType = options.mimeType || 'image/jpeg', |
|
|
|
cacheFileWriter: any; |
|
|
|
cacheFileWriter: any; |
|
|
|
var errorHandler = (error: any) => { |
|
|
|
var errorHandler = (error: any) => { |
|
|
|
deferred.reject(error) |
|
|
|
deferred.reject(error); |
|
|
|
errorHandler = () => {}; |
|
|
|
errorHandler = () => {}; |
|
|
|
|
|
|
|
|
|
|
|
if(cacheFileWriter && |
|
|
|
if(cacheFileWriter && (!error || error.type != 'DOWNLOAD_CANCELED')) { |
|
|
|
(!error || error.type != 'DOWNLOAD_CANCELED')) { |
|
|
|
|
|
|
|
cacheFileWriter.truncate(0); |
|
|
|
cacheFileWriter.truncate(0); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
@ -336,14 +351,15 @@ export class ApiFileManager { |
|
|
|
if(toFileEntry) { |
|
|
|
if(toFileEntry) { |
|
|
|
FileManager.copy(blob, toFileEntry).then(() => { |
|
|
|
FileManager.copy(blob, toFileEntry).then(() => { |
|
|
|
deferred.resolve(); |
|
|
|
deferred.resolve(); |
|
|
|
}, errorHandler) |
|
|
|
}, errorHandler); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
deferred.resolve(this.cachedDownloads[fileName] = blob); |
|
|
|
deferred.resolve(this.cachedDownloads[fileName] = blob); |
|
|
|
} |
|
|
|
} |
|
|
|
//}, () => {
|
|
|
|
//}, () => {
|
|
|
|
}).catch(() => { |
|
|
|
}).catch(() => { |
|
|
|
//this.log('not i wanted');
|
|
|
|
//this.log('not i wanted');
|
|
|
|
var fileWriterPromise = toFileEntry ? FileManager.getFileWriter(toFileEntry) : fileStorage.getFileWriter(fileName, mimeType); |
|
|
|
//var fileWriterPromise = toFileEntry ? FileManager.getFileWriter(toFileEntry) : fileStorage.getFileWriter(fileName, mimeType);
|
|
|
|
|
|
|
|
var fileWriterPromise = toFileEntry ? Promise.resolve(toFileEntry) : fileStorage.getFileWriter(fileName, mimeType); |
|
|
|
|
|
|
|
|
|
|
|
var processDownloaded = (bytes: any) => { |
|
|
|
var processDownloaded = (bytes: any) => { |
|
|
|
return Promise.resolve(bytes); |
|
|
|
return Promise.resolve(bytes); |
|
|
@ -414,7 +430,7 @@ export class ApiFileManager { |
|
|
|
return Promise.resolve(); |
|
|
|
return Promise.resolve(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return processDownloaded(result.bytes).then((processedResult) => { |
|
|
|
return processDownloaded(result.bytes).then((processedResult: Uint8Array) => { |
|
|
|
return FileManager.write(fileWriter, processedResult).then(() => { |
|
|
|
return FileManager.write(fileWriter, processedResult).then(() => { |
|
|
|
writeFileDeferred.resolve(); |
|
|
|
writeFileDeferred.resolve(); |
|
|
|
}, errorHandler).then(() => { |
|
|
|
}, errorHandler).then(() => { |
|
|
@ -427,7 +443,7 @@ export class ApiFileManager { |
|
|
|
deferred.resolve(this.cachedDownloads[fileName] = fileWriter.finalize()); |
|
|
|
deferred.resolve(this.cachedDownloads[fileName] = fileWriter.finalize()); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
this.log('deferred notify 2:', {done: offset + limit, total: size}); |
|
|
|
this.log('deferred notify 2:', {done: offset + limit, total: size}, deferred); |
|
|
|
deferred.notify({done: offset + limit, total: size}); |
|
|
|
deferred.notify({done: offset + limit, total: size}); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
@ -449,6 +465,8 @@ export class ApiFileManager { |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(deferred, deferred.notify, deferred.cancel); |
|
|
|
|
|
|
|
|
|
|
|
if(!toFileEntry) { |
|
|
|
if(!toFileEntry) { |
|
|
|
this.cachedDownloadPromises[fileName] = deferred; |
|
|
|
this.cachedDownloadPromises[fileName] = deferred; |
|
|
|
} |
|
|
|
} |
|
|
|