Updated unzip for auto create all files and delete all files
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
c38b8767a5
commit
92a0d2aafb
@ -32,6 +32,10 @@ object FileStorageFactory {
|
||||
getProvider().delete(fileName)
|
||||
}
|
||||
|
||||
fun deleteAll() {
|
||||
getProvider().deleteAll()
|
||||
}
|
||||
|
||||
fun get(fileName: String): FileResponse {
|
||||
return getProvider().get(fileName)
|
||||
}
|
||||
@ -44,9 +48,11 @@ object FileStorageFactory {
|
||||
return null
|
||||
}
|
||||
|
||||
fun unzip(inputStream: InputStream) {
|
||||
fun unzip(inputStream: InputStream): List<File> {
|
||||
if (getProvider() is FileStorageUnzip) {
|
||||
(getProvider() as FileStorageUnzip).unzip(inputStream, null)
|
||||
return (getProvider() as FileStorageUnzip).unzip(inputStream, null)
|
||||
}
|
||||
|
||||
return emptyList()
|
||||
}
|
||||
}
|
@ -54,6 +54,13 @@ open class FileStorageLocalProvider(
|
||||
}
|
||||
}
|
||||
|
||||
override fun deleteAll() {
|
||||
val file = File(basePath)
|
||||
if (file.isDirectory) {
|
||||
file.deleteRecursively()
|
||||
}
|
||||
}
|
||||
|
||||
override fun zip(sourceFolder: String?, os: OutputStream) {
|
||||
FileZipper.zipToStream(sourceFolder ?: basePath, os)
|
||||
}
|
||||
@ -62,7 +69,7 @@ open class FileStorageLocalProvider(
|
||||
return FileZipper.zipToBytes(sourceFolder ?: basePath)
|
||||
}
|
||||
|
||||
override fun unzip(inputStream: InputStream, destinationFolder: String?) {
|
||||
override fun unzip(inputStream: InputStream, destinationFolder: String?): List<File> {
|
||||
return FileZipper.unzip(inputStream, destinationFolder ?: basePath)
|
||||
}
|
||||
}
|
@ -6,4 +6,5 @@ interface FileStorageProvider {
|
||||
fun store(file: File): FileResponse
|
||||
fun get(fileName: String): FileResponse
|
||||
fun delete(fileName: String)
|
||||
fun deleteAll()
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
package com.cubetiqs.web.modules.file
|
||||
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
|
||||
interface FileStorageUnzip {
|
||||
fun unzip(inputStream: InputStream, destinationFolder: String?)
|
||||
fun unzip(inputStream: InputStream, destinationFolder: String?): List<File>
|
||||
}
|
@ -86,7 +86,9 @@ object FileZipper {
|
||||
}
|
||||
}
|
||||
|
||||
fun unzip(fis: InputStream, destFolder: String) {
|
||||
fun unzip(fis: InputStream, destFolder: String): List<File> {
|
||||
val extractFiles = mutableListOf<File>()
|
||||
|
||||
try {
|
||||
val root = File(destFolder)
|
||||
if (!root.exists()) {
|
||||
@ -104,12 +106,16 @@ object FileZipper {
|
||||
file.mkdirs()
|
||||
}
|
||||
}
|
||||
|
||||
extractFiles.add(file)
|
||||
zis.closeEntry()
|
||||
}
|
||||
zis.close()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
return extractFiles
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
|
@ -102,11 +102,13 @@ class UploaderController @Autowired constructor(
|
||||
|
||||
@ResponseStatus(value = org.springframework.http.HttpStatus.CREATED)
|
||||
@PostMapping("/unzip", consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
|
||||
@Operation(summary = "Zip a file")
|
||||
@Operation(summary = "Unzip a file")
|
||||
fun unzip(
|
||||
@RequestPart file: MultipartFile,
|
||||
) {
|
||||
FileStorageFactory.unzip(file.inputStream)
|
||||
): List<UploaderEntity> {
|
||||
val files = FileStorageFactory.unzip(file.inputStream)
|
||||
.map { UploaderEntity.fromFileWithoutStore(it) }
|
||||
return repository.saveAll(files)
|
||||
}
|
||||
|
||||
@ResponseStatus(value = org.springframework.http.HttpStatus.OK)
|
||||
@ -132,6 +134,9 @@ class UploaderController @Autowired constructor(
|
||||
val entity = repository.findById(UUID.fromString(id)).orElseThrow {
|
||||
throw IllegalArgumentException("File not found")
|
||||
}
|
||||
if (!entity.path.isNullOrEmpty()) {
|
||||
FileStorageFactory.delete(entity.path!!)
|
||||
}
|
||||
repository.delete(entity)
|
||||
}
|
||||
|
||||
@ -139,6 +144,7 @@ class UploaderController @Autowired constructor(
|
||||
@DeleteMapping
|
||||
@Operation(summary = "Delete all files")
|
||||
fun deleteAll() {
|
||||
FileStorageFactory.deleteAll()
|
||||
repository.deleteAll()
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@ import org.springframework.web.multipart.MultipartFile
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import java.io.Serializable
|
||||
import java.nio.file.Files
|
||||
import java.util.*
|
||||
import javax.persistence.*
|
||||
|
||||
@ -94,6 +95,17 @@ open class UploaderEntity(
|
||||
return uploader
|
||||
}
|
||||
|
||||
fun fromFileWithoutStore(file: File): UploaderEntity {
|
||||
val uploader = UploaderEntity()
|
||||
uploader.file = file
|
||||
uploader.providerType = "local"
|
||||
uploader.filename = file.name
|
||||
uploader.contentType = Files.probeContentType(file.toPath())
|
||||
uploader.contentLength = file.length()
|
||||
uploader.path = file.path
|
||||
return uploader
|
||||
}
|
||||
|
||||
fun fromUploader(uploader: UploaderEntity): MultipartFile? {
|
||||
if (uploader.partFile != null) {
|
||||
return uploader.partFile
|
||||
|
Loading…
Reference in New Issue
Block a user