Skip to content

Commit

Permalink
Move getFileSha1() to UniFileExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
xb2016 committed Mar 25, 2024
1 parent 0676889 commit 2fccac1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
17 changes: 3 additions & 14 deletions app/src/main/java/com/hippo/ehviewer/client/EhTagDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import com.hippo.ehviewer.EhApplication
import com.hippo.ehviewer.EhApplication.Companion.nonCacheOkHttpClient
import com.hippo.ehviewer.R
import com.hippo.ehviewer.Settings
import com.hippo.unifile.UniFile
import com.hippo.unifile.sha1
import com.hippo.yorozuya.FileUtils
import com.hippo.yorozuya.copyToFile
import kotlinx.coroutines.flow.Flow
Expand All @@ -30,8 +32,6 @@ import kotlinx.coroutines.sync.withLock
import okhttp3.Request
import okhttp3.executeAsync
import okio.BufferedSource
import okio.HashingSink.Companion.sha1
import okio.blackholeSink
import okio.buffer
import okio.source
import org.json.JSONException
Expand Down Expand Up @@ -208,19 +208,8 @@ object EhTagDatabase {
}.getOrNull()
}

private fun getFileSha1(file: File): String? {
return runCatching {
file.source().buffer().use { source ->
sha1(blackholeSink()).use {
source.readAll(it)
it.hash.hex()
}
}
}.getOrNull()
}

private fun checkData(sha1: String?, data: File): Boolean {
return sha1 != null && sha1 == getFileSha1(data)
return sha1 != null && sha1 == UniFile.fromFile(data)?.sha1()
}

private suspend fun save(url: String, file: File): Boolean {
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/hippo/ehviewer/client/EhUrl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ object EhUrl {
const val URL_FORUMS = "https://forums.e-hentai.org/"
const val URL_SIGN_IN = URL_FORUMS + "index.php?act=Login"
const val URL_REGISTER = URL_FORUMS + "index.php?act=Reg&CODE=00"
const val API_SIGN_IN = "$URL_SIGN_IN&CODE=01"
const val URL_FUNDS = HOST_E + "exchange.php?t=gp"
const val URL_HOME = HOST_E + "home.php"
const val URL_NEWS = HOST_E + "news.php"
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/com/hippo/unifile/UniFileExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ package com.hippo.unifile

import android.os.ParcelFileDescriptor.AutoCloseInputStream
import android.os.ParcelFileDescriptor.AutoCloseOutputStream
import okio.HashingSink
import okio.blackholeSink
import okio.buffer
import okio.source
import java.io.FileInputStream
import java.io.FileOutputStream

Expand All @@ -35,3 +39,15 @@ fun UniFile.openInputStream(): FileInputStream {
fun UniFile.openOutputStream(): FileOutputStream {
return AutoCloseOutputStream(openFileDescriptor("w"))
}

fun UniFile.sha1() = runCatching {
openInputStream().source().buffer().use { source ->
HashingSink.sha1(blackholeSink()).use {
source.readAll(it)
it.hash.hex()
}
}
}.getOrElse {
it.printStackTrace()
null
}

0 comments on commit 2fccac1

Please sign in to comment.