Color status of downloaded resource.

This commit is contained in:
Vahagn Khachatryan
2019-01-20 21:32:24 +00:00
parent e7e723f1cf
commit 7550b71f4d
2 changed files with 62 additions and 55 deletions

View File

@@ -8,40 +8,6 @@ var xmlSerializer = new XMLSerializer();
var domParser = new DOMParser();
var generatedBufferType = 'blob'
function Utf8ArrayToStr(array) {
var out, i, len, c;
var char2, char3;
out = "";
len = array.length;
i = 0;
while(i < len) {
c = array[i++];
switch(c >> 4)
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
}
class EpubXhtml{
constructor(filename) {
this.filename = filename

View File

@@ -1,3 +1,5 @@
var concurrency = 5
function getCurrentTab() {
console.debug("Querying active tab.");
var queryInfo = {
@@ -38,7 +40,7 @@ function extractBookId(url){
class Book{
constructor(book_id, epub) {
constructor(book_id, page) {
this.book_id = book_id
this.raw_book = {}
this.chapter_list = []
@@ -47,6 +49,7 @@ class Book{
this.book_info = null
this.book_toc = null
this.book_flattoc = null
this.page = page
}
downloadResource(url){
@@ -54,10 +57,16 @@ class Book{
return fetch(url, {
credentials: 'include',
mode: "no-cors" // no-cors, cors, *same-origin
}).then((res) => {
// console.log(`Downloaded.`)
return res;
}, onError)
})
.catch((error)=>{
console.info(`Failed ${url} - ${error.message}`)
console.info(`Retry ${url}`)
// If failed to download, try second time.
return fetch(url, {
credentials: 'include',
mode: "no-cors" // no-cors, cors, *same-origin
})
})
}
downloadJson(url){
@@ -95,13 +104,19 @@ class Book{
}))
downloads.push(
Promise.map(this.book_info.chapters, (chapter) => {
renderProgress(`${chapter}`)
let item_id = this.page.renderProgress(`${chapter}`)
return this.downloadJson(chapter)
.then((json) => {
this.chapter_info[chapter] = json
return this.extractChapterAssets(json)
})
},{concurrency: 10}))
.then(()=>{
this.page.renderDone(item_id)
})
.catch((error)=>{
this.page.renderFail(item_id, error)
})
},{concurrency: concurrency}))
return Promise.all(downloads)
}
@@ -131,7 +146,7 @@ class Book{
downloadContent(){
return Promise.map(Object.keys(this.book_files), (url) => {
renderProgress(`${this.book_files[url].filename}`)
let item_id = this.page.renderProgress(`${this.book_files[url].filename}`)
return this.downloadResource(url)
.then((res) => {
if (res.ok){
@@ -142,7 +157,14 @@ class Book{
})
}
})
},{concurrency: 10})
.then((arrBuffer)=>{
this.page.renderDone(item_id)
return arrBuffer
})
.catch((error)=>{
this.page.renderFail(item_id, error)
})
},{concurrency: concurrency})
}
insertBookFile(url, filename){
@@ -163,6 +185,7 @@ class SidebarPage{
$('#error-message').hide();
$('#book-info').hide();
$("#book-file-list").empty();
this.id = 1
}
renderInfo(book){
@@ -187,6 +210,31 @@ class SidebarPage{
}
$('#loading').hide();
}
renderProgress(txt){
let id = `item_${this.id}`
this.id++
// Add chapters to UI
var progress_dom = $("<li></li>")
.addClass("list-group-item")
.attr('id', id)
.html(txt)
$("#book-file-list").prepend(progress_dom);
return id
}
renderDone(id){
$( '#'+id )
.css( "color", "green" )
.append(' - OK');
}
renderFail(id, reason){
$( '#'+id )
.css( "color", "red" )
.append(' - Fail')
.attr('title',reason.message);
}
}
function onError(error) {
@@ -197,13 +245,6 @@ function onError(error) {
$('#book-info').hide();
}
function renderProgress(txt){
// Add chapters to UI
var progress_dom = $("<li></li>")
.addClass("list-group-item")
.html(txt)
$("#book-file-list").prepend(progress_dom);
}
function fillMetadata(epub, book)
@@ -322,7 +363,7 @@ function onDownloadBookClicked(){
.then(extractBookId, onError)
.then((book_id) => {
epub = new EpubWriter();
book = new Book(book_id);
book = new Book(book_id, page);
book.downloadBookInfo()
.then(() => { page.renderInfo(book); })
// .then(() => { return book.downloadChapterList(); })
@@ -338,7 +379,7 @@ function onDownloadBookClicked(){
+ title.replace(/[^a-z0-9]/gi, '_').toLowerCase()
+ ".epub"
console.log(`Zip file name ${filename}`)
renderProgress(`Saved to ${filename}`)
page.renderProgress(`Saved to ${filename}`)
let url = window.URL.createObjectURL(file)
return browser.downloads.download({ "filename" : filename, url : url})
})