Skip to content

Commit

Permalink
feat: feat thumbnail in EmojiAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisWooyeol authored and yangchanhk98 committed Nov 23, 2023
1 parent 61b7c64 commit c1ad5b1
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ class EmojiController(private val emojiService: EmojiService) {
fun postEmoji(
@CurrentUser username: String,
@RequestPart(value = "file") file: MultipartFile,
@RequestPart(value = "thumbnail") thumbnail: MultipartFile,
@RequestPart postEmojiRequest: PostEmojiRequest
): ResponseEntity<Unit> {
return ResponseEntity(emojiService.postEmoji(username, file, postEmojiRequest.emoji_unicode, postEmojiRequest.emoji_label), HttpStatus.CREATED)
return ResponseEntity(emojiService.postEmoji(username, file, thumbnail, postEmojiRequest.emoji_unicode, postEmojiRequest.emoji_label), HttpStatus.CREATED)
}

@GetMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.google.cloud.storage.BlobId
import com.google.cloud.storage.BlobInfo
import com.google.cloud.storage.Storage
import lombok.extern.slf4j.Slf4j
import org.apache.http.entity.ContentType
import org.springframework.stereotype.Repository
import org.springframework.web.multipart.MultipartFile
import java.io.ByteArrayInputStream
Expand Down Expand Up @@ -50,19 +51,29 @@ class EmojiDao(
return document.toObject(EmojiDto::class.java)
}

fun insertEmoji(username: String, file: MultipartFile, emojiUnicode: String, emojiLabel: String, dateTime: String): EmojiDto {
fun insertEmoji(username: String, file: MultipartFile, thumbnail: MultipartFile, emojiUnicode: String, emojiLabel: String, dateTime: String): EmojiDto {
// NOTE: created_by(username)을 video이름으로 넣어주어 유저별로 올린 비디오를 구분할 수 있게 한다.
val blobIdPart = username + "_" + dateTime
val emojiVideoBlobId: BlobId = BlobId.of(
EMOJI_STORAGE_BUCKET_NAME,
username + "_" + dateTime + ".mp4"
"$blobIdPart.mp4"
)
val emojiVideoBlob: BlobInfo = BlobInfo.newBuilder(emojiVideoBlobId)
.setContentType("video/mp4")
.build()
storage.createFrom(emojiVideoBlob, ByteArrayInputStream(file.bytes))
val emojiVideoUrl = storage.get(emojiVideoBlobId).signUrl(100, TimeUnit.DAYS)
// upload video thumbnail to emojiBucket
val emoji = EmojiDto(username, emojiUnicode, emojiLabel, emojiVideoUrl.toString(), dateTime)
val thumbnailBlobId: BlobId = BlobId.of(
EMOJI_STORAGE_BUCKET_NAME,
"$blobIdPart.jpeg"
)
val thumbnailBlob: BlobInfo = BlobInfo.newBuilder(thumbnailBlobId)
.setContentType(ContentType.IMAGE_JPEG.toString())
.build()
storage.createFrom(thumbnailBlob, ByteArrayInputStream(thumbnail.bytes))
val thumbnailUrl = storage.get(thumbnailBlobId).signUrl(100, TimeUnit.DAYS)
val emoji = EmojiDto(username, emojiUnicode, emojiLabel, emojiVideoUrl.toString(), dateTime, thumbnailUrl.toString())
db.collection(EMOJI_COLLECTION_NAME)
.document(emoji.id)
.set(emoji)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ data class EmojiDto(
var emoji_label: String = "",
var created_at: String = "",
var num_saved: Int = 0,
var thumbnail_url: String = ""
){
constructor(username: String, emojiUnicode: String, emojiLabel: String, emojiVideoUrl: String, dateTime: String) : this() {
constructor(username: String, emojiUnicode: String, emojiLabel: String, emojiVideoUrl: String, dateTime: String, thumbnailUrl: String) : this() {
val source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
val outputStrLength: Long = 20
id = java.util.Random().ints(outputStrLength, 0, source.length)
Expand All @@ -25,5 +26,6 @@ data class EmojiDto(
emoji_label = emojiLabel
created_at = dateTime
num_saved = 0
thumbnail_url = thumbnailUrl
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class EmojiService(
return emojiDao.getEmoji(emojiId)
}

fun postEmoji(username: String, file: MultipartFile, emojiUnicode: String, emojiLabel: String) {
fun postEmoji(username: String, file: MultipartFile, thumbnail: MultipartFile, emojiUnicode: String, emojiLabel: String) {
val dateTime = getDateTimeNow()
val emoji = emojiDao.insertEmoji(username, file, emojiUnicode, emojiLabel, dateTime)
val emoji = emojiDao.insertEmoji(username, file, thumbnail, emojiUnicode, emojiLabel, dateTime)
userDao.insertId(username, emoji.id, CREATED_EMOJIS)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ internal class EmojiControllerTest @Autowired constructor(
// given
val audioContent = ByteArray(100)
val audioFile = MockMultipartFile("file", "test.mp4", "audio/mp4", audioContent)
val imageContent = ByteArray(100)
val thumbnail = MockMultipartFile("thumbnail", "test.jpeg", "image/jpeg", imageContent)
val request = PostEmojiRequest(
emoji_unicode = "test_emoji_unicode",
emoji_label = "test_emoji_label"
Expand All @@ -151,6 +153,7 @@ internal class EmojiControllerTest @Autowired constructor(
val result = mockMvc.perform(
multipart("/api/emoji")
.file(audioFile)
.file(thumbnail)
.file(requestFile)
.contentType(MediaType.MULTIPART_FORM_DATA)
.characterEncoding("UTF-8")
Expand All @@ -160,7 +163,7 @@ internal class EmojiControllerTest @Autowired constructor(

// then
result.andExpect(status().isCreated)
verify(emojiService, times(1)).postEmoji(any(), any(), any(), any())
verify(emojiService, times(1)).postEmoji(any(), any(), any(), any(), any())
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal class EmojiDaoTest {
lateinit var blob: Blob

@MockBean
lateinit var emojiURL: URL
lateinit var url: URL

companion object {

Expand Down Expand Up @@ -84,6 +84,7 @@ internal class EmojiDaoTest {
id = "test_emoji" + i + "_" + j,
created_by = "test_username$i",
video_url = "test_video_url" + i + "_" + j,
thumbnail_url = "test_thumbnail_url" + i + "_" + j,
emoji_unicode = "test_emoji_unicode" + i + "_" + j,
emoji_label = "test_emoji_label" + i + "_" + j,
created_at = "test_created_at" + i + "_" + j,
Expand Down Expand Up @@ -144,26 +145,35 @@ internal class EmojiDaoTest {
val username = userList[1].username
val audioContent = ByteArray(100)
val file = MockMultipartFile("file", "test.mp4", "audio/mp4", audioContent)
val imageContent = ByteArray(100)
val thumbnail = MockMultipartFile("thumbnail", "test.jpeg", "image/jpeg", imageContent)
val emojiUnicode = "test_emoji_unicode"
val emojiLabel = "test_emoji_label"
val emojiVideoUrl = "test_emoji_video_url"
val url = "test_url"
val dateTime = "test_date_time"
val blobIdPart = username + "_" + dateTime
val emojiVideoBlobId: BlobId = BlobId.of(
"emojihub-e2023.appspot.com",
username + "_" + dateTime + ".mp4"
"$blobIdPart.mp4"
)
val thumbnailBlobId: BlobId = BlobId.of(
"emojihub-e2023.appspot.com",
"$blobIdPart.jpeg"
)
Mockito.`when`(db.collection(EMOJI_COLLECTION_NAME))
.thenReturn(testDB.collection(EMOJI_COLLECTION_NAME))
Mockito.`when`(storage.get(emojiVideoBlobId)).thenReturn(blob)
Mockito.`when`(blob.signUrl(100, TimeUnit.DAYS)).thenReturn(emojiURL)
Mockito.`when`(emojiURL.toString()).thenReturn(emojiVideoUrl)
Mockito.`when`(storage.get(thumbnailBlobId)).thenReturn(blob)
Mockito.`when`(blob.signUrl(100, TimeUnit.DAYS)).thenReturn(this.url)
Mockito.`when`(this.url.toString()).thenReturn(url)

// when
val result = emojiDao.insertEmoji(username, file, emojiUnicode, emojiLabel, dateTime)
val result = emojiDao.insertEmoji(username, file, thumbnail, emojiUnicode, emojiLabel, dateTime)

// then
verify(storage, times(1)).get(emojiVideoBlobId)
verify(blob, times(1)).signUrl(100, TimeUnit.DAYS)
verify(storage, times(1)).get(thumbnailBlobId)
verify(blob, times(2)).signUrl(100, TimeUnit.DAYS)

var emojiExist = emojiDao.existsEmoji(result.id)
var a = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ internal class EmojiServiceTest {
val username = "test_username"
val audioContent = ByteArray(100)
val file = MockMultipartFile("file", "test.mp4", "audio/mp4", audioContent)
val imageContent = ByteArray(100)
val thumbnail = MockMultipartFile("thumbnail", "test.jpeg", "image/jpeg", imageContent)
val emojiUnicode = "test_emoji_unicode"
val emojiLabel = "test_emoji_label"
val emoji = EmojiDto(
Expand All @@ -132,14 +134,14 @@ internal class EmojiServiceTest {
created_at = "test_created_at",
num_saved = 0,
)
Mockito.`when`(emojiDao.insertEmoji(any(), any(), any(), any(), any())).thenReturn(emoji)
Mockito.`when`(emojiDao.insertEmoji(any(), any(), any(), any(), any(), any())).thenReturn(emoji)

// when
emojiService.postEmoji(username, file, emojiUnicode, emojiLabel)
emojiService.postEmoji(username, file, thumbnail, emojiUnicode, emojiLabel)

// then
verify(emojiDao, times(1)).insertEmoji(any(), any(), any(), any(), any())
verify(emojiDao, times(1)).insertEmoji(any(), any(), any(), any(), any())
verify(emojiDao, times(1)).insertEmoji(any(), any(), any(), any(), any(), any())
verify(userDao, times(1)).insertId(username, emoji.id, "created_emojis")
}

@Test
Expand Down

0 comments on commit c1ad5b1

Please sign in to comment.