mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-11-10 21:26:41 +01:00
Change length to Long from Int: avoids potential overflow, Long seems to be used everywhere else
This commit is contained in:
@@ -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))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user