spring-web-modules/api/src/main/kotlin/com/cubetiqs/web/modules/uploader/FileStorageLocalProvider.kt
Sambo Chea 048c11ee62
Some checks failed
continuous-integration/drone/push Build is failing
Add uploader and file storage provider with openapi docs and demo for modules
2022-04-21 11:57:44 +07:00

54 lines
1.4 KiB
Kotlin

package com.cubetiqs.web.modules.uploader
import java.io.File
import java.io.FileNotFoundException
open class FileStorageLocalProvider(
private val basePath: String,
) : FileStorageProvider {
private fun loadBasePath(fileName: String): String {
val prefixPath = if (basePath.endsWith("/")) {
""
} else {
"/"
}
return basePath + prefixPath + fileName
}
override fun store(file: File): FileResponse {
if (!file.exists()) {
throw FileNotFoundException("File not found")
}
val path = loadBasePath(file.name)
val savedFile = file.copyTo(File(path), true)
return FileResponse(
file = savedFile,
shortPath = path,
)
}
override fun get(fileName: String): FileResponse {
val path = loadBasePath(fileName)
val file = File(path)
if (!file.exists()) {
throw FileNotFoundException("File $fileName not found")
}
return FileResponse(
file = file,
shortPath = path,
)
}
override fun delete(fileName: String) {
val path = loadBasePath(fileName)
val file = File(path)
if (file.isFile) {
file.delete()
} else {
throw IllegalArgumentException("File $fileName not found")
}
}
}