Commit 4219e67d by 章家宝

feat:根据接口控制合并是否格式化图片

1 parent 8f2460f5
...@@ -8,6 +8,7 @@ import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; ...@@ -8,6 +8,7 @@ import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import jakarta.servlet.MultipartConfigElement; import jakarta.servlet.MultipartConfigElement;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.web.server.ConfigurableWebServerFactory; import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage; import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.server.WebServerFactoryCustomizer;
...@@ -31,6 +32,7 @@ import org.summerframework.cache.CachedPropertySerializerModifier; ...@@ -31,6 +32,7 @@ import org.summerframework.cache.CachedPropertySerializerModifier;
import org.summerframework.orm.paging.JsonPageSerializer; import org.summerframework.orm.paging.JsonPageSerializer;
import org.summerframework.storage.domain.FileInfo; import org.summerframework.storage.domain.FileInfo;
import org.summerframework.storage.web.FileInfoSerializer; import org.summerframework.storage.web.FileInfoSerializer;
import org.summerframework.storage.web.interceptor.OssProcessInterceptor;
import org.summerframework.web.config.AbstractWebConfigurer; import org.summerframework.web.config.AbstractWebConfigurer;
import org.summerframework.web.rest.deserializer.LocalDateAttemptDeserializer; import org.summerframework.web.rest.deserializer.LocalDateAttemptDeserializer;
import org.summerframework.web.rest.deserializer.LocalDateTimeAttemptDeserializer; import org.summerframework.web.rest.deserializer.LocalDateTimeAttemptDeserializer;
...@@ -42,13 +44,11 @@ import java.time.LocalTime; ...@@ -42,13 +44,11 @@ import java.time.LocalTime;
import static org.summerframework.core.util.DateTimeUtils.*; import static org.summerframework.core.util.DateTimeUtils.*;
@RequiredArgsConstructor
@Configuration @Configuration
public class WebConfig extends AbstractWebConfigurer { public class WebConfig extends AbstractWebConfigurer {
private static final String[] IGNORED_URLS = { private final OssProcessInterceptor ossProcessInterceptor;
"/rest/wx/**",
"/rest/maWxCode",
};
@Bean @Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() { public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
...@@ -65,6 +65,9 @@ public class WebConfig extends AbstractWebConfigurer { ...@@ -65,6 +65,9 @@ public class WebConfig extends AbstractWebConfigurer {
@Override @Override
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {
// 注册OSS处理拦截器
registry.addInterceptor(ossProcessInterceptor).addPathPatterns("/rest/**");
//个人中心拦截 //个人中心拦截
// registry.addInterceptor(applicationInterceptor).addPathPatterns( // registry.addInterceptor(applicationInterceptor).addPathPatterns(
// "/center**", // "/center**",
......
...@@ -21,6 +21,7 @@ import org.springframework.security.core.Authentication; ...@@ -21,6 +21,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.summerframework.core.exception.FieldException; import org.summerframework.core.exception.FieldException;
import org.summerframework.orm.paging.Pagination; import org.summerframework.orm.paging.Pagination;
import org.summerframework.storage.web.annotation.OssProcess;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
...@@ -51,6 +52,7 @@ public class AuctionOrderController { ...@@ -51,6 +52,7 @@ public class AuctionOrderController {
* @param pagination * @param pagination
* @return * @return
*/ */
@OssProcess(OssProcess.Policy.ENABLE)
@PostMapping("pageAuctionOrder") @PostMapping("pageAuctionOrder")
public Page<AuctionOrder> pageAuctionOrder(@RequestBody Pagination<AuctionOrder> pagination) { public Page<AuctionOrder> pageAuctionOrder(@RequestBody Pagination<AuctionOrder> pagination) {
pagination.addFetch(root -> root.fetch(AuctionOrder_.account, JoinType.LEFT)); pagination.addFetch(root -> root.fetch(AuctionOrder_.account, JoinType.LEFT));
......
...@@ -26,6 +26,7 @@ import org.springframework.web.bind.annotation.*; ...@@ -26,6 +26,7 @@ import org.springframework.web.bind.annotation.*;
import org.summerframework.core.exception.FieldException; import org.summerframework.core.exception.FieldException;
import org.summerframework.orm.paging.Pagination; import org.summerframework.orm.paging.Pagination;
import org.summerframework.orm.paging.SortOrder; import org.summerframework.orm.paging.SortOrder;
import org.summerframework.storage.web.annotation.OssProcess;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
...@@ -83,6 +84,7 @@ public class TransactionController { ...@@ -83,6 +84,7 @@ public class TransactionController {
* @param pagination * @param pagination
* @return * @return
*/ */
@OssProcess(OssProcess.Policy.ENABLE)
@PostMapping("pageOrderTransaction") @PostMapping("pageOrderTransaction")
public Page<OrderTransaction> pageOrderTransaction(@RequestBody Pagination<OrderTransaction> pagination) { public Page<OrderTransaction> pageOrderTransaction(@RequestBody Pagination<OrderTransaction> pagination) {
pagination.addFetch(root -> { pagination.addFetch(root -> {
......
...@@ -232,9 +232,12 @@ public class AliyunOssService extends AbstractStorageService { ...@@ -232,9 +232,12 @@ public class AliyunOssService extends AbstractStorageService {
public String getFileUrl(String savePath, String fileName) { public String getFileUrl(String savePath, String fileName) {
return getFileUrl(savePath, fileName, null); return getFileUrl(savePath, fileName, null);
} }
@Override @Override
public String getFileUrl(String savePath, String fileName, Map<String, String> parameter) { public String getFileUrl(String savePath, String fileName, Map<String, String> parameter) {
return getFileUrl(savePath, fileName, parameter, false);
}
@Override
public String getFileUrl(String savePath, String fileName, Map<String, String> parameter, boolean isFormatImage) {
try { try {
// 计算过期时间 // 计算过期时间
Date expiration = new Date(System.currentTimeMillis() + unit.toMillis(time)); Date expiration = new Date(System.currentTimeMillis() + unit.toMillis(time));
...@@ -242,7 +245,7 @@ public class AliyunOssService extends AbstractStorageService { ...@@ -242,7 +245,7 @@ public class AliyunOssService extends AbstractStorageService {
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, savePath); GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, savePath);
request.setExpiration(expiration); request.setExpiration(expiration);
request.setMethod(HttpMethod.GET); request.setMethod(HttpMethod.GET);
if (isImageFile(fileName)) { if (isImageFile(fileName) && isFormatImage) {
request.setProcess("image/format,jpg"); request.setProcess("image/format,jpg");
} }
// 生成预签名URL // 生成预签名URL
......
package org.summerframework.storage; package org.summerframework.storage;
import jakarta.annotation.Nullable; import jakarta.annotation.Nullable;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.summerframework.storage.domain.FileInfo; import org.summerframework.storage.domain.FileInfo;
//import org.summerframework.poi.async.AsyncTrans;
//import org.summerframework.poi.async.Trans;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
//import org.summerframework.poi.async.AsyncTrans;
//import org.summerframework.poi.async.Trans;
/** /**
* 文件存储服务类 * 文件存储服务类
*/ */
...@@ -200,6 +199,10 @@ public interface StorageService { ...@@ -200,6 +199,10 @@ public interface StorageService {
String getFileUrl(String savePath, String fileName, Map<String, String> parameter); String getFileUrl(String savePath, String fileName, Map<String, String> parameter);
default String getFileUrl(String savePath, String fileName, Map<String, String> parameter, boolean isformatImage) {
return getFileUrl(savePath, fileName, parameter);
}
/** /**
* 裁剪图片 * 裁剪图片
* *
......
...@@ -7,6 +7,8 @@ import org.apache.commons.lang3.StringUtils; ...@@ -7,6 +7,8 @@ import org.apache.commons.lang3.StringUtils;
import org.summerframework.core.util.SpringUtils; import org.summerframework.core.util.SpringUtils;
import org.summerframework.storage.domain.FileInfo; import org.summerframework.storage.domain.FileInfo;
import org.summerframework.storage.StorageService; import org.summerframework.storage.StorageService;
import org.summerframework.storage.web.annotation.OssProcess;
import org.summerframework.storage.web.context.OssProcessContext;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
...@@ -24,11 +26,12 @@ public class FileInfoSerializer extends JsonSerializer<FileInfo> { ...@@ -24,11 +26,12 @@ public class FileInfoSerializer extends JsonSerializer<FileInfo> {
if (Objects.nonNull(fileName)) { if (Objects.nonNull(fileName)) {
fileName = fileName.replace(",", "_"); fileName = fileName.replace(",", "_");
map.put("response-Content-Disposition", "attachment;filename=" + fileName); map.put("response-Content-Disposition", "attachment;filename=" + fileName);
boolean isFormatImage = OssProcessContext.getPolicy() == OssProcess.Policy.ENABLE;
if (Objects.nonNull(value.getFilePath())) { if (Objects.nonNull(value.getFilePath())) {
value.setUrl(storageService.getFileUrl(value.getFilePath(), value.getName(), map)); value.setUrl(storageService.getFileUrl(value.getFilePath(), value.getName(), map, isFormatImage));
} }
if (Objects.nonNull(value.getThumbPath())) { if (Objects.nonNull(value.getThumbPath())) {
value.setThumbUrl(storageService.getFileUrl(value.getThumbPath(), value.getName(), map)); value.setThumbUrl(storageService.getFileUrl(value.getThumbPath(), value.getName(), map, isFormatImage));
} }
} }
String filePath = value.getFilePath(); String filePath = value.getFilePath();
......
package org.summerframework.storage.web.annotation;
import java.lang.annotation.*;
/**
* OSS图片处理注解
* 用于控制特定接口是否启用OSS图片处理
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OssProcess {
/**
* OSS处理策略
* DEFAULT: 使用全局配置(当前默认行为)
* ENABLE: 强制启用OSS图片处理
* DISABLE: 强制禁用OSS图片处理
*/
Policy value() default Policy.DEFAULT;
enum Policy {
DEFAULT,
ENABLE,
DISABLE
}
}
\ No newline at end of file \ No newline at end of file
package org.summerframework.storage.web.context;
import org.summerframework.storage.web.annotation.OssProcess;
/**
* OSS处理上下文管理类
* 使用ThreadLocal存储当前请求的OSS处理策略
*/
public class OssProcessContext {
private static final ThreadLocal<OssProcess.Policy> context = new ThreadLocal<>();
/**
* 设置当前线程的OSS处理策略
*/
public static void setPolicy(OssProcess.Policy policy) {
context.set(policy);
}
/**
* 获取当前线程的OSS处理策略
*/
public static OssProcess.Policy getPolicy() {
OssProcess.Policy policy = context.get();
return policy != null ? policy : OssProcess.Policy.DEFAULT;
}
/**
* 清除当前线程的上下文
*/
public static void clear() {
context.remove();
}
/**
* 判断是否应该处理OSS图片
*/
public static boolean shouldProcess() {
OssProcess.Policy policy = getPolicy();
// 如果明确设置为ENABLE则处理,如果DISABLE则不处理,如果DEFAULT则使用原有逻辑
return policy == OssProcess.Policy.ENABLE;
}
}
\ No newline at end of file \ No newline at end of file
package org.summerframework.storage.web.interceptor;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.summerframework.storage.web.annotation.OssProcess;
import org.summerframework.storage.web.context.OssProcessContext;
/**
* OSS处理拦截器
* 根据注解设置当前请求的OSS处理策略
*/
@Component
public class OssProcessInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
// 检查方法上的注解
OssProcess methodAnnotation = handlerMethod.getMethodAnnotation(OssProcess.class);
if (methodAnnotation != null) {
OssProcessContext.setPolicy(methodAnnotation.value());
return true;
}
// 检查类上的注解
OssProcess classAnnotation = handlerMethod.getBeanType().getAnnotation(OssProcess.class);
if (classAnnotation != null) {
OssProcessContext.setPolicy(classAnnotation.value());
return true;
}
// 默认情况:使用DEFAULT策略
OssProcessContext.setPolicy(OssProcess.Policy.DEFAULT);
}
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// 清理线程上下文
OssProcessContext.clear();
}
}
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!