From 71a2a8245db4c04be3f96a9eb9b47931d3130650 Mon Sep 17 00:00:00 2001 From: zlyx <1242874891@qq.com> Date: 星期三, 18 一月 2023 17:09:43 +0800 Subject: [PATCH] refactor 重构 common, framework 包结构, 参照 cloud 版本拆分子模块 ; update 更新模块包名 ; delete 移除 sms 模块, oss 模块, framework 模块 (并入 common 模块) ; --- ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssServiceImpl.java | 125 ++++++++++++++++++++++++++++++++--------- 1 files changed, 98 insertions(+), 27 deletions(-) diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssServiceImpl.java index b703ff6..0caf840 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssServiceImpl.java @@ -1,41 +1,72 @@ package com.ruoyi.system.service.impl; +import cn.hutool.core.io.IoUtil; +import cn.hutool.core.util.ObjectUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; -import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl; -import com.ruoyi.common.core.page.PagePlus; -import com.ruoyi.common.core.page.TableDataInfo; -import com.ruoyi.common.exception.ServiceException; -import com.ruoyi.common.utils.PageUtils; -import com.ruoyi.common.utils.StringUtils; -import com.ruoyi.oss.entity.UploadResult; -import com.ruoyi.oss.factory.OssFactory; -import com.ruoyi.oss.service.ICloudStorageStrategy; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.ruoyi.common.core.constant.CacheNames; +import com.ruoyi.common.mybatis.core.page.PageQuery; +import com.ruoyi.common.mybatis.core.page.TableDataInfo; +import com.ruoyi.common.core.exception.ServiceException; +import com.ruoyi.common.core.utils.BeanCopyUtils; +import com.ruoyi.common.core.utils.StringUtils; +import com.ruoyi.common.core.utils.file.FileUtils; +import com.ruoyi.common.core.utils.SpringUtils; +import com.ruoyi.common.oss.core.OssClient; +import com.ruoyi.common.oss.entity.UploadResult; +import com.ruoyi.common.oss.enumd.AccessPolicyType; +import com.ruoyi.common.oss.factory.OssFactory; import com.ruoyi.system.domain.SysOss; import com.ruoyi.system.domain.bo.SysOssBo; import com.ruoyi.system.domain.vo.SysOssVo; import com.ruoyi.system.mapper.SysOssMapper; import com.ruoyi.system.service.ISysOssService; +import lombok.RequiredArgsConstructor; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; /** * 鏂囦欢涓婁紶 鏈嶅姟灞傚疄鐜� * * @author Lion Li */ +@RequiredArgsConstructor @Service -public class SysOssServiceImpl extends ServicePlusImpl<SysOssMapper, SysOss, SysOssVo> implements ISysOssService { +public class SysOssServiceImpl implements ISysOssService { + + private final SysOssMapper baseMapper; @Override - public TableDataInfo<SysOssVo> queryPageList(SysOssBo bo) { - PagePlus<SysOss, SysOssVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo)); - return PageUtils.buildDataInfo(result); + public TableDataInfo<SysOssVo> queryPageList(SysOssBo bo, PageQuery pageQuery) { + LambdaQueryWrapper<SysOss> lqw = buildQueryWrapper(bo); + Page<SysOssVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw); + List<SysOssVo> filterResult = result.getRecords().stream().map(this::matchingUrl).collect(Collectors.toList()); + result.setRecords(filterResult); + return TableDataInfo.build(result); + } + + @Override + public List<SysOssVo> listByIds(Collection<Long> ossIds) { + List<SysOssVo> list = new ArrayList<>(); + for (Long id : ossIds) { + SysOssVo vo = SpringUtils.getAopProxy(this).getById(id); + if (ObjectUtil.isNotNull(vo)) { + list.add(this.matchingUrl(vo)); + } + } + return list; } private LambdaQueryWrapper<SysOss> buildQueryWrapper(SysOssBo bo) { @@ -46,17 +77,41 @@ lqw.eq(StringUtils.isNotBlank(bo.getFileSuffix()), SysOss::getFileSuffix, bo.getFileSuffix()); lqw.eq(StringUtils.isNotBlank(bo.getUrl()), SysOss::getUrl, bo.getUrl()); lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null, - SysOss::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime")); + SysOss::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime")); lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), SysOss::getCreateBy, bo.getCreateBy()); lqw.eq(StringUtils.isNotBlank(bo.getService()), SysOss::getService, bo.getService()); return lqw; } + @Cacheable(cacheNames = CacheNames.SYS_OSS, key = "#ossId") @Override - public SysOss upload(MultipartFile file) { + public SysOssVo getById(Long ossId) { + return baseMapper.selectVoById(ossId); + } + + @Override + public void download(Long ossId, HttpServletResponse response) throws IOException { + SysOssVo sysOss = SpringUtils.getAopProxy(this).getById(ossId); + if (ObjectUtil.isNull(sysOss)) { + throw new ServiceException("鏂囦欢鏁版嵁涓嶅瓨鍦�!"); + } + FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName()); + response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8"); + OssClient storage = OssFactory.instance(); + try(InputStream inputStream = storage.getObjectContent(sysOss.getUrl())) { + int available = inputStream.available(); + IoUtil.copy(inputStream, response.getOutputStream(), available); + response.setContentLength(available); + } catch (Exception e) { + throw new ServiceException(e.getMessage()); + } + } + + @Override + public SysOssVo upload(MultipartFile file) { String originalfileName = file.getOriginalFilename(); String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length()); - ICloudStorageStrategy storage = OssFactory.instance(); + OssClient storage = OssFactory.instance(); UploadResult uploadResult; try { uploadResult = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType()); @@ -64,14 +119,16 @@ throw new ServiceException(e.getMessage()); } // 淇濆瓨鏂囦欢淇℃伅 - SysOss oss = new SysOss() - .setUrl(uploadResult.getUrl()) - .setFileSuffix(suffix) - .setFileName(uploadResult.getFilename()) - .setOriginalName(originalfileName) - .setService(storage.getServiceType()); - save(oss); - return oss; + SysOss oss = new SysOss(); + oss.setUrl(uploadResult.getUrl()); + oss.setFileSuffix(suffix); + oss.setFileName(uploadResult.getFilename()); + oss.setOriginalName(originalfileName); + oss.setService(storage.getConfigKey()); + baseMapper.insert(oss); + SysOssVo sysOssVo = new SysOssVo(); + BeanCopyUtils.copy(oss, sysOssVo); + return this.matchingUrl(sysOssVo); } @Override @@ -79,12 +136,26 @@ if (isValid) { // 鍋氫竴浜涗笟鍔′笂鐨勬牎楠�,鍒ゆ柇鏄惁闇�瑕佹牎楠� } - List<SysOss> list = listByIds(ids); + List<SysOss> list = baseMapper.selectBatchIds(ids); for (SysOss sysOss : list) { - ICloudStorageStrategy storage = OssFactory.instance(sysOss.getService()); + OssClient storage = OssFactory.instance(sysOss.getService()); storage.delete(sysOss.getUrl()); } - return removeByIds(ids); + return baseMapper.deleteBatchIds(ids) > 0; } + /** + * 鍖归厤Url + * + * @param oss OSS瀵硅薄 + * @return oss 鍖归厤Url鐨凮SS瀵硅薄 + */ + private SysOssVo matchingUrl(SysOssVo oss) { + OssClient storage = OssFactory.instance(oss.getService()); + // 浠呬慨鏀规《绫诲瀷涓� private 鐨刄RL锛屼复鏃禪RL鏃堕暱涓�120s + if (AccessPolicyType.PRIVATE == storage.getAccessPolicy()) { + oss.setUrl(storage.getPrivateUrl(oss.getFileName(), 120)); + } + return oss; + } } -- Gitblit v1.9.3