Skip to content

Commit 1c4dd2b

Browse files
committed
Add a method to get the filename for a Book for downloading.
1 parent 0a3680a commit 1c4dd2b

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

code/book.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@ export class Book extends EventTarget {
183183
/** @returns {BookContainer} */
184184
getContainer() { return this.#bookContainer; }
185185

186+
/**
187+
* Returns a filename based on the source of the book (request, file, url).
188+
* @returns {string}
189+
*/
190+
getFilename() {
191+
if (this.#uri || this.#request) {
192+
let url = this.#uri ?? this.#request.url;
193+
return url.substring(url.lastIndexOf('/') + 1);
194+
} else if (this.#file || this.#fileHandle) {
195+
return (this.#file || this.#fileHandle).name;
196+
}
197+
throw 'Unknown type of book source';
198+
}
199+
186200
/** @returns {FileSystemFileHandle} */
187201
getFileSystemHandle() { return this.#fileHandle; }
188202

code/kthoom.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,7 @@ export class KthoomApp {
10881088
const blob = new Blob([ab], {type: this.currentBook_.getMIMEType()});
10891089
const link = document.createElement('a');
10901090
link.href = window.URL.createObjectURL(blob);
1091-
const fileName = this.currentBook_.getName();
1091+
const fileName = this.currentBook_.getFilename();
10921092
link.download = fileName;
10931093
link.click();
10941094
}

0 commit comments

Comments
 (0)