Skip to content

Commit

Permalink
Fixed issue with contentLength (#2742)
Browse files Browse the repository at this point in the history
### What's done:
- moved checking of error to controller
- fixed setting contentLength
  • Loading branch information
nulls authored Oct 18, 2023
1 parent b6ae0c9 commit bfdb7f8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.saveourtool.save.storage.concatS3Key
import com.saveourtool.save.utils.*
import com.saveourtool.save.v1
import org.reactivestreams.Publisher
import org.springframework.dao.DataIntegrityViolationException
import org.springframework.data.domain.PageRequest
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
Expand All @@ -21,9 +22,11 @@ import org.springframework.http.ResponseEntity
import org.springframework.http.codec.multipart.FilePart
import org.springframework.security.core.Authentication
import org.springframework.web.bind.annotation.*
import org.springframework.web.server.ResponseStatusException
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import reactor.core.scheduler.Schedulers
import java.nio.ByteBuffer
import java.nio.file.Files
import kotlin.io.path.*

Expand Down Expand Up @@ -115,7 +118,7 @@ class RawCosvFileController(
contentLength = contentLength,
)
val content = filePart.content().map { it.asByteBuffer() }
return rawCosvFileStorage.upload(key, content)
return rawCosvFileStorage.uploadAndWrapDuplicateKeyException(key, content)
}

/**
Expand Down Expand Up @@ -183,7 +186,7 @@ class RawCosvFileController(
"Processing ${file.absolutePathString()}"
}
val contentLength = file.fileSize()
rawCosvFileStorage.upload(
rawCosvFileStorage.uploadAndWrapDuplicateKeyException(
key = RawCosvFileDto(
concatS3Key(archiveFile.fileName, file.relativeTo(contentDir).toString()),
organizationName = organizationName,
Expand Down Expand Up @@ -414,5 +417,20 @@ class RawCosvFileController(

// to show progress bar
private val firstFakeResponse = UnzipRawCosvFileResponse(5, 100, updateCounters = true)

private fun RawCosvFileStorage.uploadAndWrapDuplicateKeyException(
key: RawCosvFileDto,
content: Flux<ByteBuffer>,
): Mono<RawCosvFileDto> {
val result = key.contentLength?.let {
upload(key, it, content)
} ?: upload(key, content)
return result.onErrorResume { error ->
when (error) {
is DataIntegrityViolationException -> Mono.error(ResponseStatusException(HttpStatus.BAD_REQUEST, "Duplicate file name ${key.fileName}", error))
else -> Mono.error(error)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,15 @@ class RawCosvFileS3KeyManager(
}

@Transactional
override fun updateKeyByContentLength(key: RawCosvFileDto, contentLength: Long): RawCosvFileDto =
key.contentLength?.let {
repository.getByIdOrNotFound(key.requiredId())
.let { entity ->
repository.save(entity.apply { this.contentLength = contentLength }).toDto()
}
} ?: key
override fun updateKeyByContentLength(
key: RawCosvFileDto,
contentLength: Long,
): RawCosvFileDto = key.contentLength
?.let { key }
?: run {
repository.getByIdOrNotFound(key.requiredId())
.let { entity ->
repository.save(entity.apply { this.contentLength = contentLength }).toDto()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import com.saveourtool.save.storage.DefaultStorageProjectReactor
import com.saveourtool.save.storage.ReactiveStorageWithDatabase
import com.saveourtool.save.storage.deleteUnexpectedKeys
import com.saveourtool.save.utils.*
import org.springframework.dao.DataIntegrityViolationException
import org.springframework.data.domain.PageRequest
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Component
import org.springframework.web.server.ResponseStatusException
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import java.nio.ByteBuffer
Expand Down Expand Up @@ -54,18 +51,6 @@ class RawCosvFileStorage(
}
.publishOn(s3Operations.scheduler)

override fun upload(key: RawCosvFileDto, content: Flux<ByteBuffer>): Mono<RawCosvFileDto> {
val result = key.contentLength?.let {
upload(key, it, content)
} ?: super.upload(key, content)
return result.onErrorResume { error ->
when (error) {
is DataIntegrityViolationException -> Mono.error(ResponseStatusException(HttpStatus.BAD_REQUEST, "Duplicate file name ${key.fileName}", error))
else -> Mono.error(error)
}
}
}

/**
* @param organizationName
* @param userName
Expand Down

0 comments on commit bfdb7f8

Please sign in to comment.