|
|
|
@ -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 chooseSaveFile(fileName: string, ext: string, mimeType: string) { |
|
|
|
|
return Promise.reject(); |
|
|
|
|
/* if(!window.chrome || !chrome.fileSystem || !chrome.fileSystem.chooseEntry) { |
|
|
|
|
//return qSync.reject()
|
|
|
|
|
return Promise.reject(); |
|
|
|
|
} |
|
|
|
|
var deferred = $q.defer() |
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
chrome.fileSystem.chooseEntry({ |
|
|
|
|
type: 'saveFile', |
|
|
|
|
suggestedName: fileName, |
|
|
|
|
accepts: [{ |
|
|
|
|
mimeTypes: [mimeType], |
|
|
|
|
extensions: [ext] |
|
|
|
|
}] |
|
|
|
|
}, function (writableFileEntry) { |
|
|
|
|
deferred.resolve(writableFileEntry) |
|
|
|
|
}) |
|
|
|
|
let arr = new Uint8Array(arrayBuffer); |
|
|
|
|
|
|
|
|
|
return deferred.promise */ |
|
|
|
|
fileWriter.write(arr).then(resolve, reject); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
fileReader.readAsArrayBuffer(bytes); |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
//var blob = blobConstruct([bytesToArrayBuffer(bytes)]);
|
|
|
|
|
//return fileWriter.write(blob);
|
|
|
|
|
return fileWriter.write(bytes); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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 = []; |
|
|
|
@ -280,3 +297,4 @@ class FileManager {
@@ -280,3 +297,4 @@ class FileManager {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export default new FileManager(); |
|
|
|
|
|