This commit is contained in:
lunaticbum 2025-03-21 18:37:27 +09:00
parent a11aa8fbd0
commit 3dcb077e2f
3 changed files with 58 additions and 7 deletions

View File

@ -17,7 +17,7 @@ class AppConfig : WebMvcConfigurer {
@Value("\${resource.location}") @Value("\${resource.location}")
private val resourceLocation: String? = null private val resourceLocation: String? = null
val cacheControl: CacheControl = CacheControl.maxAge(Duration.ofDays(365)) val cacheControl: CacheControl = CacheControl.maxAge(Duration.ofHours(1))
@Bean @Bean
fun authInterceptor(): BumsInterceptor { fun authInterceptor(): BumsInterceptor {

View File

@ -218,7 +218,7 @@ class BlogController() {
val firstImg: Element? = doc.select("img")?.first() val firstImg: Element? = doc.select("img")?.first()
val imgSrc: String = firstImg?.attr("src") ?: "" val imgSrc: String = firstImg?.attr("src") ?: ""
it.image = imgSrc it.image = imgSrc
it.thumb = imgSrc.replaceBeforeLast(".", "_thumbnail.") it.thumb = imgSrc.replace(imgSrc.split("/").last(), imgSrc.split("/").last().replace(".","_thumbnail."))
generateThumbnail(imgSrc.split("/").last(), 200) generateThumbnail(imgSrc.split("/").last(), 200)
it.html = doc.text() it.html = doc.text()
} }
@ -233,13 +233,15 @@ class BlogController() {
fun generateThumbnail(originalPath: String, targetWidth: Int) { fun generateThumbnail(originalPath: String, targetWidth: Int) {
try { try {
val originalFile = File("$uploadPath${File.separator}$originalPath") val originalFile = File("$uploadPath${File.separator}$originalPath")
println("origin ${originalPath}")
println("thumb ${originalPath
.replace(".", "_thumbnail.")}")
// 썸네일 경로 생성 (예: /upload/uuid.jpg → /upload/uuid_thumbnail.jpg) // 썸네일 경로 생성 (예: /upload/uuid.jpg → /upload/uuid_thumbnail.jpg)
val thumbnailPath = originalFile.path val thumbnailPath = originalPath
.replaceBeforeLast(".", "_thumbnail.") .replace(".", "_thumbnail.")
val thumbnailFile = File(thumbnailPath) val thumbnailFile = File("$uploadPath${File.separator}$thumbnailPath")
// 썸네일 이미 존재하면 종료 // 썸네일 이미 존재하면 종료
if (thumbnailFile.exists()) { if (thumbnailFile.exists()) {
println("썸네일 이미 존재: $thumbnailPath") println("썸네일 이미 존재: $thumbnailPath")
@ -257,7 +259,7 @@ class BlogController() {
Thumbnails.of(originalFile) Thumbnails.of(originalFile)
.width(targetWidth) .width(targetWidth)
.keepAspectRatio(true) .keepAspectRatio(true)
.toFile(thumbnailPath) .toFile(thumbnailFile)
println("썸네일 생성 완료: $thumbnailPath") println("썸네일 생성 완료: $thumbnailPath")
} catch (e: IOException) { } catch (e: IOException) {

View File

@ -5,16 +5,20 @@ import jakarta.servlet.http.HttpServletResponse
import kr.lunaticbum.back.lun.model.PostManageg import kr.lunaticbum.back.lun.model.PostManageg
import kr.lunaticbum.back.lun.model.ResultMV import kr.lunaticbum.back.lun.model.ResultMV
import kr.lunaticbum.back.lun.utils.LogService import kr.lunaticbum.back.lun.utils.LogService
import net.coobird.thumbnailator.Thumbnails
import org.commonmark.node.Node import org.commonmark.node.Node
import org.commonmark.parser.Parser import org.commonmark.parser.Parser
import org.commonmark.renderer.html.HtmlRenderer import org.commonmark.renderer.html.HtmlRenderer
import org.jsoup.Jsoup import org.jsoup.Jsoup
import org.jsoup.nodes.Element import org.jsoup.nodes.Element
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.data.domain.Pageable import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RestController
import java.io.File
import java.io.IOException
import java.net.URLDecoder import java.net.URLDecoder
@RestController @RestController
@ -42,6 +46,8 @@ class Home {
val firstImg: Element? = doc.select("img")?.first() val firstImg: Element? = doc.select("img")?.first()
val imgSrc: String = firstImg?.attr("src") ?: "" val imgSrc: String = firstImg?.attr("src") ?: ""
it.image = imgSrc it.image = imgSrc
it.thumb = imgSrc.replace(imgSrc.split("/").last(), imgSrc.split("/").last().replace(".","_thumbnail."))
generateThumbnail(imgSrc.split("/").last(), 200)
it.html = doc.text() it.html = doc.text()
} }
it.title = if ((it.title?.length ?: 0) >= 1) it.title else "" it.title = if ((it.title?.length ?: 0) >= 1) it.title else ""
@ -52,6 +58,49 @@ class Home {
return vm return vm
} }
@Value("\${image.upload.path}")
private val uploadPath: String? = null
@Value("\${resource.handler}")
private val resourceHandler: String? = null
fun generateThumbnail(originalPath: String, targetWidth: Int) {
try {
val originalFile = File("$uploadPath${File.separator}$originalPath")
println("origin ${originalPath}")
println("thumb ${originalPath
.replace(".", "_thumbnail.")}")
// 썸네일 경로 생성 (예: /upload/uuid.jpg → /upload/uuid_thumbnail.jpg)
val thumbnailPath = originalPath
.replace(".", "_thumbnail.")
val thumbnailFile = File("$uploadPath${File.separator}$thumbnailPath")
// 썸네일 이미 존재하면 종료
if (thumbnailFile.exists()) {
println("썸네일 이미 존재: $thumbnailPath")
return
}
// 원본 파일 존재 확인
if (!originalFile.exists()) {
println("원본 파일 없음: $originalPath")
return
}
// 썸네일 생성 (가로 기준 비율 유지)
Thumbnails.of(originalFile)
.width(targetWidth)
.keepAspectRatio(true)
.toFile(thumbnailFile)
println("썸네일 생성 완료: $thumbnailFile")
} catch (e: IOException) {
println("썸네일 생성 실패: ${e.message}")
}
}
@GetMapping("/h2") @GetMapping("/h2")
fun home2() : ResultMV { fun home2() : ResultMV {