spring-web-modules/api/src/main/kotlin/com/cubetiqs/web/modules/file/FileStorageLocalProvider.kt
Sambo Chea c38b8767a5
Some checks failed
continuous-integration/drone/push Build is failing
Add file storage unzip for uploader and extract the resources
2022-04-21 16:53:00 +07:00

68 lines
1.8 KiB
Kotlin

package com.cubetiqs.web.modules.file
import java.io.File
import java.io.FileNotFoundException
import java.io.InputStream
import java.io.OutputStream
open class FileStorageLocalProvider(
private val basePath: String,
) : FileStorageProvider, FileStorageZip, FileStorageUnzip {
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")
}
}
override fun zip(sourceFolder: String?, os: OutputStream) {
FileZipper.zipToStream(sourceFolder ?: basePath, os)
}
override fun zip(sourceFolder: String?): ByteArray {
return FileZipper.zipToBytes(sourceFolder ?: basePath)
}
override fun unzip(inputStream: InputStream, destinationFolder: String?) {
return FileZipper.unzip(inputStream, destinationFolder ?: basePath)
}
}