This commit is contained in:
lunaticbum
2024-10-23 10:07:45 +09:00
parent c4001848fc
commit 69d2df4ac4
19 changed files with 513 additions and 81 deletions
@@ -6,9 +6,10 @@ import com.google.gson.Gson
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import kr.lunaticbum.back.lun.configs.GlobalEnvironment
import kr.lunaticbum.back.lun.model.CurrentWeather
import kr.lunaticbum.back.lun.model.FileSaveResult
import kr.lunaticbum.back.lun.model.LocationLogService
import kr.lunaticbum.back.lun.controllers.UserController.Companion.ApiKeyWordKey
import kr.lunaticbum.back.lun.controllers.UserController.Companion.EncType11
import kr.lunaticbum.back.lun.controllers.UserController.Companion.EncTypeKey
import kr.lunaticbum.back.lun.model.*
import kr.lunaticbum.back.lun.utils.LogService
import kr.lunaticbum.back.lun.utils.getFileExtension
import org.springframework.beans.factory.annotation.Autowired
@@ -17,6 +18,7 @@ import org.springframework.core.io.Resource
import org.springframework.core.io.UrlResource
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import org.springframework.web.reactive.function.client.WebClient
@@ -36,11 +38,22 @@ class BlogController() {
private lateinit var locationLogService: LocationLogService
@Autowired
lateinit var logService: LogService
private lateinit var postManageg: PostManageg
@GetMapping("write")
fun writ() : ModelAndView{
@Autowired
lateinit var logService: LogService
val WRITE_PERMISSION_KEY = "PERMISSION"
@GetMapping("write/{token}","write")
fun writ(@PathVariable token : String? ) : ModelAndView{
val vm = ModelAndView("content/blog/write")
if (token.equals("TEMP_TOKEN_VIBUM")) {
vm.modelMap.put(WRITE_PERMISSION_KEY,"OK")
vm.modelMap.put(EncTypeKey, EncType11)
vm.modelMap.put(ApiKeyWordKey,"WRITE")
vm.modelMap.put("title","회원이 들어는 구나~!!")
} else {
vm.modelMap.put(WRITE_PERMISSION_KEY,"NO")
}
// when(System.currentTimeMillis() % 5L) {
// 0L -> vm.modelMap.put(EncTypeKey,"T4")
// 1L -> vm.modelMap.put(EncTypeKey,"T3")
@@ -51,6 +64,53 @@ class BlogController() {
return vm
}
@PostMapping("post.ajax")
fun post(httpServletRequest: HttpServletRequest, @RequestBody jsonString: String) : ResponseEntity<ResponceResult> {
logService.log("${httpServletRequest.requestURI}")
logService.log(jsonString)
var lResultCode = 0
var lResultMsg = "Suscces"
var u : UserDetails? = null
val decodedBytes: ByteArray = Base64.getDecoder().decode(jsonString)
String(decodedBytes).let {
Gson().fromJson<RequestModel>(it, RequestModel::class.java)?.let { model ->
logService.log(Gson().toJson(model))
model.data?.let { jsonString ->
try {
val reqString = jsonString.split(globalEvv.padding(model.getKeyword()))
val nb = arrayListOf<String>()
val na = arrayListOf<String>()
reqString[0].replace(globalEvv.padding(model.getKeyword()),"").split("").toList().let { na.addAll(it) }
reqString[1].replace(globalEvv.padding(model.getKeyword()),"").split("").toList().let { nb.addAll(it) }
var max = nb.size + na.size
var fullData = arrayListOf<String>()
for (idx in 0..max) { if (idx % 2 == 0) { if (nb.size > 0) { fullData.add(nb.removeLast()) } } else { if (na.size > 0) { fullData.add(na.removeLast()) } } }
logService.log(fullData.joinToString(""))
val target = Gson().fromJson(fullData.joinToString(""), Post::class.java) ?: Post()
var user = postManageg.save(target)
if (user != null) {
lResultMsg = "save post"
lResultCode = 0
} else {
lResultMsg = "not founding user[can't find same id,email.. ]"
lResultCode = 7100
}
} catch (e: Exception) {
e.printStackTrace()
lResultMsg = "unknown exception"
lResultCode = 7999
}
}
}
}
val responce = ResponseEntity.ok().headers {
}.contentType(MediaType.APPLICATION_JSON).body(ResponceResult().apply {
this.resultCode = lResultCode
this.resultMsg = lResultMsg
})
return responce
}
@GetMapping("viewer/{blogId}")
fun viewer(@PathVariable blogId : String) : ModelAndView{
val vm = ModelAndView("content/blog/viewer")
@@ -67,7 +127,7 @@ class BlogController() {
@GetMapping("recent")
fun recent() : ModelAndView{
val vm = ModelAndView("content/blog/viewer")
locationLogService.find50().forEach {
locationLogService.find20().forEach {
logService.log(Gson().toJson(it))
}
locationLogService.getLocationLog()?.let {
@@ -11,9 +11,9 @@ import kr.lunaticbum.back.lun.utils.LogService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
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.util.*
@@ -29,6 +29,19 @@ class BumsPrivate {
@Autowired
lateinit var locationService: LocationLogService
@GetMapping("where")
fun where() : ModelAndView {
val m = ModelAndView("content/private/where")
locationService.find20().apply {
m.modelMap.put("locations",this.reversed())
forEach {
logService.log(it.timeString.plus(it.mAddressLines.joinToString(",")))
}
}
m.modelMap.put("title","돼지 여기있다요~!!")
return m
}
@ResponseBody
@PostMapping("save/loc.api")
fun login(httpServletRequest: HttpServletRequest, @RequestBody jsonString: String) : ResponseEntity<ResponceResult> {
@@ -1,5 +1,9 @@
package kr.lunaticbum.back.lun.controllers
import com.google.gson.Gson
import kr.lunaticbum.back.lun.model.PostManageg
import kr.lunaticbum.back.lun.utils.LogService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@@ -9,9 +13,28 @@ import org.springframework.web.servlet.ModelAndView
@RequestMapping()
class Home {
@Autowired
lateinit var logService: LogService
@Autowired
private lateinit var postManageg: PostManageg
@GetMapping("/","/home")
fun home() : ModelAndView {
val vm = ModelAndView("content/blog/write")
val vm = ModelAndView("content/home")
vm.modelMap.put("posts", postManageg.find20().apply {
this.forEach {
logService.log(Gson().toJson(it))
}
})
return vm
}
@GetMapping("/licenses")
fun licenses() : ModelAndView {
val vm = ModelAndView("content/licenses")
return vm
}
}
@@ -18,8 +18,11 @@ import org.springframework.scheduling.annotation.Scheduled
import org.springframework.web.bind.annotation.*
import org.springframework.web.reactive.function.client.WebClient
import java.math.BigDecimal
import java.math.RoundingMode
import java.text.SimpleDateFormat
import java.time.Duration
import java.util.*
import java.util.concurrent.TimeUnit
@RestController
@@ -131,19 +134,21 @@ class Telegram {
) {
locationLogService.getLocationLog()?.let {
try {
val client0 = WebClient.create()
val result = client0.get()
WebClient.create().get()
.uri("http://api.weatherapi.com/v1/current.json?key=${globalEvv.weatherApiKey}&q=${it.mLatitude},${it.mLongitude}&aqi=no")
.retrieve()
.bodyToMono(String::class.java)
.block() ?: "FAIL"
Gson().fromJson(result, CurrentWeather::class.java)?.let { sss ->
val client = WebClient.create()
client.get()
.uri("https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=${sss.getSummaryInfo(BigDecimal(it.mLatitude).setScale(5).toString(),BigDecimal(it.mLongitude).setScale(5).toString())}")
.retrieve()
.bodyToMono(String::class.java).block() ?: "FAIL"
}
.timeout(Duration.ofSeconds(30L))
.block()?.let { result ->
Gson().fromJson(result, CurrentWeather::class.java)?.let { sss ->
val fullUrl = "https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=${sss.getSummaryInfo(BigDecimal(it.mLatitude).setScale(3, RoundingMode.HALF_UP).toString(),BigDecimal(it.mLongitude).setScale(3, RoundingMode.HALF_UP).toString())}"
logService.log("fullUrl >>> ${fullUrl}")
WebClient.create().get()
.uri(fullUrl)
.retrieve()
.bodyToMono(String::class.java).block() ?: "FAIL"
}
}
}
catch (e : Exception) {
@@ -23,12 +23,15 @@ import kotlin.math.log
@RestController
@RequestMapping("/user")
class UserController {
val EncTypeKey = "enc"
val EncType00 = "T0"
val EncType11 = "T3"
val EncType10 = "T2"
val EncType01 = "T1"
val ApiKeyWordKey = "keyword"
companion object{
val EncTypeKey = "enc"
val EncType00 = "T0"
val EncType11 = "T3"
val EncType10 = "T2"
val EncType01 = "T1"
val ApiKeyWordKey = "keyword"
}
@Autowired
lateinit var globalEvv : GlobalEnvironment
@@ -1,10 +1,6 @@
package kr.lunaticbum.back.lun.model
import com.google.gson.Gson
import com.mongodb.client.model.Sorts.descending
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kr.lunaticbum.back.lun.configs.GlobalEnvironment
import kr.lunaticbum.back.lun.utils.LogService
import lombok.AllArgsConstructor
@@ -14,13 +10,11 @@ import org.bson.codecs.pojo.annotations.BsonIgnore
import org.jsoup.Jsoup
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.annotation.Id
import org.springframework.data.domain.Page
import org.springframework.data.mongodb.core.mapping.Document
import org.springframework.data.mongodb.repository.ReactiveMongoRepository
import org.springframework.stereotype.Repository
import org.springframework.stereotype.Service
import org.springframework.web.reactive.function.client.WebClient
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import java.text.SimpleDateFormat
import java.time.Duration
@@ -97,8 +91,8 @@ class LocationLogService : LocationService {
@Autowired
private lateinit var logRepository: LocationLogRepository
fun find50() : List<LocationLog> {
return logRepository.findAll().takeLast(50).buffer(50).blockLast(Duration.ofSeconds(30)) ?: listOf()
fun find20() : List<LocationLog> {
return logRepository.findAll().takeLast(20).buffer(20).blockLast(Duration.ofSeconds(30)) ?: listOf()
}
fun getLocationLog() : LocationLog? {
return logRepository.findFirstByOrderByTimeDesc().block()
@@ -110,7 +104,6 @@ class LocationLogService : LocationService {
logRepository.save(log).subscribe( { println("saved msg after ${it}") },{e -> e.printStackTrace()},{
println("saved msg comp")
})
}
}
@@ -0,0 +1,75 @@
package kr.lunaticbum.back.lun.model
import kr.lunaticbum.back.lun.utils.LogService
import lombok.AllArgsConstructor
import lombok.Data
import lombok.NoArgsConstructor
import org.bson.BsonType
import org.bson.codecs.pojo.annotations.BsonCreator
import org.bson.codecs.pojo.annotations.BsonId
import org.bson.codecs.pojo.annotations.BsonRepresentation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.mongodb.core.mapping.Document
import org.springframework.data.mongodb.repository.Query
import org.springframework.data.mongodb.repository.ReactiveMongoRepository
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Repository
import org.springframework.stereotype.Service
import reactor.core.publisher.Mono
import java.time.Duration
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "Post")
class Post {
@BsonId
@BsonRepresentation(BsonType.OBJECT_ID)
var id: String? = null
var title : String? = null
var content : String? = null
var category : String? = null
var tags : String? = null
var writer : String? = null
var writeTime : Long = 0
var modifyTime : Long = 0
var posting : Boolean = false
}
@Repository
interface PostRepository : ReactiveMongoRepository<Post, String> {
}
@Service
class PostManageg {
@Autowired
private lateinit var logService: LogService
@Autowired
private lateinit var postRepository: PostRepository
@Autowired
private lateinit var bCryptPasswordEncoder: PasswordEncoder
fun find20() : List<Post> {
return postRepository.findAll().takeLast(20).buffer(20).blockLast(Duration.ofSeconds(30)) ?: listOf()
}
fun save(post: Post): Mono<Post> {
println("saved user before ${post}")
// user.hashPassword(bCryptPasswordEncoder)
return postRepository.save(post).apply {
subscribe {
println("saved user after ${this@apply}")
}
}
}
}