mirror of
https://github.com/BgaSol/sol-cloud
synced 2026-05-24 09:38:21 +00:00
feat(file): Add file download function and optimize MinIO client usage
This commit is contained in:
parent
bd35ec0bb5
commit
49f09b3245
3 changed files with 30 additions and 1 deletions
|
|
@ -11,10 +11,14 @@ import com.bgasol.model.file.file.dto.FileUpdateDto;
|
|||
import com.bgasol.model.file.file.entity.FileEntity;
|
||||
import com.bgasol.plugin.minio.service.OssService;
|
||||
import com.bgasol.web.file.file.service.FileService;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.StatObjectArgs;
|
||||
import io.minio.StatObjectResponse;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
|
|
@ -38,6 +42,7 @@ public class FileController extends BaseController<
|
|||
private final FileService fileService;
|
||||
|
||||
private final OssService ossService;
|
||||
private final MinioClient minioClient;
|
||||
|
||||
@Override
|
||||
public FileService commonBaseService() {
|
||||
|
|
@ -84,12 +89,21 @@ public class FileController extends BaseController<
|
|||
return super.findById(id);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@GetMapping("/download/{id}")
|
||||
@Operation(summary = "下载文件", operationId = "downloadFile")
|
||||
@SaCheckPermission("file:download")
|
||||
public ResponseEntity<InputStreamResource> download(@PathVariable("id") String id) {
|
||||
FileEntity file = fileService.findById(id);
|
||||
String bucket = file.getBucket();
|
||||
String objectName = ossService.buildObjectPath(file);
|
||||
|
||||
StatObjectResponse stat = minioClient.statObject(StatObjectArgs.builder()
|
||||
.bucket(bucket)
|
||||
.object(objectName)
|
||||
.build());
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_LENGTH, String.valueOf(stat.size()))
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(
|
||||
fileService.getFileName(file),
|
||||
StandardCharsets.UTF_8
|
||||
|
|
|
|||
|
|
@ -9,12 +9,17 @@ import com.bgasol.model.file.image.dto.ImageCreateDto;
|
|||
import com.bgasol.model.file.image.dto.ImagePageDto;
|
||||
import com.bgasol.model.file.image.dto.ImageUpdateDto;
|
||||
import com.bgasol.model.file.image.entity.ImageEntity;
|
||||
import com.bgasol.plugin.minio.service.OssService;
|
||||
import com.bgasol.web.file.file.service.FileService;
|
||||
import com.bgasol.web.file.image.service.ImageService;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.StatObjectArgs;
|
||||
import io.minio.StatObjectResponse;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
|
|
@ -36,6 +41,8 @@ public class ImageController extends BaseController<
|
|||
private final ImageService imageService;
|
||||
|
||||
private final FileService fileService;
|
||||
private final OssService ossService;
|
||||
private final MinioClient minioClient;
|
||||
|
||||
@Override
|
||||
public ImageService commonBaseService() {
|
||||
|
|
@ -82,13 +89,22 @@ public class ImageController extends BaseController<
|
|||
return super.delete(ids);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@GetMapping("/download/{id}")
|
||||
@Operation(summary = "下载图片", operationId = "downloadImage")
|
||||
@SaCheckPermission("file:download")
|
||||
public ResponseEntity<InputStreamResource> download(@PathVariable("id") String id) {
|
||||
ImageEntity imageEntity = imageService.findById(id);
|
||||
FileEntity file = imageEntity.getFile();
|
||||
String bucket = file.getBucket();
|
||||
String objectName = ossService.buildObjectPath(file);
|
||||
|
||||
StatObjectResponse stat = minioClient.statObject(StatObjectArgs.builder()
|
||||
.bucket(bucket)
|
||||
.object(objectName)
|
||||
.build());
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_LENGTH, String.valueOf(stat.size()))
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(
|
||||
fileService.getFileName(file),
|
||||
StandardCharsets.UTF_8
|
||||
|
|
|
|||
|
|
@ -102,7 +102,6 @@ public class VideoController extends BaseController<
|
|||
String bucket = file.getBucket();
|
||||
String objectName = ossService.buildObjectPath(file);
|
||||
|
||||
// ----------- 用 MinIO 获取对象大小(HEAD / stat) -----------
|
||||
StatObjectResponse stat = minioClient.statObject(StatObjectArgs.builder()
|
||||
.bucket(bucket)
|
||||
.object(objectName)
|
||||
|
|
|
|||
Loading…
Reference in a new issue