mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Detect support for storing blobs in IndexedDB
This commit is contained in:
parent
306474f284
commit
5abe27f423
@ -15,9 +15,12 @@ DB_NAME = 'calibre-books-db-test' # TODO: Remove test suffix
|
|||||||
|
|
||||||
class DB:
|
class DB:
|
||||||
|
|
||||||
def __init__(self, idb, interface_data):
|
def __init__(self, idb, ui, supports_blobs):
|
||||||
self.interface_data = interface_data
|
self.interface_data = ui.interface_data
|
||||||
self.idb = idb
|
self.idb = idb
|
||||||
|
self.supports_blobs = supports_blobs
|
||||||
|
if not supports_blobs:
|
||||||
|
print('IndexedDB does not support Blob storage, using base64 encoding instead')
|
||||||
|
|
||||||
idb.onerror = def(event):
|
idb.onerror = def(event):
|
||||||
self.display_error(None, event)
|
self.display_error(None, event)
|
||||||
@ -80,6 +83,8 @@ class DB:
|
|||||||
book.manifest = manifest
|
book.manifest = manifest
|
||||||
book.metadata = manifest.metadata
|
book.metadata = manifest.metadata
|
||||||
book.book_hash = manifest.book_hash.hash
|
book.book_hash = manifest.book_hash.hash
|
||||||
|
book.is_complete = False
|
||||||
|
book.stores_blobs = True
|
||||||
v'delete manifest["metadata"]'
|
v'delete manifest["metadata"]'
|
||||||
self.do_op(['books'], book, _('Failed to write to the books database'), proceed, op='put')
|
self.do_op(['books'], book, _('Failed to write to the books database'), proceed, op='put')
|
||||||
|
|
||||||
@ -88,12 +93,25 @@ def create_db(ui, interface_data):
|
|||||||
ui.db = _('Your browser does not support IndexedDB. Cannot read books. Consider using a modern browser, such as Firefox, Chrome or Edge.')
|
ui.db = _('Your browser does not support IndexedDB. Cannot read books. Consider using a modern browser, such as Firefox, Chrome or Edge.')
|
||||||
return
|
return
|
||||||
request = window.indexedDB.open(DB_NAME, 1)
|
request = window.indexedDB.open(DB_NAME, 1)
|
||||||
request.onblocked = def(event):
|
|
||||||
alert(_('Please close all other tabs with a calibre book open'))
|
|
||||||
request.onerror = def(event):
|
|
||||||
ui.db = _('You must allow calibre to use IndexedDB storage in your browser to read books')
|
|
||||||
request.onsuccess = def(event):
|
|
||||||
ui.db = DB(event.target.result, interface_data)
|
|
||||||
ui.db_initialized()
|
|
||||||
request.onupgradeneeded = def(event):
|
request.onupgradeneeded = def(event):
|
||||||
upgrade_schema(event.target.result, event.oldVersion, event.newVersion)
|
upgrade_schema(event.target.result, event.oldVersion, event.newVersion)
|
||||||
|
|
||||||
|
request.onblocked = def(event):
|
||||||
|
alert(_('Please close all other tabs with a calibre book open'))
|
||||||
|
|
||||||
|
request.onerror = def(event):
|
||||||
|
ui.db = _('You must allow calibre to use IndexedDB storage in your browser to read books')
|
||||||
|
|
||||||
|
request.onsuccess = def(event):
|
||||||
|
blob = Blob(['test'], {'type':"text/plain"})
|
||||||
|
idb = event.target.result
|
||||||
|
try:
|
||||||
|
req = idb.transaction(['files'], 'readwrite').objectStore('files').put(blob, ':-test-blob-:')
|
||||||
|
except Exception:
|
||||||
|
ui.db_initialized(DB(idb, ui, False))
|
||||||
|
return
|
||||||
|
req.onsuccess = def(event):
|
||||||
|
ui.db_initialized(DB(idb, ui, True))
|
||||||
|
req.onerror = def(event):
|
||||||
|
ui.db_initialized(DB(idb, ui, False))
|
||||||
|
@ -24,7 +24,8 @@ class ReadUI:
|
|||||||
return
|
return
|
||||||
self.start_load(book_id, fmt, metadata)
|
self.start_load(book_id, fmt, metadata)
|
||||||
|
|
||||||
def db_initialized(self):
|
def db_initialized(self, db):
|
||||||
|
self.db = db
|
||||||
if self.pending_load is not None:
|
if self.pending_load is not None:
|
||||||
pl, self.pending_load = self.pending_load, None
|
pl, self.pending_load = self.pending_load, None
|
||||||
self.start_load(*pl)
|
self.start_load(*pl)
|
||||||
@ -41,7 +42,7 @@ class ReadUI:
|
|||||||
if not book.manifest or book.manifest.version != RENDER_VERSION:
|
if not book.manifest or book.manifest.version != RENDER_VERSION:
|
||||||
self.get_manifest(book)
|
self.get_manifest(book)
|
||||||
return
|
return
|
||||||
self.download_book(book)
|
self.display_book(book) if book.is_complete else self.download_book(book)
|
||||||
|
|
||||||
def get_manifest(self, book):
|
def get_manifest(self, book):
|
||||||
library_id, book_id, fmt = book.key
|
library_id, book_id, fmt = book.key
|
||||||
@ -74,3 +75,6 @@ class ReadUI:
|
|||||||
|
|
||||||
def download_book(self, book):
|
def download_book(self, book):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def display_book(self, book):
|
||||||
|
pass
|
||||||
|
Loading…
x
Reference in New Issue
Block a user