Commit 7c930679 authored by Ghitha Dinan's avatar Ghitha Dinan

file fixing

parent 1cd7e2f4
package id.co.sangkuriang.inovasi.daerah.master.controller package id.co.sangkuriang.inovasi.daerah.master.controller
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.exception.NotFoundException import id.co.sangkuriang.inovasi.daerah.master.exception.NotFoundException
import org.apache.commons.io.IOUtils import org.apache.commons.io.IOUtils
import org.springframework.http.MediaType import org.springframework.http.MediaType
...@@ -28,6 +29,7 @@ class FileController { ...@@ -28,6 +29,7 @@ class FileController {
@RequestParam fileName: String, @RequestParam fileName: String,
response: HttpServletResponse response: HttpServletResponse
): ByteArray? { ): ByteArray? {
var fis: FileInputStream? = null
try { try {
var dir = "" var dir = ""
when (type) { when (type) {
...@@ -41,13 +43,15 @@ class FileController { ...@@ -41,13 +43,15 @@ class FileController {
val file = File(dir + File.separator + fileName) val file = File(dir + File.separator + fileName)
if (file.exists()) { if (file.exists()) {
val fis = FileInputStream(file) fis = FileInputStream(file)
return IOUtils.toByteArray(fis) return IOUtils.toByteArray(fis)
} }
throw NotFoundException() throw NotFoundException()
} catch (e: Exception) { } catch (e: Exception) {
throw e throw e
} finally {
fis?.close()
} }
} }
} }
...@@ -9,12 +9,9 @@ import org.apache.commons.io.FileUtils ...@@ -9,12 +9,9 @@ import org.apache.commons.io.FileUtils
import org.springframework.security.core.context.SecurityContextHolder import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.web.multipart.MultipartFile import org.springframework.web.multipart.MultipartFile
import java.io.File import java.io.File
import java.io.IOException import java.io.FileOutputStream
import java.net.MalformedURLException import java.net.MalformedURLException
import java.net.URL import java.net.URL
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletRequest
fun getUsersLogin(): Users? { fun getUsersLogin(): Users? {
...@@ -59,11 +56,12 @@ fun getPathUrl(request: HttpServletRequest): String { ...@@ -59,11 +56,12 @@ fun getPathUrl(request: HttpServletRequest): String {
fun uploadFile( fun uploadFile(
file: MultipartFile, multipart: MultipartFile,
httpServletRequest: HttpServletRequest, httpServletRequest: HttpServletRequest,
path: String, path: String,
fileType: String? = "images" fileType: String? = "images"
): String { ): String {
var fos: FileOutputStream? = null
try { try {
var dirType = "" var dirType = ""
when (path) { when (path) {
...@@ -75,28 +73,30 @@ fun uploadFile( ...@@ -75,28 +73,30 @@ fun uploadFile(
} }
} }
val root: Path = Paths.get(path) val fileName =
if (!Files.exists(root)) { System.currentTimeMillis().toString() + "-" + multipart.originalFilename?.trim()?.replace(" ", "-")
try {
Files.createDirectories(Paths.get(path)) val dir = File(path)
} catch (e: IOException) { if (!dir.exists()) {
throw RuntimeException("Could not create upload folder!") dir.mkdirs()
}
} }
val fileName = val convFile = File(dir.absolutePath + File.separator + fileName)
System.currentTimeMillis().toString() + "-" + file.originalFilename?.trim()?.replace(" ", "-") fos = FileOutputStream(convFile)
Files.copy(file.inputStream, root.resolve(fileName)) fos.write(multipart.bytes)
fos.close()
return getBaseUrl(httpServletRequest) + "file/$fileType?type=$dirType&fileName=" + fileName return getBaseUrl(httpServletRequest) + "file/$fileType?type=$dirType&fileName=" + fileName
} catch (e: Exception) { } catch (e: Exception) {
throw RuntimeException("Could not store the file. Error: " + e.message) throw RuntimeException("Could not store the file. Error: " + e.message)
} finally {
fos?.close()
} }
} }
fun deleteFile(fileDir: String, file: String) { fun deleteFile(fileDir: String, file: String?) {
try { try {
val delete = File(fileDir + File.separator + file.substringAfterLast("fileName=")) val delete = File(fileDir + File.separator + file?.substringAfterLast("fileName="))
if (delete.exists()) { if (delete.exists()) {
FileUtils.forceDelete(delete) FileUtils.forceDelete(delete)
} }
......
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