Refactor SpiderDen
This commit is contained in:
1 parent
54c8183666
commit
487925b1f2
2 files changed
+56
-163
No files matched your search
@@ -27,24 +27,20 @@ import com.hippo.ehviewer.client.EhUtils.getSuitableTitle
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.gallery.PageLoader2
|
||||
import com.hippo.image.Image.ByteBufferSource
|
||||
import com.hippo.io.UniFileInputStreamPipe
|
||||
import com.hippo.streampipe.InputStreamPipe
|
||||
import com.hippo.unifile.RawFile
|
||||
import com.hippo.unifile.UniFile
|
||||
import com.hippo.yorozuya.FileUtils
|
||||
import com.hippo.yorozuya.IOUtils
|
||||
import com.hippo.yorozuya.MathUtils
|
||||
import com.hippo.yorozuya.Utilities
|
||||
import okhttp3.ResponseBody
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.io.RandomAccessFile
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.channels.FileChannel
|
||||
import java.util.Locale
|
||||
import kotlin.io.path.readText
|
||||
|
||||
class SpiderDen(private val mGalleryInfo: GalleryInfo) {
|
||||
private val mGid: Long = mGalleryInfo.gid
|
||||
@@ -117,7 +113,7 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
|
||||
val file = dir.createFile(generateImageFilename(index, extension)) ?: return false
|
||||
(file.openOutputStream() as FileOutputStream).use { outputStream ->
|
||||
outputStream.channel.use { outChannel ->
|
||||
FileInputStream(snapshot.data.toFile()).use { inputStream ->
|
||||
snapshot.data.toFile().inputStream().use { inputStream ->
|
||||
inputStream.channel.use {
|
||||
outChannel.transferFrom(it, 0, it.size())
|
||||
}
|
||||
@@ -210,7 +206,7 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
|
||||
var received: Long = 0
|
||||
runCatching {
|
||||
editor.metadata.toFile().writeText(extension)
|
||||
FileOutputStream(editor.data.toFile()).use { outputStream ->
|
||||
editor.data.toFile().outputStream().use { outputStream ->
|
||||
outputStream.channel.use { received = doSave(it) }
|
||||
}
|
||||
}.onFailure {
|
||||
@@ -226,61 +222,56 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
|
||||
return -1
|
||||
}
|
||||
|
||||
private fun openCacheInputStreamPipe(index: Int): InputStreamPipe? {
|
||||
val key = EhCacheKeyFactory.getImageKey(mGid, index)
|
||||
val snapshot = sCache[key] ?: return null
|
||||
return object : InputStreamPipe {
|
||||
private var `is`: InputStream? = null
|
||||
override fun release() {
|
||||
snapshot.close()
|
||||
}
|
||||
fun saveToUniFile(index: Int, file: UniFile): Boolean {
|
||||
(file.openOutputStream() as FileOutputStream).use { outputStream ->
|
||||
outputStream.channel.use { outChannel ->
|
||||
val key = EhCacheKeyFactory.getImageKey(mGid, index)
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun open(): InputStream {
|
||||
if (`is` != null) IOUtils.closeQuietly(`is`)
|
||||
`is` = FileInputStream(snapshot.data.toFile())
|
||||
return `is`!!
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
if (`is` != null) IOUtils.closeQuietly(`is`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun openDownloadInputStreamPipe(index: Int): InputStreamPipe? {
|
||||
val dir = downloadDir ?: return null
|
||||
for (i in 0..1) {
|
||||
val file = findImageFile(dir, index)
|
||||
if (file != null) {
|
||||
return UniFileInputStreamPipe(file)
|
||||
} else if (!copyFromCacheToDownloadDir(index)) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun openInputStreamPipe(index: Int): InputStreamPipe? {
|
||||
return when (mMode) {
|
||||
SpiderQueen.MODE_READ -> {
|
||||
var pipe = openCacheInputStreamPipe(index)
|
||||
if (pipe == null) {
|
||||
pipe = openDownloadInputStreamPipe(index)
|
||||
fun copy(inputStream: FileInputStream) {
|
||||
inputStream.channel.use {
|
||||
outChannel.transferFrom(it, 0, it.size())
|
||||
}
|
||||
}
|
||||
pipe
|
||||
}
|
||||
|
||||
SpiderQueen.MODE_DOWNLOAD -> {
|
||||
openDownloadInputStreamPipe(index)
|
||||
}
|
||||
// Read from diskCache first
|
||||
sCache[key]?.use { snapshot ->
|
||||
runCatching {
|
||||
snapshot.data.toFile().inputStream().use { copy(it) }
|
||||
}.onSuccess {
|
||||
return true
|
||||
}.onFailure {
|
||||
it.printStackTrace()
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
null
|
||||
downloadDir?.let { uniFile ->
|
||||
runCatching {
|
||||
findImageFile(uniFile, index)?.openInputStream()?.use {
|
||||
copy(it as FileInputStream)
|
||||
}
|
||||
}.onFailure {
|
||||
it.printStackTrace()
|
||||
return false
|
||||
}.onSuccess {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun checkPlainText(index: Int): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
fun getExtension(index: Int): String? {
|
||||
val key = EhCacheKeyFactory.getImageKey(mGid, index)
|
||||
return sCache[key]?.use { it.metadata.toNioPath().readText() }
|
||||
?: downloadDir?.let { findImageFile(it, index) }?.name.let { FileUtils.getExtensionFromFilename(it) }
|
||||
}
|
||||
|
||||
fun getImageSource(index: Int): ByteBufferSource? {
|
||||
if (mMode == SpiderQueen.MODE_READ) {
|
||||
val key = EhCacheKeyFactory.getImageKey(mGid, index)
|
||||
|
||||
@@ -16,11 +16,9 @@
|
||||
|
||||
package com.hippo.ehviewer.spider;
|
||||
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.Process;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import androidx.annotation.IntDef;
|
||||
import androidx.annotation.NonNull;
|
||||
@@ -45,20 +43,15 @@ import com.hippo.ehviewer.client.parser.GalleryPageApiParser;
|
||||
import com.hippo.ehviewer.client.parser.GalleryPageParser;
|
||||
import com.hippo.ehviewer.client.parser.GalleryPageUrlParser;
|
||||
import com.hippo.image.Image;
|
||||
import com.hippo.streampipe.InputStreamPipe;
|
||||
import com.hippo.unifile.UniFile;
|
||||
import com.hippo.util.ExceptionUtils;
|
||||
import com.hippo.yorozuya.IOUtils;
|
||||
import com.hippo.yorozuya.MathUtils;
|
||||
import com.hippo.yorozuya.OSUtils;
|
||||
import com.hippo.yorozuya.StringUtils;
|
||||
import com.hippo.yorozuya.thread.PriorityThread;
|
||||
import com.hippo.yorozuya.thread.PriorityThreadFactory;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.ArrayList;
|
||||
@@ -563,23 +556,7 @@ public final class SpiderQueen implements Runnable {
|
||||
return false;
|
||||
}
|
||||
|
||||
InputStreamPipe pipe = mSpiderDen.openInputStreamPipe(index);
|
||||
if (null == pipe) {
|
||||
return false;
|
||||
}
|
||||
|
||||
OutputStream os = null;
|
||||
try {
|
||||
os = file.openOutputStream();
|
||||
IOUtils.copy(pipe.open(), os);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
} finally {
|
||||
pipe.close();
|
||||
pipe.release();
|
||||
IOUtils.closeQuietly(os);
|
||||
}
|
||||
return mSpiderDen.saveToUniFile(index, file);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -589,35 +566,13 @@ public final class SpiderQueen implements Runnable {
|
||||
return null;
|
||||
}
|
||||
|
||||
InputStreamPipe pipe = mSpiderDen.openInputStreamPipe(index);
|
||||
if (null == pipe) {
|
||||
var ext = mSpiderDen.getExtension(index);
|
||||
UniFile dst = dir.subFile(null != ext ? filename + "." + ext : filename);
|
||||
if (null == dst) {
|
||||
return null;
|
||||
}
|
||||
|
||||
OutputStream os = null;
|
||||
try {
|
||||
// Get dst file
|
||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inJustDecodeBounds = true;
|
||||
BitmapFactory.decodeStream(pipe.open(), null, options);
|
||||
pipe.close();
|
||||
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(options.outMimeType);
|
||||
UniFile dst = dir.subFile(null != extension ? filename + "." + extension : filename);
|
||||
if (null == dst) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Copy
|
||||
os = dst.openOutputStream();
|
||||
IOUtils.copy(pipe.open(), os);
|
||||
return dst;
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
} finally {
|
||||
pipe.close();
|
||||
pipe.release();
|
||||
IOUtils.closeQuietly(os);
|
||||
}
|
||||
if (!mSpiderDen.saveToUniFile(index, dst)) return null;
|
||||
return dst;
|
||||
}
|
||||
|
||||
|
||||
@@ -628,25 +583,7 @@ public final class SpiderQueen implements Runnable {
|
||||
return null;
|
||||
}
|
||||
|
||||
InputStreamPipe pipe = mSpiderDen.openInputStreamPipe(index);
|
||||
if (null == pipe) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
// Get dst file
|
||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inJustDecodeBounds = true;
|
||||
BitmapFactory.decodeStream(pipe.open(), null, options);
|
||||
pipe.close();
|
||||
|
||||
return MimeTypeMap.getSingleton().getExtensionFromMimeType(options.outMimeType);
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
} finally {
|
||||
pipe.close();
|
||||
pipe.release();
|
||||
}
|
||||
return mSpiderDen.getExtension(index);
|
||||
}
|
||||
|
||||
// Mark as suspend fun when kotlinize, need IO
|
||||
@@ -1248,45 +1185,10 @@ public final class SpiderQueen implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
InputStreamPipe isPipe = null;
|
||||
try {
|
||||
// Get InputStreamPipe
|
||||
isPipe = mSpiderDen.openInputStreamPipe(index);
|
||||
if (isPipe == null) {
|
||||
// Can't get pipe
|
||||
error = GetText.getString(R.string.error_reading_failed);
|
||||
break;
|
||||
}
|
||||
|
||||
// Check plain txt
|
||||
InputStream inputStream = new BufferedInputStream(isPipe.open());
|
||||
boolean isPlainTxt = true;
|
||||
int j = 0;
|
||||
for (; ; ) {
|
||||
int b = inputStream.read();
|
||||
if (b == -1) {
|
||||
break;
|
||||
}
|
||||
// Skip first three bytes
|
||||
if (j < 3) {
|
||||
j++;
|
||||
continue;
|
||||
}
|
||||
if (b > 126) {
|
||||
isPlainTxt = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isPlainTxt) {
|
||||
error = GetText.getString(R.string.error_reading_failed);
|
||||
forceHtml = true;
|
||||
continue;
|
||||
}
|
||||
} finally {
|
||||
if (isPipe != null) {
|
||||
isPipe.close();
|
||||
isPipe.release();
|
||||
}
|
||||
if (mSpiderDen.checkPlainText(index)) {
|
||||
error = GetText.getString(R.string.error_reading_failed);
|
||||
forceHtml = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check Stopped
|
||||
|
||||
Reference in new issue
Block a user