Skip to content

Commit

Permalink
fix url redirect issue when saving images
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao committed Apr 17, 2021
1 parent d43894d commit 109fff6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 47 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
//applicationId "info.plateaukao.einkbro.debug"
minSdkVersion 23
targetSdkVersion 30
versionCode 870
versionName "8.7.0"
versionCode 871
versionName "8.7.1"
vectorDrawables.useSupportLibrary = true
}

Expand Down
16 changes: 1 addition & 15 deletions app/src/main/assets/MozReadability.js
Original file line number Diff line number Diff line change
Expand Up @@ -2354,18 +2354,4 @@ function getReadingTime(length, lang = "en") {
}

return "";
}

/*
var documentClone = document.cloneNode(true);
var article = new Readability(documentClone, {classesToPreserve: preservedClasses}).parse();
var innerHTMLCache = document.body.innerHTML;
article.readingTime = getReadingTime(article.length, document.lang);
document.body.outerHTML = createHtmlBody(article)
// change font type
var bodyClasses = document.body.classList;
bodyClasses.add("serif");
*/
}
61 changes: 31 additions & 30 deletions app/src/main/java/de/baumann/browser/epub/EpubManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ package de.baumann.browser.epub

import android.content.Context
import android.net.Uri
import androidx.lifecycle.lifecycleScope
import de.baumann.browser.util.Constants
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import nl.siegmann.epublib.domain.Author
import nl.siegmann.epublib.domain.Book
import nl.siegmann.epublib.domain.MediaType
import nl.siegmann.epublib.domain.Resource
import nl.siegmann.epublib.epub.EpubReader
import nl.siegmann.epublib.epub.EpubWriter
Expand All @@ -24,22 +19,6 @@ import java.net.URL

class EpubManager(private val context: Context) {

private fun createBook(domain: String, bookName: String): Book = Book().apply {
metadata.addTitle(bookName)
metadata.addAuthor(Author(domain, "EinkBro App"))
}

private fun openBook(uri: Uri): Book {
try {
val epubInputStream: InputStream = context.contentResolver.openInputStream(uri)
?: return createBook("", "EinkBro")

return EpubReader().readEpub(epubInputStream)
} catch(e: IOException) {
return createBook("", "EinkBro")
}
}

suspend fun saveEpub(
isNew: Boolean,
fileUri: Uri,
Expand All @@ -61,14 +40,28 @@ class EpubManager(private val context: Context) {

book.addSection(chapterName, Resource(processedHtml.byteInputStream(), chapterFileName))

//if(isNew) {
saveImageResources(book, imageMap)
//}
saveImageResources(book, imageMap)

saveBook(book, fileUri)
doneAction.invoke()
}

private fun createBook(domain: String, bookName: String): Book = Book().apply {
metadata.addTitle(bookName)
metadata.addAuthor(Author(domain, "EinkBro App"))
}

private fun openBook(uri: Uri): Book {
try {
val epubInputStream: InputStream = context.contentResolver.openInputStream(uri)
?: return createBook("", "EinkBro")

return EpubReader().readEpub(epubInputStream)
} catch (e: IOException) {
return createBook("", "EinkBro")
}
}

private fun saveBook(book: Book, uri: Uri) {
try {
val outputStream = context.contentResolver.openOutputStream(uri)
Expand Down Expand Up @@ -103,19 +96,27 @@ class EpubManager(private val context: Context) {
}

private suspend fun getResourceFromUrl(url: String): ByteArray {
return withContext(Dispatchers.IO) {
var byteArray: ByteArray = "".toByteArray()
withContext(Dispatchers.IO) {
try {
val connection: HttpURLConnection = URL(url).openConnection() as HttpURLConnection
connection.doInput = true
connection.addRequestProperty("User-Agent", "Mozilla/4.76")
connection.connect()
val bytes = connection.inputStream.readBytes()
connection.inputStream.close()
bytes
if (connection.responseCode != HttpURLConnection.HTTP_OK) {
if (isRedirect(connection.responseCode)) {
val redirectUrl = connection.getHeaderField("Location")
byteArray = getResourceFromUrl(redirectUrl)
}
} else {
byteArray = connection.inputStream.readBytes()
connection.inputStream.close()
}
} catch (e: IOException) {
e.printStackTrace()
"".toByteArray()
}
}
return byteArray
}

private fun isRedirect(responseCode: Int): Boolean = responseCode in 301..399
}

0 comments on commit 109fff6

Please sign in to comment.