2022-04-21 08:57:54 +07:00
|
|
|
package com.cubetiqs.web.modules.uploader
|
|
|
|
|
2022-04-21 15:09:01 +07:00
|
|
|
import com.cubetiqs.web.modules.file.FileStorageFactory
|
2022-04-21 08:57:54 +07:00
|
|
|
import com.cubetiqs.web.util.RouteConstants
|
2022-04-21 11:57:44 +07:00
|
|
|
import io.swagger.v3.oas.annotations.Operation
|
2022-04-21 08:57:54 +07:00
|
|
|
import io.swagger.v3.oas.annotations.Parameter
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag
|
|
|
|
import org.springdoc.core.converters.models.PageableAsQueryParam
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired
|
|
|
|
import org.springframework.data.domain.Page
|
|
|
|
import org.springframework.data.domain.Pageable
|
2022-04-21 11:57:44 +07:00
|
|
|
import org.springframework.http.MediaType
|
|
|
|
import org.springframework.util.FileCopyUtils
|
2022-04-21 08:57:54 +07:00
|
|
|
import org.springframework.web.bind.annotation.*
|
2022-04-21 11:57:44 +07:00
|
|
|
import org.springframework.web.multipart.MultipartFile
|
2022-04-21 08:57:54 +07:00
|
|
|
import java.util.*
|
2022-04-21 11:57:44 +07:00
|
|
|
import javax.servlet.http.HttpServletResponse
|
2022-04-21 08:57:54 +07:00
|
|
|
|
|
|
|
@UploaderModule
|
|
|
|
@Tag(name = "Uploader Controller")
|
|
|
|
@RestController
|
|
|
|
@RequestMapping(RouteConstants.INDEX + "uploader")
|
|
|
|
class UploaderController @Autowired constructor(
|
|
|
|
private val repository: UploaderRepository,
|
|
|
|
) {
|
|
|
|
@GetMapping
|
|
|
|
@PageableAsQueryParam
|
2022-04-21 11:57:44 +07:00
|
|
|
@Operation(summary = "Get all files")
|
2022-04-21 08:57:54 +07:00
|
|
|
fun getAll(
|
|
|
|
@Parameter(hidden = true)
|
|
|
|
pageable: Pageable?,
|
|
|
|
): Page<UploaderEntity> {
|
|
|
|
return repository.findAll(pageable ?: Pageable.unpaged())
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:57:44 +07:00
|
|
|
@ResponseStatus(value = org.springframework.http.HttpStatus.OK)
|
|
|
|
@GetMapping("/{id}")
|
|
|
|
@Operation(summary = "Get a file by id")
|
|
|
|
fun get(
|
|
|
|
@PathVariable id: String,
|
|
|
|
): UploaderEntity {
|
|
|
|
val entity = repository.findById(UUID.fromString(id)).orElseThrow {
|
|
|
|
throw IllegalArgumentException("File not found")
|
|
|
|
}
|
|
|
|
return repository.save(entity)
|
|
|
|
}
|
|
|
|
|
|
|
|
@ResponseStatus(value = org.springframework.http.HttpStatus.OK)
|
|
|
|
@GetMapping("/{id}/stream", produces = [MediaType.APPLICATION_OCTET_STREAM_VALUE])
|
|
|
|
@Operation(summary = "Get file stream by id")
|
|
|
|
fun stream(
|
|
|
|
@PathVariable id: String,
|
|
|
|
@RequestParam(required = false, value = "download") download: Boolean?,
|
|
|
|
response: HttpServletResponse,
|
|
|
|
) {
|
|
|
|
val entity = repository.findById(UUID.fromString(id)).orElseThrow {
|
|
|
|
throw IllegalArgumentException("User not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entity.isFileExists()) {
|
|
|
|
throw IllegalArgumentException("File not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
val file = entity.getFile() ?: throw IllegalArgumentException("File source not found")
|
|
|
|
val disposition = if (download == true) {
|
|
|
|
"attachment"
|
|
|
|
} else {
|
|
|
|
"inline"
|
|
|
|
}
|
|
|
|
|
|
|
|
response.setHeader("Content-Disposition", "$disposition; filename=\"${entity.filename}\"")
|
|
|
|
response.contentType = entity.contentType
|
|
|
|
response.setContentLengthLong(file.length())
|
|
|
|
|
|
|
|
FileCopyUtils.copy(file.readBytes(), response.outputStream)
|
|
|
|
}
|
|
|
|
|
2022-04-21 15:09:01 +07:00
|
|
|
@ResponseStatus(value = org.springframework.http.HttpStatus.OK)
|
|
|
|
@GetMapping("/zip", produces = [MediaType.APPLICATION_OCTET_STREAM_VALUE])
|
|
|
|
@Operation(summary = "Zip all files")
|
|
|
|
fun zipAll(
|
2022-04-21 16:53:00 +07:00
|
|
|
@RequestParam(required = false, value = "filename", defaultValue = "files") filename: String?,
|
2022-04-21 15:09:01 +07:00
|
|
|
response: HttpServletResponse,
|
|
|
|
) {
|
|
|
|
val zipBytes = FileStorageFactory.zipAll() ?: throw IllegalArgumentException("Zip file not found")
|
2022-04-21 16:53:00 +07:00
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=\"$filename.zip\"")
|
2022-04-21 15:09:01 +07:00
|
|
|
response.contentType = "application/zip"
|
|
|
|
response.setContentLengthLong(zipBytes.size.toLong())
|
|
|
|
|
|
|
|
FileCopyUtils.copy(zipBytes, response.outputStream)
|
|
|
|
}
|
|
|
|
|
2022-04-21 08:57:54 +07:00
|
|
|
@ResponseStatus(value = org.springframework.http.HttpStatus.CREATED)
|
2022-04-21 11:57:44 +07:00
|
|
|
@PostMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
|
|
|
|
@Operation(summary = "Upload a file")
|
2022-04-21 08:57:54 +07:00
|
|
|
fun create(
|
2022-04-21 11:57:44 +07:00
|
|
|
@RequestPart file: MultipartFile,
|
2022-04-21 08:57:54 +07:00
|
|
|
): UploaderEntity {
|
2022-04-21 11:57:44 +07:00
|
|
|
val entity = UploaderEntity.fromFile(file)
|
|
|
|
return repository.save(entity)
|
2022-04-21 08:57:54 +07:00
|
|
|
}
|
|
|
|
|
2022-04-21 16:53:00 +07:00
|
|
|
@ResponseStatus(value = org.springframework.http.HttpStatus.CREATED)
|
|
|
|
@PostMapping("/unzip", consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
|
2022-04-21 17:11:13 +07:00
|
|
|
@Operation(summary = "Unzip a file")
|
2022-04-21 16:53:00 +07:00
|
|
|
fun unzip(
|
|
|
|
@RequestPart file: MultipartFile,
|
2022-04-21 17:11:13 +07:00
|
|
|
): List<UploaderEntity> {
|
|
|
|
val files = FileStorageFactory.unzip(file.inputStream)
|
|
|
|
.map { UploaderEntity.fromFileWithoutStore(it) }
|
|
|
|
return repository.saveAll(files)
|
2022-04-21 16:53:00 +07:00
|
|
|
}
|
|
|
|
|
2022-04-21 08:57:54 +07:00
|
|
|
@ResponseStatus(value = org.springframework.http.HttpStatus.OK)
|
|
|
|
@PutMapping("/{id}")
|
2022-04-21 11:57:44 +07:00
|
|
|
@Operation(summary = "Update a file by id")
|
2022-04-21 08:57:54 +07:00
|
|
|
fun update(
|
|
|
|
@PathVariable id: String,
|
|
|
|
@RequestBody body: UploaderEntity
|
|
|
|
): UploaderEntity {
|
2022-04-21 11:57:44 +07:00
|
|
|
val entity = repository.findById(UUID.fromString(id)).orElseThrow {
|
2022-04-21 08:57:54 +07:00
|
|
|
throw IllegalArgumentException("File not found")
|
|
|
|
}
|
2022-04-21 11:57:44 +07:00
|
|
|
body.id = entity.id
|
2022-04-21 08:57:54 +07:00
|
|
|
return repository.save(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
@ResponseStatus(value = org.springframework.http.HttpStatus.NO_CONTENT)
|
|
|
|
@DeleteMapping("/{id}")
|
2022-04-21 11:57:44 +07:00
|
|
|
@Operation(summary = "Delete a file by id")
|
2022-04-21 08:57:54 +07:00
|
|
|
fun delete(
|
|
|
|
@PathVariable id: String,
|
|
|
|
) {
|
2022-04-21 11:57:44 +07:00
|
|
|
val entity = repository.findById(UUID.fromString(id)).orElseThrow {
|
2022-04-21 08:57:54 +07:00
|
|
|
throw IllegalArgumentException("File not found")
|
|
|
|
}
|
2022-04-21 17:11:13 +07:00
|
|
|
if (!entity.path.isNullOrEmpty()) {
|
|
|
|
FileStorageFactory.delete(entity.path!!)
|
|
|
|
}
|
2022-04-21 11:57:44 +07:00
|
|
|
repository.delete(entity)
|
|
|
|
}
|
|
|
|
|
|
|
|
@ResponseStatus(value = org.springframework.http.HttpStatus.NO_CONTENT)
|
|
|
|
@DeleteMapping
|
|
|
|
@Operation(summary = "Delete all files")
|
|
|
|
fun deleteAll() {
|
2022-04-21 17:11:13 +07:00
|
|
|
FileStorageFactory.deleteAll()
|
2022-04-21 11:57:44 +07:00
|
|
|
repository.deleteAll()
|
2022-04-21 08:57:54 +07:00
|
|
|
}
|
|
|
|
}
|