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

file fixing

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