UNPKG

uppy

Version:

Almost as cute as a Puppy :dog:

186 lines (151 loc) 5.34 kB
'use strict'; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var _Promise = typeof Promise === 'undefined' ? require('es6-promise').Promise : Promise; var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.OIndexedDB || window.msIndexedDB; var isSupported = !!indexedDB; var DB_NAME = 'uppy-blobs'; var STORE_NAME = 'files'; // maybe have a thumbnail store in the future var DB_VERSION = 2; function connect(dbName) { var request = indexedDB.open(dbName, DB_VERSION); return new _Promise(function (resolve, reject) { request.onupgradeneeded = function (event) { var db = event.target.result; var store = db.createObjectStore(STORE_NAME, { keyPath: 'id' }); store.createIndex('store', 'store', { unique: false }); store.transaction.oncomplete = function () { resolve(db); }; }; request.onsuccess = function (event) { resolve(event.target.result); }; request.onerror = reject; }); } function waitForRequest(request) { return new _Promise(function (resolve, reject) { request.onsuccess = function (event) { resolve(event.target.result); }; request.onerror = reject; }); } var IndexedDBStore = function () { function IndexedDBStore(core, opts) { _classCallCheck(this, IndexedDBStore); this.opts = _extends({ dbName: DB_NAME, storeName: 'default', maxFileSize: 10 * 1024 * 1024, // 10 MB maxTotalSize: 300 * 1024 * 1024 // 300 MB }, opts); this.name = this.opts.storeName; this.ready = connect(this.opts.dbName); } IndexedDBStore.prototype.key = function key(fileID) { return this.name + '!' + fileID; }; /** * List all file blobs currently in the store. */ IndexedDBStore.prototype.list = function list() { var _this = this; return this.ready.then(function (db) { var transaction = db.transaction([STORE_NAME], 'readonly'); var store = transaction.objectStore(STORE_NAME); var request = store.index('store').getAll(IDBKeyRange.only(_this.name)); return waitForRequest(request); }).then(function (files) { var result = {}; files.forEach(function (file) { result[file.fileID] = file.data; }); return result; }); }; /** * Get one file blob from the store. */ IndexedDBStore.prototype.get = function get(fileID) { var _this2 = this; return this.ready.then(function (db) { var transaction = db.transaction([STORE_NAME], 'readonly'); var request = transaction.objectStore(STORE_NAME).get(_this2.key(fileID)); return waitForRequest(request); }).then(function (result) { return { id: result.data.fileID, data: result.data.data }; }); }; /** * Get the total size of all stored files. * * @private */ IndexedDBStore.prototype.getSize = function getSize() { var _this3 = this; return this.ready.then(function (db) { var transaction = db.transaction([STORE_NAME], 'readonly'); var store = transaction.objectStore(STORE_NAME); var request = store.index('store').openCursor(IDBKeyRange.only(_this3.name)); return new _Promise(function (resolve, reject) { var size = 0; request.onsuccess = function (event) { var cursor = event.target.result; if (cursor) { size += cursor.value.data.size; cursor.continue(); } else { resolve(size); } }; request.onerror = function () { reject(new Error('Could not retrieve stored blobs size')); }; }); }); }; /** * Save a file in the store. */ IndexedDBStore.prototype.put = function put(file) { var _this4 = this; if (file.data.size > this.opts.maxFileSize) { return Promise.reject(new Error('File is too big to store.')); } return this.getSize().then(function (size) { if (size > _this4.opts.maxTotalSize) { return Promise.reject(new Error('No space left')); } return _this4.ready; }).then(function (db) { var transaction = db.transaction([STORE_NAME], 'readwrite'); var request = transaction.objectStore(STORE_NAME).add({ id: _this4.key(file.id), fileID: file.id, store: _this4.name, data: file.data }); return waitForRequest(request); }); }; /** * Delete a file blob from the store. */ IndexedDBStore.prototype.delete = function _delete(fileID) { var _this5 = this; return this.ready.then(function (db) { var transaction = db.transaction([STORE_NAME], 'readwrite'); var request = transaction.objectStore(STORE_NAME).delete(_this5.key(fileID)); return waitForRequest(request); }); }; return IndexedDBStore; }(); IndexedDBStore.isSupported = isSupported; module.exports = IndexedDBStore; //# sourceMappingURL=IndexedDBStore.js.map