Index: lib/io.js |
diff --git a/lib/io.js b/lib/io.js |
index 62d5f30ed1941a6d88c1b29afff9a714f938278f..2334ca3e94488e18bc3240f0e918af3b14c1ff42 100644 |
--- a/lib/io.js |
+++ b/lib/io.js |
@@ -23,14 +23,14 @@ function fileToKey(file, chunk) |
{ |
let key = keyPrefix; |
+ if (typeof file == "string") |
+ return file; |
+ |
if (file instanceof FakeFile) |
key += file.path; |
else |
key += file.spec; |
- if (typeof chunk != "undefined") |
- key += ":" + chunk; |
- |
return key; |
} |
@@ -42,6 +42,19 @@ function loadFile(file) |
ext.storage.get([key], items => |
{ |
let entry = items[key]; |
+ if (entry && entry.isSplit) |
+ { |
+ // Load both left and right parts and combine them |
+ return Promise.all([loadFile(key + "_l"), loadFile(key + "_r")]). |
+ then(values => |
+ { |
+ entry.content = values[0].content.concat(values[1].content); |
+ resolve(entry); |
+ }).catch(() => |
+ { |
+ reject(new Error("File doesn't exist")); |
+ }); |
+ } |
if (entry) |
resolve(entry); |
else |
@@ -65,26 +78,8 @@ exports.IO = |
{ |
for (let line of entry.content) |
listener.process(line); |
- |
- listener.process(null); |
- callback(null); |
- } |
- else |
- { |
- let keys = []; |
- for (let i = 0; i < entry.chunks; i++) |
- keys.push(fileToKey(file, i)); |
- |
- ext.storage.get(keys, items => |
- { |
- for (let key of keys) |
- for (let line of items[key]) |
- listener.process(line); |
- |
- listener.process(null); |
- callback(null); |
- }); |
} |
+ callback(null); |
} |
loadFile(file).then(onLoaded, callback); |
@@ -92,47 +87,24 @@ exports.IO = |
writeToFile: function(file, data, callback) |
{ |
- let items = {}; |
- let entry = items[fileToKey(file)] = {lastModified: Date.now()}; |
- |
- if (typeof browser == "object") |
+ let entry = {}; |
+ var key = fileToKey(file); |
+ entry[key] = {lastModified: Date.now(), content: data}; |
+ try |
{ |
- loadFile(file).catch(() => null) |
- .then(oldEntry => |
- { |
- let quota = 1024 * 1024 / 2 - 1000; |
- let chunks = []; |
- let chunk = []; |
- let chunkSize = 0; |
- |
- for (let line of data) |
- { |
- if (line.length + chunkSize > quota) |
- { |
- chunks.push(chunk); |
- chunk = []; |
- chunkSize = 0; |
- } |
- |
- chunk.push(line); |
- chunkSize += line.length; |
- } |
- chunks.push(chunk); |
- |
- for (let i = 0; i < chunks.length; i++) |
- items[fileToKey(file, i)] = chunks[i]; |
- entry.chunks = chunks.length; |
- ext.storage.set(items, callback); |
- |
- if (oldEntry && "chunks" in oldEntry) |
- for (let i = entry.chunks; i < oldEntry.chunks; i++) |
- ext.storage.remove(fileToKey(file, i)); |
- }); |
+ ext.storage.set(entry, callback); |
} |
- else |
+ catch(e) |
{ |
- entry.content = data; |
- ext.storage.set(items, callback); |
+ // Edge throws an exception when trying to write files larger than 1Mb. |
+ // Split the file in halfs and write them. |
+ var leftPart = data.slice(0, data.length / 2); |
+ var rightPart = data.slice(data.length / 2, data.length); |
+ exports.IO.writeToFile(key + "_l", leftPart); |
+ exports.IO.writeToFile(key + "_r", rightPart); |
+ entry[key].content = null; |
+ entry[key].isSplit = true; |
+ ext.storage.set(entry, callback); |
} |
}, |