diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/LunApplication.kt b/src/main/kotlin/kr/lunaticbum/back/lun/LunApplication.kt index 86b915c..ec6fae1 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/LunApplication.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/LunApplication.kt @@ -1,12 +1,9 @@ package kr.lunaticbum.back.lun -import org.springframework.beans.factory.annotation.Value +import jdk.incubator.vector.VectorOperators.Test import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication -import org.springframework.context.EnvironmentAware -import org.springframework.core.env.Environment import org.springframework.scheduling.annotation.EnableScheduling -import org.springframework.stereotype.Component import org.springframework.web.reactive.function.client.WebClient 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 76e6f8d..0c79082 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/configs/AppConfig.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/configs/AppConfig.kt @@ -1,13 +1,22 @@ package kr.lunaticbum.back.lun.configs import org.springframework.beans.factory.annotation.Value -import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration -import org.springframework.web.servlet.config.annotation.EnableWebMvc +import org.springframework.web.reactive.config.ResourceHandlerRegistry +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer @Configuration -class AppConfig { +class AppConfig : WebMvcConfigurer { + @Value("\${resource.handler}") + private val resourceHandler: String? = null + + @Value("\${resource.location}") + private val resourceLocation: String? = null + + override fun addResourceHandlers(registry: org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry) { + registry.addResourceHandler(resourceHandler).addResourceLocations(resourceLocation) + } // @Bean // fun getProperty() : Map{ diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BlogController.kt b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BlogController.kt new file mode 100644 index 0000000..8582740 --- /dev/null +++ b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BlogController.kt @@ -0,0 +1,98 @@ +package kr.lunaticbum.back.lun.controllers + +import jakarta.servlet.http.HttpServletRequest +import jakarta.servlet.http.HttpServletResponse +import kr.lunaticbum.back.lun.utils.LogService +import kr.lunaticbum.back.lun.utils.getFileExtension +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.beans.factory.annotation.Value +import org.springframework.web.bind.annotation.* +import org.springframework.web.multipart.MultipartFile +import org.springframework.web.servlet.ModelAndView +import java.io.* +import java.util.* + + +@RestController +@RequestMapping("blog") +class BlogController() { + + @Autowired + lateinit var logService: LogService + + @GetMapping("","/write") + fun writ() : ModelAndView{ + val vm = ModelAndView("/content/blog/write") +// when(System.currentTimeMillis() % 5L) { +// 0L -> vm.modelMap.put(EncTypeKey,"T4") +// 1L -> vm.modelMap.put(EncTypeKey,"T3") +// 2L -> vm.modelMap.put(EncTypeKey,"T2") +// else -> vm.modelMap.put(EncTypeKey,"T0") +// } + + return vm + } + + @Value("\${image.upload.path}") + private val uploadPath: String? = null + + @Value("\${resource.handler}") + private val resourceHandler: String? = null + + + @PostMapping("/post/imageUpload") + fun postImage(@RequestPart("file") upload: MultipartFile, res: HttpServletResponse, req: HttpServletRequest) { + var out: OutputStream? = null + var printWriter: PrintWriter? = null + logService.log("imgUploadPath ${upload.originalFilename}") + res.characterEncoding = "utf-8" + res.contentType = "text/html;charset=utf-8" + + try { + val uuid = UUID.randomUUID() + logService.log("imgUploadPath ${uuid.toString()}") + + val extension: String = getFileExtension(upload.originalFilename) ?: "" + + val bytes = upload.bytes + + var f = File(uploadPath) + logService.log("imgUploadPath ${f.exists()}") + if (f.exists() == false) f.mkdirs() + // 실제 이미지 저장 경로 + val imgUploadPath = (uploadPath + File.separator + uuid).toString() + "." + extension + logService.log("imgUploadPath $imgUploadPath") + File(imgUploadPath).parentFile.mkdirs() + // 이미지 저장 + out = FileOutputStream(imgUploadPath) + out.write(bytes) + out.flush() + + // ckEditor 로 전송 + printWriter = res.writer + val callback = req.getParameter("CKEditorFuncNum") + val fileUrl = "/blog/post/image/$uuid.$extension" + + printWriter.println( + ("") + ) + + printWriter.flush() + logService.log("imgUploadPath $imgUploadPath") + logService.log("imgUploadPath ${File(imgUploadPath).exists()}") + } catch (e: IOException) { + e.printStackTrace() + } finally { + try { + out?.close() + + printWriter?.close() + } catch (e: IOException) { + e.printStackTrace() + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/UserController.kt b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/UserController.kt index 1ff2b70..76cacb9 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/UserController.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/UserController.kt @@ -15,7 +15,9 @@ import org.springframework.security.core.userdetails.UserDetails import org.springframework.web.bind.annotation.* import org.springframework.web.reactive.function.client.WebClient import org.springframework.web.servlet.ModelAndView +import java.io.File import java.util.* +import kotlin.math.log @RestController @@ -46,6 +48,10 @@ class UserController { // 2L -> vm.modelMap.put(EncTypeKey,"T2") // else -> vm.modelMap.put(EncTypeKey,"T0") // } + + val file = File("/") + + logService.log(file.absolutePath) vm.modelMap.put(EncTypeKey,EncType11) vm.modelMap.put(ApiKeyWordKey,"JOIN") return vm diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/model/ArticleSaveFile.kt b/src/main/kotlin/kr/lunaticbum/back/lun/model/ArticleSaveFile.kt new file mode 100644 index 0000000..b74cc9a --- /dev/null +++ b/src/main/kotlin/kr/lunaticbum/back/lun/model/ArticleSaveFile.kt @@ -0,0 +1,7 @@ +package kr.lunaticbum.back.lun.model + +import lombok.Getter +import lombok.NoArgsConstructor +import lombok.Setter +import org.springframework.data.annotation.Id + diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/service/SaveFileService.kt b/src/main/kotlin/kr/lunaticbum/back/lun/service/SaveFileService.kt new file mode 100644 index 0000000..51fada0 --- /dev/null +++ b/src/main/kotlin/kr/lunaticbum/back/lun/service/SaveFileService.kt @@ -0,0 +1,4 @@ +package kr.lunaticbum.back.lun.service + +class SaveFileService { +} \ No newline at end of file diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/utils/FileUtils.kt b/src/main/kotlin/kr/lunaticbum/back/lun/utils/FileUtils.kt new file mode 100644 index 0000000..938d70c --- /dev/null +++ b/src/main/kotlin/kr/lunaticbum/back/lun/utils/FileUtils.kt @@ -0,0 +1,27 @@ +package kr.lunaticbum.back.lun.utils + +import org.apache.tomcat.util.http.fileupload.FileUploadException +import java.util.* + + +val EXTENSIONS_IMAGE = Arrays.asList("bmp", "gif", "jpg", "png", "jpeg") +val EXTENSIONS_VIDEO = Arrays.asList("mp4", "avi", "mov", "mpg", "wmv", "mpeg") + +private fun validateFileExtension(fileName: String) { + var fileName = fileName + fileName = getFileExtension(fileName.lowercase(Locale.getDefault())) ?: "" + if (EXTENSIONS_IMAGE.contains(fileName) || EXTENSIONS_VIDEO.contains(fileName)) { + return + } + throw FileUploadException("유효하지않은 파일 형식입니다.") +} +fun getFileExtension(filename: String?): String? { + if (filename == null) { + return null + } + val dotIndex = filename.lastIndexOf(".") + if (dotIndex >= 0) { + return filename.substring(dotIndex + 1) + } + return "" +} \ No newline at end of file diff --git a/src/main/resources/templates/content/blog/write.html b/src/main/resources/templates/content/blog/write.html new file mode 100644 index 0000000..d43c656 --- /dev/null +++ b/src/main/resources/templates/content/blog/write.html @@ -0,0 +1,86 @@ + + + + + + + + + + + + + Spring Boot + + + + + + + +
+
+ +
+ +
+ + + \ No newline at end of file