mirror of
https://gitee.com/lxp135/minio-plus.git
synced 2025-12-07 17:38:23 +08:00
修改文件预览接口,当文件为图片时返回图片的缩略图,当文件不是图片时返回文件类型图标。
This commit is contained in:
parent
dc2bb12805
commit
41ced28d44
@ -9,14 +9,14 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 文件检查结果VO
|
||||
* 文件初始化结果
|
||||
*
|
||||
* @author contact@liuxp.me
|
||||
* @since 2023-06-26
|
||||
* @since 2024-06-26
|
||||
**/
|
||||
@Getter
|
||||
@Setter
|
||||
@ApiModel("文件检查结果")
|
||||
@ApiModel("文件初始化结果")
|
||||
public class FileCheckResultVo {
|
||||
/**
|
||||
* 主键
|
||||
|
||||
@ -22,6 +22,7 @@ public enum MinioPlusErrorCode {
|
||||
FILE_BYTES_FAILED(1005,"文件流不能为空"),
|
||||
FILE_UPLOAD_FAILED(1006,"文件上传失败"),
|
||||
FILE_PREVIEW_WRITE_FAILED(1007,"缩略图生成失败"),
|
||||
FILE_ICON_FAILED(1008,"图标获取失败"),
|
||||
|
||||
/**
|
||||
* MinIO 异常
|
||||
|
||||
@ -365,6 +365,12 @@ public class StorageEngineServiceImpl implements StorageEngineService {
|
||||
try {
|
||||
// 文件权限校验,元数据为空或者当前登录用户不是文件所有者时抛出异常
|
||||
this.authentication(metadata, fileKey, userId);
|
||||
|
||||
if(!StorageBucketEnums.IMAGE.getCode().equals(metadata.getStorageBucket())){
|
||||
// 不是图片时,返回文件类型
|
||||
return metadata.getFileSuffix();
|
||||
}
|
||||
|
||||
// 生成缩略图
|
||||
generatePreviewImage(metadata);
|
||||
// 创建图片预览地址
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package org.liuxp.minioplus.extension.controller;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -7,16 +8,23 @@ import org.liuxp.minioplus.api.StorageService;
|
||||
import org.liuxp.minioplus.api.model.vo.CompleteResultVo;
|
||||
import org.liuxp.minioplus.api.model.vo.FileCheckResultVo;
|
||||
import org.liuxp.minioplus.api.model.vo.FilePreShardingVo;
|
||||
import org.liuxp.minioplus.common.enums.MinioPlusErrorCode;
|
||||
import org.liuxp.minioplus.common.enums.StorageBucketEnums;
|
||||
import org.liuxp.minioplus.common.exception.MinioPlusException;
|
||||
import org.liuxp.minioplus.extension.context.Response;
|
||||
import org.liuxp.minioplus.extension.context.UserHolder;
|
||||
import org.liuxp.minioplus.extension.dto.FileCheckDTO;
|
||||
import org.liuxp.minioplus.extension.dto.FileCompleteDTO;
|
||||
import org.liuxp.minioplus.extension.dto.PreShardingDTO;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
/**
|
||||
* 对象存储标准接口定义
|
||||
@ -35,6 +43,11 @@ public class StorageController {
|
||||
*/
|
||||
private static final String REDIRECT_PREFIX = "redirect:";
|
||||
|
||||
/**
|
||||
* 图标请求地址
|
||||
*/
|
||||
private static final String ICON_PATH = "/storage/icon/";
|
||||
|
||||
/**
|
||||
* 存储引擎Service接口定义
|
||||
*/
|
||||
@ -113,7 +126,7 @@ public class StorageController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片原图预览
|
||||
* 获取图像
|
||||
* @param fileKey 文件KEY
|
||||
* @return 原图地址
|
||||
*/
|
||||
@ -129,7 +142,9 @@ public class StorageController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片缩略图预览
|
||||
* 文件预览
|
||||
* 当文件为图片时,返回图片的缩略图
|
||||
* 当文件不是图片时,返回文件类型图标
|
||||
* @param fileKey 文件KEY
|
||||
* @return 缩略图地址
|
||||
*/
|
||||
@ -140,8 +155,44 @@ public class StorageController {
|
||||
// 取得当前登录用户信息
|
||||
String userId = UserHolder.get();
|
||||
|
||||
String url = storageService.preview(fileKey, userId);
|
||||
if(url.length()<10){
|
||||
// 当返回值为文件类型时,取得图标
|
||||
url = ICON_PATH + url;
|
||||
}
|
||||
|
||||
// 取得文件读取路径
|
||||
return REDIRECT_PREFIX + storageService.preview(fileKey, userId);
|
||||
return REDIRECT_PREFIX + url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件类型取得图标
|
||||
* @param response HttpServletResponse
|
||||
*/
|
||||
@ApiOperation(value = "获取图标")
|
||||
@GetMapping("/icon/{fileType}")
|
||||
public void icon(HttpServletResponse response,@PathVariable String fileType) {
|
||||
try {
|
||||
|
||||
// 根据文件后缀取得桶
|
||||
String storageBucket = StorageBucketEnums.getBucketByFileSuffix(fileType);
|
||||
|
||||
ClassPathResource cpr = new ClassPathResource(storageBucket+".png");
|
||||
|
||||
byte[] bytes = FileCopyUtils.copyToByteArray(cpr.getInputStream());
|
||||
|
||||
response.setHeader("content-disposition", "inline");
|
||||
response.setHeader("Content-Length", String.valueOf(bytes.length));
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
|
||||
|
||||
IoUtil.copy(inputStream, response.getOutputStream());
|
||||
inputStream.close();
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error(MinioPlusErrorCode.FILE_ICON_FAILED.getMessage(),e);
|
||||
// 图标获取失败
|
||||
throw new MinioPlusException(MinioPlusErrorCode.FILE_ICON_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
BIN
minio-plus-extension/src/main/resources/audio.png
Normal file
BIN
minio-plus-extension/src/main/resources/audio.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.8 KiB |
BIN
minio-plus-extension/src/main/resources/document.png
Normal file
BIN
minio-plus-extension/src/main/resources/document.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
BIN
minio-plus-extension/src/main/resources/other.png
Normal file
BIN
minio-plus-extension/src/main/resources/other.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.9 KiB |
BIN
minio-plus-extension/src/main/resources/package.png
Normal file
BIN
minio-plus-extension/src/main/resources/package.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.9 KiB |
BIN
minio-plus-extension/src/main/resources/video.png
Normal file
BIN
minio-plus-extension/src/main/resources/video.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.3 KiB |
Loading…
x
Reference in New Issue
Block a user