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