Change length to Long from Int: avoids potential overflow, Long seems to be used everywhere else

This commit is contained in:
davotoula
2025-10-25 23:54:56 +02:00
parent 2332623cde
commit d01f6a2178

View File

@@ -96,8 +96,6 @@ class BlossomUploader {
val myContentType = contentType ?: contentResolver.getType(uri) val myContentType = contentType ?: contentResolver.getType(uri)
val fileName = context.getFileName(uri) val fileName = context.getFileName(uri)
// Calculate hash and size by streaming the file in chunks
// to avoid loading the entire file into memory
val imageInputStreamForHash = contentResolver.openInputStream(uri) val imageInputStreamForHash = contentResolver.openInputStream(uri)
checkNotNull(imageInputStreamForHash) { "Can't open the image input stream" } checkNotNull(imageInputStreamForHash) { "Can't open the image input stream" }
@@ -107,22 +105,14 @@ class BlossomUploader {
} }
val imageInputStream = contentResolver.openInputStream(uri) val imageInputStream = contentResolver.openInputStream(uri)
checkNotNull(imageInputStream) { "Can't open the image input stream" } checkNotNull(imageInputStream) { "Can't open the image input stream" }
return imageInputStream.use { stream -> return imageInputStream.use { stream ->
// Validate file size to prevent overflow when converting to Int
val sizeInt =
streamInfo.size.let {
require(it <= Int.MAX_VALUE) {
"File too large: ${it / 1_048_576}MB exceeds maximum size of ${Int.MAX_VALUE / 1_048_576}MB"
}
it.toInt()
}
upload( upload(
stream, stream,
streamInfo.hash, streamInfo.hash,
sizeInt, streamInfo.size,
fileName, fileName,
myContentType, myContentType,
alt, alt,
@@ -143,7 +133,7 @@ class BlossomUploader {
suspend fun upload( suspend fun upload(
inputStream: InputStream, inputStream: InputStream,
hash: HexKey, hash: HexKey,
length: Int, length: Long,
baseFileName: String?, baseFileName: String?,
contentType: String?, contentType: String?,
alt: String?, alt: String?,
@@ -168,14 +158,14 @@ class BlossomUploader {
object : RequestBody() { object : RequestBody() {
override fun contentType() = contentType?.toMediaType() override fun contentType() = contentType?.toMediaType()
override fun contentLength() = length.toLong() override fun contentLength() = length
override fun writeTo(sink: BufferedSink) { override fun writeTo(sink: BufferedSink) {
inputStream.source().use(sink::writeAll) inputStream.source().use(sink::writeAll)
} }
} }
httpAuth(hash, length.toLong(), alt?.let { "Uploading $it" } ?: "Uploading $fileName")?.let { httpAuth(hash, length, alt?.let { "Uploading $it" } ?: "Uploading $fileName")?.let {
requestBuilder.addHeader("Authorization", encodeAuth(it)) requestBuilder.addHeader("Authorization", encodeAuth(it))
} }