SysOssServiceImpl.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package com.ruoyi.system.service.impl;
  2. import cn.hutool.core.convert.Convert;
  3. import cn.hutool.core.io.IoUtil;
  4. import cn.hutool.core.util.ObjectUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  7. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  8. import com.ruoyi.common.constant.CacheNames;
  9. import com.ruoyi.common.core.domain.PageQuery;
  10. import com.ruoyi.common.core.page.TableDataInfo;
  11. import com.ruoyi.common.core.service.OssService;
  12. import com.ruoyi.common.exception.ServiceException;
  13. import com.ruoyi.common.utils.BeanCopyUtils;
  14. import com.ruoyi.common.utils.StringUtils;
  15. import com.ruoyi.common.utils.file.FileUtils;
  16. import com.ruoyi.common.utils.spring.SpringUtils;
  17. import com.ruoyi.oss.core.OssClient;
  18. import com.ruoyi.oss.entity.UploadResult;
  19. import com.ruoyi.oss.enumd.AccessPolicyType;
  20. import com.ruoyi.oss.factory.OssFactory;
  21. import com.ruoyi.system.domain.SysOss;
  22. import com.ruoyi.system.domain.bo.SysOssBo;
  23. import com.ruoyi.system.domain.vo.SysOssVo;
  24. import com.ruoyi.system.mapper.SysOssMapper;
  25. import com.ruoyi.system.service.ISysOssService;
  26. import lombok.RequiredArgsConstructor;
  27. import org.springframework.cache.annotation.Cacheable;
  28. import org.springframework.http.MediaType;
  29. import org.springframework.stereotype.Service;
  30. import org.springframework.web.multipart.MultipartFile;
  31. import javax.servlet.http.HttpServletResponse;
  32. import java.io.IOException;
  33. import java.io.InputStream;
  34. import java.util.*;
  35. import java.util.stream.Collectors;
  36. /**
  37. * 文件上传 服务层实现
  38. *
  39. * @author Lion Li
  40. */
  41. @RequiredArgsConstructor
  42. @Service
  43. public class SysOssServiceImpl implements ISysOssService, OssService {
  44. private final SysOssMapper baseMapper;
  45. @Override
  46. public TableDataInfo<SysOssVo> queryPageList(SysOssBo bo, PageQuery pageQuery) {
  47. LambdaQueryWrapper<SysOss> lqw = buildQueryWrapper(bo);
  48. Page<SysOssVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
  49. List<SysOssVo> filterResult = result.getRecords().stream().map(this::matchingUrl).collect(Collectors.toList());
  50. result.setRecords(filterResult);
  51. return TableDataInfo.build(result);
  52. }
  53. @Override
  54. public List<SysOssVo> listByIds(Collection<Long> ossIds) {
  55. List<SysOssVo> list = new ArrayList<>();
  56. for (Long id : ossIds) {
  57. SysOssVo vo = SpringUtils.getAopProxy(this).getById(id);
  58. if (ObjectUtil.isNotNull(vo)) {
  59. list.add(this.matchingUrl(vo));
  60. }
  61. }
  62. return list;
  63. }
  64. @Override
  65. public String selectUrlByIds(String ossIds) {
  66. List<String> list = new ArrayList<>();
  67. for (Long id : StringUtils.splitTo(ossIds, Convert::toLong)) {
  68. SysOssVo vo = SpringUtils.getAopProxy(this).getById(id);
  69. if (ObjectUtil.isNotNull(vo)) {
  70. list.add(this.matchingUrl(vo).getUrl());
  71. }
  72. }
  73. return String.join(StringUtils.SEPARATOR, list);
  74. }
  75. private LambdaQueryWrapper<SysOss> buildQueryWrapper(SysOssBo bo) {
  76. Map<String, Object> params = bo.getParams();
  77. LambdaQueryWrapper<SysOss> lqw = Wrappers.lambdaQuery();
  78. lqw.like(StringUtils.isNotBlank(bo.getFileName()), SysOss::getFileName, bo.getFileName());
  79. lqw.like(StringUtils.isNotBlank(bo.getOriginalName()), SysOss::getOriginalName, bo.getOriginalName());
  80. lqw.eq(StringUtils.isNotBlank(bo.getFileSuffix()), SysOss::getFileSuffix, bo.getFileSuffix());
  81. lqw.eq(StringUtils.isNotBlank(bo.getUrl()), SysOss::getUrl, bo.getUrl());
  82. lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
  83. SysOss::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
  84. lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), SysOss::getCreateBy, bo.getCreateBy());
  85. lqw.eq(StringUtils.isNotBlank(bo.getService()), SysOss::getService, bo.getService());
  86. return lqw;
  87. }
  88. @Cacheable(cacheNames = CacheNames.SYS_OSS, key = "#ossId")
  89. @Override
  90. public SysOssVo getById(Long ossId) {
  91. return baseMapper.selectVoById(ossId);
  92. }
  93. @Override
  94. public void download(Long ossId, HttpServletResponse response) throws IOException {
  95. SysOssVo sysOss = SpringUtils.getAopProxy(this).getById(ossId);
  96. if (ObjectUtil.isNull(sysOss)) {
  97. throw new ServiceException("文件数据不存在!");
  98. }
  99. FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName());
  100. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
  101. OssClient storage = OssFactory.instance(sysOss.getService());
  102. try(InputStream inputStream = storage.getObjectContent(sysOss.getUrl())) {
  103. int available = inputStream.available();
  104. IoUtil.copy(inputStream, response.getOutputStream(), available);
  105. response.setContentLength(available);
  106. } catch (Exception e) {
  107. throw new ServiceException(e.getMessage());
  108. }
  109. }
  110. @Override
  111. public SysOssVo upload(MultipartFile file) {
  112. String originalfileName = file.getOriginalFilename();
  113. String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
  114. OssClient storage = OssFactory.instance();
  115. UploadResult uploadResult;
  116. try {
  117. uploadResult = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType());
  118. } catch (IOException e) {
  119. throw new ServiceException(e.getMessage());
  120. }
  121. // 保存文件信息
  122. SysOss oss = new SysOss();
  123. oss.setUrl(uploadResult.getUrl());
  124. oss.setFileSuffix(suffix);
  125. oss.setFileName(uploadResult.getFilename());
  126. oss.setOriginalName(originalfileName);
  127. oss.setService(storage.getConfigKey());
  128. baseMapper.insert(oss);
  129. SysOssVo sysOssVo = new SysOssVo();
  130. BeanCopyUtils.copy(oss, sysOssVo);
  131. return this.matchingUrl(sysOssVo);
  132. }
  133. @Override
  134. public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
  135. if (isValid) {
  136. // 做一些业务上的校验,判断是否需要校验
  137. }
  138. List<SysOss> list = baseMapper.selectBatchIds(ids);
  139. for (SysOss sysOss : list) {
  140. OssClient storage = OssFactory.instance(sysOss.getService());
  141. storage.delete(sysOss.getUrl());
  142. }
  143. return baseMapper.deleteBatchIds(ids) > 0;
  144. }
  145. /**
  146. * 匹配Url
  147. *
  148. * @param oss OSS对象
  149. * @return oss 匹配Url的OSS对象
  150. */
  151. private SysOssVo matchingUrl(SysOssVo oss) {
  152. OssClient storage = OssFactory.instance(oss.getService());
  153. // 仅修改桶类型为 private 的URL,临时URL时长为120s
  154. if (AccessPolicyType.PRIVATE == storage.getAccessPolicy()) {
  155. oss.setUrl(storage.getPrivateUrl(oss.getFileName(), 120));
  156. }
  157. return oss;
  158. }
  159. }