01d450fd by 村长大人

处理文件存储URL问题

1 parent 22e4a1ac
......@@ -4,9 +4,11 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lego.common.core.service.ConfigService;
import com.lego.system.domain.bo.SysOssBo;
import com.lego.system.service.ISysOssService;
import com.lego.common.constant.CacheNames;
......@@ -51,6 +53,7 @@ import java.util.stream.Collectors;
public class SysOssServiceImpl implements ISysOssService, OssService {
private final SysOssMapper baseMapper;
private final ConfigService configService;
@Override
public TableDataInfo<SysOssVo> queryPageList(SysOssBo bo, PageQuery pageQuery) {
......@@ -61,9 +64,28 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
return TableDataInfo.build(result);
}
private void repUrl(List<SysOssVo> list) {
String baseUrl = configService.getConfigValue("sys.oss.baseUrl");
for (SysOssVo vo : list) {
String url = vo.getUrl();
if (StrUtil.isNotBlank(url) && StrUtil.isNotBlank(baseUrl)) {
vo.setUrl(url.replace("127.0.0.1", baseUrl));
}
}
}
private void repUrl(SysOssVo vo) {
String baseUrl = configService.getConfigValue("sys.oss.baseUrl");
String url = vo.getUrl();
if (StrUtil.isNotBlank(url) && StrUtil.isNotBlank(baseUrl)) {
vo.setUrl(url.replace("127.0.0.1", baseUrl));
}
}
@Override
public List<SysOssVo> listByIds(Collection<Long> ossIds) {
List<SysOssVo> list = new ArrayList<>();
String baseUrl = configService.getConfigValue("sys.oss.baseUrl");
for (Long id : ossIds) {
SysOssVo vo = SpringUtils.getAopProxy(this).getById(id);
if (ObjectUtil.isNotNull(vo)) {
......@@ -72,8 +94,10 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
} catch (Exception ignored) {
// 如果oss异常无法连接则将数据直接返回
list.add(vo);
} }
}
}
}
repUrl(list);
return list;
}
......@@ -91,7 +115,14 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
}
}
}
return String.join(StringUtils.SEPARATOR, list);
String baseUrl = configService.getConfigValue("sys.oss.baseUrl");
List<String> repList = new ArrayList<>();
for (String url : list) {
if (StrUtil.isNotBlank(url) && StrUtil.isNotBlank(baseUrl)) {
repList.add(url.replace("127.0.0.1", baseUrl));
}
}
return String.join(StringUtils.SEPARATOR, repList);
}
private LambdaQueryWrapper<SysOss> buildQueryWrapper(SysOssBo bo) {
......@@ -111,7 +142,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
@Cacheable(cacheNames = CacheNames.SYS_OSS, key = "#ossId")
@Override
public SysOssVo getById(Long ossId) {
return baseMapper.selectVoById(ossId);
SysOssVo vo = baseMapper.selectVoById(ossId);
repUrl(vo);
return vo;
}
@Override
......@@ -123,7 +156,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName());
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
OssClient storage = OssFactory.instance(sysOss.getService());
try(InputStream inputStream = storage.getObjectContent(sysOss.getUrl())) {
try (InputStream inputStream = storage.getObjectContent(sysOss.getUrl())) {
int available = inputStream.available();
IoUtil.copy(inputStream, response.getOutputStream(), available);
response.setContentLength(available);
......@@ -144,7 +177,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
throw new ServiceException(e.getMessage());
}
// 保存文件信息
return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult);
SysOssVo vo = buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult);
repUrl(vo);
return vo;
}
@Override
......@@ -154,7 +189,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
OssClient storage = OssFactory.instance();
UploadResult uploadResult = storage.uploadSuffix(file, suffix);
// 保存文件信息
return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult);
SysOssVo vo = buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult);
repUrl(vo);
return vo;
}
@Override
......@@ -172,7 +209,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
baseMapper.insert(oss);
SysOssVo sysOssVo = new SysOssVo();
BeanCopyUtils.copy(oss, sysOssVo);
return this.matchingUrl(sysOssVo);
SysOssVo vo = this.matchingUrl(sysOssVo);
repUrl(vo);
return vo;
}
private SysOssVo buildResultEntity(String originalfileName, String suffix, String configKey, UploadResult uploadResult) {
......@@ -184,7 +223,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
oss.setService(configKey);
baseMapper.insert(oss);
SysOssVo sysOssVo = BeanUtil.toBean(oss, SysOssVo.class);
return this.matchingUrl(sysOssVo);
SysOssVo vo = this.matchingUrl(sysOssVo);
repUrl(vo);
return vo;
}
@Override
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!