Skip to content

Commit

Permalink
fix: Prevent OutOfMemoryException when uploading files
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewWestberg committed Nov 14, 2024
1 parent 19d8209 commit 3157cb2
Showing 1 changed file with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ import io.ktor.http.isSuccess
import io.ktor.http.quote
import io.ktor.server.application.ApplicationEnvironment
import io.ktor.utils.io.core.isNotEmpty
import io.ktor.utils.io.core.readBytes
import io.ktor.utils.io.core.toByteArray
import io.ktor.utils.io.core.use
import io.ktor.utils.io.streams.asInput
import io.ktor.utils.io.streams.writePacket
import io.newm.chain.util.toB64String
import io.newm.server.config.repo.ConfigRepository
import io.newm.server.config.repo.ConfigRepository.Companion.CONFIG_KEY_EVEARA_CLIENT_ID
Expand Down Expand Up @@ -1990,17 +1991,18 @@ class EvearaDistributionRepositoryImpl(
throw audioFileResponse.status.toException("Error downloading url: $url")
}

val trackFile =
withContext(Dispatchers.IO) {
File.createTempFile("newm_track_", url.getFileNameWithExtensionFromUrl())
}
val channel = audioFileResponse.bodyAsChannel()
while (!channel.isClosedForRead) {
val packet = channel.readRemaining(DEFAULT_BUFFER_SIZE.toLong())
while (packet.isNotEmpty) {
val bytes = packet.readBytes()
trackFile.appendBytes(bytes)
val trackFile = withContext(Dispatchers.IO) {
val tempTrackFile = File.createTempFile("newm_track_", url.getFileNameWithExtensionFromUrl())
tempTrackFile.outputStream().use { trackFileStream ->
val channel = audioFileResponse.bodyAsChannel()
while (!channel.isClosedForRead) {
val packet = channel.readRemaining(DEFAULT_BUFFER_SIZE.toLong())
if (packet.isNotEmpty) {
trackFileStream.writePacket(packet)
}
}
}
tempTrackFile
}
log.info { "Downloaded track ${mutableSong.title} to ${trackFile.absolutePath} having size ${trackFile.length()}" }
val response = addTrack(user, trackFile)
Expand Down

0 comments on commit 3157cb2

Please sign in to comment.