Add zipper for zip all files with file local provider
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Sambo Chea 2022-04-21 15:09:01 +07:00
parent 048c11ee62
commit fd12704bd5
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG Key ID: 3C7CF22A05D95490
9 changed files with 113 additions and 9 deletions

View File

@ -1,7 +1,7 @@
package com.cubetiqs.web.modules package com.cubetiqs.web.modules
import com.cubetiqs.web.modules.uploader.FileStorageFactory import com.cubetiqs.web.modules.file.FileStorageFactory
import com.cubetiqs.web.modules.uploader.FileStorageLocalProvider import com.cubetiqs.web.modules.file.FileStorageLocalProvider
import org.springframework.beans.factory.annotation.Value import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Lazy import org.springframework.context.annotation.Lazy
import org.springframework.stereotype.Component import org.springframework.stereotype.Component

View File

@ -1,4 +1,4 @@
package com.cubetiqs.web.modules.uploader package com.cubetiqs.web.modules.file
import java.io.File import java.io.File

View File

@ -1,4 +1,4 @@
package com.cubetiqs.web.modules.uploader package com.cubetiqs.web.modules.file
import org.springframework.web.multipart.MultipartFile import org.springframework.web.multipart.MultipartFile
import java.io.File import java.io.File
@ -7,7 +7,7 @@ object FileStorageFactory {
private var provider: FileStorageProvider? = null private var provider: FileStorageProvider? = null
fun setProvider(provider: FileStorageProvider) { fun setProvider(provider: FileStorageProvider) {
this.provider = provider FileStorageFactory.provider = provider
} }
fun getProvider(): FileStorageProvider { fun getProvider(): FileStorageProvider {
@ -24,7 +24,7 @@ object FileStorageFactory {
) "C:\\Windows\\Temp" else "/tmp" ) "C:\\Windows\\Temp" else "/tmp"
val temp = File("$tempPath/${file.originalFilename}") val temp = File("$tempPath/${file.originalFilename}")
file.transferTo(temp) file.transferTo(temp)
return this.store(temp) return store(temp)
} }
fun delete(fileName: String) { fun delete(fileName: String) {
@ -34,4 +34,12 @@ object FileStorageFactory {
fun get(fileName: String): FileResponse { fun get(fileName: String): FileResponse {
return getProvider().get(fileName) return getProvider().get(fileName)
} }
fun zipAll(): ByteArray? {
if (getProvider() is FileStorageZipper) {
return (getProvider() as FileStorageZipper).zip(null)
}
return null
}
} }

View File

@ -1,11 +1,12 @@
package com.cubetiqs.web.modules.uploader package com.cubetiqs.web.modules.file
import java.io.File import java.io.File
import java.io.FileNotFoundException import java.io.FileNotFoundException
import java.io.OutputStream
open class FileStorageLocalProvider( open class FileStorageLocalProvider(
private val basePath: String, private val basePath: String,
) : FileStorageProvider { ) : FileStorageProvider, FileStorageZipper {
private fun loadBasePath(fileName: String): String { private fun loadBasePath(fileName: String): String {
val prefixPath = if (basePath.endsWith("/")) { val prefixPath = if (basePath.endsWith("/")) {
"" ""
@ -51,4 +52,12 @@ open class FileStorageLocalProvider(
throw IllegalArgumentException("File $fileName not found") 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)
}
} }

View File

@ -1,4 +1,4 @@
package com.cubetiqs.web.modules.uploader package com.cubetiqs.web.modules.file
import java.io.File import java.io.File

View File

@ -0,0 +1,13 @@
package com.cubetiqs.web.modules.file
import java.io.ByteArrayOutputStream
import java.io.OutputStream
interface FileStorageZipper {
fun zip(sourceFolder: String?, os: OutputStream)
fun zip(sourceFolder: String?): ByteArray {
val os = ByteArrayOutputStream()
zip(sourceFolder, os)
return os.toByteArray()
}
}

View File

@ -0,0 +1,58 @@
package com.cubetiqs.web.modules.file
import java.io.ByteArrayOutputStream
import java.io.FileOutputStream
import java.io.IOException
import java.io.OutputStream
import java.nio.file.*
import java.nio.file.attribute.BasicFileAttributes
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
object FileZipper {
fun zip(sourceFolder: String, destFolder: String) {
try {
val zipFolder = if (destFolder.endsWith(".zip")) {
""
} else {
".zip"
}
FileOutputStream(zipFolder).use { fos ->
zipToStream(sourceFolder, fos)
}
} catch (e: IOException) {
e.printStackTrace()
}
}
fun zipToStream(sourceFolder: String, os: OutputStream) {
ZipOutputStream(os).use { zos ->
val sourcePath: Path = Paths.get(sourceFolder)
// Walk the tree structure using WalkFileTree method
Files.walkFileTree(sourcePath, object : SimpleFileVisitor<Path>() {
@Throws(IOException::class)
override fun preVisitDirectory(dir: Path, attrs: BasicFileAttributes): FileVisitResult {
if (sourcePath != dir) {
zos.putNextEntry(ZipEntry(sourcePath.relativize(dir).toString() + "/"))
zos.closeEntry()
}
return FileVisitResult.CONTINUE
}
@Throws(IOException::class)
override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult {
zos.putNextEntry(ZipEntry(sourcePath.relativize(file).toString()))
Files.copy(file, zos)
zos.closeEntry()
return FileVisitResult.CONTINUE
}
})
}
}
fun zipToBytes(sourceFolder: String): ByteArray {
val bos = ByteArrayOutputStream()
zipToStream(sourceFolder, bos)
return bos.toByteArray()
}
}

View File

@ -1,5 +1,6 @@
package com.cubetiqs.web.modules.uploader package com.cubetiqs.web.modules.uploader
import com.cubetiqs.web.modules.file.FileStorageFactory
import com.cubetiqs.web.util.RouteConstants import com.cubetiqs.web.util.RouteConstants
import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter import io.swagger.v3.oas.annotations.Parameter
@ -74,6 +75,20 @@ class UploaderController @Autowired constructor(
FileCopyUtils.copy(file.readBytes(), response.outputStream) FileCopyUtils.copy(file.readBytes(), response.outputStream)
} }
@ResponseStatus(value = org.springframework.http.HttpStatus.OK)
@GetMapping("/zip", produces = [MediaType.APPLICATION_OCTET_STREAM_VALUE])
@Operation(summary = "Zip all files")
fun zipAll(
response: HttpServletResponse,
) {
val zipBytes = FileStorageFactory.zipAll() ?: throw IllegalArgumentException("Zip file not found")
response.setHeader("Content-Disposition", "attachment; filename=\"files.zip\"")
response.contentType = "application/zip"
response.setContentLengthLong(zipBytes.size.toLong())
FileCopyUtils.copy(zipBytes, response.outputStream)
}
@ResponseStatus(value = org.springframework.http.HttpStatus.CREATED) @ResponseStatus(value = org.springframework.http.HttpStatus.CREATED)
@PostMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE]) @PostMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
@Operation(summary = "Upload a file") @Operation(summary = "Upload a file")

View File

@ -1,5 +1,6 @@
package com.cubetiqs.web.modules.uploader package com.cubetiqs.web.modules.uploader
import com.cubetiqs.web.modules.file.FileStorageFactory
import com.fasterxml.jackson.annotation.JsonIgnore import com.fasterxml.jackson.annotation.JsonIgnore
import org.hibernate.Hibernate import org.hibernate.Hibernate
import org.springframework.data.annotation.CreatedDate import org.springframework.data.annotation.CreatedDate