2025-03-30 16:42:35 +08:00

106 lines
4.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.heyu.api.alibaba;
import com.alibaba.fastjson.JSON;
import com.heyu.api.alibaba.request.ACommonTextRequest;
import com.heyu.api.data.dto.ImageInfoModel;
import com.heyu.api.data.utils.*;
import com.heyu.api.oss.OssFileUploadService;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public abstract class AlibabaBaseHandle<PP, RR> {
/**
* 较验参数
*/
public abstract String check(PP p);
public ApiR<RR> handle(PP p) {
try {
String check = check(p);
if (check != null) {
log.info("AlibabaBaseHandle check:{}", check);
return ApiR.error(check);
}
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
String value = AnnotationUtils.getAnnotation(this.getClass(), "QPS");
if (StringUtils.isNotEmpty(value)) {
RedisUtils redisUtils = SpringContextUtils.getBean(RedisUtils.class);
String key = this.getClass().getName() + "#QPS";
boolean flag = redisUtils.tryGetAccess(key, NumberUtil.objToIntDefault(value, 1), 1);
if (!flag) {
log.info("AlibabaBaseHandle redis limit :{}", key);
return ApiR.error("限流了");
}
}
Object resp = run(p, runtime);
log.info(this.getClass().getSimpleName() + " handle resp:{}", JSON.toJSONString(resp));
if (resp != null) {
if (resp instanceof String) {
return ApiR.error((String) resp);
} else {
return ApiR.setData(resp);
}
}
} catch (Exception e) {
log.error("AlibabaBaseHandle handle error", e);
}
return null;
}
public abstract RR run(PP ap, com.aliyun.teautil.models.RuntimeOptions runtime) throws Exception;
public String checkUrlBase64(ACommonTextRequest request) {
OssFileUploadService ossFileUploadService = SpringContextUtils.getBean(OssFileUploadService.class);
String className = request.getClass().getName();
if (StringUtils.isBlank(request.getImageUrl())
&& StringUtils.isBlank(request.getImageBase64())) {
log.info(className + " url and body not null");
return "url 或 base64 不能同时为空";
}
if (StringUtils.isNotBlank(request.getImageBase64())) {
try {
//下面对图片大小,格式做判断是否符合要求,不符合要求直接返回提示
String base64;
if (request.getImageBase64().startsWith("data")) {
base64 = request.getImageBase64().substring(request.getImageBase64().indexOf("base64,") + 7);
} else {
base64 = request.getImageBase64();
}
if (!base64.startsWith("/9j/")) {
log.info("图片格式错误只支持jpg格式");
return "图片格式错误只支持jpg格式";
}
ImageInfoModel imageInfoModel = Base64Utils.getBase64ImageInfo(base64);
if (imageInfoModel == null) {
log.info("无法解析图片、或者格式错误只支持jpg格式");
return "无法解析图片格式请检查图片格式只支持jpg格式";
}
if (imageInfoModel.getSize() > 4 * 1024 * 1024) {
log.info("图片大小超过1M");
return "图片大小超过1M";
}
if (imageInfoModel.getWidth() > 4096 || imageInfoModel.getHeight() > 4096) {
log.info("图片尺寸过大");
return "图片尺寸过大不能超过2048x2048";
}
if (imageInfoModel.getWidth() < 15 || imageInfoModel.getHeight() < 15) {
log.info("图片尺寸过小");
return "图片尺寸过小不能小于640x480";
}
String url = ossFileUploadService.uploadFileContentByBase64(request.getImageBase64(), "jpg");
if (StringUtils.isBlank(url)) {
log.error(className + " oss file upload is null");
return "你的base64内容有误";
}
request.setImageUrl(url);
} catch (Exception e) {
log.error(className + " oss file upload error", e);
}
}
return null;
}
}