diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/content/AudioContentService.kt b/src/main/kotlin/kr/co/vividnext/sodalive/content/AudioContentService.kt index a48239e..fbeb3ee 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/content/AudioContentService.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/content/AudioContentService.kt @@ -28,9 +28,11 @@ import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional import org.springframework.web.multipart.MultipartFile +import java.text.SimpleDateFormat import java.time.LocalDateTime import java.time.ZoneId import java.time.format.DateTimeFormatter +import java.util.Locale @Service @Transactional(readOnly = true) @@ -137,6 +139,9 @@ class AudioContentService( // request 내용 파싱 val request = objectMapper.readValue(requestString, CreateAudioContentRequest::class.java) + // 미리듣기 시간 체크 + validatePreviewTime(request.previewStartTime, request.previewEndTime) + // contentFile 체크 if (contentFile == null && request.type == AudioContentType.INDIVIDUAL) { throw SodaException("콘텐츠를 선택해 주세요.") @@ -228,6 +233,11 @@ class AudioContentService( metadata.contentLength = contentFile.size metadata.addUserMetadata("generate_preview", "true") + if (request.previewStartTime != null && request.previewEndTime != null) { + metadata.addUserMetadata("preview_start_time", request.previewStartTime) + metadata.addUserMetadata("preview_end_time", request.previewEndTime) + } + val contentPath = s3Uploader.upload( inputStream = contentFile.inputStream, bucket = audioContentBucket, @@ -253,6 +263,64 @@ class AudioContentService( return CreateAudioContentResponse(contentId = audioContent.id!!) } + private fun validatePreviewTime(previewStartTime: String?, previewEndTime: String?) { + if (previewStartTime != null && previewEndTime != null) { + val startTimeArray = previewStartTime.split(":") + if (startTimeArray.size != 3) { + throw SodaException("미리 듣기 시간 형식은 00:30:00 과 같아야 합니다") + } + + for (time in startTimeArray) { + if (time.length != 2) { + throw SodaException("미리 듣기 시간 형식은 00:30:00 과 같아야 합니다") + } + } + + val endTimeArray = previewEndTime.split(":") + if (endTimeArray.size != 3) { + throw SodaException("미리 듣기 시간 형식은 00:30:00 과 같아야 합니다") + } + + for (time in endTimeArray) { + if (time.length != 2) { + throw SodaException("미리 듣기 시간 형식은 00:30:00 과 같아야 합니다") + } + } + + val timeDifference = timeDifference(previewStartTime, previewEndTime) + + if (timeDifference < 30000) { + throw SodaException("미리 듣기의 최소 시간은 30초 입니다.") + } + } else { + if (previewStartTime != null || previewEndTime != null) { + throw SodaException("미리 듣기 시작 시간과 종료 시간 둘 다 입력을 하거나 둘 다 입력 하지 않아야 합니다.") + } + } + } + + private fun timeDifference(startTime: String, endTime: String): Long { + try { + // Define a date format for parsing the times + val dateFormat = SimpleDateFormat("HH:mm:ss", Locale.KOREAN) + + // Parse the input times into Date objects + val date1 = dateFormat.parse(startTime) + val date2 = dateFormat.parse(endTime) + + // Check if either date is null + if (date1 == null || date2 == null) { + return 0 + } + + // Check if the time difference is greater than 30 seconds (30000 milliseconds) + return date2.time - date1.time + } catch (e: Exception) { + // Handle invalid time formats or parsing errors + return 0 + } + } + @Transactional fun uploadComplete(contentId: Long, content: String, duration: String) { val keyFileName = content.split("/").last() diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/content/CreateAudioContentRequest.kt b/src/main/kotlin/kr/co/vividnext/sodalive/content/CreateAudioContentRequest.kt index 8952c84..cab17ae 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/content/CreateAudioContentRequest.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/content/CreateAudioContentRequest.kt @@ -10,5 +10,7 @@ data class CreateAudioContentRequest( val isGeneratePreview: Boolean = true, val isCommentAvailable: Boolean = false, val type: AudioContentType = AudioContentType.INDIVIDUAL, - val childIds: List? = null + val childIds: List? = null, + val previewStartTime: String? = null, + val previewEndTime: String? = null )