NicovideoSeiga: Fix file type handling. (#10352)

When downloading NicovideoSeiga image, the saved file extension has become wrong type. This commit addresses the issue by fixing following errors.
- Images is determined the type by file header. The logic has wrongly assumed Byte.toInt() returns unsigned integer, but it actually returns signed value.
- The server returns application/octet-stream as Content-Type header. But the logic only worked when header is empty.
This commit is contained in:
Oo8Ez5ai 2022-01-05 21:11:25 +09:00 committed by GitHub
parent 5c2d385531
commit 05addfb95e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 6 deletions

View File

@ -6,7 +6,7 @@ ext {
extName = 'Nicovideo Seiga'
pkgNameSuffix = 'ja.nicovideoseiga'
extClass = '.NicovideoSeiga'
extVersionCode = 1
extVersionCode = 2
}
apply from: "$rootDir/common.gradle"

View File

@ -238,8 +238,7 @@ class NicovideoSeiga : HttpSource() {
val decryptedImage = decryptImage(key, encryptedImage)
// Construct a new response
val contentType = response.header("Content-Type", "image/${getImageType(decryptedImage)}")
val body = decryptedImage.toResponseBody(contentType!!.toMediaTypeOrNull())
val body = decryptedImage.toResponseBody("image/${getImageType(decryptedImage)}".toMediaTypeOrNull())
return response.newBuilder().body(body).build()
}
@ -272,11 +271,11 @@ class NicovideoSeiga : HttpSource() {
* This is also how Nicovideo does it
*/
private fun getImageType(image: ByteArray): String {
return if (image[0].toInt() == 0xff && image[1].toInt() == 0xd8 && image[image.size - 2].toInt() == 0xff && image[image.size - 1].toInt() == 0xd9) {
return if (image[0].toInt() == -1 && image[1].toInt() == -40 && image[image.size - 2].toInt() == -1 && image[image.size - 1].toInt() == -39) {
"jpeg"
} else if (image[0].toInt() == 0x89 && image[1].toInt() == 0x50 && image[2].toInt() == 0x4e && image[3].toInt() == 0x47) {
} else if (image[0].toInt() == -119 && image[1].toInt() == 80 && image[2].toInt() == 78 && image[3].toInt() == 71) {
"png"
} else if (image[0].toInt() == 0x47 && image[1].toInt() == 0x49 && image[2].toInt() == 0x46 && image[3].toInt() == 0x38) {
} else if (image[0].toInt() == 71 && image[1].toInt() == 73 && image[2].toInt() == 70 && image[3].toInt() == 56) {
"gif"
} else {
// It defaults to null in the site, but it's a webp image