From bfdb7f8069b5960d793365a969d6f578f50d4ce5 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 18 Oct 2023 10:59:04 +0300 Subject: [PATCH] Fixed issue with contentLength (#2742) ### What's done: - moved checking of error to controller - fixed setting contentLength --- .../cosv/controllers/RawCosvFileController.kt | 22 +++++++++++++++++-- .../cosv/storage/RawCosvFileS3KeyManager.kt | 18 +++++++++------ .../save/cosv/storage/RawCosvFileStorage.kt | 15 ------------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/save-cosv/src/main/kotlin/com/saveourtool/save/cosv/controllers/RawCosvFileController.kt b/save-cosv/src/main/kotlin/com/saveourtool/save/cosv/controllers/RawCosvFileController.kt index 292710101a..8113fb8849 100644 --- a/save-cosv/src/main/kotlin/com/saveourtool/save/cosv/controllers/RawCosvFileController.kt +++ b/save-cosv/src/main/kotlin/com/saveourtool/save/cosv/controllers/RawCosvFileController.kt @@ -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 @@ -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.* @@ -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) } /** @@ -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, @@ -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, + ): Mono { + 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) + } + } + } } } diff --git a/save-cosv/src/main/kotlin/com/saveourtool/save/cosv/storage/RawCosvFileS3KeyManager.kt b/save-cosv/src/main/kotlin/com/saveourtool/save/cosv/storage/RawCosvFileS3KeyManager.kt index 581d2dad93..02768c6bcf 100644 --- a/save-cosv/src/main/kotlin/com/saveourtool/save/cosv/storage/RawCosvFileS3KeyManager.kt +++ b/save-cosv/src/main/kotlin/com/saveourtool/save/cosv/storage/RawCosvFileS3KeyManager.kt @@ -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() + } + } } diff --git a/save-cosv/src/main/kotlin/com/saveourtool/save/cosv/storage/RawCosvFileStorage.kt b/save-cosv/src/main/kotlin/com/saveourtool/save/cosv/storage/RawCosvFileStorage.kt index a906ae9244..845e4c6d78 100644 --- a/save-cosv/src/main/kotlin/com/saveourtool/save/cosv/storage/RawCosvFileStorage.kt +++ b/save-cosv/src/main/kotlin/com/saveourtool/save/cosv/storage/RawCosvFileStorage.kt @@ -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 @@ -54,18 +51,6 @@ class RawCosvFileStorage( } .publishOn(s3Operations.scheduler) - override fun upload(key: RawCosvFileDto, content: Flux): Mono { - 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