OssFileInfoService.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package cc.uncarbon.module.oss.service;
  2. import cc.uncarbon.framework.core.constant.HelioConstant;
  3. import cc.uncarbon.framework.core.exception.BusinessException;
  4. import cc.uncarbon.framework.core.page.PageParam;
  5. import cc.uncarbon.framework.core.page.PageResult;
  6. import cc.uncarbon.module.oss.constant.OssConstant;
  7. import cc.uncarbon.module.oss.entity.OssFileInfoEntity;
  8. import cc.uncarbon.module.oss.enums.OssErrorEnum;
  9. import cc.uncarbon.module.oss.mapper.OssFileInfoMapper;
  10. import cc.uncarbon.module.oss.model.request.AdminListOssFileInfoDTO;
  11. import cc.uncarbon.module.oss.model.request.UploadFileAttributeDTO;
  12. import cc.uncarbon.module.oss.model.response.OssFileInfoBO;
  13. import cn.hutool.core.bean.BeanUtil;
  14. import cn.hutool.core.collection.CollUtil;
  15. import cn.hutool.core.io.FileUtil;
  16. import cn.hutool.core.text.CharSequenceUtil;
  17. import cn.hutool.core.util.ObjectUtil;
  18. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  19. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  20. import lombok.NonNull;
  21. import lombok.RequiredArgsConstructor;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.dromara.x.file.storage.core.FileInfo;
  24. import org.dromara.x.file.storage.core.FileStorageService;
  25. import org.springframework.stereotype.Service;
  26. import org.springframework.transaction.annotation.Transactional;
  27. import java.util.ArrayList;
  28. import java.util.Collection;
  29. import java.util.Collections;
  30. import java.util.List;
  31. /**
  32. * 上传文件信息
  33. */
  34. @Slf4j
  35. @Service
  36. @RequiredArgsConstructor
  37. public class OssFileInfoService {
  38. private final OssFileInfoMapper ossFileInfoMapper;
  39. private final FileStorageService fileStorageService;
  40. /**
  41. * 后台管理-分页列表
  42. */
  43. public PageResult<OssFileInfoBO> adminList(PageParam pageParam, AdminListOssFileInfoDTO dto) {
  44. Page<OssFileInfoEntity> entityPage = ossFileInfoMapper.selectPage(
  45. new Page<>(pageParam.getPageNum(), pageParam.getPageSize()),
  46. new QueryWrapper<OssFileInfoEntity>()
  47. .lambda()
  48. // 原始文件名
  49. .like(CharSequenceUtil.isNotBlank(dto.getOriginalFilename()), OssFileInfoEntity::getOriginalFilename, CharSequenceUtil.cleanBlank(dto.getOriginalFilename()))
  50. // 扩展名
  51. .eq(CharSequenceUtil.isNotBlank(dto.getExtendName()), OssFileInfoEntity::getExtendName, CharSequenceUtil.cleanBlank(dto.getExtendName()))
  52. // 文件类别
  53. .eq(CharSequenceUtil.isNotBlank(dto.getClassified()), OssFileInfoEntity::getClassified, CharSequenceUtil.cleanBlank(dto.getClassified()))
  54. // 时间区间
  55. .between(ObjectUtil.isNotNull(dto.getBeginAt()) && ObjectUtil.isNotNull(dto.getEndAt()), OssFileInfoEntity::getCreatedAt, dto.getBeginAt(), dto.getEndAt())
  56. // 排序
  57. .orderByDesc(OssFileInfoEntity::getCreatedAt)
  58. );
  59. return this.entityPage2BOPage(entityPage);
  60. }
  61. /**
  62. * 根据 ID 取详情
  63. *
  64. * @param id 主键ID
  65. * @return null or BO
  66. */
  67. public OssFileInfoBO getOneById(Long id) {
  68. return this.getOneById(id, false);
  69. }
  70. /**
  71. * 根据 ID 取详情
  72. *
  73. * @param id 主键ID
  74. * @param throwIfInvalidId 是否在 ID 无效时抛出异常
  75. * @return null or BO
  76. */
  77. public OssFileInfoBO getOneById(Long id, boolean throwIfInvalidId) throws BusinessException {
  78. OssFileInfoEntity entity = ossFileInfoMapper.selectById(id);
  79. if (throwIfInvalidId) {
  80. OssErrorEnum.INVALID_ID.assertNotNull(entity);
  81. }
  82. return this.entity2BO(entity);
  83. }
  84. /**
  85. * 后台管理-删除
  86. */
  87. @Transactional(rollbackFor = Exception.class)
  88. public void adminDelete(Collection<Long> ids) {
  89. log.info("[后台管理-删除上传文件信息] >> ids={}", ids);
  90. // 1. 删除原始文件
  91. List<OssFileInfoEntity> entityList = ossFileInfoMapper.selectByIds(ids);
  92. for (OssFileInfoEntity entity : entityList) {
  93. fileStorageService.delete(toFileInfo(entity));
  94. }
  95. // 2. 删除文件记录
  96. ossFileInfoMapper.deleteBatchIds(ids);
  97. }
  98. /**
  99. * 根据 MD5 取文件信息
  100. */
  101. public OssFileInfoBO getOneByMd5(String md5) {
  102. OssFileInfoEntity entity = ossFileInfoMapper.selectOne(
  103. new QueryWrapper<OssFileInfoEntity>()
  104. .lambda()
  105. .eq(OssFileInfoEntity::getMd5, md5)
  106. .last(HelioConstant.CRUD.SQL_LIMIT_1)
  107. );
  108. return this.entity2BO(entity);
  109. }
  110. /**
  111. * 上传成功后,保存文件记录
  112. *
  113. * @return 文件ID
  114. */
  115. @Transactional(rollbackFor = Exception.class)
  116. public OssFileInfoBO save(@NonNull FileInfo fileInfo, @NonNull UploadFileAttributeDTO attr) {
  117. OssFileInfoEntity entity = new OssFileInfoEntity()
  118. .setStoragePlatform(fileInfo.getPlatform())
  119. .setStorageBasePath(fileInfo.getBasePath())
  120. .setStoragePath(fileInfo.getPath())
  121. // 需要裁切掉后缀名
  122. .setStorageFilename(FileUtil.getPrefix(fileInfo.getFilename()))
  123. .setOriginalFilename(FileUtil.getPrefix(fileInfo.getOriginalFilename()))
  124. .setExtendName(fileInfo.getExt())
  125. .setFileSize(fileInfo.getSize())
  126. .setMd5(attr.getMd5())
  127. .setClassified(attr.getClassified());
  128. if (!isLocalPlatform(fileInfo.getPlatform())) {
  129. // 非本地存储,保存对象存储直链
  130. entity.setDirectUrl(fileInfo.getUrl());
  131. }
  132. ossFileInfoMapper.insert(entity);
  133. return this.entity2BO(entity);
  134. }
  135. /**
  136. * 转换为 FileInfo 对象
  137. */
  138. public static FileInfo toFileInfo(OssFileInfoBO source) {
  139. FileInfo fileInfo = new FileInfo();
  140. fileInfo.setPlatform(source.getStoragePlatform());
  141. fileInfo.setBasePath(source.getStorageBasePath());
  142. fileInfo.setPath(source.getStoragePath());
  143. fileInfo.setFilename(source.getStorageFilenameFull());
  144. fileInfo.setSize(source.getFileSize());
  145. fileInfo.setOriginalFilename(source.getOriginalFilenameFull());
  146. return fileInfo;
  147. }
  148. /**
  149. * 转换为 FileInfo 对象
  150. */
  151. public static FileInfo toFileInfo(OssFileInfoEntity source) {
  152. FileInfo fileInfo = new FileInfo();
  153. fileInfo.setPlatform(source.getStoragePlatform());
  154. fileInfo.setBasePath(source.getStorageBasePath());
  155. fileInfo.setPath(source.getStoragePath());
  156. fileInfo.setFilename(source.getStorageFilenameFull());
  157. fileInfo.setSize(source.getFileSize());
  158. fileInfo.setOriginalFilename(source.getOriginalFilenameFull());
  159. return fileInfo;
  160. }
  161. /**
  162. * 是否为本地存储平台
  163. * @param storagePlatform 存储平台名
  164. */
  165. public static boolean isLocalPlatform(String storagePlatform) {
  166. return CharSequenceUtil.startWith(storagePlatform, OssConstant.PLATFORM_PREFIX_LOCAL);
  167. }
  168. /*
  169. ----------------------------------------------------------------
  170. 私有方法 private methods
  171. ----------------------------------------------------------------
  172. */
  173. /**
  174. * 实体转 BO
  175. *
  176. * @param entity 实体
  177. * @return BO
  178. */
  179. private OssFileInfoBO entity2BO(OssFileInfoEntity entity) {
  180. if (entity == null) {
  181. return null;
  182. }
  183. OssFileInfoBO bo = new OssFileInfoBO();
  184. BeanUtil.copyProperties(entity, bo);
  185. // 可以在此处为BO填充字段
  186. return bo;
  187. }
  188. /**
  189. * 实体 List 转 BO List
  190. *
  191. * @param entityList 实体 List
  192. * @return BO List
  193. */
  194. private List<OssFileInfoBO> entityList2BOs(List<OssFileInfoEntity> entityList) {
  195. if (CollUtil.isEmpty(entityList)) {
  196. return Collections.emptyList();
  197. }
  198. // 深拷贝
  199. List<OssFileInfoBO> ret = new ArrayList<>(entityList.size());
  200. entityList.forEach(
  201. entity -> ret.add(this.entity2BO(entity))
  202. );
  203. return ret;
  204. }
  205. /**
  206. * 实体分页转 BO 分页
  207. *
  208. * @param entityPage 实体分页
  209. * @return BO 分页
  210. */
  211. private PageResult<OssFileInfoBO> entityPage2BOPage(Page<OssFileInfoEntity> entityPage) {
  212. return new PageResult<OssFileInfoBO>()
  213. .setCurrent(entityPage.getCurrent())
  214. .setSize(entityPage.getSize())
  215. .setTotal(entityPage.getTotal())
  216. .setRecords(this.entityList2BOs(entityPage.getRecords()))
  217. ;
  218. }
  219. }