|
|
|
@ -0,0 +1,134 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* https://www.mall4j.com/
|
|
|
|
|
*
|
|
|
|
|
* 未经允许,不可做商业用途!
|
|
|
|
|
*
|
|
|
|
|
* 版权所有,侵权必究!
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 定义了一个名为com.yami.shop.service.impl的包,用于组织代码
|
|
|
|
|
package com.yami.shop.service.impl;
|
|
|
|
|
|
|
|
|
|
// 导入了Hutool工具类,用于日期、文件和ID操作
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
|
|
// 导入了MyBatis Plus框架中的LambdaQueryWrapper类,用于构建条件查询
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
// 导入了MyBatis Plus框架中的ServiceImpl类,用于提供基础的CRUD操作实现
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
// 导入了七牛云存储相关类
|
|
|
|
|
import com.qiniu.common.QiniuException;
|
|
|
|
|
import com.qiniu.http.Response;
|
|
|
|
|
import com.qiniu.storage.BucketManager;
|
|
|
|
|
import com.qiniu.storage.UploadManager;
|
|
|
|
|
import com.qiniu.storage.model.DefaultPutRet;
|
|
|
|
|
import com.qiniu.util.Auth;
|
|
|
|
|
// 导入了项目中的枚举、模型、工具类和配置类
|
|
|
|
|
import com.yami.shop.bean.enums.UploadType;
|
|
|
|
|
import com.yami.shop.bean.model.AttachFile;
|
|
|
|
|
import com.yami.shop.common.bean.Qiniu;
|
|
|
|
|
import com.yami.shop.common.util.ImgUploadUtil;
|
|
|
|
|
import com.yami.shop.common.util.Json;
|
|
|
|
|
import com.yami.shop.dao.AttachFileMapper;
|
|
|
|
|
import com.yami.shop.service.AttachFileService;
|
|
|
|
|
// 导入了Spring框架的注解,用于注入依赖和定义服务
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
// 导入了Spring框架的MultipartFile类,用于处理上传的文件
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 这是一个服务实现类,用于实现AttachFileService接口中定义的业务操作。
|
|
|
|
|
*
|
|
|
|
|
* @author lanhai 表示这个类的作者是lanhai。
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
|
|
|
|
public class AttachFileServiceImpl extends ServiceImpl<AttachFileMapper, AttachFile> implements AttachFileService {
|
|
|
|
|
|
|
|
|
|
// 使用Spring的@Autowired注解自动注入AttachFileMapper
|
|
|
|
|
@Autowired
|
|
|
|
|
private AttachFileMapper attachFileMapper;
|
|
|
|
|
// 使用Spring的@Autowired注解自动注入七牛云的UploadManager
|
|
|
|
|
@Autowired
|
|
|
|
|
private UploadManager uploadManager;
|
|
|
|
|
// 使用Spring的@Autowired注解自动注入七牛云的BucketManager
|
|
|
|
|
@Autowired
|
|
|
|
|
private BucketManager bucketManager;
|
|
|
|
|
// 使用Spring的@Autowired注解自动注入七牛云的配置信息
|
|
|
|
|
@Autowired
|
|
|
|
|
private Qiniu qiniu;
|
|
|
|
|
// 使用Spring的@Autowired注解自动注入七牛云的认证工具
|
|
|
|
|
@Autowired
|
|
|
|
|
private Auth auth;
|
|
|
|
|
// 使用Spring的@Autowired注解自动注入图片上传工具类
|
|
|
|
|
@Autowired
|
|
|
|
|
private ImgUploadUtil imgUploadUtil;
|
|
|
|
|
// 定义了一个常量,用于格式化文件名
|
|
|
|
|
public final static String NORM_MONTH_PATTERN = "yyyy/MM/";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 上传文件的方法实现。
|
|
|
|
|
* 使用了Spring的@Transactional注解,表示这个方法是一个事务性操作,如果发生异常则回滚。
|
|
|
|
|
*
|
|
|
|
|
* @param file 要上传的文件对象,类型为MultipartFile
|
|
|
|
|
* @throws IOException 如果上传过程中发生I/O错误,会抛出IOException异常
|
|
|
|
|
* @return 返回一个字符串,通常是上传文件后的一些信息,比如文件路径或者文件ID
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public String uploadFile(MultipartFile file) throws IOException {
|
|
|
|
|
// 获取文件扩展名
|
|
|
|
|
String extName = FileUtil.extName(file.getOriginalFilename());
|
|
|
|
|
// 格式化文件名,包含日期和UUID
|
|
|
|
|
String fileName = DateUtil.format(new Date(), NORM_MONTH_PATTERN) + IdUtil.simpleUUID() + "." + extName;
|
|
|
|
|
// 创建AttachFile对象,并设置属性
|
|
|
|
|
AttachFile attachFile = new AttachFile();
|
|
|
|
|
attachFile.setFilePath(fileName);
|
|
|
|
|
attachFile.setFileSize(file.getBytes().length);
|
|
|
|
|
attachFile.setFileType(extName);
|
|
|
|
|
attachFile.setUploadTime(new Date());
|
|
|
|
|
// 判断上传类型,如果是本地上传,则保存文件信息到数据库,并上传文件到本地
|
|
|
|
|
if (Objects.equals(imgUploadUtil.getUploadType(), 1)) {
|
|
|
|
|
attachFileMapper.insert(attachFile);
|
|
|
|
|
return imgUploadUtil.upload(file, fileName);
|
|
|
|
|
} else {
|
|
|
|
|
// 如果是七牛云上传,则获取上传凭证,并上传文件到七牛云
|
|
|
|
|
String upToken = auth.uploadToken(qiniu.getBucket(), fileName);
|
|
|
|
|
Response response = uploadManager.put(file.getBytes(), fileName, upToken);
|
|
|
|
|
// 解析上传响应
|
|
|
|
|
Json.parseObject(response.bodyString(), DefaultPutRet.class);
|
|
|
|
|
return fileName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除文件的方法实现。
|
|
|
|
|
*
|
|
|
|
|
* @param fileName 要删除的文件名称
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void deleteFile(String fileName){
|
|
|
|
|
// 根据文件名删除数据库中的记录
|
|
|
|
|
attachFileMapper.delete(new LambdaQueryWrapper<AttachFile>().eq(AttachFile::getFilePath, fileName));
|
|
|
|
|
try {
|
|
|
|
|
// 判断上传类型,如果是本地上传,则删除本地文件
|
|
|
|
|
if (Objects.equals(imgUploadUtil.getUploadType(), UploadType.LOCAL.value())) {
|
|
|
|
|
imgUploadUtil.delete(fileName);
|
|
|
|
|
} else if (Objects.equals(imgUploadUtil.getUploadType(), UploadType.QINIU.value())) {
|
|
|
|
|
// 如果是七牛云上传,则删除七牛云上的文件
|
|
|
|
|
bucketManager.delete(qiniu.getBucket(), fileName);
|
|
|
|
|
}
|
|
|
|
|
} catch (QiniuException e) {
|
|
|
|
|
// 如果删除失败,抛出运行时异常
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|