|
|
@ -1,17 +1,23 @@
|
|
|
|
package com.ruoyi.resource.service.impl;
|
|
|
|
package com.ruoyi.resource.service.impl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.convert.Convert;
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
|
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.conditions.query.LambdaQueryWrapper;
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
import com.ruoyi.common.core.constant.CacheNames;
|
|
|
|
import com.ruoyi.common.core.constant.CacheNames;
|
|
|
|
import com.ruoyi.common.core.exception.ServiceException;
|
|
|
|
import com.ruoyi.common.core.exception.ServiceException;
|
|
|
|
|
|
|
|
import com.ruoyi.common.core.utils.BeanCopyUtils;
|
|
|
|
import com.ruoyi.common.core.utils.SpringUtils;
|
|
|
|
import com.ruoyi.common.core.utils.SpringUtils;
|
|
|
|
import com.ruoyi.common.core.utils.StringUtils;
|
|
|
|
import com.ruoyi.common.core.utils.StringUtils;
|
|
|
|
|
|
|
|
import com.ruoyi.common.core.utils.file.FileUtils;
|
|
|
|
import com.ruoyi.common.mybatis.core.page.PageQuery;
|
|
|
|
import com.ruoyi.common.mybatis.core.page.PageQuery;
|
|
|
|
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
|
|
|
|
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
|
|
|
|
import com.ruoyi.common.oss.core.OssClient;
|
|
|
|
import com.ruoyi.common.oss.core.OssClient;
|
|
|
|
import com.ruoyi.common.oss.entity.UploadResult;
|
|
|
|
import com.ruoyi.common.oss.entity.UploadResult;
|
|
|
|
|
|
|
|
import com.ruoyi.common.oss.enumd.AccessPolicyType;
|
|
|
|
import com.ruoyi.common.oss.factory.OssFactory;
|
|
|
|
import com.ruoyi.common.oss.factory.OssFactory;
|
|
|
|
import com.ruoyi.resource.domain.SysOss;
|
|
|
|
import com.ruoyi.resource.domain.SysOss;
|
|
|
|
import com.ruoyi.resource.domain.bo.SysOssBo;
|
|
|
|
import com.ruoyi.resource.domain.bo.SysOssBo;
|
|
|
@ -20,14 +26,17 @@ import com.ruoyi.resource.mapper.SysOssMapper;
|
|
|
|
import com.ruoyi.resource.service.ISysOssService;
|
|
|
|
import com.ruoyi.resource.service.ISysOssService;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
|
|
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 文件上传 服务层实现
|
|
|
|
* 文件上传 服务层实现
|
|
|
@ -44,6 +53,8 @@ public class SysOssServiceImpl implements ISysOssService {
|
|
|
|
public TableDataInfo<SysOssVo> queryPageList(SysOssBo bo, PageQuery pageQuery) {
|
|
|
|
public TableDataInfo<SysOssVo> queryPageList(SysOssBo bo, PageQuery pageQuery) {
|
|
|
|
LambdaQueryWrapper<SysOss> lqw = buildQueryWrapper(bo);
|
|
|
|
LambdaQueryWrapper<SysOss> lqw = buildQueryWrapper(bo);
|
|
|
|
Page<SysOssVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
|
|
|
|
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);
|
|
|
|
return TableDataInfo.build(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -51,9 +62,9 @@ public class SysOssServiceImpl implements ISysOssService {
|
|
|
|
public List<SysOssVo> listByIds(Collection<Long> ossIds) {
|
|
|
|
public List<SysOssVo> listByIds(Collection<Long> ossIds) {
|
|
|
|
List<SysOssVo> list = new ArrayList<>();
|
|
|
|
List<SysOssVo> list = new ArrayList<>();
|
|
|
|
for (Long id : ossIds) {
|
|
|
|
for (Long id : ossIds) {
|
|
|
|
SysOssVo vo = SpringUtils.getAopProxy(this).getById(id);
|
|
|
|
SysOssVo vo = getById(id);
|
|
|
|
if (ObjectUtil.isNotNull(vo)) {
|
|
|
|
if (ObjectUtil.isNotNull(vo)) {
|
|
|
|
list.add(vo);
|
|
|
|
list.add(this.matchingUrl(vo));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
return list;
|
|
|
@ -80,7 +91,28 @@ public class SysOssServiceImpl implements ISysOssService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public SysOss upload(MultipartFile file) {
|
|
|
|
public void download(Long ossId, HttpServletResponse response) throws IOException {
|
|
|
|
|
|
|
|
SysOssVo sysOss = this.matchingUrl(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");
|
|
|
|
|
|
|
|
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 SysOssVo upload(MultipartFile file) {
|
|
|
|
String originalfileName = file.getOriginalFilename();
|
|
|
|
String originalfileName = file.getOriginalFilename();
|
|
|
|
String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
|
|
|
|
String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
|
|
|
|
OssClient storage = OssFactory.instance();
|
|
|
|
OssClient storage = OssFactory.instance();
|
|
|
@ -98,7 +130,9 @@ public class SysOssServiceImpl implements ISysOssService {
|
|
|
|
oss.setOriginalName(originalfileName);
|
|
|
|
oss.setOriginalName(originalfileName);
|
|
|
|
oss.setService(storage.getConfigKey());
|
|
|
|
oss.setService(storage.getConfigKey());
|
|
|
|
baseMapper.insert(oss);
|
|
|
|
baseMapper.insert(oss);
|
|
|
|
return oss;
|
|
|
|
SysOssVo sysOssVo = new SysOssVo();
|
|
|
|
|
|
|
|
BeanCopyUtils.copy(oss, sysOssVo);
|
|
|
|
|
|
|
|
return this.matchingUrl(sysOssVo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -114,4 +148,21 @@ public class SysOssServiceImpl implements ISysOssService {
|
|
|
|
return baseMapper.deleteBatchIds(ids) > 0;
|
|
|
|
return baseMapper.deleteBatchIds(ids) > 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 匹配Url
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param oss OSS对象
|
|
|
|
|
|
|
|
* @return oss 匹配Url的OSS对象
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private SysOssVo matchingUrl(SysOssVo oss) {
|
|
|
|
|
|
|
|
OssClient storage = OssFactory.instance(oss.getService());
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 仅修改桶类型为 private 的URL,临时URL时长为100s
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (AccessPolicyType.PRIVATE == storage.getAccessPolicy()) {
|
|
|
|
|
|
|
|
oss.setUrl(storage.getPrivateUrl(oss.getFileName(), 100));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return oss;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|