mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2025-02-22 01:19:36 +08:00
feat(file): adds chunked blob writing (#529)
This prevents devices crashing when user picks a big file to write
This commit is contained in:
parent
26dead93ff
commit
bbbd0d52e9
@ -1119,6 +1119,10 @@ export class File {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
private static write(writer: FileWriter, gu: string | Blob): Promise<void> {
|
private static write(writer: FileWriter, gu: string | Blob): Promise<void> {
|
||||||
|
if (gu instanceof Blob) {
|
||||||
|
return this.writeFileInChunks(writer, gu);
|
||||||
|
}
|
||||||
|
|
||||||
return new Promise<void>((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
writer.onwriteend = (evt) => {
|
writer.onwriteend = (evt) => {
|
||||||
if (writer.error) {
|
if (writer.error) {
|
||||||
@ -1130,4 +1134,32 @@ export class File {
|
|||||||
writer.write(gu);
|
writer.write(gu);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
private static writeFileInChunks(writer: FileWriter, file: Blob) {
|
||||||
|
const BLOCK_SIZE = 1024 * 1024;
|
||||||
|
let writtenSize = 0;
|
||||||
|
|
||||||
|
function writeNextChunk() {
|
||||||
|
const size = Math.min(BLOCK_SIZE, file.size - writtenSize);
|
||||||
|
const chunk = file.slice(writtenSize, writtenSize + size);
|
||||||
|
|
||||||
|
writtenSize += size;
|
||||||
|
writer.write(chunk);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
writer.onerror = reject;
|
||||||
|
writer.onwrite = () => {
|
||||||
|
if (writtenSize < file.size) {
|
||||||
|
writeNextChunk();
|
||||||
|
} else {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
writeNextChunk();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user