Skip to content

Commit

Permalink
feat: SVGの場合でもキャッシュできるようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
pantasystem committed Sep 8, 2024
1 parent 348a155 commit 83a538d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ private const val SVG_HEADER: Int = 0x3C737667
private const val SVG_HEADER_STARTS_WITH_XML = 0x3c3f786d
class SvgDecoder : ResourceDecoder<InputStream, SVG>{

companion object {
fun isSvg(source: InputStream): Boolean {
val buffer = ByteArray(8)
val cnt = source.read(buffer)
if (cnt < 8) {
Log.d("SvgDecoder", "svgではない")
return false
}

override fun handles(source: InputStream, options: Options): Boolean {
val buffer = ByteArray(8)
val cnt = source.read(buffer)
if (cnt < 8) {
Log.d("SvgDecoder", "svgではない")
return false
val header = ByteBuffer.wrap(buffer).int
return header == SVG_HEADER || header == SVG_HEADER_STARTS_WITH_XML
}
}

Log.d("SvgDecoder", "svgだった")
val header = ByteBuffer.wrap(buffer).int
return header == SVG_HEADER || header == SVG_HEADER_STARTS_WITH_XML
override fun handles(source: InputStream, options: Options): Boolean {
return isSvg(source)
}

override fun decode(
Expand Down
2 changes: 2 additions & 0 deletions modules/data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ dependencies {

testImplementation libs.robolectric

implementation 'com.caverock:androidsvg-aar:1.4'

}

kapt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package net.pantasystem.milktea.data.infrastructure.image

import android.content.Context
import android.graphics.BitmapFactory
import android.util.Log
import com.caverock.androidsvg.SVG
import com.caverock.androidsvg.SVGParseException
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
import kotlinx.datetime.Clock
import net.pantasystem.milktea.api.misskey.OkHttpClientProvider
import net.pantasystem.milktea.common.Hash
import net.pantasystem.milktea.common.glide.svg.SvgDecoder
import net.pantasystem.milktea.common.runCancellableCatching
import net.pantasystem.milktea.common_android.hilt.IODispatcher
import net.pantasystem.milktea.model.image.ImageCache
Expand Down Expand Up @@ -121,12 +125,28 @@ class ImageCacheRepositoryImpl @Inject constructor(
throw Exception("Download failed: url=$url")
}
}
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeFile(
file.absolutePath, options
) ?: return@use null to null
options.outWidth to options.outHeight

// check svg
val source = file.inputStream()
source.use {
if (SvgDecoder.isSvg(file.inputStream())) {
try {
val svg = SVG.getFromInputStream(source)
Log.d("ImageCache", "SVGのサイズ: width=${svg.documentWidth}, height=${svg.documentHeight}")
svg.documentWidth.toInt() to svg.documentHeight.toInt()
} catch (e: SVGParseException) {
Log.e("ImageCache", "SVGParseException", e)
throw e
}
} else {
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeFile(
file.absolutePath, options
) ?: return null to null
options.outWidth to options.outHeight
}
}
}
}

Expand Down

0 comments on commit 83a538d

Please sign in to comment.