44 lines
1.7 KiB
Kotlin
44 lines
1.7 KiB
Kotlin
package com.cubetiqs.x.lovex.rest
|
|
|
|
import com.cubetiqs.x.lovex.INDEX_PATH
|
|
import org.springframework.beans.factory.annotation.Autowired
|
|
import org.springframework.core.io.ByteArrayResource
|
|
import org.springframework.http.HttpMethod
|
|
import org.springframework.util.FileCopyUtils
|
|
import org.springframework.web.bind.annotation.GetMapping
|
|
import org.springframework.web.bind.annotation.RequestMapping
|
|
import org.springframework.web.bind.annotation.RestController
|
|
import org.springframework.web.client.RestTemplate
|
|
import javax.servlet.http.HttpServletResponse
|
|
|
|
|
|
@RestController
|
|
@RequestMapping("$INDEX_PATH/background")
|
|
class BackgroundGeneratorRest @Autowired constructor(
|
|
private val restTemplate: RestTemplate,
|
|
) {
|
|
@GetMapping
|
|
fun randomBackground(
|
|
response: HttpServletResponse,
|
|
) {
|
|
val url = "https://images.unsplash.com/photo-1417325384643-aac51acc9e5d?q=75&fm=jpg&w=1080&fit=max"
|
|
val image = getByteArrayFromUrl(url, webClient = restTemplate)
|
|
FileCopyUtils.copy(image, response.outputStream)
|
|
}
|
|
|
|
@GetMapping("/random")
|
|
fun getRandomBackground(
|
|
response: HttpServletResponse,
|
|
) {
|
|
val url = "https://source.unsplash.com/random/1920x1080"
|
|
val responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, ByteArrayResource::class.java)
|
|
println(responseEntity.body?.byteArray)
|
|
FileCopyUtils.copy(responseEntity.body?.byteArray ?: ByteArray(0), response.outputStream)
|
|
}
|
|
}
|
|
|
|
fun getByteArrayFromUrl(url: String, webClient: RestTemplate): ByteArray {
|
|
return webClient
|
|
.getForObject(url, ByteArrayResource::class.java)
|
|
?.byteArray ?: ByteArray(0)
|
|
} |