-
Notifications
You must be signed in to change notification settings - Fork 1
/
PSPDFKitFileStore.js
133 lines (121 loc) · 3.46 KB
/
PSPDFKitFileStore.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
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
/**
* PSPDFKitFileStore is a general purpose IndexedDB storage that can be used for
* storing your PDFs offline.
*
* In the PWA example, we use it to allow documents to be downloaded and be
* available even if you restart the application.
*/
(function() {
window.indexedDB =
window.indexedDB ||
window.webkitIndexedDB ||
window.mozIndexedDB ||
window.OIndexedDB ||
window.msIndexedDB;
var NOT_SUPPORTED_ERROR_MESSAGE =
"PSPDFKit File Store works only in browsers that support IndexedDB";
function notSupported() {
return Promise.reject(new Error(NOT_SUPPORTED_ERROR_MESSAGE));
}
if (!window.indexedDB) {
console.warn(NOT_SUPPORTED_ERROR_MESSAGE);
window.PSPDFKitFileStore = {
get: notSupported,
set: notSupported,
delete: notSupported,
clear: notSupported,
listAll: notSupported
};
return;
}
var STORE_NAME = "PSPDFKIT_FILES_STORE";
var STORE_VERSION = 1;
var dbPromise = idb.open(STORE_NAME, STORE_VERSION, function(upgradeDB) {
upgradeDB.createObjectStore(STORE_NAME);
});
window.PSPDFKitFileStore = {
/**
* Retrieve the contents of a file stored in the PSPDFKitFileStore.
*
* @param {string} filename
*/
get: function(filename) {
return dbPromise.then(function(db) {
return db
.transaction(STORE_NAME)
.objectStore(STORE_NAME)
.get(filename);
});
},
/**
* Persists a file in the PSPDFKitFileStore. A file is referenced by its
* filename.
*
* @param {string} filename
* @param {ArrayBuffer} pdfBuffer
* @param {ArrayBuffer} thumbnailData
*/
set: function(filename, pdfBuffer, thumbnailData) {
return dbPromise.then(function(db) {
var tx = db.transaction(STORE_NAME, "readwrite");
tx.objectStore(STORE_NAME).put(
{
pdfBuffer: pdfBuffer,
thumbnailData: thumbnailData
},
filename
);
return tx.complete;
});
},
/**
* Deletes a file in the PSPDFKitFileStore.
*
* @param {string} filename
*/
delete: function(filename) {
return dbPromise.then(function(db) {
var tx = db.transaction(STORE_NAME, "readwrite");
tx.objectStore(STORE_NAME).delete(filename);
return tx.complete;
});
},
/**
* Resets the PSPDFKitFileStore. After that, every file will be deleted.
*
* @returns {Promise}
*/
clear: function() {
return dbPromise.then(function(db) {
var tx = db.transaction(STORE_NAME, "readwrite");
tx.objectStore(STORE_NAME).clear();
return tx.complete;
});
},
/**
* Returns a promise that resolves to all files that are currently stored
* in the PSPDFKitFileStore.
*
* @returns {Array<string>}
*/
listAll: function() {
return dbPromise.then(function(db) {
var tx = db.transaction(STORE_NAME);
var files = [];
var store = tx.objectStore(STORE_NAME);
// This could use `getAllKeys()`, but isn't supported in some browsers.
// `iterateKeyCursor` is also not supported by Safari.
(store.iterateKeyCursor || store.iterateCursor).call(store, function(
cursor
) {
if (!cursor) return;
files.push(cursor.key);
cursor.continue();
});
return tx.complete.then(function() {
return files;
});
});
}
};
})();