From 3dcb077e2f9c83cfd939b79549b955b47605bda2 Mon Sep 17 00:00:00 2001 From: lunaticbum Date: Fri, 21 Mar 2025 18:37:27 +0900 Subject: [PATCH] ... --- .../lunaticbum/back/lun/configs/AppConfig.kt | 2 +- .../back/lun/controllers/BlogController.kt | 14 +++--- .../lunaticbum/back/lun/controllers/Home.kt | 49 +++++++++++++++++++ 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/configs/AppConfig.kt b/src/main/kotlin/kr/lunaticbum/back/lun/configs/AppConfig.kt index 8af2989..f1304b7 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/configs/AppConfig.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/configs/AppConfig.kt @@ -17,7 +17,7 @@ class AppConfig : WebMvcConfigurer { @Value("\${resource.location}") private val resourceLocation: String? = null - val cacheControl: CacheControl = CacheControl.maxAge(Duration.ofDays(365)) + val cacheControl: CacheControl = CacheControl.maxAge(Duration.ofHours(1)) @Bean fun authInterceptor(): BumsInterceptor { diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BlogController.kt b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BlogController.kt index b711842..7de4a07 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BlogController.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BlogController.kt @@ -218,7 +218,7 @@ class BlogController() { val firstImg: Element? = doc.select("img")?.first() val imgSrc: String = firstImg?.attr("src") ?: "" 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) it.html = doc.text() } @@ -233,13 +233,15 @@ class BlogController() { 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 = originalFile.path - .replaceBeforeLast(".", "_thumbnail.") + val thumbnailPath = originalPath + .replace(".", "_thumbnail.") - val thumbnailFile = File(thumbnailPath) + val thumbnailFile = File("$uploadPath${File.separator}$thumbnailPath") // 썸네일 이미 존재하면 종료 if (thumbnailFile.exists()) { println("썸네일 이미 존재: $thumbnailPath") @@ -257,7 +259,7 @@ class BlogController() { Thumbnails.of(originalFile) .width(targetWidth) .keepAspectRatio(true) - .toFile(thumbnailPath) + .toFile(thumbnailFile) println("썸네일 생성 완료: $thumbnailPath") } catch (e: IOException) { diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/Home.kt b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/Home.kt index 3be9e4c..e0ca3cd 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/Home.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/Home.kt @@ -5,16 +5,20 @@ import jakarta.servlet.http.HttpServletResponse import kr.lunaticbum.back.lun.model.PostManageg import kr.lunaticbum.back.lun.model.ResultMV import kr.lunaticbum.back.lun.utils.LogService +import net.coobird.thumbnailator.Thumbnails import org.commonmark.node.Node import org.commonmark.parser.Parser import org.commonmark.renderer.html.HtmlRenderer import org.jsoup.Jsoup import org.jsoup.nodes.Element import org.springframework.beans.factory.annotation.Autowired +import org.springframework.beans.factory.annotation.Value import org.springframework.data.domain.Pageable import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController +import java.io.File +import java.io.IOException import java.net.URLDecoder @RestController @@ -42,6 +46,8 @@ class Home { val firstImg: Element? = doc.select("img")?.first() val imgSrc: String = firstImg?.attr("src") ?: "" 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.title = if ((it.title?.length ?: 0) >= 1) it.title else "" @@ -52,6 +58,49 @@ class Home { 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") fun home2() : ResultMV {