Skip to content

Commit

Permalink
refactor : 비디오 모듈 리팩토링
Browse files Browse the repository at this point in the history
모든 코드가 Reactor기반으로 동작하도록 구현.
  • Loading branch information
van1164 committed May 7, 2024
1 parent 37e0f17 commit 9a47c25
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 129 deletions.
11 changes: 11 additions & 0 deletions user/src/main/kotlin/com/van1164/user/UserRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.van1164.user

import com.van1164.common.domain.UserR2dbc
import org.springframework.data.r2dbc.repository.R2dbcRepository
import org.springframework.stereotype.Repository
import reactor.core.publisher.Mono

@Repository
interface UserRepository : R2dbcRepository<UserR2dbc,Long> {
fun findFirstByUserId(userId: String): Mono<UserR2dbc>
}
22 changes: 22 additions & 0 deletions user/src/main/kotlin/com/van1164/user/UserService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.van1164.user

import com.van1164.common.domain.UserR2dbc
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories
import org.springframework.stereotype.Service
import reactor.core.publisher.Mono


@Service
@EnableR2dbcRepositories(basePackageClasses = [UserRepository::class])
class UserService(
val userRepository: UserRepository
) {

fun findByUserId(userId : String): Mono<UserR2dbc> {
return userRepository.findFirstByUserId(userId)
}

fun save(user: UserR2dbc): Mono<UserR2dbc> {
return userRepository.save(user)
}
}
25 changes: 17 additions & 8 deletions util/src/main/kotlin/com/van1164/common/s3/S3UploadComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.springframework.core.io.buffer.DataBufferUtils
import org.springframework.http.codec.multipart.FilePart
import org.springframework.stereotype.Component
import reactor.core.publisher.Mono
import reactor.kotlin.core.publisher.toMono
import java.io.File

@Component
Expand Down Expand Up @@ -57,17 +58,25 @@ class S3UploadComponent(
s3Utils.delete("$videoUUID.part$i")
}

fun uploadM3U8(m3u8Path: String, outputUUID: String) {
fun uploadM3U8(m3u8Path: String, outputUUID: String): Mono<File> {
println("Upload M3U8")
val m3u8File = File(m3u8Path)
s3Utils.put(key = "$outputUUID/$m3u8Path", file = m3u8File)
fileUtils.delete(m3u8File)
return File(m3u8Path).toMono()
.doOnNext { m3U8File ->
s3Utils.put(key = "$outputUUID/$m3u8Path",file=m3U8File)
}
.doOnNext {m3U8File ->
fileUtils.delete(m3U8File)
}

}

fun uploadThumbnail(thumbNailPath: String) {
val thumbNailFile = File(thumbNailPath)
s3Utils.put("thumb/$thumbNailPath", thumbNailFile)
fileUtils.delete(thumbNailFile)
fun uploadThumbnail(thumbNailPath: String): Mono<File> {
return File(thumbNailPath).toMono()
.doOnNext { thumbNailFile ->
s3Utils.put("thumb/$thumbNailPath", thumbNailFile)
}.doOnNext{thumbNailFile->
fileUtils.delete(thumbNailFile)
}
}


Expand Down
25 changes: 25 additions & 0 deletions util/src/main/kotlin/com/van1164/common/util/S3Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import org.springframework.http.codec.multipart.FilePart
import org.springframework.stereotype.Component
import org.springframework.web.multipart.MultipartFile
import reactor.core.publisher.Mono
import reactor.kotlin.core.publisher.toMono
import java.io.File
import java.io.FileInputStream
import java.io.InputStream

@Component(value = "s3Utils")
Expand Down Expand Up @@ -67,6 +69,29 @@ class S3Utils(
}
}

fun put(
key: String,
videoInputStream: FileInputStream
): Mono<Boolean> {
return videoInputStream.toMono()
.map {
val request = PutObjectRequest(
bucketName,
key,
videoInputStream,
ObjectMetadata()
)
request.requestClientOptions.readLimit = 80000
request
}
.doOnNext {request->
amazonS3.putObject(request)
}
.map {
true
}
}


fun get(key : String): S3Object {
return amazonS3.getObject(
Expand Down
17 changes: 17 additions & 0 deletions util/src/main/kotlin/com/van1164/common/util/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
package com.van1164.common.util

import mu.KotlinLogging
import java.util.UUID


object Utils {
val logger = KotlinLogging.logger { }

fun createFilePath(fileUUID : String, extension : String): String {
return "$fileUUID.$extension"
}

fun createImagePath(): String {
return UUID.randomUUID().toString() + ".jpg"
}

fun createVideoPath(): String {
return UUID.randomUUID().toString() + ".mp4"
}

fun createM3U8Path() : String{
return UUID.randomUUID().toString() + ".m3u8"
}
}
18 changes: 0 additions & 18 deletions video/src/main/kotlin/com/van1164/video/VideoController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,13 @@ class VideoController(
@RequestPart chunkNumber : String,
@RequestPart totalChunk : String,
@RequestPart fileUUID : String): Mono<ResponseEntity<HttpStatus>> {
println("FFFFFFFFFFFFFFFFFFFFFFFF$chunkNumber")
logger.info { "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" }
return Mono.just(UploadVideoPartDTO(title,chunkNumber.toInt(),totalChunk.toInt(),fileUUID))
.doOnNext {
logger.info{ "여기에 있어요!!!"}
logger.info { video.filename() }
}
.flatMap {videoData->
videoService.uploadVideoPart(video, videoData)
}
.doOnError {
logger.error { "에러" }
}
.onErrorReturn (
ResponseEntity.badRequest().build()
)
// val videoData = UploadVideoPartDTO(title,chunkNumber,totalChunk,fileUUID)
// return videoService.uploadVideoPart(video, videoData)


}
@ResponseBody
@GetMapping("/videoPartLast/{id}/{totalChunk}", produces = [MediaType.TEXT_EVENT_STREAM_VALUE])
Expand All @@ -77,17 +64,12 @@ class VideoController(
@AuthenticationPrincipal user : PrincipalDetails,
@RequestBody uploadVideoDataDTO: UploadVideoDataDTO
): Mono<ResponseEntity<HttpStatus>> {
println(user)
logger.info { uploadVideoDataDTO.title }
logger.info { uploadVideoDataDTO.fileUUID }
logger.info { "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" }
return videoService.saveVideoData(uploadVideoDataDTO.title,uploadVideoDataDTO.fileUUID,user.name)
}

//@PreAuthorize("isAuthenticated()")
@GetMapping("/uploadPage")
fun uploadPage() : Mono<String> {
logger.info { "업로드 페이지" }
return Mono.just("uploadPage")
}

Expand Down
Loading

0 comments on commit 9a47c25

Please sign in to comment.