Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
I
Inovasi Daerah Master
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ghitha Dinan
Inovasi Daerah Master
Commits
7c930679
Commit
7c930679
authored
Nov 09, 2021
by
Ghitha Dinan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
file fixing
parent
1cd7e2f4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
19 deletions
+23
-19
FileController.kt
...uriang/inovasi/daerah/master/controller/FileController.kt
+6
-2
Helpers.kt
...d/co/sangkuriang/inovasi/daerah/master/utility/Helpers.kt
+17
-17
No files found.
src/main/kotlin/id/co/sangkuriang/inovasi/daerah/master/controller/FileController.kt
View file @
7c930679
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
()
}
}
}
src/main/kotlin/id/co/sangkuriang/inovasi/daerah/master/utility/Helpers.kt
View file @
7c930679
...
...
@@ -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
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment