Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Open big ebooks faster and stop the reader running the phone hot on every open

Barnabas3466b29b7266
diff --git a/lib/services/ebook_cache.dart b/lib/services/ebook_cache.dart
index 5b52de87..eeeec26b 100644
--- a/lib/services/ebook_cache.dart
+++ b/lib/services/ebook_cache.dart
@@ -1,3 +1,4 @@
+import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
@@ -111,6 +112,35 @@ Future<File> ebookCacheFileFor(String itemId, Map<String, dynamic> ebookFile) as
return File('${dir.path}/$itemId${ebookExtFromFile(ebookFile)}');
}
+/// The location index epub.js builds by walking the whole book, kept beside
+/// the cached epub so the walk happens once per file instead of on every
+/// open. Tagged with the epub's byte size so a re-downloaded file rebuilds
+/// it. Named `<item>.<ext>.locations.json`, so deleteCachedEbook sweeps it
+/// with the book and cachedEbookFileFor never mistakes it for one.
+File _locationsFileFor(File epub) => File('${epub.path}.locations.json');
+
+Future<String?> loadCachedLocations(File epub) async {
+ try {
+ final f = _locationsFileFor(epub);
+ if (!f.existsSync()) return null;
+ final decoded = jsonDecode(await f.readAsString());
+ if (decoded is! Map) return null;
+ if (decoded['size'] != await epub.length()) return null;
+ final locations = decoded['locations'];
+ return locations is String && locations.isNotEmpty ? locations : null;
+ } catch (_) {
+ return null;
+ }
+}
+
+Future<void> saveCachedLocations(File epub, String locations) async {
+ try {
+ await _locationsFileFor(epub).writeAsString(
+ jsonEncode({'size': await epub.length(), 'locations': locations}),
+ );
+ } catch (_) {}
+}
+
/// True if the item's ebook is already cached on disk (readable offline).
Future<bool> isEbookCached(String itemId, Map<String, dynamic> ebookFile) async {
final f = await ebookCacheFileFor(itemId, ebookFile);
diff --git a/lib/widgets/ebook_reader_view.dart b/lib/widgets/ebook_reader_view.dart
index 0642de23..849edb73 100644
--- a/lib/widgets/ebook_reader_view.dart
+++ b/lib/widgets/ebook_reader_view.dart
@@ -86,6 +86,7 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
bool _loading = true;
String? _error;
File? _cachedFile;
+ String? _cachedLocations;
// Swiping to the recents/app-switcher un-hides the system bars. The frozen
// safe-area padding (see _buildViewerArea) keeps that from resizing the
// WebView, but if a resize still gets through (some OEMs resize the window
@@ -712,9 +713,15 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
'cached=${await isEbookCached(widget.itemId, widget.ebookFile)} playing=$playing');
final file = await fetchEbookToCache(api, widget.itemId, widget.ebookFile, widget.title);
final len = file.existsSync() ? await file.length() : 0;
- debugPrint('[EbookReader] file ready item=${widget.itemId} bytes=$len path=${file.path}');
+ final locations = await loadCachedLocations(file);
+ debugPrint('[EbookReader] file ready item=${widget.itemId} bytes=$len '
+ 'locations=${locations == null ? 'none' : 'cached'} path=${file.path}');
if (mounted) {
- setState(() { _cachedFile = file; _loading = false; });
+ setState(() {
+ _cachedFile = file;
+ _cachedLocations = locations;
+ _loading = false;
+ });
}
} catch (e) {
debugPrint('[EbookReader] Error: $e');
@@ -3529,6 +3536,12 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
epubSource: EpubSource.fromFile(_cachedFile!),
epubController: _epubController!,
initialCfi: _initialCfi,
+ cachedLocations: _cachedLocations,
+ onLocationsGenerated: (json) {
+ debugPrint('[EbookReader] locations generated item=${widget.itemId} chars=${json.length}');
+ final file = _cachedFile;
+ if (file != null) saveCachedLocations(file, json);
+ },
displaySettings: EpubDisplaySettings(
flow: EpubFlow.paginated,
spread: _spread,
diff --git a/packages/flutter_epub_viewer/lib/assets/webpage/html/epubView.js b/packages/flutter_epub_viewer/lib/assets/webpage/html/epubView.js
index 61b94e9b..cf2b9094 100644
--- a/packages/flutter_epub_viewer/lib/assets/webpage/html/epubView.js
+++ b/packages/flutter_epub_viewer/lib/assets/webpage/html/epubView.js
@@ -5,6 +5,19 @@ var chapters = []
var clearSelectionOnPageChange = true; // Global flag for selection clearing behavior
var selectAnnotationRange = false; // Global flag for programmatically selecting annotation ranges
var initialXPathProcessed = false; // Flag to prevent processing initialXPath multiple times
+// Location index from an earlier open of this file, handed in by the app
+// before loadBook so the page can skip walking the whole book again.
+var cachedLocations = null;
+function setCachedLocations(json) {
+ cachedLocations = json;
+}
+function handBackLocations() {
+ try {
+ window.flutter_inappwebview.callHandler('locationsGenerated', book.locations.save());
+ } catch (e) {
+ console.error('Error handing locations back:', e);
+ }
+}
var xpathDisplayInProgress = false; // Flag to prevent multiple XPath displays
var initialPositionLoading = false; // Flag to track if initial position is being loaded
// Global selection state tracking (needed for blocking navigation when selection is active)
@@ -87,6 +100,12 @@ var bookParts = [];
var lastRenderOpts = null;
var lastTheme = {bg: null, fg: null, css: null, fontSize: null};
var bookPartsLen = 0;
+// A loopback URL the page fetches the book from itself; set by Flutter for
+// file sources right before loadBook(null, ...).
+var bookUrl = null;
+function setBookUrl(url) {
+ bookUrl = url;
+}
function beginBookData() {
bookParts = [];
bookPartsLen = 0;
@@ -111,21 +130,27 @@ function loadBook(data, cfi, initialXPath, manager, flow, spread, snap, allowScr
selectAnnotationRange = selectAnnotationRangeParam !== undefined ? selectAnnotationRangeParam : false;
var viewportHeight = window.innerHeight;
document.getElementById('viewer').style.height = viewportHeight;
- var uint8Array;
- if (data) {
- uint8Array = new Uint8Array(data);
+ if (bookUrl) {
+ var url = bookUrl;
+ bookUrl = null;
+ book.open(url);
} else {
- uint8Array = new Uint8Array(bookPartsLen);
- var off = 0;
- for (var p = 0; p < bookParts.length; p++) {
- uint8Array.set(bookParts[p], off);
- off += bookParts[p].length;
- bookParts[p] = null; // let each chunk collect while assembling
+ var uint8Array;
+ if (data) {
+ uint8Array = new Uint8Array(data);
+ } else {
+ uint8Array = new Uint8Array(bookPartsLen);
+ var off = 0;
+ for (var p = 0; p < bookParts.length; p++) {
+ uint8Array.set(bookParts[p], off);
+ off += bookParts[p].length;
+ bookParts[p] = null; // let each chunk collect while assembling
+ }
+ bookParts = [];
+ bookPartsLen = 0;
}
- bookParts = [];
- bookPartsLen = 0;
+ book.open(uint8Array,)
}
- book.open(uint8Array,)
lastRenderOpts = {
manager: manager,
flow: flow,
@@ -1102,7 +1127,27 @@ function loadBook(data, cfi, initialXPath, manager, flow, spread, snap, allowScr
})
book.ready.then(function () {
- book.locations.generate(1600).then(() => {
+ // Walking every section to build the location index is the heaviest
+ // thing the reader does, and it ran on every open - minutes of a pegged
+ // core on a big book, with the first page waiting on it. The app keeps
+ // the result next to the cached epub: load that when there is one, and
+ // only generate (and hand the result back) when there isn't.
+ var locationsReady;
+ if (cachedLocations) {
+ var cached = cachedLocations;
+ cachedLocations = null;
+ locationsReady = Promise.resolve().then(function () {
+ try {
+ book.locations.load(cached);
+ } catch (e) {
+ console.error('Cached locations unusable, regenerating:', e);
+ return book.locations.generate(1600).then(handBackLocations);
+ }
+ });
+ } else {
+ locationsReady = book.locations.generate(1600).then(handBackLocations);
+ }
+ locationsReady.then(() => {
// Handle initial position after locations are generated
// XPath takes precedence over CFI
if (initialXPath && !initialXPathProcessed) {
diff --git a/packages/flutter_epub_viewer/lib/src/epub_viewer.dart b/packages/flutter_epub_viewer/lib/src/epub_viewer.dart
index 0451be4f..c68618bd 100644
--- a/packages/flutter_epub_viewer/lib/src/epub_viewer.dart
+++ b/packages/flutter_epub_viewer/lib/src/epub_viewer.dart
@@ -8,6 +8,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import '../flutter_epub_viewer.dart';
+import 'local_book_server.dart';
import 'utils.dart';
/// Callback for text selection events with WebView-relative coordinates.
@@ -48,6 +49,8 @@ class EpubViewer extends StatefulWidget {
this.onDeselection,
this.onInitialPositionLoading,
this.onInitialPositionLoaded,
+ this.cachedLocations,
+ this.onLocationsGenerated,
this.onTouchDown,
this.onTouchUp,
this.suppressNativeContextMenu = false,
@@ -76,6 +79,14 @@ class EpubViewer extends StatefulWidget {
/// Callback when the location are generated for epub, progress will be only available after this
final VoidCallback? onLocationLoaded;
+ /// Location index saved from an earlier open of this same file (epub.js
+ /// JSON); handed to the page so it can skip generating it.
+ final String? cachedLocations;
+
+ /// Fires with the freshly generated location index when no cached one was
+ /// supplied, so the caller can keep it for next time.
+ final ValueChanged<String>? onLocationsGenerated;
+
///Call back when chapters are loaded
final ValueChanged<List<EpubChapter>>? onChaptersLoaded;
@@ -221,8 +232,14 @@ class _EpubViewerState extends State<EpubViewer> {
allowsLinkPreview: false,
verticalScrollBarEnabled: false,
selectionGranularity: SelectionGranularity.CHARACTER,
+ // The page lives on a file: URL and fetches the book from the loopback
+ // server; without this the browser treats that as a cross-origin request
+ // from a file page and refuses it.
+ allowUniversalAccessFromFileURLs: true,
);
+ LocalBookServer? _bookServer;
+
@override
void initState() {
super.initState();
@@ -480,6 +497,16 @@ class _EpubViewerState extends State<EpubViewer> {
},
);
+ webViewController?.addJavaScriptHandler(
+ handlerName: "locationsGenerated",
+ callback: (data) {
+ final json = data.isNotEmpty ? data[0] : null;
+ if (json is String && json.isNotEmpty) {
+ widget.onLocationsGenerated?.call(json);
+ }
+ },
+ );
+
webViewController?.addJavaScriptHandler(
handlerName: "markClicked",
callback: (data) {
@@ -570,7 +597,6 @@ class _EpubViewerState extends State<EpubViewer> {
}
Future<void> loadBook() async {
- var data = await widget.epubSource.epubData;
final displaySettings = widget.displaySettings ?? EpubDisplaySettings();
String manager = displaySettings.manager.name;
String flow = displaySettings.flow.name;
@@ -597,18 +623,44 @@ class _EpubViewerState extends State<EpubViewer> {
String xpathParam = initialXPath != null ? '"$initialXPath"' : 'null';
- // Stream the file into the page in base64 chunks and assemble it there.
- // Serializing the whole book into the script source (the upstream
- // data.join(',') approach) builds a string several times the file size on
- // both sides of the bridge; a 322MB epub took the entire app down with an
- // out-of-memory crash before it ever reached the WebView.
- const chunkBytes = 2 * 1024 * 1024;
- await webViewController?.evaluateJavascript(source: 'beginBookData()');
- for (var i = 0; i < data.length; i += chunkBytes) {
- final end = (i + chunkBytes < data.length) ? i + chunkBytes : data.length;
- final b64 = base64Encode(Uint8List.sublistView(data, i, end));
+ final sourceFile = widget.epubSource.file;
+ final started = DateTime.now();
+ if (sourceFile != null) {
+ // A file on disk is served to the page over loopback and fetched by
+ // the page itself: no Dart-side copy, no base64, no bridge round trips.
+ // A 322MB book spent about twelve seconds in that transfer.
+ await _bookServer?.stop();
+ final server = LocalBookServer(sourceFile);
+ _bookServer = server;
+ final url = await server.start();
+ await webViewController?.evaluateJavascript(
+ source: 'setBookUrl(${jsonEncode(url)})',
+ );
+ debugPrint('[EpubViewer] serving book over loopback (${await sourceFile.length()} bytes)');
+ } else {
+ // Bytes from a URL or an asset: stream them into the page in base64
+ // chunks and assemble there. Serializing the whole book into the script
+ // source (the upstream data.join(',') approach) builds a string several
+ // times the file size on both sides of the bridge; a 322MB epub took
+ // the entire app down with an out-of-memory crash before it ever
+ // reached the WebView.
+ final data = await widget.epubSource.epubData;
+ const chunkBytes = 2 * 1024 * 1024;
+ await webViewController?.evaluateJavascript(source: 'beginBookData()');
+ for (var i = 0; i < data.length; i += chunkBytes) {
+ final end = (i + chunkBytes < data.length) ? i + chunkBytes : data.length;
+ final b64 = base64Encode(Uint8List.sublistView(data, i, end));
+ await webViewController?.evaluateJavascript(
+ source: 'appendBookData("$b64")',
+ );
+ }
+ debugPrint('[EpubViewer] pushed ${data.length} bytes into the page in '
+ '${DateTime.now().difference(started).inMilliseconds}ms');
+ }
+ final cachedLocations = widget.cachedLocations;
+ if (cachedLocations != null && cachedLocations.isNotEmpty) {
await webViewController?.evaluateJavascript(
- source: 'appendBookData("$b64")',
+ source: 'setCachedLocations(${jsonEncode(cachedLocations)})',
);
}
webViewController?.evaluateJavascript(
@@ -733,6 +785,8 @@ class _EpubViewerState extends State<EpubViewer> {
@override
void dispose() {
_stopSelectionMonitoring();
+ _bookServer?.stop();
+ _bookServer = null;
super.dispose();
}
}
diff --git a/packages/flutter_epub_viewer/lib/src/local_book_server.dart b/packages/flutter_epub_viewer/lib/src/local_book_server.dart
new file mode 100644
index 00000000..be6ba504
--- /dev/null
+++ b/packages/flutter_epub_viewer/lib/src/local_book_server.dart
@@ -0,0 +1,99 @@
+import 'dart:io';
+import 'dart:math';
+
+/// Serves one file to the WebView over the loopback interface. The page then
+/// fetches the epub straight from disk, instead of Dart reading the whole
+/// file into memory, base64-encoding it in chunks and pushing every chunk
+/// across the bridge to be decoded again: on a 322MB book that took about
+/// twelve seconds before the first page could even start rendering.
+///
+/// One random path per server, loopback only, closed with the viewer.
+class LocalBookServer {
+ LocalBookServer(this.file);
+
+ final File file;
+ final String _token = _randomToken();
+ HttpServer? _server;
+
+ String? get url => _server == null
+ ? null
+ : 'http://127.0.0.1:${_server!.port}/$_token/book.epub';
+
+ Future<String> start() async {
+ if (_server == null) {
+ final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
+ server.listen(_handle, onError: (_) {});
+ _server = server;
+ }
+ return url!;
+ }
+
+ Future<void> stop() async {
+ final server = _server;
+ _server = null;
+ await server?.close(force: true);
+ }
+
+ Future<void> _handle(HttpRequest req) async {
+ final res = req.response;
+ try {
+ final wanted = req.uri.path == '/$_token/book.epub' &&
+ (req.method == 'GET' || req.method == 'HEAD');
+ if (!wanted) {
+ res.statusCode = HttpStatus.notFound;
+ await res.close();
+ return;
+ }
+ final length = await file.length();
+ res.headers.set(HttpHeaders.accessControlAllowOriginHeader, '*');
+ res.headers.set(HttpHeaders.contentTypeHeader, 'application/epub+zip');
+ res.headers.set(HttpHeaders.acceptRangesHeader, 'bytes');
+ var start = 0;
+ var end = length - 1;
+ final range = req.headers.value(HttpHeaders.rangeHeader);
+ final m = range == null
+ ? null
+ : RegExp(r'^bytes=(\d*)-(\d*)$').firstMatch(range.trim());
+ if (m != null && (m.group(1)!.isNotEmpty || m.group(2)!.isNotEmpty)) {
+ if (m.group(1)!.isEmpty) {
+ start = length - int.parse(m.group(2)!);
+ } else {
+ start = int.parse(m.group(1)!);
+ if (m.group(2)!.isNotEmpty) end = int.parse(m.group(2)!);
+ }
+ if (start < 0) start = 0;
+ if (end > length - 1) end = length - 1;
+ if (start >= length || end < start) {
+ res.statusCode = HttpStatus.requestedRangeNotSatisfiable;
+ res.headers.set(HttpHeaders.contentRangeHeader, 'bytes */$length');
+ await res.close();
+ return;
+ }
+ res.statusCode = HttpStatus.partialContent;
+ res.headers.set(
+ HttpHeaders.contentRangeHeader,
+ 'bytes $start-$end/$length',
+ );
+ }
+ res.contentLength = end - start + 1;
+ if (req.method == 'HEAD') {
+ await res.close();
+ return;
+ }
+ await res.addStream(file.openRead(start, end + 1));
+ await res.close();
+ } catch (_) {
+ try {
+ await res.close();
+ } catch (_) {}
+ }
+ }
+
+ static String _randomToken() {
+ final r = Random.secure();
+ return List.generate(
+ 16,
+ (_) => r.nextInt(256).toRadixString(16).padLeft(2, '0'),
+ ).join();
+ }
+}
diff --git a/packages/flutter_epub_viewer/lib/src/models/epub_source.dart b/packages/flutter_epub_viewer/lib/src/models/epub_source.dart
index 0ae942f5..0df3cc49 100644
--- a/packages/flutter_epub_viewer/lib/src/models/epub_source.dart
+++ b/packages/flutter_epub_viewer/lib/src/models/epub_source.dart
@@ -5,26 +5,26 @@ import 'package:flutter_epub_viewer/src/epub_data_loader.dart';
/// Epub file source
class EpubSource {
- // final Uint8List epubData;
- final Future<Uint8List> epubData;
+ final EpubDataLoader _loader;
- EpubSource._({required this.epubData});
+ /// Set for file sources. The viewer serves such a file to the page over
+ /// loopback instead of pushing its bytes across the bridge, so the bytes
+ /// are only read on demand, never eagerly at construction.
+ final File? file;
+
+ EpubSource._(this._loader, {this.file});
+
+ Future<Uint8List> get epubData => _loader.loadData();
///Loading from a file
- factory EpubSource.fromFile(File file) {
- final loader = FileEpubLoader(file);
- return EpubSource._(epubData: loader.loadData());
- }
+ factory EpubSource.fromFile(File file) =>
+ EpubSource._(FileEpubLoader(file), file: file);
///load from a url with optional headers
- factory EpubSource.fromUrl(String url, {Map<String, String>? headers}) {
- final loader = UrlEpubLoader(url, headers: headers);
- return EpubSource._(epubData: loader.loadData());
- }
+ factory EpubSource.fromUrl(String url, {Map<String, String>? headers}) =>
+ EpubSource._(UrlEpubLoader(url, headers: headers));
///load from assets
- factory EpubSource.fromAsset(String assetPath) {
- final loader = AssetEpubLoader(assetPath);
- return EpubSource._(epubData: loader.loadData());
- }
+ factory EpubSource.fromAsset(String assetPath) =>
+ EpubSource._(AssetEpubLoader(assetPath));
}