Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #626 : multiple import don't stop if an import fail #680

Merged
merged 3 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/common/models/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
export enum ToastType {
DownloadComplete = "download-complete",
DownloadStarted = "download-started",
DownloadFailed = "download-failed",
}
16 changes: 14 additions & 2 deletions src/main/api/publication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
// ==LICENSE-END==

import * as debug_ from "debug";
import { inject, injectable } from "inversify";
import { ToastType } from "readium-desktop/common/models/toast";
import { open } from "readium-desktop/common/redux/actions/toast";
Expand All @@ -19,6 +20,9 @@ import { Store } from "redux";
import { downloadActions } from "readium-desktop/common/redux/actions";
import { ImportState } from "readium-desktop/common/redux/states/import";

// Logger
const debug = debug_("readium-desktop:main#services/catalog");

@injectable()
export class PublicationApi {
@inject("publication-repository")
Expand Down Expand Up @@ -122,8 +126,16 @@ export class PublicationApi {
const newDocs = [];

for (const path of paths) {
const newDoc = await this.catalogService.importFile(path);
newDocs.push(newDoc);
try {
const newDoc = await this.catalogService.importFile(path);
if (newDoc) {
newDocs.push(newDoc);
}
} catch (error) {
debug(`Import file - FAIL : ${path}`, error);
this.dispatchToastRequest(ToastType.DownloadFailed,
this.translator.translate("message.import.fail", {path}));
}
}

return newDocs.map((doc) => {
Expand Down
2 changes: 2 additions & 0 deletions src/main/services/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ export class CatalogService {
public async importFile(filePath: string, isLcpFile?: boolean): Promise<PublicationDocument> {
const ext = path.extname(filePath);

debug("Import File - START");
if (ext === ".lcpl" || (ext === ".part" && isLcpFile)) {
return this.importLcplFile(filePath);
} else if (/\.epub[3]?$/.test(ext) || (ext === ".part" && !isLcpFile)) {
return this.importEpubFile(filePath);
}
debug("Import File - END");

return null;
}
Expand Down
8 changes: 8 additions & 0 deletions src/renderer/assets/styles/toast.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
animation-name: start;
animation-duration: 0.5s;

&.error {
background: rgba(255, 0, 0, 0.1);
}

&.success {
background: rgba(0, 255, 0, 0.1);
}

& p {
margin: 0;
flex: 1;
Expand Down
29 changes: 27 additions & 2 deletions src/renderer/components/toast/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ import { TranslatorProps, withTranslator } from "../utils/translator";

import * as styles from "readium-desktop/renderer/assets/styles/toast.css";

export enum ToastType {
Error,
Default,
Success,

}

interface Props extends TranslatorProps {
close: (id: string) => void;
className?: string;
id?: string;
icon?: any;
message?: string;
displaySystemNotification: boolean;
type?: ToastType;
}

interface State {
Expand Down Expand Up @@ -61,12 +69,29 @@ export class Toast extends React.Component<Props, State> {
}

public render(): React.ReactElement<{}> {
const { icon } = this.props;
const { icon, type } = this.props;
const { willLeave, toRemove } = this.state;

let typeClassName: string;
switch (type) {
case ToastType.Error:
typeClassName = styles.error;
break;
case ToastType.Success:
typeClassName = styles.success;
break;
default:
break;
}
return (
<div
ref={(ref) => this.ref = ref}
className={classNames(styles.toast, willLeave && styles.leave, toRemove && styles.toRemove)}
className={classNames(
styles.toast,
willLeave && styles.leave,
toRemove && styles.toRemove,
typeClassName,
)}
>
{ icon && <SVG className={styles.icon} svg={icon} /> }
<p>{ this.props.message }</p>
Expand Down
16 changes: 15 additions & 1 deletion src/renderer/components/toast/ToastManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ToastState } from "readium-desktop/common/redux/states/toast";

import { RootState } from "readium-desktop/renderer/redux/states";

import Toast from "./Toast";
import Toast, {ToastType as CompToastType} from "./Toast";

import * as DownloadIcon from "readium-desktop/renderer/assets/icons/download.svg";

Expand Down Expand Up @@ -67,6 +67,8 @@ export class ToastManager extends React.Component<Props, State> {
return this.buildFileImportToast(toast, id);
case ToastType.DownloadStarted:
return this.buildFileImportStartToast(toast, id);
case ToastType.DownloadFailed:
return this.buildFileImportFailToast(toast, id);
default:
return (<></>);
}
Expand Down Expand Up @@ -99,6 +101,18 @@ export class ToastManager extends React.Component<Props, State> {
);
}

private buildFileImportFailToast(toast: ToastState, id: string) {
return (
<Toast
message={toast.data}
key={id}
icon={ DownloadIcon }
close={ () => this.close(id) }
type={CompToastType.Error}
/>
);
}

private close(id: string) {
const { toastList } = this.state;
toastList[id] = undefined;
Expand Down
3 changes: 2 additions & 1 deletion src/resources/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@
"success": "Der Download von {{- title}} wurde abgeschlossen"
},
"import": {
"success": ""
"fail": "Der Import von {{- path}} ist fehlgeschlagen. Stellen Sie sicher, dass die Datei gültig ist",
"success": "Der Import von {{- title}} ist abgeschlossen."
}
},
"opds": {
Expand Down
1 change: 1 addition & 0 deletions src/resources/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"success": "The download of {{- title}} is finished."
},
"import": {
"fail": "The import of {{- path}} failed. Verify that the file is valid",
"success": "The import of {{- title}} is finished."
}
},
Expand Down
1 change: 1 addition & 0 deletions src/resources/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"success": "Le téléchargement de {{- title}} est terminé."
},
"import": {
"fail": "L'importation de {{- path}} a échoué. Vérifier que le fichier est valide",
"success": "L'import de {{- title}} est terminé."
}
},
Expand Down
5 changes: 3 additions & 2 deletions src/typings/en.translation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,13 @@ declare namespace typed_i18n {
readonly "start": string,
readonly "success": string
},
readonly "import": { readonly "success": string }
readonly "import": { readonly "fail": string, readonly "success": string }
};
(_: "message.download", __?: {}): { readonly "start": string, readonly "success": string };
(_: "message.download.start", __?: {}): string;
(_: "message.download.success", __?: {}): string;
(_: "message.import", __?: {}): { readonly "success": string };
(_: "message.import", __?: {}): { readonly "fail": string, readonly "success": string };
(_: "message.import.fail", __?: {}): string;
(_: "message.import.success", __?: {}): string;
(_: "opds", __?: {}): {
readonly "addForm": {
Expand Down