-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeFileAndroid.js
60 lines (51 loc) · 1.6 KB
/
writeFileAndroid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const isString = require("lodash/isString")
const writeFile = (fileEntry, data, append) => {
return new Promise((resolve, reject) => {
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function (e) {
resolve()
}
fileWriter.onerror = reject
// If we are appending data to file, go to the end of the file.
if (append) {
try {
fileWriter.seek(fileWriter.length)
} catch (e) {
reject("file doesn't exist!")
}
}
fileWriter.write(data)
}, reject)
})
}
const chunkSize = 1024 * 1024 //1MB est la taille optimale sous cordova
const writeBinaryFileByChunk = async (fileEntry, blob) => {
const blobSize = blob.size
let startIndex = 0
while (startIndex < blobSize) {
const endIndex = Math.min(startIndex + chunkSize, blobSize)
await writeFile(fileEntry, blob.slice(startIndex, endIndex), startIndex > 0)
startIndex = endIndex
}
}
const chunkSubstr = (str, size) => {
const numChunks = Math.ceil(str.length / size)
const chunks = new Array(numChunks)
for (let i = 0, o = 0; i < numChunks; ++i, o += size) {
chunks[i] = str.substr(o, size)
}
return chunks
}
const writeTextFileByChunk = async (fileEntry, txt) => {
const chunks = chunkSubstr(txt, chunkSize)
for (let i = 0; i < chunks.length; i++) {
await writeFile(fileEntry, chunks[i], i > 0)
}
}
module.exports = (fileEntry, data) => {
if (isString(data)) return writeTextFileByChunk(fileEntry, data)
if (data instanceof Uint8Array) {
data = new Blob([data])
}
return writeBinaryFileByChunk(fileEntry, data)
}