feat(subtitle): 업로드와 BOT 콜백 및 자막 응답을 연동한다
This commit is contained in:
@@ -157,6 +157,13 @@ class SecurityConfig(
|
||||
JwtFilter::class.java
|
||||
)
|
||||
.authorizeRequests()
|
||||
.antMatchers("/internal/api/subtitle-jobs/**")
|
||||
.access(
|
||||
"hasRole('BOT') and " +
|
||||
"principal instanceof T(kr.co.vividnext.sodalive.member.MemberAdapter) and " +
|
||||
"principal.member.role == T(kr.co.vividnext.sodalive.member.MemberRole).BOT and " +
|
||||
"principal.member.isActive"
|
||||
)
|
||||
.antMatchers("/readyz").permitAll()
|
||||
.requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
|
||||
.antMatchers("/member/check/email").permitAll()
|
||||
|
||||
@@ -43,15 +43,20 @@ import kr.co.vividnext.sodalive.utils.generateFileName
|
||||
import kr.co.vividnext.sodalive.v2.creatorname.application.CreatorLocalizedNameService
|
||||
import kr.co.vividnext.sodalive.v2.home.following.application.HomeFollowingNewsPublishService
|
||||
import kr.co.vividnext.sodalive.v2.recommendation.application.CreatorContentViewHistoryService
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.application.SubtitleJobService
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.cache.annotation.Cacheable
|
||||
import org.springframework.context.ApplicationEventPublisher
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.PlatformTransactionManager
|
||||
import org.springframework.transaction.TransactionDefinition
|
||||
import org.springframework.transaction.annotation.Propagation
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.transaction.support.TransactionSynchronization
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager
|
||||
import org.springframework.transaction.support.TransactionTemplate
|
||||
import org.springframework.web.multipart.MultipartFile
|
||||
import java.text.SimpleDateFormat
|
||||
import java.time.LocalDateTime
|
||||
@@ -93,6 +98,8 @@ class AudioContentService(
|
||||
private val contentThemeTranslationRepository: ContentThemeTranslationRepository,
|
||||
private val homeFollowingNewsPublishService: HomeFollowingNewsPublishService,
|
||||
private val memberContentPreferenceService: MemberContentPreferenceService,
|
||||
private val subtitleJobService: SubtitleJobService,
|
||||
transactionManager: PlatformTransactionManager,
|
||||
|
||||
@Value("\${cloud.aws.s3.content-bucket}")
|
||||
private val audioContentBucket: String,
|
||||
@@ -104,6 +111,9 @@ class AudioContentService(
|
||||
private val coverImageHost: String
|
||||
) {
|
||||
private val log = LoggerFactory.getLogger(javaClass)
|
||||
private val registrationTransaction = TransactionTemplate(transactionManager).apply {
|
||||
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun audioContentLike(request: PutAudioContentLikeRequest, member: Member): PutAudioContentLikeResponse {
|
||||
@@ -220,7 +230,7 @@ class AudioContentService(
|
||||
pinContent?.isActive = false
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun createAudioContent(
|
||||
contentFile: MultipartFile?,
|
||||
coverImage: MultipartFile?,
|
||||
@@ -245,7 +255,7 @@ class AudioContentService(
|
||||
return createAudioContent(contentFile, coverImage, request, releaseDate, member)
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun createAudioContent(
|
||||
contentFile: MultipartFile?,
|
||||
coverImage: MultipartFile?,
|
||||
@@ -260,115 +270,118 @@ class AudioContentService(
|
||||
throw SodaException(messageKey = "content.error.content_required")
|
||||
}
|
||||
|
||||
validatePreviewTime(request.previewStartTime, request.previewEndTime)
|
||||
val registration = requireNotNull(
|
||||
registrationTransaction.execute {
|
||||
validatePreviewTime(request.previewStartTime, request.previewEndTime)
|
||||
|
||||
// 테마 체크
|
||||
val theme = themeQueryRepository.findThemeByIdAndActive(id = request.themeId)
|
||||
?: throw SodaException(messageKey = "content.error.invalid_theme")
|
||||
// 테마 체크
|
||||
val theme = themeQueryRepository.findThemeByIdAndActive(id = request.themeId)
|
||||
?: throw SodaException(messageKey = "content.error.invalid_theme")
|
||||
|
||||
if ((request.themeId == 12L || request.themeId == 13L || request.themeId == 14L) && request.price < 5) {
|
||||
throw SodaException(messageKey = "content.error.alarm_theme_price_min")
|
||||
}
|
||||
if ((request.themeId == 12L || request.themeId == 13L || request.themeId == 14L) && request.price < 5) {
|
||||
throw SodaException(messageKey = "content.error.alarm_theme_price_min")
|
||||
}
|
||||
|
||||
if (request.price in 1..4) throw SodaException(messageKey = "content.error.minimum_price")
|
||||
if (request.price in 1..4) throw SodaException(messageKey = "content.error.minimum_price")
|
||||
|
||||
val isFullDetailVisible = if (request.price >= 50) {
|
||||
request.isFullDetailVisible
|
||||
} else {
|
||||
true
|
||||
}
|
||||
val isFullDetailVisible = if (request.price >= 50) {
|
||||
request.isFullDetailVisible
|
||||
} else {
|
||||
true
|
||||
}
|
||||
|
||||
val purchaseOption = if (request.themeId == 12L || request.themeId == 13L || request.themeId == 14L) {
|
||||
PurchaseOption.BUY_ONLY
|
||||
} else {
|
||||
request.purchaseOption
|
||||
}
|
||||
val purchaseOption = if (request.themeId == 12L || request.themeId == 13L || request.themeId == 14L) {
|
||||
PurchaseOption.BUY_ONLY
|
||||
} else {
|
||||
request.purchaseOption
|
||||
}
|
||||
|
||||
val isOnlyRental = if (
|
||||
(request.limited != null && request.limited > 0) ||
|
||||
purchaseOption == PurchaseOption.BUY_ONLY
|
||||
) {
|
||||
false
|
||||
} else if (request.purchaseOption == PurchaseOption.RENT_ONLY) {
|
||||
true
|
||||
} else {
|
||||
request.isOnlyRental
|
||||
}
|
||||
val isOnlyRental = if (
|
||||
(request.limited != null && request.limited > 0) ||
|
||||
purchaseOption == PurchaseOption.BUY_ONLY
|
||||
) {
|
||||
false
|
||||
} else if (request.purchaseOption == PurchaseOption.RENT_ONLY) {
|
||||
true
|
||||
} else {
|
||||
request.isOnlyRental
|
||||
}
|
||||
|
||||
// DB에 값 추가
|
||||
val audioContent = AudioContent(
|
||||
title = request.title.trim(),
|
||||
detail = request.detail.trim(),
|
||||
languageCode = request.languageCode,
|
||||
price = if (request.price > 0) {
|
||||
request.price
|
||||
} else {
|
||||
0
|
||||
},
|
||||
releaseDate = releaseDate,
|
||||
limited = request.limited,
|
||||
remaining = request.limited,
|
||||
isAdult = request.isAdult,
|
||||
purchaseOption = purchaseOption,
|
||||
isGeneratePreview = request.isGeneratePreview,
|
||||
isOnlyRental = isOnlyRental,
|
||||
isPointAvailable = request.isPointAvailable,
|
||||
isCommentAvailable = request.isCommentAvailable,
|
||||
isFullDetailVisible = isFullDetailVisible
|
||||
)
|
||||
audioContent.theme = theme
|
||||
audioContent.member = member
|
||||
repository.save(audioContent)
|
||||
|
||||
// 태그 분리, #추가, 등록
|
||||
if (request.tags.isNotBlank()) {
|
||||
val tags = request.tags
|
||||
.replace("#", " #")
|
||||
.split(" ")
|
||||
.asSequence()
|
||||
.map { it.trim() }
|
||||
.filter { it.isNotBlank() }
|
||||
.map {
|
||||
val tag = if (!it.startsWith("#")) {
|
||||
"#$it"
|
||||
// DB에 값 추가
|
||||
val audioContent = AudioContent(
|
||||
title = request.title.trim(),
|
||||
detail = request.detail.trim(),
|
||||
languageCode = request.languageCode,
|
||||
price = if (request.price > 0) {
|
||||
request.price
|
||||
} else {
|
||||
it
|
||||
}
|
||||
0
|
||||
},
|
||||
releaseDate = releaseDate,
|
||||
limited = request.limited,
|
||||
remaining = request.limited,
|
||||
isAdult = request.isAdult,
|
||||
purchaseOption = purchaseOption,
|
||||
isGeneratePreview = request.isGeneratePreview,
|
||||
isOnlyRental = isOnlyRental,
|
||||
isPointAvailable = request.isPointAvailable,
|
||||
isCommentAvailable = request.isCommentAvailable,
|
||||
isFullDetailVisible = isFullDetailVisible
|
||||
)
|
||||
audioContent.theme = theme
|
||||
audioContent.member = member
|
||||
repository.save(audioContent)
|
||||
|
||||
val hashTag = hashTagRepository.findByTag(tag)
|
||||
?: hashTagRepository.save(HashTag(tag))
|
||||
// 태그 분리, #추가, 등록
|
||||
if (request.tags.isNotBlank()) {
|
||||
val tags = request.tags
|
||||
.replace("#", " #")
|
||||
.split(" ")
|
||||
.asSequence()
|
||||
.map { it.trim() }
|
||||
.filter { it.isNotBlank() }
|
||||
.map {
|
||||
val tag = if (!it.startsWith("#")) {
|
||||
"#$it"
|
||||
} else {
|
||||
it
|
||||
}
|
||||
|
||||
val audioContentHashTag = AudioContentHashTag()
|
||||
audioContentHashTag.audioContent = audioContent
|
||||
audioContentHashTag.hashTag = hashTag
|
||||
val hashTag = hashTagRepository.findByTag(tag)
|
||||
?: hashTagRepository.save(HashTag(tag))
|
||||
|
||||
audioContentHashTag
|
||||
}.toList()
|
||||
val audioContentHashTag = AudioContentHashTag()
|
||||
audioContentHashTag.audioContent = audioContent
|
||||
audioContentHashTag.hashTag = hashTag
|
||||
|
||||
audioContent.audioContentHashTags.addAll(tags)
|
||||
}
|
||||
audioContentHashTag
|
||||
}.toList()
|
||||
|
||||
var metadata = ObjectMetadata()
|
||||
metadata.contentLength = coverImage.size
|
||||
audioContent.audioContentHashTags.addAll(tags)
|
||||
}
|
||||
|
||||
// 커버 이미지 파일명 생성
|
||||
val coverImageFileName = generateFileName(prefix = "${audioContent.id}-cover")
|
||||
val metadata = ObjectMetadata()
|
||||
metadata.contentLength = coverImage.size
|
||||
|
||||
// 커버 이미지 업로드
|
||||
val coverImagePath = s3Uploader.upload(
|
||||
inputStream = coverImage.inputStream,
|
||||
bucket = coverImageBucket,
|
||||
filePath = "audio_content_cover/${audioContent.id}/$coverImageFileName",
|
||||
metadata = metadata
|
||||
// 커버 이미지 파일명 생성
|
||||
val coverImageFileName = generateFileName(prefix = "${audioContent.id}-cover")
|
||||
|
||||
// 커버 이미지 업로드
|
||||
val coverImagePath = s3Uploader.upload(
|
||||
inputStream = coverImage.inputStream,
|
||||
bucket = coverImageBucket,
|
||||
filePath = "audio_content_cover/${audioContent.id}/$coverImageFileName",
|
||||
metadata = metadata
|
||||
)
|
||||
|
||||
audioContent.coverImage = coverImagePath
|
||||
audioContent to subtitleJobService.register(audioContent, contentFile.originalFilename)
|
||||
}
|
||||
)
|
||||
|
||||
audioContent.coverImage = coverImagePath
|
||||
|
||||
// 콘텐츠 파일명 생성
|
||||
val contentFileName = generateFileName(prefix = "${audioContent.id}-content")
|
||||
val (audioContent, subtitleJob) = registration
|
||||
|
||||
// 콘텐츠 파일 업로드
|
||||
metadata = ObjectMetadata()
|
||||
val metadata = ObjectMetadata()
|
||||
metadata.contentLength = contentFile.size
|
||||
metadata.addUserMetadata(
|
||||
"generate_preview",
|
||||
@@ -384,38 +397,45 @@ class AudioContentService(
|
||||
metadata.addUserMetadata("preview_end_time", request.previewEndTime)
|
||||
}
|
||||
|
||||
val contentPath = s3Uploader.upload(
|
||||
inputStream = contentFile.inputStream,
|
||||
bucket = audioContentBucket,
|
||||
filePath = "input/${audioContent.id}/$contentFileName",
|
||||
metadata = metadata
|
||||
)
|
||||
|
||||
audioContent.content = contentPath
|
||||
|
||||
// 언어 코드가 지정되지 않은 경우, 파파고 언어 감지 API를 통해 비동기로 언어를 식별한다.
|
||||
if (audioContent.languageCode.isNullOrBlank()) {
|
||||
val papagoQuery = listOf(
|
||||
request.title.trim(),
|
||||
request.detail.trim(),
|
||||
request.tags.trim()
|
||||
)
|
||||
.filter { it.isNotBlank() }
|
||||
.joinToString(" ")
|
||||
|
||||
applicationEventPublisher.publishEvent(
|
||||
LanguageDetectEvent(
|
||||
id = audioContent.id!!,
|
||||
query = papagoQuery
|
||||
try {
|
||||
contentFile.inputStream.use { inputStream ->
|
||||
s3Uploader.upload(
|
||||
inputStream = inputStream,
|
||||
bucket = audioContentBucket,
|
||||
filePath = subtitleJob.sourceKey,
|
||||
metadata = metadata
|
||||
)
|
||||
)
|
||||
} else {
|
||||
applicationEventPublisher.publishEvent(
|
||||
LanguageTranslationEvent(
|
||||
id = audioContent.id!!,
|
||||
targetType = LanguageTranslationTargetType.CONTENT
|
||||
}
|
||||
} catch (exception: Exception) {
|
||||
subtitleJobService.recordUploadError(subtitleJob.jobId)
|
||||
throw exception
|
||||
}
|
||||
|
||||
registrationTransaction.executeWithoutResult {
|
||||
// 언어 코드가 지정되지 않은 경우, 파파고 언어 감지 API를 통해 비동기로 언어를 식별한다.
|
||||
if (audioContent.languageCode.isNullOrBlank()) {
|
||||
val papagoQuery = listOf(
|
||||
request.title.trim(),
|
||||
request.detail.trim(),
|
||||
request.tags.trim()
|
||||
)
|
||||
)
|
||||
.filter { it.isNotBlank() }
|
||||
.joinToString(" ")
|
||||
|
||||
applicationEventPublisher.publishEvent(
|
||||
LanguageDetectEvent(
|
||||
id = audioContent.id!!,
|
||||
query = papagoQuery
|
||||
)
|
||||
)
|
||||
} else {
|
||||
applicationEventPublisher.publishEvent(
|
||||
LanguageTranslationEvent(
|
||||
id = audioContent.id!!,
|
||||
targetType = LanguageTranslationTargetType.CONTENT
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return CreateAudioContentResponse(contentId = audioContent.id!!)
|
||||
@@ -481,8 +501,9 @@ class AudioContentService(
|
||||
|
||||
@Transactional
|
||||
fun uploadComplete(contentId: Long, content: String, duration: String) {
|
||||
val keyFileName = content.split("/").last()
|
||||
if (!keyFileName.startsWith(contentId.toString())) throw SodaException(messageKey = "common.error.invalid_request")
|
||||
val registeredOutputMatches = subtitleJobService.matchesOutput(contentId, content)
|
||||
val validOutput = registeredOutputMatches ?: content.substringAfterLast("/").startsWith(contentId.toString())
|
||||
if (!validOutput) throw SodaException(messageKey = "common.error.invalid_request")
|
||||
|
||||
val audioContent = repository.findByIdOrNull(contentId)
|
||||
?: throw SodaException(messageKey = "common.error.invalid_request")
|
||||
@@ -978,7 +999,19 @@ class AudioContentService(
|
||||
nextContent = nextContent,
|
||||
buyerList = buyerList,
|
||||
isAvailableUsePoint = audioContent.isPointAvailable && audioContent.price > 0,
|
||||
translated = translated
|
||||
translated = translated,
|
||||
subtitle = if (
|
||||
(isExistsAudioContent || creatorId == member.id || audioContent.price <= 0) &&
|
||||
(audioContent.releaseDate == null || audioContent.releaseDate!! <= LocalDateTime.now() || creatorId == member.id)
|
||||
) {
|
||||
subtitleJobService.subtitle(
|
||||
audioContent,
|
||||
langContext.lang,
|
||||
1000 * 60 * 60 * ((audioContent.duration ?: "00:00:00").split(":")[0].toLong() + 2)
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1302,16 +1335,25 @@ class AudioContentService(
|
||||
contentId = audioContent.id!!
|
||||
)
|
||||
|
||||
val contentUrl = if (isExistsAudioContent) {
|
||||
audioContentCloudFront.generateSignedURL(
|
||||
resourcePath = audioContent.content!!,
|
||||
expirationTime = 1000 * 60 * 60 * (audioContent.duration!!.split(":")[0].toLong() + 2)
|
||||
return if (isExistsAudioContent) {
|
||||
val expirationTime = 1000 * 60 * 60 * (audioContent.duration!!.split(":")[0].toLong() + 2)
|
||||
GenerateUrlResponse(
|
||||
contentUrl = audioContentCloudFront.generateSignedURL(
|
||||
resourcePath = audioContent.content!!,
|
||||
expirationTime = expirationTime
|
||||
),
|
||||
subtitle = if (
|
||||
audioContent.releaseDate == null || audioContent.releaseDate!! <= LocalDateTime.now() ||
|
||||
audioContent.member!!.id == member.id
|
||||
) {
|
||||
subtitleJobService.subtitle(audioContent, langContext.lang, expirationTime)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
)
|
||||
} else {
|
||||
""
|
||||
GenerateUrlResponse("")
|
||||
}
|
||||
|
||||
return GenerateUrlResponse(contentUrl)
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package kr.co.vividnext.sodalive.content
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.dto.ContentSubtitleResponse
|
||||
|
||||
data class GenerateUrlResponse(
|
||||
val contentUrl: String
|
||||
val contentUrl: String,
|
||||
val subtitle: ContentSubtitleResponse? = null
|
||||
)
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.querydsl.core.annotations.QueryProjection
|
||||
import kr.co.vividnext.sodalive.content.comment.GetAudioContentCommentListItem
|
||||
import kr.co.vividnext.sodalive.content.order.OrderType
|
||||
import kr.co.vividnext.sodalive.content.translation.TranslatedContent
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.dto.ContentSubtitleResponse
|
||||
|
||||
data class GetAudioContentDetailResponse(
|
||||
val contentId: Long,
|
||||
@@ -42,7 +43,8 @@ data class GetAudioContentDetailResponse(
|
||||
val nextContent: OtherContentResponse?,
|
||||
val buyerList: List<ContentBuyer>,
|
||||
val isAvailableUsePoint: Boolean,
|
||||
val translated: TranslatedContent?
|
||||
val translated: TranslatedContent?,
|
||||
val subtitle: ContentSubtitleResponse? = null
|
||||
)
|
||||
|
||||
data class OtherContentResponse @QueryProjection constructor(
|
||||
|
||||
+2
-1
@@ -19,6 +19,7 @@ import kr.co.vividnext.sodalive.v2.api.admin.aicharacter.error.AiCharacterAdminA
|
||||
import org.springframework.data.domain.PageRequest
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Propagation
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.web.multipart.MultipartFile
|
||||
import java.time.DateTimeException
|
||||
@@ -158,7 +159,7 @@ class AiCharacterAdminAudioContentFacade(
|
||||
)
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
fun create(
|
||||
characterId: Long,
|
||||
coverImage: MultipartFile,
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package kr.co.vividnext.sodalive.v2.subtitle.adapter.web
|
||||
|
||||
import org.springframework.core.Ordered
|
||||
import org.springframework.core.annotation.Order
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
@RestControllerAdvice(assignableTypes = [SubtitleJobController::class])
|
||||
class SubtitleExceptionHandler {
|
||||
@ExceptionHandler(ResponseStatusException::class)
|
||||
fun handle(exception: ResponseStatusException): ResponseEntity<Map<String, String>> =
|
||||
ResponseEntity.status(exception.status).body(mapOf("error" to "Subtitle request rejected"))
|
||||
|
||||
@ExceptionHandler(HttpMessageNotReadableException::class)
|
||||
fun handleUnreadableBody(): ResponseEntity<Map<String, String>> =
|
||||
ResponseEntity.unprocessableEntity().body(mapOf("error" to "Subtitle request rejected"))
|
||||
|
||||
@ExceptionHandler(Exception::class)
|
||||
fun handleUnexpectedException(): ResponseEntity<Map<String, String>> =
|
||||
ResponseEntity.internalServerError().body(mapOf("error" to "Subtitle request failed"))
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package kr.co.vividnext.sodalive.v2.subtitle.adapter.web
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.application.SubtitleJobService
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RequestParam
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/internal/api/subtitle-jobs")
|
||||
class SubtitleJobController(private val service: SubtitleJobService, private val parser: SubtitleRequestParser) {
|
||||
@GetMapping("/resolve")
|
||||
fun resolve(@RequestParam bucket: String, @RequestParam key: String) = service.resolve(bucket, key)
|
||||
|
||||
@GetMapping("/{jobId}")
|
||||
fun get(@PathVariable jobId: String) = service.get(jobId)
|
||||
|
||||
@PostMapping("/{jobId}/started", consumes = ["application/json"])
|
||||
fun started(@PathVariable jobId: String, @RequestBody body: ByteArray): Map<String, String> {
|
||||
service.started(jobId, parser.started(body))
|
||||
return emptyMap()
|
||||
}
|
||||
|
||||
@PostMapping("/callback", consumes = ["application/json"])
|
||||
fun callback(@RequestBody body: ByteArray): Map<String, String> {
|
||||
service.callback(parser.callback(body), body)
|
||||
return emptyMap()
|
||||
}
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
package kr.co.vividnext.sodalive.v2.subtitle.adapter.web
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser
|
||||
import com.fasterxml.jackson.core.JsonProcessingException
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature
|
||||
import com.fasterxml.jackson.databind.JsonNode
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.dto.SubtitleCallbackRequest
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.dto.SubtitleResultRequest
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.dto.SubtitleStartedRequest
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.stereotype.Component
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
|
||||
@Component
|
||||
class SubtitleRequestParser(objectMapper: ObjectMapper) {
|
||||
private val mapper = objectMapper.copy()
|
||||
.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION)
|
||||
.enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
|
||||
|
||||
fun started(body: ByteArray): SubtitleStartedRequest {
|
||||
val node = parse(body)
|
||||
fields(node, setOf("transcribeJobName", "status"))
|
||||
return SubtitleStartedRequest(text(node, "transcribeJobName"), text(node, "status"))
|
||||
}
|
||||
|
||||
fun callback(body: ByteArray): SubtitleCallbackRequest {
|
||||
val node = parse(body)
|
||||
fields(
|
||||
node,
|
||||
setOf("eventId", "jobId", "contentId", "transcribeJobName", "status", "subtitles", "errorCode", "errorMessage")
|
||||
)
|
||||
val contentId = node.path("contentId")
|
||||
if (!contentId.isIntegralNumber || !contentId.canConvertToLong() || contentId.longValue() <= 0) throw invalid()
|
||||
val subtitles = node.path("subtitles")
|
||||
if (!subtitles.isArray || subtitles.size() > 2) throw invalid()
|
||||
return SubtitleCallbackRequest(
|
||||
eventId = text(node, "eventId"),
|
||||
jobId = text(node, "jobId"),
|
||||
contentId = contentId.longValue(),
|
||||
transcribeJobName = text(node, "transcribeJobName"),
|
||||
status = text(node, "status"),
|
||||
subtitles = subtitles.map {
|
||||
fields(it, setOf("language", "format", "s3Bucket", "s3Key"))
|
||||
SubtitleResultRequest(text(it, "language"), text(it, "format"), text(it, "s3Bucket"), text(it, "s3Key"))
|
||||
},
|
||||
errorCode = nullableText(node, "errorCode"),
|
||||
errorMessage = nullableText(node, "errorMessage")
|
||||
)
|
||||
}
|
||||
|
||||
private fun parse(body: ByteArray): JsonNode = try {
|
||||
mapper.readTree(body) ?: throw invalid()
|
||||
} catch (_: JsonProcessingException) {
|
||||
throw invalid()
|
||||
}
|
||||
|
||||
private fun fields(node: JsonNode, expected: Set<String>) {
|
||||
if (!node.isObject || node.fieldNames().asSequence().toSet() != expected) throw invalid()
|
||||
}
|
||||
|
||||
private fun text(node: JsonNode, field: String): String {
|
||||
val value = node.path(field)
|
||||
if (!value.isTextual || value.textValue().isBlank()) throw invalid()
|
||||
return value.textValue()
|
||||
}
|
||||
|
||||
private fun nullableText(node: JsonNode, field: String): String? =
|
||||
if (node.path(field).isNull) null else text(node, field)
|
||||
|
||||
private fun invalid() = ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY, "Invalid subtitle request")
|
||||
}
|
||||
+219
@@ -0,0 +1,219 @@
|
||||
package kr.co.vividnext.sodalive.v2.subtitle.application
|
||||
|
||||
import kr.co.vividnext.sodalive.aws.cloudfront.AudioContentCloudFront
|
||||
import kr.co.vividnext.sodalive.content.AudioContent
|
||||
import kr.co.vividnext.sodalive.i18n.Lang
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.adapter.persistence.Subtitle
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.adapter.persistence.SubtitleCallbackReceipt
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.adapter.persistence.SubtitleCallbackReceiptRepository
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.adapter.persistence.SubtitleJob
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.adapter.persistence.SubtitleJobRepository
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.adapter.persistence.SubtitleRepository
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.domain.SubtitleJobStatus
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.dto.ContentSubtitleResponse
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.dto.SubtitleCallbackRequest
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.dto.SubtitleJobResponse
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.dto.SubtitleResultRequest
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.dto.SubtitleSourceResponse
|
||||
import kr.co.vividnext.sodalive.v2.subtitle.dto.SubtitleStartedRequest
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.dao.DataIntegrityViolationException
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.PlatformTransactionManager
|
||||
import org.springframework.transaction.TransactionDefinition
|
||||
import org.springframework.transaction.annotation.Propagation
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.transaction.support.TransactionTemplate
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
import java.security.MessageDigest
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneOffset
|
||||
import java.time.temporal.ChronoUnit
|
||||
import java.util.Locale
|
||||
import java.util.UUID
|
||||
|
||||
@Service
|
||||
class SubtitleJobService(
|
||||
private val jobs: SubtitleJobRepository,
|
||||
private val receipts: SubtitleCallbackReceiptRepository,
|
||||
private val subtitles: SubtitleRepository,
|
||||
private val cloudFront: AudioContentCloudFront,
|
||||
transactionManager: PlatformTransactionManager,
|
||||
@Value("\${cloud.aws.s3.content-bucket}") private val bucket: String
|
||||
) {
|
||||
private val transaction = TransactionTemplate(transactionManager).apply {
|
||||
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW
|
||||
isolationLevel = TransactionDefinition.ISOLATION_READ_COMMITTED
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.MANDATORY)
|
||||
fun register(content: AudioContent, originalFilename: String?): SubtitleJob {
|
||||
val contentId = requireNotNull(content.id)
|
||||
val sourceName = UUID.randomUUID().toString()
|
||||
val extension = originalFilename?.replace('\\', '/')?.substringAfterLast('/')
|
||||
?.substringAfterLast('.', "")?.takeIf { it.matches(Regex("[A-Za-z0-9]{1,16}")) }
|
||||
?.lowercase(Locale.ROOT)
|
||||
val sourceFileName = if (extension == null) sourceName else "$sourceName.$extension"
|
||||
val job = jobs.save(
|
||||
SubtitleJob(
|
||||
jobId = UUID.randomUUID().toString(),
|
||||
contentId = contentId,
|
||||
sourceBucket = bucket,
|
||||
sourceKey = "input/$contentId/$sourceFileName",
|
||||
outputKey = "output/$contentId/$sourceName"
|
||||
)
|
||||
)
|
||||
content.latestSubtitleJobId = job.jobId
|
||||
content.content = job.sourceKey
|
||||
return job
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
fun resolve(bucket: String, key: String): SubtitleJobResponse =
|
||||
response(jobs.findBySourceBucketAndSourceKey(bucket, key) ?: throw missing())
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
fun get(jobId: String): SubtitleJobResponse = response(jobs.findByIdOrNull(jobId) ?: throw missing())
|
||||
|
||||
fun started(jobId: String, request: SubtitleStartedRequest) {
|
||||
if (request.transcribeJobName != "subtitle-$jobId" || request.status != "PROCESSING") throw invalid()
|
||||
transaction.executeWithoutResult {
|
||||
val job = jobs.findForUpdate(jobId) ?: throw missing()
|
||||
if (job.status == SubtitleJobStatus.PENDING) {
|
||||
job.status = SubtitleJobStatus.PROCESSING
|
||||
job.updatedAt = now()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun callback(request: SubtitleCallbackRequest, rawBody: ByteArray) {
|
||||
if (request.eventId.isBlank() || request.eventId.length > 255) throw invalid()
|
||||
val hash = MessageDigest.getInstance("SHA-256").digest(rawBody).joinToString("") { "%02x".format(it) }
|
||||
try {
|
||||
transaction.executeWithoutResult {
|
||||
val existing = receipts.findByIdOrNull(request.eventId)
|
||||
if (existing != null) {
|
||||
replay(existing, hash)
|
||||
return@executeWithoutResult
|
||||
}
|
||||
val job = jobs.findForUpdate(request.jobId) ?: throw missing()
|
||||
val afterLock = receipts.findByIdOrNull(request.eventId)
|
||||
if (afterLock != null) {
|
||||
replay(afterLock, hash)
|
||||
return@executeWithoutResult
|
||||
}
|
||||
val status = validate(job, request)
|
||||
val existingResults = subtitles.findAllByJobId(job.jobId).map {
|
||||
SubtitleResultRequest(it.language, it.format, it.s3Bucket, it.s3Key)
|
||||
}.sortedBy { it.language }
|
||||
if (job.status.terminal && (
|
||||
job.status != status || existingResults != request.subtitles.sortedBy { it.language } ||
|
||||
job.errorCode != request.errorCode || job.errorMessage != request.errorMessage
|
||||
)
|
||||
) {
|
||||
throw ResponseStatusException(HttpStatus.CONFLICT, "Subtitle job already finished")
|
||||
}
|
||||
|
||||
if (!job.status.terminal) {
|
||||
subtitles.saveAll(
|
||||
request.subtitles.map {
|
||||
Subtitle(job.jobId, it.language, it.format, it.s3Bucket, it.s3Key)
|
||||
}
|
||||
)
|
||||
job.status = status
|
||||
job.errorCode = request.errorCode
|
||||
job.errorMessage = request.errorMessage
|
||||
job.updatedAt = now()
|
||||
}
|
||||
receipts.saveAndFlush(SubtitleCallbackReceipt(request.eventId, job.jobId, hash))
|
||||
}
|
||||
} catch (exception: DataIntegrityViolationException) {
|
||||
transaction.executeWithoutResult {
|
||||
val receipt = receipts.findByIdOrNull(request.eventId) ?: throw exception
|
||||
replay(receipt, hash)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun recordUploadError(jobId: String) {
|
||||
transaction.executeWithoutResult {
|
||||
val job = jobs.findForUpdate(jobId) ?: throw missing()
|
||||
val recordedAt = now()
|
||||
job.sourceUploadErrorCode = "SOURCE_UPLOAD_UNCERTAIN"
|
||||
job.sourceUploadErrorAt = recordedAt
|
||||
job.updatedAt = recordedAt
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
fun matchesOutput(contentId: Long, outputKey: String): Boolean? =
|
||||
jobs.findFirstByContentIdOrderByCreatedAtAsc(contentId)?.let { job ->
|
||||
outputKey.substringBeforeLast('.', "") == job.outputKey &&
|
||||
outputKey.substringAfterLast('.', "") in setOf("aac", "m4a", "mp3")
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
fun subtitle(content: AudioContent, lang: Lang, expirationTime: Long): ContentSubtitleResponse? {
|
||||
val jobId = content.latestSubtitleJobId ?: return null
|
||||
val job = jobs.findByIdOrNull(jobId) ?: return null
|
||||
if (job.contentId != content.id || !job.status.terminal || job.status == SubtitleJobStatus.FAILED) return null
|
||||
val language = when (lang) {
|
||||
Lang.KO -> "ko-KR"
|
||||
Lang.JA -> "ja-JP"
|
||||
else -> return null
|
||||
}
|
||||
val subtitle = subtitles.findByJobIdAndLanguage(jobId, language) ?: return null
|
||||
return ContentSubtitleResponse(
|
||||
subtitle.language,
|
||||
subtitle.format,
|
||||
cloudFront.generateSignedURL(subtitle.s3Key, expirationTime)
|
||||
)
|
||||
}
|
||||
|
||||
private fun validate(job: SubtitleJob, request: SubtitleCallbackRequest): SubtitleJobStatus {
|
||||
if (request.contentId != job.contentId || request.transcribeJobName != "subtitle-${job.jobId}") throw invalid()
|
||||
if (request.subtitles.map { it.language }.distinct().size != request.subtitles.size) throw invalid()
|
||||
request.subtitles.forEach {
|
||||
val language = when (it.language) {
|
||||
"ko-KR" -> "ko"
|
||||
"ja-JP" -> "ja"
|
||||
else -> throw invalid()
|
||||
}
|
||||
if (it.format != "SRT" || it.s3Bucket != bucket ||
|
||||
it.s3Key != "subtitles/${job.contentId}/${job.jobId}/$language.srt"
|
||||
) {
|
||||
throw invalid()
|
||||
}
|
||||
}
|
||||
val status = when (request.subtitles.size) {
|
||||
2 -> SubtitleJobStatus.COMPLETED
|
||||
1 -> SubtitleJobStatus.PARTIAL_COMPLETED
|
||||
0 -> SubtitleJobStatus.FAILED
|
||||
else -> throw invalid()
|
||||
}
|
||||
if (request.status != status.name) throw invalid()
|
||||
if (status == SubtitleJobStatus.COMPLETED) {
|
||||
if (request.errorCode != null || request.errorMessage != null) throw invalid()
|
||||
} else if (request.errorCode.isNullOrBlank() || request.errorMessage.isNullOrBlank() || request.errorCode.length > 255) {
|
||||
throw invalid()
|
||||
}
|
||||
return status
|
||||
}
|
||||
|
||||
private fun replay(receipt: SubtitleCallbackReceipt, hash: String) {
|
||||
if (receipt.bodyHash != hash) throw invalid()
|
||||
}
|
||||
|
||||
private fun response(job: SubtitleJob) = SubtitleJobResponse(
|
||||
job.jobId,
|
||||
job.contentId,
|
||||
job.status,
|
||||
SubtitleSourceResponse(job.sourceBucket, job.sourceKey)
|
||||
)
|
||||
|
||||
private fun now() = LocalDateTime.now(ZoneOffset.UTC).truncatedTo(ChronoUnit.SECONDS)
|
||||
private fun missing() = ResponseStatusException(HttpStatus.NOT_FOUND, "Subtitle job not found")
|
||||
private fun invalid() = ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY, "Invalid subtitle request")
|
||||
}
|
||||
Reference in New Issue
Block a user