Skip to content

Commit

Permalink
๐Ÿ“ :: ์ž˜๋ชป๋œ Image URL
Browse files Browse the repository at this point in the history
  • Loading branch information
gurdl0525 committed Nov 22, 2023
1 parent 10236f1 commit f3156e4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.onui.domain.diary.exception

import com.example.onui.global.config.error.data.ErrorCode
import com.example.onui.global.config.error.exception.BusinessException

object InvalidImageUrlException : BusinessException(ErrorCode.INVALID_IMG_URL) {
private fun readResolve(): Any = InvalidImageUrlException
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.example.onui.domain.diary.entity.Diary
import com.example.onui.domain.diary.exception.DiaryNotFoundException
import com.example.onui.domain.diary.exception.InvalidImageUrlException
import com.example.onui.domain.diary.presentation.request.ChattingWithGPTRequest
import com.example.onui.domain.diary.presentation.request.CreateDiaryRequest
import com.example.onui.domain.diary.presentation.request.UpdateDiaryRequest
Expand All @@ -12,6 +13,7 @@ import com.example.onui.domain.diary.repository.DiaryRepository
import com.example.onui.domain.diary.repository.QDiaryRepository
import com.example.onui.global.common.facade.UserFacade
import com.example.onui.global.config.error.exception.PermissionDeniedException
import com.example.onui.global.env.S3Property
import com.example.onui.infra.feign.gpt.GPTClient
import com.example.onui.infra.feign.gpt.dto.request.GPTQueryRequest
import com.example.onui.infra.feign.gpt.dto.request.Message
Expand All @@ -20,25 +22,32 @@ import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDate
import java.time.LocalDateTime
import java.util.regex.Pattern

@Service
@Transactional(readOnly = true)
class DiaryServiceImpl(
private val diaryRepository: DiaryRepository,
private val userFacade: UserFacade,
private val qDiaryRepository: QDiaryRepository,
private val gptClient: GPTClient
private val gptClient: GPTClient,
private val s3Property: S3Property
) : DiaryService {

private companion object {
const val M_SET = "๋„ˆ์˜ ์ด๋ฆ„์€ ์˜ค๋ˆ„์ด์ด๊ณ  ์ง์—…์€ ์ƒ๋‹ด์‚ฌ์•ผ.\n์•„๋ž˜ ๋ฆฌ์ŠคํŠธ๋Š” ๋‚ด๊ฐ€ ์„ ํƒํ•œ ๊ฐ์ •์ด์•ผ.\n"
const val M_SET2 = "\n๋‚ด ๊ฐ์ •์„ ๋ถ„์„ํ•˜๊ณ  ๋˜๋„๋ก ์งง๊ฒŒ ์กด๋Œ“๋ง๋กœ ์†”๋ฃจ์…˜์„ ์ œ๊ณตํ•ด์ค˜."
const val REGEX = "https://%s.s3.%s.amazonaws.com/%s([\\w-]+)/([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}).(png|jpg|jpeg|pdf|svg|HEIC)"
}

@Transactional
override fun createDiary(req: CreateDiaryRequest): DiaryDetailResponse {

val user = userFacade.getCurrentUser()

if(req.image != null && !REGEX.format(s3Property.bucket, s3Property.region, s3Property.dir).toRegex().matches(req.image))
throw InvalidImageUrlException

val now = LocalDateTime.now()

val diary =
Expand All @@ -56,9 +65,9 @@ class DiaryServiceImpl(
}

override fun getDiaryByMonth(year: Int, month: Int) = DiaryListResponse(
diaryRepository.findAllByUserAndYearAndMonthOrderByCreatedAtAsc(userFacade.getCurrentUser(), year, month)?.map {
diaryRepository.findAllByUserAndYearAndMonthOrderByCreatedAtAsc(userFacade.getCurrentUser(), year, month).map {
it.toResponse()
}?.toMutableList()
}.toMutableList()
)

override fun getDetailById(date: LocalDate): DiaryDetailResponse? = diaryRepository.findByUserAndYearAndMonthAndDay(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,15 @@ class ImageServiceImpl(
this.contentLength = bytes.size.toLong()
}

var fileName: String = file.originalFilename ?: file.name


val ext = fileName.split('.').last()
val ext = (file.originalFilename ?: file.name).split('.').last()

try {
FileType.values().first { it.extension == ext }
} catch (e: NoSuchElementException) {
throw InvalidFileExtensionException
}

fileName = "${s3Property.dir}$sub/${UUID.randomUUID()}$fileName"
val fileName = "${s3Property.dir}$sub/${UUID.randomUUID()}.$ext"

val putObjectRequest = PutObjectRequest(
s3Property.bucket,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum class ErrorCode(
INVALID_DATE_FORMAT(HttpStatus.BAD_REQUEST, "date๊ฐ€ ์˜ฌ๋น„๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค."),
TYPE_COAST_MISS_MATCHED(HttpStatus.BAD_REQUEST, "type์ด ASSIGN์ผ ๋•Œ ๋ฐ˜๋“œ์‹œ coast๋ฅผ ์„ค์ •ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค."),
CAN_NOT_BUY_THEME(HttpStatus.BAD_REQUEST, "์Œ€์ด ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค."),
INVALID_IMG_URL(HttpStatus.BAD_REQUEST, "์ž˜๋ชป๋œ ์ด๋ฏธ์ง€ URL์ž…๋‹ˆ๋‹ค."),

// 401
INVALID_TOKEN(HttpStatus.UNAUTHORIZED, "์œ ํšจํ•˜์ง€ ์•Š์€ ํ† ํฐ์ž…๋‹ˆ๋‹ค."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import javax.servlet.http.HttpServletRequest
class JwtTokenResolver {

companion object{
private const val REGEX_BEARER_TOKEN = "Bearer [(a-zA-Z0-9-._~+/=*)]{30,600}"
private const val REGEX_BEARER_TOKEN = "Bearer [(a-zA-Z0-9-e._~+/=*)]{30,600}"
}

fun resolveToken(httpServletRequest: HttpServletRequest) = parseToken(
Expand Down

0 comments on commit f3156e4

Please sign in to comment.