Skip to content

Commit

Permalink
fix: about dialog box now a publication to open in reader view, with …
Browse files Browse the repository at this point in the history
…full document accessibility, including readaloud (PR #1199 Fixes #992 Fixes #576)
  • Loading branch information
panaC authored Jan 13, 2021
1 parent 87aebfb commit 2cbd7e3
Show file tree
Hide file tree
Showing 12 changed files with 409 additions and 45 deletions.
5 changes: 5 additions & 0 deletions src/common/api/interface/publicationApi.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export interface IPublicationApi {
link: IOpdsLinkView,
pub?: IOpdsPublicationView,
) => SagaGenerator<PublicationView>;
importFromString: (
manifest: string,
baseFileUrl: string, // should starts with 'file://'
) => SagaGenerator<PublicationView>;
importFromFs: (
filePathArray: string | string[],
) => SagaGenerator<PublicationView[]>;
Expand All @@ -53,6 +57,7 @@ export interface IPublicationModuleApi {
"publication/getAllTags": IPublicationApi["getAllTags"];
"publication/importFromLink": IPublicationApi["importFromLink"];
"publication/importFromFs": IPublicationApi["importFromFs"];
"publication/importFromString": IPublicationApi["importFromString"];
"publication/search": IPublicationApi["search"];
"publication/exportPublication": IPublicationApi["exportPublication"];
}
8 changes: 8 additions & 0 deletions src/common/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// ==LICENSE-BEGIN==
// Copyright 2017 European Digital Reading Lab. All rights reserved.
// Licensed to the Readium Foundation under one or more contributor license agreements.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
// ==LICENSE-END==

export const ABOUT_BOOK_TITLE_PREFIX = "_______________";
8 changes: 6 additions & 2 deletions src/main/api/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import * as debug_ from "debug";
import { inject, injectable } from "inversify";
import { ICatalogApi } from "readium-desktop/common/api/interface/catalog.interface";
import { ABOUT_BOOK_TITLE_PREFIX } from "readium-desktop/common/constant";
import { isAudiobookFn, isDivinaFn, isPdfFn } from "readium-desktop/common/isManifestType";
import { ToastType } from "readium-desktop/common/models/toast";
import { toastActions } from "readium-desktop/common/redux/actions";
Expand Down Expand Up @@ -90,8 +91,11 @@ export class CatalogApi implements ICatalogApi {
},
} = await this.getPublicationView();

const allAdded_ = allAdded.slice(0, NB_PUB);
const epubReaded_ = epubReaded.slice(0, NB_PUB);
const _allAdded = allAdded.filter(({title}) => !title.startsWith(ABOUT_BOOK_TITLE_PREFIX));
const _epubReaded = epubReaded.filter(({title}) => !title.startsWith(ABOUT_BOOK_TITLE_PREFIX));

