Skip to content

Commit

Permalink
Merge pull request #90
Browse files Browse the repository at this point in the history
Add support for loading readium webpub via HTTP
  • Loading branch information
jmgeffroy authored Jul 5, 2023
2 parents 7cd34a9 + 4d84f0f commit 6f2bb79
Show file tree
Hide file tree
Showing 11 changed files with 2,265 additions and 2,175 deletions.
20 changes: 10 additions & 10 deletions components/navigator/assets/_scripts/src/index-reflowable.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ import flutter from "./flutter";
import "tocca";
import { isScrollModeEnabled } from "./utils";

document.scrollingElement.addEventListener("swipeup", function (event) {
event.stopPropagation();
flutter.onSwipeUp();
});

document.scrollingElement.addEventListener("swipedown", function (event) {
event.stopPropagation();
flutter.onSwipeDown();
});

/**
* Remove all child nodes from an element
* @param {Object} element The element to empty
Expand Down Expand Up @@ -285,4 +275,14 @@ document.addEventListener("DOMContentLoaded", function () {
"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no"
);
document.head.appendChild(meta);

document.scrollingElement.addEventListener("swipeup", function (event) {
event.stopPropagation();
flutter.onSwipeUp();
});

document.scrollingElement.addEventListener("swipedown", function (event) {
event.stopPropagation();
flutter.onSwipeDown();
});
});
4,302 changes: 2,170 additions & 2,132 deletions components/navigator/assets/_scripts/yarn.lock

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion components/navigator/lib/src/epub/epub_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class EpubController extends PublicationController {
Function onServerClosed,
Function? onPageJump,
Future<String?> locationFuture,
FileAsset fileAsset,
PublicationAsset fileAsset,
Future<Streamer> streamerFuture,
ReaderAnnotationRepository readerAnnotationRepository,
Function0<List<RequestHandler>> handlersProvider,
Expand Down
2 changes: 2 additions & 0 deletions components/shared/lib/src/fetcher/file_fetcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,7 @@ class FileResource extends Resource {
// We don't want to catch any Error, only OOM.
return ResourceTry<T>.failure(ResourceException.wrap(e));
}

return ResourceTry<T>.failure(ResourceException.wrap(e));
});
}
51 changes: 28 additions & 23 deletions components/shared/lib/src/fetcher/http_fetcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:collection';
import 'dart:math';
import 'dart:typed_data';

import 'package:dartx/dartx.dart';
import 'package:fimber/fimber.dart';
import 'package:http/http.dart' as http;
import 'package:mno_commons/extensions/strings.dart';
import 'package:mno_commons/io.dart';
Expand All @@ -20,12 +22,12 @@ class HttpFetcher extends Fetcher {
HttpFetcher(this.rootHref);

@override
Future<List<Link>> links() async => [];
Future<List<Link>> links() async => [Link(href: Uri.parse(rootHref).path)];

@override
Resource get(Link link) {
String linkHref = link.href.addPrefix("/");
Uri url = Uri.parse("$rootHref$linkHref");
String linkHref = link.href.addPrefix("./");
Uri url = Uri.parse(rootHref).resolve(linkHref);
return HttpResource(url, link);
}

Expand All @@ -39,20 +41,22 @@ class HttpFetcher extends Fetcher {
class HttpResource extends Resource {
final Uri url;
final Link _link;
ResourceTry<http.Response>? _response;

static HashMap<Uri, ResourceTry<http.Response>> caching =
HashMap<Uri, ResourceTry<http.Response>>();

HttpResource(this.url, this._link);

@override
Future<Link> link() async => _link;

Future<ResourceTry<http.Response>> get response async =>
_response ??= await catching(() async {
Uri uri = Uri.parse("https://cros-anywhere.herokuapp.com/$url");
caching[url] ??= await catching(() async {
Uri uri = Uri.parse("$url");
return http.get(uri, headers: {
'Access-Control-Allow-Origin': 'http://localhost:49430',
'Access-Control-Allow-Methods':
'GET, POST, PATCH, PUT, DELETE, OPTIONS',
'GET, POST, PATCH, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Origin, Content-Type, X-Auth-Token',
});
});
Expand Down Expand Up @@ -102,20 +106,21 @@ class HttpResource extends Resource {
@override
String toString() => "HttpResource($url)";

static Future<ResourceTry<T>> catching<T>(Future<T> Function() closure) =>
closure().then((value) => ResourceTry.success(value)).catchError((e, st) {
if (e is http.ClientException) {
return ResourceTry<T>.failure(ResourceException.notFound);
}
if (e is FileNotFoundException) {
return ResourceTry<T>.failure(ResourceException.notFound);
}
if (e is Exception) {
return ResourceTry<T>.failure(ResourceException.wrap(e));
}
if (e is OutOfMemoryError) {
// We don't want to catch any Error, only OOM.
return ResourceTry<T>.failure(ResourceException.wrap(e));
}
});
static Future<ResourceTry<T>> catching<T>(
Future<T> Function() closure) async {
try {
T value = await closure();
return ResourceTry.success(value);
} on http.ClientException catch (_) {
return ResourceTry<T>.failure(ResourceException.notFound);
} on FileNotFoundException catch (_) {
return ResourceTry<T>.failure(ResourceException.notFound);
} on Exception catch (e) {
return ResourceTry<T>.failure(ResourceException.wrap(e));
} on OutOfMemoryError catch (e) {
return ResourceTry<T>.failure(ResourceException.wrap(e));
} catch (e) {
return ResourceTry<T>.failure(ResourceException.wrap(e));
}
}
}
24 changes: 21 additions & 3 deletions components/shared/lib/src/publication/asset/file_asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,27 @@ class FileAsset extends PublicationAsset {
String get name => basename(file.path);

@override
Future<MediaType> get mediaType async => _mediaType ??= (knownMediaType ??
(await MediaType.ofFileWithSingleHint(file, mediaType: mediaTypeHint)) ??
MediaType.binary);
Future<MediaType> get mediaType async => await getMediaType();

Future<MediaType> getMediaType() async {
if (_mediaType != null) {
return Future.value(_mediaType);
}

if (knownMediaType != null) {
_mediaType = knownMediaType;
return Future.value(_mediaType ??= knownMediaType);
}

MediaType? ofFile =
await MediaType.ofFileWithSingleHint(file, mediaType: mediaTypeHint);

if (ofFile != null) {
return _mediaType ??= ofFile;
}

return _mediaType ??= MediaType.binary;
}

@override
Future<Try<Fetcher, OpeningException>> createFetcher(
Expand Down
2 changes: 1 addition & 1 deletion components/streamer/lib/src/streamer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class Streamer {
EpubParser(),
if (pdfFactory != null) PdfParser(pdfFactory!),
ImageParser(),
if (pdfFactory != null) ReadiumWebPubParser(pdfFactory),
ReadiumWebPubParser(pdfFactory),
];

List<StreamPublicationParser> _getParsers() => List.of(_parsers)
Expand Down
4 changes: 2 additions & 2 deletions reader_widget/lib/views/viewers/book_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import 'package:mno_webview/webview.dart';
typedef PaginationCallback = Function(PaginationInfo paginationInfo);

abstract class BookScreen extends StatefulWidget {
final FileAsset asset;
final PublicationAsset asset;
final ReaderAnnotationRepository? readerAnnotationRepository;
final PaginationCallback? paginationCallback;

Expand Down Expand Up @@ -68,7 +68,7 @@ abstract class BookScreenState<T extends BookScreen,
Function onServerClosed,
Function? onPageJump,
Future<String?> locationFuture,
FileAsset fileAsset,
PublicationAsset fileAsset,
Future<Streamer> streamerFuture,
ReaderAnnotationRepository readerAnnotationRepository,
Function0<List<RequestHandler>> handlersProvider);
Expand Down
29 changes: 28 additions & 1 deletion reader_widget/lib/views/viewers/epub_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:mno_commons/utils/functions.dart';
import 'package:mno_navigator/epub.dart';
import 'package:mno_navigator/publication.dart';
import 'package:mno_server/mno_server.dart';
import 'package:mno_shared/mediatype.dart';
import 'package:mno_shared/publication.dart';
import 'package:mno_streamer/parser.dart';

Expand Down Expand Up @@ -79,6 +80,32 @@ class EpubScreen extends BookScreen {
);
}

factory EpubScreen.fromUri(
{Key? key,
required String rootHref,
ReaderAnnotationRepository? readerAnnotationRepository,
PaginationCallback? paginationCallback,
MediaType? mimeType,
String? location,
String? settings,
String? theme}) {
Map<String, dynamic>? decodedTheme;
try {
decodedTheme = json.decode(theme!);
} catch (e) {
debugPrint('failure to decode theme: $e');
}
return EpubScreen(
key: key,
asset: HttpAsset(rootHref, knownMediaType: mimeType),
readerAnnotationRepository: readerAnnotationRepository,
paginationCallback: paginationCallback,
location: location,
settings: int.tryParse(settings ?? '100'),
theme: decodedTheme,
);
}

@override
State<StatefulWidget> createState() => EpubScreenState();
}
Expand Down Expand Up @@ -133,7 +160,7 @@ class EpubScreenState extends BookScreenState<EpubScreen, EpubController> {
Function onServerClosed,
Function? onPageJump,
Future<String?> locationFuture,
FileAsset fileAsset,
PublicationAsset fileAsset,
Future<Streamer> streamerFuture,
ReaderAnnotationRepository readerAnnotationRepository,
Function0<List<RequestHandler>> handlersProvider) =>
Expand Down

0 comments on commit 6f2bb79

Please sign in to comment.