画像を変換した差異、変換後のフォーマットに合ったファイル拡張子でサーバに送る
This commit is contained in:
parent
7d977dc2b2
commit
661f7cc187
|
@ -176,7 +176,7 @@ class AttachmentRequest(
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
val canUseOriginal = when {
|
val canUseOriginal: Boolean = when {
|
||||||
// WebPを使っていい場合、PNG画像をWebPに変換したい
|
// WebPを使っていい場合、PNG画像をWebPに変換したい
|
||||||
canUseWebP && mimeType == MIME_TYPE_PNG -> false
|
canUseWebP && mimeType == MIME_TYPE_PNG -> false
|
||||||
// WebPを使わない場合、入力がWebPなら強制的にPNGかJPEGにする
|
// WebPを使わない場合、入力がWebPなら強制的にPNGかJPEGにする
|
||||||
|
@ -219,7 +219,7 @@ class AttachmentRequest(
|
||||||
@Suppress("DEPRECATION")
|
@Suppress("DEPRECATION")
|
||||||
Bitmap.CompressFormat.WEBP
|
Bitmap.CompressFormat.WEBP
|
||||||
}
|
}
|
||||||
return compressToTempFileOpener(MIME_TYPE_WEBP, format, 90)
|
return compressToTempFileOpener("webp", MIME_TYPE_WEBP, format, 90)
|
||||||
} catch (ex: Throwable) {
|
} catch (ex: Throwable) {
|
||||||
log.w(ex, "compress to WebP lossy failed.")
|
log.w(ex, "compress to WebP lossy failed.")
|
||||||
// 失敗したらJPEG or PNG にフォールバック
|
// 失敗したらJPEG or PNG にフォールバック
|
||||||
|
@ -234,12 +234,14 @@ class AttachmentRequest(
|
||||||
}
|
}
|
||||||
return when (hasAlpha) {
|
return when (hasAlpha) {
|
||||||
true -> compressToTempFileOpener(
|
true -> compressToTempFileOpener(
|
||||||
|
"png",
|
||||||
MIME_TYPE_PNG,
|
MIME_TYPE_PNG,
|
||||||
Bitmap.CompressFormat.PNG,
|
Bitmap.CompressFormat.PNG,
|
||||||
100
|
100
|
||||||
)
|
)
|
||||||
|
|
||||||
else -> compressToTempFileOpener(
|
else -> compressToTempFileOpener(
|
||||||
|
"jpg",
|
||||||
MIME_TYPE_JPEG,
|
MIME_TYPE_JPEG,
|
||||||
Bitmap.CompressFormat.JPEG,
|
Bitmap.CompressFormat.JPEG,
|
||||||
95
|
95
|
||||||
|
@ -255,6 +257,7 @@ class AttachmentRequest(
|
||||||
* 失敗したら例外を投げる
|
* 失敗したら例外を投げる
|
||||||
*/
|
*/
|
||||||
private fun Bitmap.compressToTempFileOpener(
|
private fun Bitmap.compressToTempFileOpener(
|
||||||
|
fixExt: String?,
|
||||||
outMimeType: String,
|
outMimeType: String,
|
||||||
format: Bitmap.CompressFormat,
|
format: Bitmap.CompressFormat,
|
||||||
quality: Int,
|
quality: Int,
|
||||||
|
@ -262,7 +265,7 @@ class AttachmentRequest(
|
||||||
val tempFile = context.generateTempFile("createResizedImageOpener")
|
val tempFile = context.generateTempFile("createResizedImageOpener")
|
||||||
try {
|
try {
|
||||||
FileOutputStream(tempFile).use { compress(format, quality, it) }
|
FileOutputStream(tempFile).use { compress(format, quality, it) }
|
||||||
return tempFileOpener(tempFile, outMimeType, isImage = true)
|
return tempFileOpener(tempFile, outMimeType, isImage = true, fixExt = fixExt)
|
||||||
} catch (ex: Throwable) {
|
} catch (ex: Throwable) {
|
||||||
tempFile.delete()
|
tempFile.delete()
|
||||||
throw ex
|
throw ex
|
||||||
|
@ -362,6 +365,10 @@ class AttachmentRequest(
|
||||||
else -> "video/mp4"
|
else -> "video/mp4"
|
||||||
},
|
},
|
||||||
isImage = false,
|
isImage = false,
|
||||||
|
fixExt = when (result) {
|
||||||
|
tempFile -> null
|
||||||
|
else -> "mp4"
|
||||||
|
},
|
||||||
)
|
)
|
||||||
} finally {
|
} finally {
|
||||||
if (outFile != resultFile) outFile.delete()
|
if (outFile != resultFile) outFile.delete()
|
||||||
|
|
|
@ -177,7 +177,10 @@ class AttachmentUploader(
|
||||||
error(safeContext.getString(R.string.mime_type_not_acceptable, opener.mimeType))
|
error(safeContext.getString(R.string.mime_type_not_acceptable, opener.mimeType))
|
||||||
}
|
}
|
||||||
|
|
||||||
val fileName = fixDocumentName(getDocumentName(safeContext.contentResolver, uri))
|
val fileName = fixDocumentName(
|
||||||
|
getDocumentName(safeContext.contentResolver, uri),
|
||||||
|
fixExt = opener.fixExt,
|
||||||
|
)
|
||||||
pa.progress = safeContext.getString(R.string.attachment_handling_uploading, 0)
|
pa.progress = safeContext.getString(R.string.attachment_handling_uploading, 0)
|
||||||
fun writeProgress(percent: Int) {
|
fun writeProgress(percent: Int) {
|
||||||
pa.progress = if (percent < 100) {
|
pa.progress = if (percent < 100) {
|
||||||
|
@ -289,7 +292,7 @@ class AttachmentUploader(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fixDocumentName(s: String): String {
|
private fun fixDocumentName(s: String, fixExt: String?): String {
|
||||||
val sLength = s.length
|
val sLength = s.length
|
||||||
val m = """([^\x20-\x7f])""".asciiPattern().matcher(s)
|
val m = """([^\x20-\x7f])""".asciiPattern().matcher(s)
|
||||||
m.reset()
|
m.reset()
|
||||||
|
@ -302,7 +305,11 @@ class AttachmentUploader(
|
||||||
lastEnd = m.end()
|
lastEnd = m.end()
|
||||||
}
|
}
|
||||||
if (lastEnd < sLength) sb.append(s.substring(lastEnd, sLength))
|
if (lastEnd < sLength) sb.append(s.substring(lastEnd, sLength))
|
||||||
return sb.toString()
|
var escaped = sb.toString()
|
||||||
|
if (!fixExt.isNullOrEmpty()) {
|
||||||
|
escaped = """\.[^./\\]*\z""".toRegex().replace(escaped, "") + ".${fixExt}"
|
||||||
|
}
|
||||||
|
return escaped
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////
|
||||||
|
@ -339,8 +346,10 @@ class AttachmentUploader(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
val fileName =
|
val fileName = fixDocumentName(
|
||||||
fixDocumentName(getDocumentName(safeContext.contentResolver, src.uri))
|
getDocumentName(safeContext.contentResolver, src.uri),
|
||||||
|
fixExt = opener.fixExt,
|
||||||
|
)
|
||||||
|
|
||||||
if (account.isMisskey) {
|
if (account.isMisskey) {
|
||||||
TootApiResult("custom thumbnail is not supported on misskey account.")
|
TootApiResult("custom thumbnail is not supported on misskey account.")
|
||||||
|
|
|
@ -16,6 +16,8 @@ abstract class InputStreamOpener {
|
||||||
abstract val mimeType: String
|
abstract val mimeType: String
|
||||||
abstract val isImage: Boolean
|
abstract val isImage: Boolean
|
||||||
|
|
||||||
|
open val fixExt :String? = null
|
||||||
|
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
abstract fun open(): InputStream
|
abstract fun open(): InputStream
|
||||||
|
|
||||||
|
@ -74,9 +76,11 @@ fun tempFileOpener(
|
||||||
file: File,
|
file: File,
|
||||||
mimeType: String,
|
mimeType: String,
|
||||||
isImage: Boolean,
|
isImage: Boolean,
|
||||||
|
fixExt:String? = null,
|
||||||
) = object : InputStreamOpener() {
|
) = object : InputStreamOpener() {
|
||||||
override val mimeType = mimeType
|
override val mimeType = mimeType
|
||||||
override val isImage = isImage
|
override val isImage = isImage
|
||||||
|
override val fixExt = fixExt
|
||||||
|
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
override fun open() = FileInputStream(file)
|
override fun open() = FileInputStream(file)
|
||||||
|
|
Loading…
Reference in New Issue