Skip to content
Snippets Groups Projects
Verified Commit 402ab91e authored by Torsten Grote's avatar Torsten Grote
Browse files

Delete uploaded temp file for all exceptions

and rethrow exception
parent 85376dab
No related branches found
No related tags found
1 merge request!86Limit the size of uploaded files
...@@ -38,6 +38,8 @@ import org.briarproject.mailbox.core.setup.WipeManager ...@@ -38,6 +38,8 @@ import org.briarproject.mailbox.core.setup.WipeManager
import org.briarproject.mailbox.core.system.InvalidIdException import org.briarproject.mailbox.core.system.InvalidIdException
import org.briarproject.mailbox.core.system.RandomIdManager import org.briarproject.mailbox.core.system.RandomIdManager
import org.slf4j.LoggerFactory.getLogger import org.slf4j.LoggerFactory.getLogger
import java.io.InputStream
import java.io.OutputStream
import javax.inject.Inject import javax.inject.Inject
private val LOG = getLogger(FileManager::class.java) private val LOG = getLogger(FileManager::class.java)
...@@ -98,19 +100,12 @@ class FileRouteManager @Inject constructor( ...@@ -98,19 +100,12 @@ class FileRouteManager @Inject constructor(
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val tmpFile = fileProvider.getTemporaryFile(fileId) val tmpFile = fileProvider.getTemporaryFile(fileId)
tmpFile.outputStream().use { outputStream -> tmpFile.outputStream().use { outputStream ->
@Suppress("BlockingMethodInNonBlockingContext")
call.receiveStream().use { inputStream -> call.receiveStream().use { inputStream ->
var bytesCopied: Long = 0 try {
val buffer = ByteArray(DEFAULT_BUFFER_SIZE) copyFile(inputStream, outputStream)
var bytes = inputStream.read(buffer) } catch (e: Exception) {
while (bytes >= 0) { tmpFile.delete()
outputStream.write(buffer, 0, bytes) throw e
bytesCopied += bytes
if (bytesCopied > MAX_FILE_SIZE) {
tmpFile.delete()
throw BadRequestException("File larger than allowed.")
}
bytes = inputStream.read(buffer)
} }
} }
} }
...@@ -121,6 +116,20 @@ class FileRouteManager @Inject constructor( ...@@ -121,6 +116,20 @@ class FileRouteManager @Inject constructor(
call.respond(HttpStatusCode.OK) call.respond(HttpStatusCode.OK)
} }
private fun copyFile(inputStream: InputStream, outputStream: OutputStream) {
var bytesCopied: Long = 0
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var bytes = inputStream.read(buffer)
while (bytes >= 0) {
outputStream.write(buffer, 0, bytes)
bytesCopied += bytes
if (bytesCopied > MAX_FILE_SIZE) {
throw BadRequestException("File larger than allowed.")
}
bytes = inputStream.read(buffer)
}
}
/** /**
* Used by owner and contacts to list their files to retrieve. * Used by owner and contacts to list their files to retrieve.
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment