-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathstorage.js
More file actions
377 lines (360 loc) · 12.7 KB
/
storage.js
File metadata and controls
377 lines (360 loc) · 12.7 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
///////////////////////////////////////////////////////////////////////////
// STORAGE AND CACHE SERVICE
///////////////////////////////////////////////////////////////////////////
define(['jquery', 'see', 'filetype'],
function($, see, filetype) {
var sw = null;
function installServiceWorker() {
navigator.serviceWorker.register('/worker.js').then(function(reg) {
console.log('happy', reg);
}, function(err) {
console.log('sad', err);
});
}
installServiceWorker();
eval(see.scope('storage'));
function hasBackup(filename) {
try {
if (!window.localStorage) return false;
return ('backup:' + filename) in window.localStorage;
} catch (e) {
return false;
}
}
function isOnline() {
// PhantomJS (headless testing) gets this wrong.
// https://github.com/ariya/phantomjs/issues/10647
return window.navigator.onLine ||
null != window.navigator.userAgent.match(/PhantomJS/);
}
function loadBackup(filename, annotation) {
try {
var result = JSON.parse(window.localStorage['backup:' + filename]);
if (annotation) {
$.extend(result, annotation);
}
return result;
} catch(e) {
return { error: 'Backup load failed.', down: true };
}
}
function saveBackup(filename, msg) {
try {
if (!window.localStorage) return;
if (msg == null) { msg = null; }
window.localStorage['backup:' + filename] = JSON.stringify(msg);
} catch(e) { }
}
function deleteBackup(filename) {
try {
if (!window.localStorage) return;
delete window.localStorage['backup:' + filename];
} catch(e) { }
}
function deleteBackupPrefix(filename) {
try {
if (!window.localStorage) return;
var prefix = 'backup:' + filename;
delete window.localStorage[prefix];
if (filename.length > 0 &&
filename.substring(filename.length - 1) != '/') { prefix += '/'; }
var toDelete = [];
for (var j = 0; j < localStorage.length; j++) {
if (localStorage.key(j).indexOf(prefix) === 0) {
toDelete.push(localStorage.key(j));
}
}
for (var j = 0; j < toDelete.length; j++) {
delete window.localStorage[toDelete[j]];
}
} catch(e) {
}
}
function isBackupPreferred(filename, m, preferUnsaved) {
try {
if (!window.localStorage) return false;
var backup = window.localStorage.getItem('backup:' + filename);
if (!backup) return false;
backup = JSON.parse(backup);
// If backup is empty, then don't prefer the backup.
if (!backup.data || /^\s*$/.test(backup.data)) {
return false;
}
// If backup is identical to net file (ignoring extra blank lines),
// then don't prefer the backup.
if (backup.data.replace(/\s*($|\n)/g, '$1') ===
m.data.replace(/\s*($|\n)/g, '$1')) {
return false;
}
// If we have an unsaved backup, show it.
if (preferUnsaved && backup.unsaved) { return true; }
var btime = backup.btime || backup.btime;
if (btime && m.mtime && btime > m.mtime) {
// If the backup is newer than original file and still
// less than 12 hours old then prefer it; otherwise discard it.
if ((new Date - btime) / 1000 / 60 / 60 < 12) {
return true;
}
}
} catch(e) { console.log(e); }
return false;
}
// When there is a problem posting cross-domain, display a link
// to the domain so that users can bring up firewall UI for an expln.
function networkErrorMessage(domain) {
if (domain != window.location.hostname) {
return 'Test your connection to <a href="//' + domain +
'/" target="_blank">' + domain + '</a>.';
} else {
return 'Network error.';
}
}
window.pencilcode.storage = {
loadUserList: function(cb) {
$.getJSON('//' + window.pencilcode.domain + '/load/', function(m) {
if (m && m.directory && m.list) {
var result = [];
for (var j = 0; j < m.list.length; ++j) {
var reserved = (m.list[j].mode.indexOf('d') < 0);
result.push({ name: m.list[j].name, reserved: reserved});
}
cb(result);
return;
}
cb(null);
});
},
// Given a filename (no owner, leading, or trailing slash),
// attempts to load the file (or directory) and then calls callback
// with the message. Also automatically caches things in the backup store.
loadFile: function(ownername, filename, ignoreBackup, callback) {
if (!ownername && filename.indexOf('/') >= 0) {
setTimeout(function() {
callback({error: "Cannot load."});
}, 0);
return;
}
var preloaded = null;
if (window.pencilcode.preloaded) {
var data = window.pencilcode.preloaded,
expect = (ownername ? '/' + ownername : '') + '/' + filename;
window.pencilcode.preloaded = null;
if (data.directory == expect || data.file == expect) {
console.log('Preloaded data', expect);
preloaded = data;
}
}
if (ownername == 'frame') {
setTimeout(function() {
callback({data: null, newfile: true});
}, 0);
return;
}
if (!ignoreBackup && !isOnline() && hasBackup(filename)) {
// If the user is offline, then the cached backup is returned
// marked with {offline:true}.
setTimeout(function() {
callback(loadBackup(filename, {offline:true}));
}, 0);
return;
}
if (preloaded) {
setTimeout(function() { handleNetworkLoad(preloaded); }, 0);
} else {
$.getJSON((ownername ? '//' + ownername + '.' +
window.pencilcode.domain : '') +
'/load/' + filename, handleNetworkLoad).error(handleNetworkError);
}
function handleNetworkLoad(m) {
// If there is no owner, we are not allowed to load directories
if (!ownername && filename && m.directory) {
callback({error: "Cannot load."});
}
if (m.error) {
if (!ignoreBackup & hasBackup(filename)) {
// If something failed in the load, fall back to the cached
// backup {offline:true}.
callback(loadBackup(filename, {offline:true}));
}
// If there was a failure without a cached backup, return the
// error without caching it.
callback(m);
return;
}
if (!ignoreBackup && isBackupPreferred(filename, m)) {
// If the backup is preferred (newer or unsaved), then return it
// and mark it as a backup.
callback(loadBackup(filename, {backup:true,offline:false}));
} else {
// Otherwise, return the network loaded file. Note that we only
// back up this loaded file if ignoreBackup is false.
if (!ignoreBackup) {
saveBackup(filename, m);
}
callback(m);
}
}
function handleNetworkError() {
if (!ignoreBackup & hasBackup(filename)) {
// If something failed in the load, fall back to the cached
// backup {offline:true}.
callback(loadBackup(filename, {offline:true}));
} else {
// Unless there is no cached backup; then report a generic network
// down error message.
callback({error:"Network down.", down:true});
}
}
},
// Given the filename (no owner, leading, or trailing slash),
// attempts to save the file and then calls callback with the success code.
// 2. Otherwise we're online and the network save is attempted, conditional
// on not overwriting a file newer than the given overwriteMtime (if set).
// 3. If the network save failed with newer:newtime or needauth:key,
// or some other reason, a backup is made with unsaved:true set, and
// the error message is returned.
// 4. If the network save succeeded, the file is saved as a backup (without
// unsaved set) and success:true, mtime:mtime is the status.
saveFile: function(ownername,
filename, data, force, key, backupOnly, callback) {
// Always stick the data in the backup immediately, marked as unsaved.
var msg = $.extend({}, data), now = +(new Date);
if (!/pencil/.test(filetype.mimeForFilename(filename)) ||
filetype.isDefaultMeta(msg.meta)) {
// Only 'pencil' type documents should save meta.
msg.meta = null;
}
msg.unsaved = true;
// When backupOnly is requested, it's not a network-save request.
if (!backupOnly) {
// The needsave flag will indicate that the user wanted to do a
// full save.
msg.needsave = true;
} else {
// When making a pure backup, stamp with the current backup time.
msg.btime = now;
}
delete msg.offline;
if (!msg.data || msg.data == '' || msg.data == '\n') {
// If saving an empty file, delete the backup.
deleteBackup(filename);
} else {
saveBackup(filename, msg);
}
if (backupOnly || !isOnline()) {
// If the user is offline or the backupOnly flag is set, then we're done.
// The return status is backup:true if backupOnly and offline:true
// otherwise.
setTimeout(function() { callback && callback(
backupOnly ? {backup:true} : {offline:true}); }, 0);
return;
}
// Attempt the network save: pack up any metadata, and set up
// the conditional argument and the weak authentication key.
var payload = { data: msg.data, meta: JSON.stringify(msg.meta) };
if (msg.mtime && !force) {
payload.conditional = msg.mtime;
}
if (key) {
payload.key = key;
}
var payloadsize = payload.data.length + payload.meta.length;
// Do the network save.
var domain = (ownername ? ownername + '.' : '') + window.pencilcode.domain;
var crossdomain = (window.location.hostname != domain);
$.ajax({
url: (ownername ? '//' + domain : '') +
'/save/' + filename,
data: payload,
dataType: 'json',
// Use a GET if crossdomain and the payload is short. Note that
// a longer payload will fail on IE, and anecdotally a crossdomain
// POST (even with CORS) will fail on mobile browsers.
type: (crossdomain && payloadsize < 1024) ? 'GET' : 'POST',
success: function(m) {
var check;
if (m.error) {
// Pass errors on to calback. Backup already done.
console.log('got error ' + m.error);
} else if (m.deleted) {
// On a successful delete, also delete the backup
// (if nothing new has since been entered in backup).
check = loadBackup(filename);
if (check && check.data === msg.data) {
deleteBackup(filename);
}
} else {
// On a successful save, re-commit the backup with the new
// mtime and without the unsaved bit (if nothing new has since
// been entered in the backup).
check = loadBackup(filename);
if (check && check.data === msg.data) {
// Commit backup without 'unsaved' if it is still unchanged.
delete msg.unsaved;
delete msg.needsave;
delete msg.btime;
delete msg.offline;
if (m.mtime) { msg.mtime = m.mtime; }
saveBackup(filename, msg);
}
}
callback && callback(m);
}
}).error(function() {
console.log('got error ' + domain);
callback && callback({
error: networkErrorMessage(domain),
offline:true
});
});
},
moveFile: function(ownername, sourcefile, filename, key, copy, callback) {
var payload = {
source: ownername + '/' + sourcefile
};
if (!copy) { payload.mode = 'mv'; }
if (key) { payload.key = key; }
$.post('//' + ownername + '.' + window.pencilcode.domain + '/save/' +
filename, payload, function(m) {
var check;
if (m.error) {
// Pass errors on to calback. Don't affect backup.
} else {
// On a successful move, just delete the backup of everything
// in the source area. TODO: move the backup tree instead.
deleteBackupPrefix(sourcefile);
}
callback && callback(m);
}, 'json').error(function() {
callback && callback({
error: networkErrorMessage(ownername + '.' + window.pencilcode.domain),
offline:true
});
});
},
setPassKey: function(ownername, key, oldkey, callback) {
$.post('//' + ownername + '.' + window.pencilcode.domain + '/save/',
$.extend({ mode: 'setkey', data: key}, oldkey ? { key: oldkey } : {}),
function(m) {
callback && callback(m);
}, 'json').error(function() {
console.log('got error here');
callback && callback({
error: networkErrorMessage(ownername + '.' + window.pencilcode.domain),
offline:true
});
});
},
deleteBackup: deleteBackup,
deleteAllBackup: function() {
deleteBackupPrefix('');
},
// TODO: cache-refreshing crawl.
recrawlCache: function() {
// Load dirtree URL
// Then backup all directories
// Then (breadthfirst) compare all file entry mtimes to backup mtimes.
}
};
return window.pencilcode.storage;
});