|
@@ -1,14 +1,19 @@
|
|
|
package edu.travel.tenant.service.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.io.FileTypeUtil;
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import edu.travel.adapter.service.upload.UploadAdapter;
|
|
|
+import edu.travel.commodity.constant.BaseConstant;
|
|
|
+import edu.travel.commodity.utils.FIleUtil;
|
|
|
import edu.travel.remote.feign.mode.dto.tenant.BannerDto;
|
|
|
import edu.travel.remote.feign.mode.vo.banner.BannerVo;
|
|
|
+import edu.travel.remote.upload.dto.EduFileDTO;
|
|
|
import edu.travel.rpc.RPCBaseResponse;
|
|
|
import edu.travel.tenant.entity.ShopBanner;
|
|
|
import edu.travel.tenant.mapper.ShopBannerMapper;
|
|
@@ -21,6 +26,9 @@ import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
|
|
|
+import static edu.travel.rpc.RPCBaseResponse.error;
|
|
|
+import static edu.travel.rpc.RPCBaseResponse.success;
|
|
|
+
|
|
|
@Service
|
|
|
public class ShopBannerServiceImpl extends ServiceImpl<ShopBannerMapper, ShopBanner> implements ShopBannerService {
|
|
|
@Autowired
|
|
@@ -45,17 +53,43 @@ public class ShopBannerServiceImpl extends ServiceImpl<ShopBannerMapper, ShopBan
|
|
|
|
|
|
@Override
|
|
|
public RPCBaseResponse<String> uploadBannerImage(MultipartFile file) {
|
|
|
- //判断文件是否为空
|
|
|
+ // 判断上传的文件是否为空或未选择
|
|
|
if (file == null || file.isEmpty()) {
|
|
|
- return RPCBaseResponse.error();
|
|
|
+ return error();
|
|
|
}
|
|
|
-// try{
|
|
|
-// String fileType = FileTypeUtil.getType(file.getInputStream());
|
|
|
-//
|
|
|
-// }catch (){
|
|
|
-// return RPCBaseResponse.error();
|
|
|
-// }
|
|
|
- return null;
|
|
|
- }
|
|
|
+ try {
|
|
|
+ // 获取文件类型(MIME 类型)
|
|
|
+ String fileType = FileTypeUtil.getType(file.getInputStream());
|
|
|
+
|
|
|
+ // 检查文件类型是否有效且是否在允许的横幅图片类型列表中
|
|
|
+ if (fileType != null && BaseConstant.getBannerImageTypes().contains(fileType.toLowerCase())) {
|
|
|
+ // 获取用户上传的文件名
|
|
|
+ String filename = file.getOriginalFilename();
|
|
|
+ // 检查文件名是否为空
|
|
|
+ if (ObjectUtil.isEmpty(filename)) {
|
|
|
+ return error(); // 返回错误响应
|
|
|
+ }
|
|
|
+ // 初始化上传,获取上传 ID
|
|
|
+ RPCBaseResponse<String> stringRPCBaseResponse = uploadAdapter.initializeUpload();
|
|
|
+ // 创建文件 DTO 对象,用于存储文件相关信息
|
|
|
+ EduFileDTO eduFileDTO = new EduFileDTO();
|
|
|
+ eduFileDTO.setUploadId(stringRPCBaseResponse.getData()); // 设置上传 ID
|
|
|
+ eduFileDTO.setFileType(fileType); // 设置文件类型
|
|
|
+ eduFileDTO.setFileName(filename); // 设置文件原始名称
|
|
|
|
|
|
+ // 生成系统文件名,确保唯一性
|
|
|
+ eduFileDTO.setFileSysName(IdUtil.fastSimpleUUID() + DateUtil.format(new Date(), "yyyyMMddHHmmss") + "." + fileType);
|
|
|
+ // 计算文件的 MD5 值以便后续验证
|
|
|
+ eduFileDTO.setFileMd5(FIleUtil.calculateFileMd5(file.getBytes()));
|
|
|
+ // 调用上传适配器进行文件上传
|
|
|
+ RPCBaseResponse rpcBaseResponse = uploadAdapter.uploadFile(eduFileDTO, file);
|
|
|
+ // 返回成功响应,包含上传后的数据
|
|
|
+ return success(rpcBaseResponse.getData().toString());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace(); // 打印异常栈信息
|
|
|
+ return error("file upload fail"); // 返回表示文件上传失败的错误响应
|
|
|
+ }
|
|
|
+ return error(); // 如果文件类型不支持,返回错误响应
|
|
|
+ }
|
|
|
}
|