Skip to content

Commit

Permalink
画像を変換した差異、変換後のフォーマットに合ったファイル拡張子でサーバに送る
Browse files Browse the repository at this point in the history
  • Loading branch information
tateisu committed Jul 20, 2023
1 parent 7d977dc commit 661f7cc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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にする
Expand Down Expand Up @@ -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 にフォールバック
Expand All @@ -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
Expand All @@ -255,14 +257,15 @@ class AttachmentRequest(
* 失敗したら例外を投げる
*/
private fun Bitmap.compressToTempFileOpener(
fixExt: String?,
outMimeType: String,
format: Bitmap.CompressFormat,
quality: Int,
): InputStreamOpener {
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
Expand Down Expand Up @@ -362,6 +365,10 @@ class AttachmentRequest(
else -> "video/mp4"
},
isImage = false,
fixExt = when (result) {
tempFile -> null
else -> "mp4"
},
)
} finally {
if (outFile != resultFile) outFile.delete()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand All @@ -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
}

///////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 661f7cc

Please sign in to comment.