Commit d7ad7ed6 authored by Ghitha Dinan's avatar Ghitha Dinan

add file contoller

parent 76247feb
...@@ -6,10 +6,5 @@ const val ROOT_DIR = "C:\\Users\\ghith\\Desktop\\inovasi_daerah_master\\" ...@@ -6,10 +6,5 @@ const val ROOT_DIR = "C:\\Users\\ghith\\Desktop\\inovasi_daerah_master\\"
//const val ROOT_DIR = "/home/inovasi_daerah_master/data/" //const val ROOT_DIR = "/home/inovasi_daerah_master/data/"
val UPLOAD_DIR = "${ROOT_DIR}uploads${File.separator}" val UPLOAD_DIR = "${ROOT_DIR}uploads${File.separator}"
val UPLOAD_USER_DIR = "${UPLOAD_DIR}users"
val UPLOAD_APPLICATION_DIR = "${UPLOAD_DIR}application"
val UPLOAD_BANNER_APPLICATION_DIR = "$UPLOAD_APPLICATION_DIR${File.separator}video"
val UPLOAD_IMAGE_APPLICATION_QR_DIR = "$UPLOAD_APPLICATION_DIR${File.separator}qr"
val UPLOAD_IMAGE_APPLICATION_DETAIL_DIR = "$UPLOAD_APPLICATION_DIR${File.separator}detail"
val UPLOAD_ICON_APPLICATION_CATEGORY_DIR = "${UPLOAD_DIR}apllication_category/icon" val UPLOAD_ICON_APPLICATION_CATEGORY_DIR = "${UPLOAD_DIR}apllication_category/icon"
val UPLOAD_IMAGE_APPLICATION_CATEGORY_DIR = "${UPLOAD_DIR}apllication_category/image" val UPLOAD_IMAGE_APPLICATION_CATEGORY_DIR = "${UPLOAD_DIR}apllication_category/image"
package id.co.sangkuriang.inovasi.daerah.master.controller
import id.co.sangkuriang.inovasi.daerah.master.constant.*
import id.co.sangkuriang.inovasi.daerah.master.exception.NotFoundException
import org.apache.commons.io.IOUtils
import org.springframework.http.MediaType
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody
import java.io.File
import java.io.FileInputStream
import javax.servlet.http.HttpServletResponse
@Controller
@RequestMapping("file")
class FileController {
@RequestMapping(
value = ["images"],
method = [RequestMethod.GET],
produces = [MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE]
)
@ResponseBody
fun images(
@RequestParam type: String,
@RequestParam fileName: String,
response: HttpServletResponse
): ByteArray? {
try {
var dir = ""
when (type) {
"application-category-icon" -> {
dir = UPLOAD_ICON_APPLICATION_CATEGORY_DIR
}
"application-category-image" -> {
dir = UPLOAD_IMAGE_APPLICATION_CATEGORY_DIR
}
}
val file = File(dir + File.separator + fileName)
if (file.exists()) {
val fis = FileInputStream(file)
return IOUtils.toByteArray(fis)
}
throw NotFoundException()
} catch (e: Exception) {
throw e
}
}
}
package id.co.sangkuriang.inovasi.daerah.master.utility package id.co.sangkuriang.inovasi.daerah.master.utility
import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.ObjectMapper
import id.co.sangkuriang.inovasi.daerah.master.constant.* import id.co.sangkuriang.inovasi.daerah.master.constant.UPLOAD_ICON_APPLICATION_CATEGORY_DIR
import id.co.sangkuriang.inovasi.daerah.master.constant.UPLOAD_IMAGE_APPLICATION_CATEGORY_DIR
import id.co.sangkuriang.inovasi.daerah.master.pojo.Users import id.co.sangkuriang.inovasi.daerah.master.pojo.Users
import org.apache.commons.io.FileUtils import org.apache.commons.io.FileUtils
import org.springframework.security.core.context.SecurityContextHolder import org.springframework.security.core.context.SecurityContextHolder
...@@ -63,27 +64,12 @@ fun uploadFile( ...@@ -63,27 +64,12 @@ fun uploadFile(
try { try {
var dirType = "" var dirType = ""
when (path) { when (path) {
UPLOAD_USER_DIR -> {
dirType = "users"
}
UPLOAD_APPLICATION_DIR -> {
dirType = "application"
}
UPLOAD_ICON_APPLICATION_CATEGORY_DIR -> { UPLOAD_ICON_APPLICATION_CATEGORY_DIR -> {
dirType = "application-category-icon" dirType = "application-category-icon"
} }
UPLOAD_IMAGE_APPLICATION_CATEGORY_DIR -> { UPLOAD_IMAGE_APPLICATION_CATEGORY_DIR -> {
dirType = "application-category-image" dirType = "application-category-image"
} }
UPLOAD_BANNER_APPLICATION_DIR -> {
dirType = "application-banner"
}
UPLOAD_IMAGE_APPLICATION_QR_DIR -> {
dirType = "application-qr"
}
UPLOAD_IMAGE_APPLICATION_DETAIL_DIR -> {
dirType = "application-detail"
}
} }
val root: Path = Paths.get(path) val root: Path = Paths.get(path)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment