Skip to content

Commit

Permalink
EPUB: Show error when EPUB appears to be encrypted
Browse files Browse the repository at this point in the history
And update .error-bar appearance.
  • Loading branch information
AbeJellinek committed May 1, 2024
1 parent 63b9524 commit 5a046b7
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/common/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,10 @@ class Reader {
this._onSetZoom(iframe, zoom);
});

let onEPUBEncrypted = () => {
this.setErrorMessage(this._getString('pdfReader.epubEncrypted'));
};

let data;
if (this._type === 'pdf') {
data = this._data;
Expand Down Expand Up @@ -812,7 +816,8 @@ class Reader {
} else if (this._type === 'epub') {
view = new EPUBView({
...common,
fontFamily: this._state.fontFamily
fontFamily: this._state.fontFamily,
onEPUBEncrypted,
});
} else if (this._type === 'snapshot') {
view = new SnapshotView({
Expand Down
6 changes: 4 additions & 2 deletions src/common/stylesheets/components/_other.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
left: 0;
right: 0;
bottom: 0;
height: 25px;
font-size: rem(16);
height: 2.5em;
display: flex;
justify-content: center;
align-items: center;
background-color: #ff7474;
background-color: var(--accent-red);
color: var(--accent-white);
z-index: $z-index-error-bar;
}

Expand Down
1 change: 1 addition & 0 deletions src/dom/common/dom-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,7 @@ export type DOMViewOptions<State extends DOMViewState, Data> = {
onTabOut: (isShiftTab?: boolean) => void;
onKeyUp: (event: KeyboardEvent) => void;
onKeyDown: (event: KeyboardEvent) => void;
onEPUBEncrypted: () => void;
data: Data & {
buf?: Uint8Array,
url?: string
Expand Down
16 changes: 16 additions & 0 deletions src/dom/epub/epub-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ class EPUBView extends DOMView<EPUBViewState, EPUBViewData> {
let styleScoper = new StyleScoper(this._iframeDocument);
await Promise.all(this.book.spine.spineItems.map(section => this._displaySection(section, styleScoper)));

if (this._sectionViews.some(view => view.error) && await this._isEncrypted()) {
this._options.onEPUBEncrypted();
this._sectionsContainer.remove();
return;
}

if (this._options.fontFamily) {
this._iframeDocument.documentElement.style.setProperty('--content-font-family', this._options.fontFamily);
}
Expand Down Expand Up @@ -195,6 +201,16 @@ class EPUBView extends DOMView<EPUBViewState, EPUBViewData> {
this.book.archive.zip = null;
}

private async _isEncrypted() {
try {
let xml = await this.book.archive.request('/META-INF/encryption.xml', 'text') as string;
return xml.includes('<EncryptedData');
}
catch (e) {
return false;
}
}

private async _displaySection(section: Section, styleScoper: StyleScoper) {
let sectionView = new SectionView({
section,
Expand Down
3 changes: 3 additions & 0 deletions src/dom/epub/section-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class SectionView {

body!: HTMLElement;

error = false;

private readonly _window: Window & typeof globalThis;

private readonly _document: Document;
Expand Down Expand Up @@ -100,6 +102,7 @@ class SectionView {
errorDiv.append(`[Section ${this.section.index}: ${message}]`);
this.container.replaceChildren(errorDiv);
this.body = errorDiv;
this.error = true;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/en-us.strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,6 @@ export default {
'pdfReader.preparingDocumentForPrinting': 'Preparing document for printing…',
'pdfReader.phraseNotFound': 'Phrase not found',
'pdfReader.selectedPages': '{count, plural, one {# page} other {# pages}} selected',
'pdfReader.pageOptions': 'Page Options'
'pdfReader.pageOptions': 'Page Options',
'pdfReader.epubEncrypted': 'This ebook appears to be encrypted and cannot be opened.'
};

0 comments on commit 5a046b7

Please sign in to comment.