diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysOssController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysOssController.java index fac5ddef6..92bd3e3de 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysOssController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysOssController.java @@ -15,6 +15,8 @@ import com.ruoyi.common.core.validate.QueryGroup; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.file.FileUtils; +import com.ruoyi.oss.core.OssClient; +import com.ruoyi.oss.factory.OssFactory; import com.ruoyi.system.domain.SysOss; import com.ruoyi.system.domain.bo.SysOssBo; import com.ruoyi.system.domain.vo.SysOssVo; @@ -96,23 +98,7 @@ public class SysOssController extends BaseController { @SaCheckPermission("system:oss:download") @GetMapping("/download/{ossId}") public void download(@PathVariable Long ossId, HttpServletResponse response) throws IOException { - SysOssVo sysOss = iSysOssService.getById(ossId); - if (ObjectUtil.isNull(sysOss)) { - throw new ServiceException("文件数据不存在!"); - } - FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName()); - response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8"); - long data; - try { - data = HttpUtil.download(sysOss.getUrl(), response.getOutputStream(), false); - } catch (HttpException e) { - if (e.getMessage().contains("403")) { - throw new ServiceException("无读取权限, 请在对应的OSS开启'公有读'权限!"); - } else { - throw new ServiceException(e.getMessage()); - } - } - response.setContentLength(Convert.toInt(data)); + iSysOssService.download(ossId,response); } /** diff --git a/ruoyi-oss/src/main/java/com/ruoyi/oss/constant/OssConstant.java b/ruoyi-oss/src/main/java/com/ruoyi/oss/constant/OssConstant.java index 43aa07a45..ff88c1172 100644 --- a/ruoyi-oss/src/main/java/com/ruoyi/oss/constant/OssConstant.java +++ b/ruoyi-oss/src/main/java/com/ruoyi/oss/constant/OssConstant.java @@ -10,11 +10,6 @@ import java.util.List; */ public interface OssConstant { - /** - * OSS模块KEY - */ - String SYS_OSS_KEY = "sys_oss:"; - /** * 默认配置KEY */ diff --git a/ruoyi-oss/src/main/java/com/ruoyi/oss/core/OssClient.java b/ruoyi-oss/src/main/java/com/ruoyi/oss/core/OssClient.java index 0ec7c7120..4b50d7ee7 100644 --- a/ruoyi-oss/src/main/java/com/ruoyi/oss/core/OssClient.java +++ b/ruoyi-oss/src/main/java/com/ruoyi/oss/core/OssClient.java @@ -172,11 +172,10 @@ public class OssClient { } public String getPrivateUrl(String objectKey, Integer second) { -// objectKey = "file/2022/10/13/81eef51cb0734dedbfa616493a021fe4.jpg"; GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(properties.getBucketName(), objectKey) .withMethod(HttpMethod.GET) - .withExpiration(new Date(Instant.now().toEpochMilli() + 1000 * second)); + .withExpiration(new Date(System.currentTimeMillis() + 1000 * second)); URL url = client.generatePresignedUrl(generatePresignedUrlRequest); return url.toString(); } diff --git a/ruoyi-oss/src/main/java/com/ruoyi/oss/enumd/AccessPolicyType.java b/ruoyi-oss/src/main/java/com/ruoyi/oss/enumd/AccessPolicyType.java new file mode 100644 index 000000000..38db396f9 --- /dev/null +++ b/ruoyi-oss/src/main/java/com/ruoyi/oss/enumd/AccessPolicyType.java @@ -0,0 +1,35 @@ +package com.ruoyi.oss.enumd; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * 桶访问策略配置 + * + * @author 陈賝 + */ +@Getter +@AllArgsConstructor +public enum AccessPolicyType { + + /** + * private + */ + PRIVATE("0"), + + /** + * public + */ + PUBLIC("1"), + + /** + * constum + */ + CONSTUM("2"); + + /** + * 类型 + */ + private final String type; + +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysOssService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysOssService.java index f02ea3d98..280db1f43 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysOssService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysOssService.java @@ -7,6 +7,8 @@ import com.ruoyi.system.domain.bo.SysOssBo; import com.ruoyi.system.domain.vo.SysOssVo; import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; import java.util.Collection; import java.util.List; @@ -25,6 +27,8 @@ public interface ISysOssService { SysOss upload(MultipartFile file); + void download(Long ossId, HttpServletResponse response) throws IOException; + Boolean deleteWithValidByIds(Collection ids, Boolean isValid); } 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 81e4265d1..94885cc3b 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,6 +1,9 @@ package com.ruoyi.system.service.impl; +import cn.hutool.core.convert.Convert; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.http.HttpException; +import cn.hutool.http.HttpUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @@ -10,10 +13,13 @@ import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.JsonUtils; import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.utils.file.FileUtils; +import com.ruoyi.common.utils.redis.CacheUtils; import com.ruoyi.common.utils.redis.RedisUtils; import com.ruoyi.oss.constant.OssConstant; import com.ruoyi.oss.core.OssClient; import com.ruoyi.oss.entity.UploadResult; +import com.ruoyi.oss.enumd.AccessPolicyType; import com.ruoyi.oss.exception.OssException; import com.ruoyi.oss.factory.OssFactory; import com.ruoyi.oss.properties.OssProperties; @@ -24,9 +30,11 @@ 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 javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; @@ -49,19 +57,7 @@ public class SysOssServiceImpl implements ISysOssService { public TableDataInfo queryPageList(SysOssBo bo, PageQuery pageQuery) { LambdaQueryWrapper lqw = buildQueryWrapper(bo); Page result = baseMapper.selectVoPage(pageQuery.build(), lqw); - List filterResult = result.getRecords().stream().map(o -> { - Object json = RedisUtils.getCacheObject(OssConstant.SYS_OSS_KEY + o.getService()); - OssProperties properties = JsonUtils.parseObject(json.toString(), OssProperties.class); - if (properties == null) { - throw new OssException("系统异常, '" + o.getService() + "'配置信息不存在!"); - } - String privateFlag = "0"; - if (properties.getAccessPolicy().equals(privateFlag)) { - OssClient storage = OssFactory.instance(o.getService()); - o.setUrl(storage.getPrivateUrl(o.getFileName(), 100)); - } - return o; - }).collect(Collectors.toList()); + List filterResult = result.getRecords().stream().map(this::matchingUrl).collect(Collectors.toList()); result.setRecords(filterResult); return TableDataInfo.build(result); } @@ -98,6 +94,27 @@ public class SysOssServiceImpl implements ISysOssService { return baseMapper.selectVoById(ossId); } + @Override + public void download(Long ossId, HttpServletResponse response) throws IOException { + SysOssVo sysOss = this.matchingUrl(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"); + long data; + try { + data = HttpUtil.download(sysOss.getUrl(), response.getOutputStream(), false); + } catch (HttpException e) { + if (e.getMessage().contains("403")) { + throw new ServiceException("无读取权限, 请在对应的OSS开启'公有读'权限!"); + } else { + throw new ServiceException(e.getMessage()); + } + } + response.setContentLength(Convert.toInt(data)); + } + @Override public SysOss upload(MultipartFile file) { String originalfileName = file.getOriginalFilename(); @@ -133,4 +150,24 @@ public class SysOssServiceImpl implements ISysOssService { return baseMapper.deleteBatchIds(ids) > 0; } + /** + * 匹配Url + * + * @param oss OSS对象 + * @return oss 匹配Url的OSS对象 + */ + private SysOssVo matchingUrl(SysOssVo oss) { + OssProperties properties = JsonUtils.parseObject( + CacheUtils.get(CacheNames.SYS_OSS_CONFIG, oss.getService()).toString(), + OssProperties.class + ); + if (properties == null) { + throw new OssException("系统异常, '" + oss.getService() + "'配置信息不存在!"); + } + if (StringUtils.equals(AccessPolicyType.PRIVATE.getType(), properties.getAccessPolicy())) { + OssClient storage = OssFactory.instance(oss.getService()); + oss.setUrl(storage.getPrivateUrl(oss.getFileName(), 100)); + } + return oss; + } } diff --git a/ruoyi-ui/src/views/system/oss/config.vue b/ruoyi-ui/src/views/system/oss/config.vue index a2f2101d1..6110f9d02 100644 --- a/ruoyi-ui/src/views/system/oss/config.vue +++ b/ruoyi-ui/src/views/system/oss/config.vue @@ -80,6 +80,7 @@ +