文件下载支持图片样式处理

This commit is contained in:
tianliyong 2022-12-13 18:07:07 +08:00
parent baf2c7db71
commit 7b1fd43699
8 changed files with 26 additions and 18 deletions

View File

@ -113,10 +113,7 @@ public class WebFileController {
@GetMapping("/v1/file/download")
@CrossOrigin
@PreBuildContext
public void download(@Valid @RequestParam String fileKey, HttpServletResponse response) {
ServerFileDownloadDto dto = ServerFileDownloadDto.builder()
.fileKey(fileKey)
.build();
public void download(@Valid ServerFileDownloadDto dto, HttpServletResponse response) {
ServerFileDownloadResponse result = fileService.download(dto);
InputStream inputStream = null;
OutputStream outputStream = response.getOutputStream();

View File

@ -39,7 +39,7 @@ public interface BaseS3Service {
*/
ResponseEntity<byte[]> download(String tgtFileKey);
InputStream downloadFile(String bucket, String tgtFileKey);
InputStream downloadFile(String bucket, String tgtFileKey, String style);
/**
* get file address

View File

@ -9,6 +9,7 @@ import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
@ -88,13 +89,13 @@ public class AliOssServiceImpl implements AliOssService {
}
@Override
public InputStream downloadFile(String bucket, String tgtFileKey) {
public InputStream downloadFile(String bucket, String tgtFileKey, String style) {
OSS client = aliOssClient.getClient();
// TODO: 2022/12/9 支持图片样式
/*GetObjectRequest request = new GetObjectRequest(bucket, tgtFileKey);
request.setProcess("");
client.getObject(request);*/
OSSObject ossObject = client.getObject(bucket, tgtFileKey);
GetObjectRequest request = new GetObjectRequest(bucket, tgtFileKey);
if (StringUtils.isNotEmpty(style)) {
request.setProcess(style);
}
OSSObject ossObject = client.getObject(request);
return ossObject.getObjectContent();
}

View File

@ -35,6 +35,6 @@ public interface FileManager {
* @param bucketName 桶名称
* @param tgtFileKey 目标文件key
*/
InputStream downloadFile(String bucketName, String tgtFileKey);
InputStream downloadFile(String bucketName, String tgtFileKey, String style);
}

View File

@ -5,6 +5,7 @@ import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
@ -21,5 +22,6 @@ public class FindFileUrlDto {
/**
* 文件uuid
*/
@NotNull
private List<String> fileKey;
}

View File

@ -5,6 +5,8 @@ import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
/**
* @Author: liyong.tian
* @Date: 2022/11/29 15:42
@ -19,5 +21,11 @@ public class ServerFileDownloadDto {
/**
* 文件uuid
*/
@NotBlank
private String fileKey;
/**
* 图片样式
*/
private String style;
}

View File

@ -46,8 +46,8 @@ public class FileManagerImpl implements FileManager {
}
@Override
public InputStream downloadFile(String bucketName, String tgtFileKey) {
return aliOssService.downloadFile(bucketName, tgtFileKey);
public InputStream downloadFile(String bucketName, String tgtFileKey, String style) {
return aliOssService.downloadFile(bucketName, tgtFileKey, style);
}
}

View File

@ -147,16 +147,16 @@ public class FileServiceImpl implements FileService {
@Override
public ServerFileDownloadResponse download(ServerFileDownloadDto dto) {
log.info("file download dto = {}", JsonUtil.obj2Str(dto));
File file = fileDao.getByFileUuid(dto.getFileKey());
String fileKey = dto.getFileKey();
File file = fileDao.getByFileUuid(fileKey);
if (Utility.objIsNull(file)) {
log.warn("file download is null, fileKey = {}", dto.getFileKey());
log.warn("file download is null, fileKey = {}", fileKey);
return null;
}
String tgtFileKey = Utility
.generateFileKey(file.getDirectory(), file.getFileUuid(), file.getFileFormat());
log.info("file download tgtFileKey = {}", tgtFileKey);
InputStream fileStream = fileManager.downloadFile(file.getBucketName(), tgtFileKey);
InputStream fileStream = fileManager.downloadFile(file.getBucketName(), tgtFileKey, dto.getStyle());
return setFileDownloadResponse(file, fileStream);
}