|
|
|
@ -1,9 +1,13 @@
@@ -1,9 +1,13 @@
|
|
|
|
|
import {bytesToArrayBuffer, blobSafeMimeType, blobConstruct, bytesToBase64} from './bin_utils'; |
|
|
|
|
|
|
|
|
|
class FileManager { |
|
|
|
|
/* $window.URL = $window.URL || $window.webkitURL |
|
|
|
|
$window.BlobBuilder = $window.BlobBuilder || $window.WebKitBlobBuilder || $window.MozBlobBuilder */ |
|
|
|
|
import 'web-streams-polyfill/ponyfill'; |
|
|
|
|
// @ts-ignore
|
|
|
|
|
import streamSaver from 'streamsaver'; |
|
|
|
|
if(window.location.href.indexOf('localhost') === -1) { |
|
|
|
|
streamSaver.mitm = 'mitm.html'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class FileManager { |
|
|
|
|
public isSafari = 'safari' in window; |
|
|
|
|
public safariVersion = parseFloat(this.isSafari && (navigator.userAgent.match(/Version\/(\d+\.\d+).* Safari/) || [])[1]); |
|
|
|
|
public safariWithDownload = this.isSafari && this.safariVersion >= 11.0; |
|
|
|
@ -23,7 +27,7 @@ class FileManager {
@@ -23,7 +27,7 @@ class FileManager {
|
|
|
|
|
return this.blobSupported; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public copy(fromFileEntry: any, toFileEntry: any) { |
|
|
|
|
/* public copy(fromFileEntry: any, toFileEntry: any) { |
|
|
|
|
return this.getFileWriter(toFileEntry).then((fileWriter) => { |
|
|
|
|
return this.write(fileWriter, fromFileEntry).then(() => { |
|
|
|
|
return fileWriter; |
|
|
|
@ -36,9 +40,24 @@ class FileManager {
@@ -36,9 +40,24 @@ class FileManager {
|
|
|
|
|
return Promise.reject(error); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
} */ |
|
|
|
|
public copy(fromFileEntry: any, toFileEntry: any) { |
|
|
|
|
return this.write(toFileEntry, fromFileEntry).then(() => { |
|
|
|
|
console.log('copy success'); |
|
|
|
|
return toFileEntry; |
|
|
|
|
}, (error: any) => { |
|
|
|
|
console.error('copy error 1:', error); |
|
|
|
|
try { |
|
|
|
|
toFileEntry.truncate(0); |
|
|
|
|
} catch(e) { |
|
|
|
|
console.error('copy error', e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return Promise.reject(error); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public write(fileWriter: any, bytes: any) { |
|
|
|
|
/* public write(fileWriter: any, bytes: any) { |
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
fileWriter.onwriteend = function(e: any) { |
|
|
|
|
resolve(); |
|
|
|
@ -62,54 +81,52 @@ class FileManager {
@@ -62,54 +81,52 @@ class FileManager {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} */ |
|
|
|
|
|
|
|
|
|
public write(fileWriter: any, bytes: any): Promise<void> { |
|
|
|
|
if(bytes.file) { |
|
|
|
|
return bytes.file((file: any) => { |
|
|
|
|
return fileWriter.write(file); |
|
|
|
|
}); |
|
|
|
|
} else if(bytes instanceof Blob) { // is file bytes
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
let fileReader = new FileReader(); |
|
|
|
|
fileReader.onload = function(event) { |
|
|
|
|
let arrayBuffer = event.target.result as ArrayBuffer; |
|
|
|
|
|
|
|
|
|
let arr = new Uint8Array(arrayBuffer); |
|
|
|
|
|
|
|
|
|
fileWriter.write(arr).then(resolve, reject); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
public chooseSaveFile(fileName: string, ext: string, mimeType: string) { |
|
|
|
|
return Promise.reject(); |
|
|
|
|
/* if(!window.chrome || !chrome.fileSystem || !chrome.fileSystem.chooseEntry) { |
|
|
|
|
//return qSync.reject()
|
|
|
|
|
return Promise.reject(); |
|
|
|
|
fileReader.readAsArrayBuffer(bytes); |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
//var blob = blobConstruct([bytesToArrayBuffer(bytes)]);
|
|
|
|
|
//return fileWriter.write(blob);
|
|
|
|
|
return fileWriter.write(bytes); |
|
|
|
|
} |
|
|
|
|
var deferred = $q.defer() |
|
|
|
|
|
|
|
|
|
chrome.fileSystem.chooseEntry({ |
|
|
|
|
type: 'saveFile', |
|
|
|
|
suggestedName: fileName, |
|
|
|
|
accepts: [{ |
|
|
|
|
mimeTypes: [mimeType], |
|
|
|
|
extensions: [ext] |
|
|
|
|
}] |
|
|
|
|
}, function (writableFileEntry) { |
|
|
|
|
deferred.resolve(writableFileEntry) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
return deferred.promise */ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public getFileWriter(fileEntry: any) { |
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
fileEntry.createWriter(resolve, reject); |
|
|
|
|
public chooseSaveFile(fileName: string, ext: string, mimeType: string, size?: number): any { |
|
|
|
|
let fileStream = streamSaver.createWriteStream(fileName, { |
|
|
|
|
size: size, |
|
|
|
|
writableStrategy: undefined, |
|
|
|
|
readableStrategy: undefined |
|
|
|
|
}); |
|
|
|
|
let writer = fileStream.getWriter(); |
|
|
|
|
return writer; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public getFakeFileWriter(mimeType: string, saveFileCallback: any) { |
|
|
|
|
var blobParts: Array<Blob> = []; |
|
|
|
|
var fakeFileWriter: any = { |
|
|
|
|
write: (blob: Blob) => { |
|
|
|
|
var fakeFileWriter = { |
|
|
|
|
write: async(blob: Blob) => { |
|
|
|
|
if(!this.blobSupported) { |
|
|
|
|
if(fakeFileWriter.onerror) { |
|
|
|
|
fakeFileWriter.onerror(new Error('Blob not supported by browser')); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
throw false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
blobParts.push(blob); |
|
|
|
|
setTimeout(() => { |
|
|
|
|
if(fakeFileWriter.onwriteend) { |
|
|
|
|
fakeFileWriter.onwriteend(); |
|
|
|
|
} |
|
|
|
|
}, 0); |
|
|
|
|
}, |
|
|
|
|
truncate: () => { |
|
|
|
|
blobParts = []; |
|
|
|
@ -146,7 +163,7 @@ class FileManager {
@@ -146,7 +163,7 @@ class FileManager {
|
|
|
|
|
var reader = new FileReader(); |
|
|
|
|
reader.onloadend = (e) => { |
|
|
|
|
// @ts-ignore
|
|
|
|
|
resolve(new Uint8Array(e.target.result)); |
|
|
|
|
resolve(new Uint8Array(e.target.result)); |
|
|
|
|
}; |
|
|
|
|
reader.onerror = (e) => { |
|
|
|
|
reject(e); |
|
|
|
@ -206,14 +223,14 @@ class FileManager {
@@ -206,14 +223,14 @@ class FileManager {
|
|
|
|
|
var subdir = 'telegram/'; |
|
|
|
|
switch(mimeType.split('/')[0]) { |
|
|
|
|
case 'video': |
|
|
|
|
storageName = 'videos'; |
|
|
|
|
break; |
|
|
|
|
storageName = 'videos'; |
|
|
|
|
break; |
|
|
|
|
case 'audio': |
|
|
|
|
storageName = 'music'; |
|
|
|
|
break; |
|
|
|
|
storageName = 'music'; |
|
|
|
|
break; |
|
|
|
|
case 'image': |
|
|
|
|
storageName = 'pictures'; |
|
|
|
|
break; |
|
|
|
|
storageName = 'pictures'; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
@ -262,21 +279,22 @@ class FileManager {
@@ -262,21 +279,22 @@ class FileManager {
|
|
|
|
|
clickEvent.initMouseEvent( |
|
|
|
|
'click', true, false, window, 0, 0, 0, 0, 0 |
|
|
|
|
, false, false, false, false, 0, null |
|
|
|
|
) |
|
|
|
|
anchor.dispatchEvent(clickEvent); |
|
|
|
|
} catch (e) { |
|
|
|
|
console.error('Download click error', e); |
|
|
|
|
try { |
|
|
|
|
anchor.click(); |
|
|
|
|
) |
|
|
|
|
anchor.dispatchEvent(clickEvent); |
|
|
|
|
} catch (e) { |
|
|
|
|
window.open(url as string, '_blank'); |
|
|
|
|
console.error('Download click error', e); |
|
|
|
|
try { |
|
|
|
|
anchor.click(); |
|
|
|
|
} catch (e) { |
|
|
|
|
window.open(url as string, '_blank'); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
setTimeout(() => { |
|
|
|
|
anchor.remove(); |
|
|
|
|
}, 100); |
|
|
|
|
}) |
|
|
|
|
setTimeout(() => { |
|
|
|
|
anchor.remove(); |
|
|
|
|
}, 100); |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export default new FileManager(); |
|
|
|
|
export default new FileManager(); |
|
|
|
|
|