const allAdded_ = _allAdded.slice(0, NB_PUB);
const epubReaded_ = _epubReaded.slice(0, NB_PUB);
const audiobookReaded_ = audiobookReaded.slice(0, NB_PUB);
const divinaReaded_ = divinaReaded.slice(0, NB_PUB);
const pdfReaded_ = pdfReaded.slice(0, NB_PUB);
Expand Down
32 changes: 20 additions & 12 deletions src/main/redux/sagas/api/publication/import/importFromLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function* importLinkFromPath(
let returnPublicationDocument = publicationDocument;
if (!alreadyImported && publicationDocument) {

const tags = pub?.tags?.map((v) => v.name);
const tags = pub?.tags?.map((v) => v.name) || [];

// Merge with the original publication
const publicationDocumentAssigned = Object.assign(
Expand Down Expand Up @@ -88,7 +88,6 @@ export function* importFromLinkService(
}
} catch (e) {
debug("can't fetch url to determine the type", url.toString());

link.type = "";
}
}
Expand Down Expand Up @@ -116,20 +115,29 @@ export function* importFromLinkService(
}

if (isHtml || isJson) {
debug("the link need to be packaged");
link = { url: url.toString() };
}

const packagePath = yield* callTyped(packageFromLink, url.toString(), isHtml);
if (packagePath) {
return yield* callTyped(importLinkFromPath, packagePath, { url: url.toString() }, pub);
}
const downloadMayBePackageLink = function*() {

} else {
debug("Start the download", link);
if (isHtml || isJson) {
debug("the link need to be packaged");

const [downloadPath] = yield* callTyped(downloader, [{ href: link.url, type: link.type }], title);
if (downloadPath) {
return yield* callTyped(importLinkFromPath, downloadPath, link, pub);
return yield* callTyped(packageFromLink, url.toString(), isHtml);

} else {
debug("Start the download", link);

const [downloadPath] = yield* callTyped(downloader, [{ href: link.url, type: link.type }], title);
return downloadPath;
}
};

const fileOrPackagePath = yield* callTyped(downloadMayBePackageLink);
if (fileOrPackagePath) {
return yield* callTyped(importLinkFromPath, fileOrPackagePath, link, pub);
} else {
debug("downloaded file path or package path is empty");
}

return [undefined, false];
Expand Down
33 changes: 33 additions & 0 deletions src/main/redux/sagas/api/publication/import/importFromString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// ==LICENSE-BEGIN==
// Copyright 2017 European Digital Reading Lab. All rights reserved.
// Licensed to the Readium Foundation under one or more contributor license agreements.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
// ==LICENSE-END==

import * as debug_ from "debug";
import { callTyped } from "readium-desktop/common/redux/sagas/typed-saga";
import { PublicationDocument } from "readium-desktop/main/db/document/publication";
import { SagaGenerator } from "typed-redux-saga";

import { packageFromManifestBuffer } from "../packager/packageLink";
import { importFromFsService } from "./importFromFs";

// Logger
const debug = debug_("readium-desktop:main#saga/api/publication/importFromStringService");

export function* importFromStringService(
manifest: string,
baseFileUrl: string, // should starts with 'file://'
): SagaGenerator<[publicationDoc: PublicationDocument, alreadyImported: boolean]> {

const baseUrl = baseFileUrl.startsWith("file://") ? baseFileUrl : "file://";
const packagePath = yield* callTyped(packageFromManifestBuffer, baseUrl, Buffer.from(manifest));
if (packagePath) {
return yield* callTyped(importFromFsService, packagePath);
} else {
debug("package path is empty");
}

return [undefined, false];
}
30 changes: 28 additions & 2 deletions src/main/redux/sagas/api/publication/import/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
import * as debug_ from "debug";
import { ToastType } from "readium-desktop/common/models/toast";
import { toastActions } from "readium-desktop/common/redux/actions";
import { allTyped, callTyped /*, raceTyped*/ } from "readium-desktop/common/redux/sagas/typed-saga";
import { allTyped, callTyped } from "readium-desktop/common/redux/sagas/typed-saga";
import { IOpdsLinkView, IOpdsPublicationView } from "readium-desktop/common/views/opds";
import { PublicationView } from "readium-desktop/common/views/publication";
import { diMainGet } from "readium-desktop/main/di";
import { put } from "redux-saga/effects";
import { /*delay,*/ SagaGenerator } from "typed-redux-saga";
import { SagaGenerator } from "typed-redux-saga";

import { importFromFsService } from "./importFromFs";
import { importFromLinkService } from "./importFromLink";
import { importFromStringService } from "./importFromString";

// Logger
const debug = debug_("readium-desktop:main#saga/api/publication/import");
Expand Down Expand Up @@ -76,6 +77,31 @@ export function* importFromLink(
return undefined;
}

export function* importFromString(
manifest: string,
baseFileUrl: string, // should starts with 'file://'
): SagaGenerator<PublicationView | undefined> {

if (manifest) {

try {
const [publicationDocument] = yield* callTyped(importFromStringService, manifest, baseFileUrl);

if (!publicationDocument) {
throw new Error("publicationDocument not imported on db");
}

const publicationViewConverter = diMainGet("publication-view-converter");
return publicationViewConverter.convertDocumentToView(publicationDocument);

} catch (error) {
throw new Error(`importFromLink error ${error}`);
}
}

return undefined;
}

export function* importFromFs(
filePath: string | string[],
): SagaGenerator<PublicationView[] | undefined> {
Expand Down
3 changes: 2 additions & 1 deletion src/main/redux/sagas/api/publication/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { findAll } from "./findAll";
import { findByTag } from "./findByTag";
import { getAllTags } from "./getAllTags";
import { getPublication } from "./getPublication";
import { importFromFs, importFromLink } from "./import";
import { importFromFs, importFromLink, importFromString } from "./import";
import { search } from "./search";
import { updateTags } from "./updateTags";

Expand All @@ -28,4 +28,5 @@ export const publicationApi: IPublicationApi = {
exportPublication,
importFromFs,
importFromLink,
importFromString,
};
13 changes: 11 additions & 2 deletions src/main/redux/sagas/api/publication/packager/packageLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function* BufferManifestToR2Publication(manifest: Buffer, href: string): SagaGen

} catch (e) {

debug("error to parse manifest");
debug("error to parse manifest", e, manifest);
return undefined;
}

Expand Down Expand Up @@ -210,9 +210,18 @@ export function* packageFromLink(

const [manifest, manifestUrl] = yield* callTyped(packageGetManifestBuffer, href, isHtml);
if (!manifest) {
throw new Error("manifest not found from content link");
throw new Error("manifest not found from content link " + href);
}

return yield* callTyped(packageFromManifestBuffer, href, manifest, manifestUrl);
}

export function* packageFromManifestBuffer(
href: string, // 'file://' for local resources
manifest: Buffer,
manifestUrl?: string,
) {

const r2Publication = yield* callTyped(BufferManifestToR2Publication, manifest, href);
if (!r2Publication) {
throw new Error("r2Publication parsing failed");
Expand Down
Loading

0 comments on commit 2cbd7e3

Please sign in to comment.