Downloads book content.
This commit is contained in:
@@ -105,12 +105,9 @@ class Book{
|
||||
downloadMetaContent(){
|
||||
let downloads = []
|
||||
|
||||
this.insertBookFile({
|
||||
url: this.book_info.cover,
|
||||
file: "cover.img",
|
||||
context: null,
|
||||
mime: null
|
||||
})
|
||||
this.insertBookFile(
|
||||
this.book_info.cover,
|
||||
"cover.img")
|
||||
|
||||
downloads.push(
|
||||
this.downloadJson(this.book_info.toc)
|
||||
@@ -135,41 +132,49 @@ class Book{
|
||||
|
||||
|
||||
extractChapterAssets(json){
|
||||
if (!json.asset_base_url
|
||||
|| !json.content){
|
||||
if (!json.asset_base_url || !json.content){
|
||||
throw "Missing data."
|
||||
}
|
||||
|
||||
// Html
|
||||
this.insertBookFile({
|
||||
url: json.content,
|
||||
file: json.full_path,
|
||||
context: null,
|
||||
mime: null,
|
||||
content: null,
|
||||
})
|
||||
this.insertBookFile(
|
||||
json.content,
|
||||
json.full_path)
|
||||
// List of images.
|
||||
for (let idx in json.images){
|
||||
this.insertBookFile({
|
||||
url: json.asset_base_url + json.images[idx],
|
||||
file: json.images[idx],
|
||||
context: null,
|
||||
mime: null
|
||||
})
|
||||
this.insertBookFile(
|
||||
json.asset_base_url + json.images[idx],
|
||||
json.images[idx])
|
||||
}
|
||||
// List of stylesheets.
|
||||
for (let idx in json.stylesheets){
|
||||
this.insertBookFile({
|
||||
url: json.stylesheets[idx].original_url,
|
||||
file: json.stylesheets[idx].full_path,
|
||||
context: null,
|
||||
mime: null
|
||||
})
|
||||
this.insertBookFile(
|
||||
json.stylesheets[idx].original_url,
|
||||
json.stylesheets[idx].full_path)
|
||||
}
|
||||
}
|
||||
|
||||
insertBookFile(obj){
|
||||
this.book_files[obj.url] = obj
|
||||
downloadContent(){
|
||||
return Promise.map(Object.keys(this.book_files), (url) => {
|
||||
return this.downloadResource(url)
|
||||
.then((res) => {
|
||||
if (res.ok){
|
||||
console.log(res.headers.get('Content-Type'))
|
||||
this.book_files[url].headers = res.headers
|
||||
this.book_files[url].mime = res.headers.get('Content-Type')
|
||||
this.book_files[url].body = res.blob()
|
||||
}
|
||||
})
|
||||
},{concurrency: 1})
|
||||
}
|
||||
|
||||
insertBookFile(url, filename){
|
||||
this.book_files[url] = {
|
||||
url: url,
|
||||
filename: filename,
|
||||
headers: null,
|
||||
mime: null,
|
||||
body: null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,7 +187,6 @@ function renderInfo(book){
|
||||
function renderChapterList(book){
|
||||
// Add chapters to UI
|
||||
for (let chapter_idx in book.chapter_list) {
|
||||
console.log(book.chapter_list[chapter_idx])
|
||||
let chapter = book.chapter_list[chapter_idx];
|
||||
var chapter_dom = $("<li></li>")
|
||||
.addClass("list-group-item")
|
||||
@@ -194,15 +198,11 @@ function renderChapterList(book){
|
||||
}
|
||||
|
||||
function createEpub(book, epub){
|
||||
for (let url in book.raw_book){
|
||||
let name = "meta/"+book.raw_book[url].url.replace(/[^a-z0-9]/gi, '_').toLowerCase()
|
||||
epub.addFile(name, book.raw_book[url].clone().blob())
|
||||
}
|
||||
// epub.addFile("book_info.json", JSON.stringify(book.book_info))
|
||||
// epub.addFile("book_files.json", JSON.stringify(book.book_files))
|
||||
// epub.addFile("book_toc.json", JSON.stringify(book.book_toc))
|
||||
// epub.addFile("book_flattoc.json", JSON.stringify(book.book_flattoc))
|
||||
epub.addFile("book.json", JSON.stringify(book, null, '\t'))
|
||||
for (let url in book.book_files){
|
||||
file = book.book_files[url]
|
||||
epub.addFile(file.filename, file.body)
|
||||
}
|
||||
}
|
||||
|
||||
function onDownloadBookClicked(){
|
||||
@@ -218,15 +218,18 @@ function onDownloadBookClicked(){
|
||||
book = new Book(book_id);
|
||||
book.downloadBookInfo()
|
||||
.then(() => { renderInfo(book); })
|
||||
.then(() => { return book.downloadChapterList(); })
|
||||
.then(() => { return renderChapterList(book); })
|
||||
// .then(() => { return book.downloadChapterList(); })
|
||||
// .then(() => { return renderChapterList(book); })
|
||||
.then(() => { return book.downloadMetaContent(); })
|
||||
// .then(() => { return book.downloadContent(); })
|
||||
.then(() => { return book.downloadContent(); })
|
||||
.then(() => { return createEpub(book, epub); })
|
||||
.then(() => { return epub.generateAsync(); })
|
||||
.then((file) => {
|
||||
let filename = "books/" + "test" + ".zip"
|
||||
// book.filename.replace(/[^a-z0-9]/gi, '_').toLowerCase()
|
||||
let title = book.book_info.title
|
||||
let filename = "books/"
|
||||
+ title.replace(/[^a-z0-9]/gi, '_').toLowerCase()
|
||||
+ ".zip"
|
||||
console.log(`Zip file name ${filename}`)
|
||||
let url = window.URL.createObjectURL(file)
|
||||
browser.downloads.download({ "filename" : filename, url : url})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user