处理文件存储URL问题
Showing
1 changed file
with
49 additions
and
8 deletions
| ... | @@ -4,9 +4,11 @@ import cn.hutool.core.bean.BeanUtil; | ... | @@ -4,9 +4,11 @@ import cn.hutool.core.bean.BeanUtil; |
| 4 | import cn.hutool.core.convert.Convert; | 4 | import cn.hutool.core.convert.Convert; |
| 5 | import cn.hutool.core.io.IoUtil; | 5 | import cn.hutool.core.io.IoUtil; |
| 6 | import cn.hutool.core.util.ObjectUtil; | 6 | import cn.hutool.core.util.ObjectUtil; |
| 7 | import cn.hutool.core.util.StrUtil; | ||
| 7 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 8 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| 8 | import com.baomidou.mybatisplus.core.toolkit.Wrappers; | 9 | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| 9 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 10 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| 11 | import com.lego.common.core.service.ConfigService; | ||
| 10 | import com.lego.system.domain.bo.SysOssBo; | 12 | import com.lego.system.domain.bo.SysOssBo; |
| 11 | import com.lego.system.service.ISysOssService; | 13 | import com.lego.system.service.ISysOssService; |
| 12 | import com.lego.common.constant.CacheNames; | 14 | import com.lego.common.constant.CacheNames; |
| ... | @@ -51,6 +53,7 @@ import java.util.stream.Collectors; | ... | @@ -51,6 +53,7 @@ import java.util.stream.Collectors; |
| 51 | public class SysOssServiceImpl implements ISysOssService, OssService { | 53 | public class SysOssServiceImpl implements ISysOssService, OssService { |
| 52 | 54 | ||
| 53 | private final SysOssMapper baseMapper; | 55 | private final SysOssMapper baseMapper; |
| 56 | private final ConfigService configService; | ||
| 54 | 57 | ||
| 55 | @Override | 58 | @Override |
| 56 | public TableDataInfo<SysOssVo> queryPageList(SysOssBo bo, PageQuery pageQuery) { | 59 | public TableDataInfo<SysOssVo> queryPageList(SysOssBo bo, PageQuery pageQuery) { |
| ... | @@ -61,9 +64,28 @@ public class SysOssServiceImpl implements ISysOssService, OssService { | ... | @@ -61,9 +64,28 @@ public class SysOssServiceImpl implements ISysOssService, OssService { |
| 61 | return TableDataInfo.build(result); | 64 | return TableDataInfo.build(result); |
| 62 | } | 65 | } |
| 63 | 66 | ||
| 67 | private void repUrl(List<SysOssVo> list) { | ||
| 68 | String baseUrl = configService.getConfigValue("sys.oss.baseUrl"); | ||
| 69 | for (SysOssVo vo : list) { | ||
| 70 | String url = vo.getUrl(); | ||
| 71 | if (StrUtil.isNotBlank(url) && StrUtil.isNotBlank(baseUrl)) { | ||
| 72 | vo.setUrl(url.replace("127.0.0.1", baseUrl)); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | private void repUrl(SysOssVo vo) { | ||
| 78 | String baseUrl = configService.getConfigValue("sys.oss.baseUrl"); | ||
| 79 | String url = vo.getUrl(); | ||
| 80 | if (StrUtil.isNotBlank(url) && StrUtil.isNotBlank(baseUrl)) { | ||
| 81 | vo.setUrl(url.replace("127.0.0.1", baseUrl)); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 64 | @Override | 85 | @Override |
| 65 | public List<SysOssVo> listByIds(Collection<Long> ossIds) { | 86 | public List<SysOssVo> listByIds(Collection<Long> ossIds) { |
| 66 | List<SysOssVo> list = new ArrayList<>(); | 87 | List<SysOssVo> list = new ArrayList<>(); |
| 88 | String baseUrl = configService.getConfigValue("sys.oss.baseUrl"); | ||
| 67 | for (Long id : ossIds) { | 89 | for (Long id : ossIds) { |
| 68 | SysOssVo vo = SpringUtils.getAopProxy(this).getById(id); | 90 | SysOssVo vo = SpringUtils.getAopProxy(this).getById(id); |
| 69 | if (ObjectUtil.isNotNull(vo)) { | 91 | if (ObjectUtil.isNotNull(vo)) { |
| ... | @@ -72,8 +94,10 @@ public class SysOssServiceImpl implements ISysOssService, OssService { | ... | @@ -72,8 +94,10 @@ public class SysOssServiceImpl implements ISysOssService, OssService { |
| 72 | } catch (Exception ignored) { | 94 | } catch (Exception ignored) { |
| 73 | // 如果oss异常无法连接则将数据直接返回 | 95 | // 如果oss异常无法连接则将数据直接返回 |
| 74 | list.add(vo); | 96 | list.add(vo); |
| 75 | } } | ||
| 76 | } | 97 | } |
| 98 | } | ||
| 99 | } | ||
| 100 | repUrl(list); | ||
| 77 | return list; | 101 | return list; |
| 78 | } | 102 | } |
| 79 | 103 | ||
| ... | @@ -91,7 +115,14 @@ public class SysOssServiceImpl implements ISysOssService, OssService { | ... | @@ -91,7 +115,14 @@ public class SysOssServiceImpl implements ISysOssService, OssService { |
| 91 | } | 115 | } |
| 92 | } | 116 | } |
| 93 | } | 117 | } |
| 94 | return String.join(StringUtils.SEPARATOR, list); | 118 | String baseUrl = configService.getConfigValue("sys.oss.baseUrl"); |
| 119 | List<String> repList = new ArrayList<>(); | ||
| 120 | for (String url : list) { | ||
| 121 | if (StrUtil.isNotBlank(url) && StrUtil.isNotBlank(baseUrl)) { | ||
| 122 | repList.add(url.replace("127.0.0.1", baseUrl)); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | return String.join(StringUtils.SEPARATOR, repList); | ||
| 95 | } | 126 | } |
| 96 | 127 | ||
| 97 | private LambdaQueryWrapper<SysOss> buildQueryWrapper(SysOssBo bo) { | 128 | private LambdaQueryWrapper<SysOss> buildQueryWrapper(SysOssBo bo) { |
| ... | @@ -111,7 +142,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService { | ... | @@ -111,7 +142,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService { |
| 111 | @Cacheable(cacheNames = CacheNames.SYS_OSS, key = "#ossId") | 142 | @Cacheable(cacheNames = CacheNames.SYS_OSS, key = "#ossId") |
| 112 | @Override | 143 | @Override |
| 113 | public SysOssVo getById(Long ossId) { | 144 | public SysOssVo getById(Long ossId) { |
| 114 | return baseMapper.selectVoById(ossId); | 145 | SysOssVo vo = baseMapper.selectVoById(ossId); |
| 146 | repUrl(vo); | ||
| 147 | return vo; | ||
| 115 | } | 148 | } |
| 116 | 149 | ||
| 117 | @Override | 150 | @Override |
| ... | @@ -123,7 +156,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService { | ... | @@ -123,7 +156,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService { |
| 123 | FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName()); | 156 | FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName()); |
| 124 | response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8"); | 157 | response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8"); |
| 125 | OssClient storage = OssFactory.instance(sysOss.getService()); | 158 | OssClient storage = OssFactory.instance(sysOss.getService()); |
| 126 | try(InputStream inputStream = storage.getObjectContent(sysOss.getUrl())) { | 159 | try (InputStream inputStream = storage.getObjectContent(sysOss.getUrl())) { |
| 127 | int available = inputStream.available(); | 160 | int available = inputStream.available(); |
| 128 | IoUtil.copy(inputStream, response.getOutputStream(), available); | 161 | IoUtil.copy(inputStream, response.getOutputStream(), available); |
| 129 | response.setContentLength(available); | 162 | response.setContentLength(available); |
| ... | @@ -144,7 +177,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService { | ... | @@ -144,7 +177,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService { |
| 144 | throw new ServiceException(e.getMessage()); | 177 | throw new ServiceException(e.getMessage()); |
| 145 | } | 178 | } |
| 146 | // 保存文件信息 | 179 | // 保存文件信息 |
| 147 | return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult); | 180 | SysOssVo vo = buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult); |
| 181 | repUrl(vo); | ||
| 182 | return vo; | ||
| 148 | } | 183 | } |
| 149 | 184 | ||
| 150 | @Override | 185 | @Override |
| ... | @@ -154,7 +189,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService { | ... | @@ -154,7 +189,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService { |
| 154 | OssClient storage = OssFactory.instance(); | 189 | OssClient storage = OssFactory.instance(); |
| 155 | UploadResult uploadResult = storage.uploadSuffix(file, suffix); | 190 | UploadResult uploadResult = storage.uploadSuffix(file, suffix); |
| 156 | // 保存文件信息 | 191 | // 保存文件信息 |
| 157 | return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult); | 192 | SysOssVo vo = buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult); |
| 193 | repUrl(vo); | ||
| 194 | return vo; | ||
| 158 | } | 195 | } |
| 159 | 196 | ||
| 160 | @Override | 197 | @Override |
| ... | @@ -172,7 +209,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService { | ... | @@ -172,7 +209,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService { |
| 172 | baseMapper.insert(oss); | 209 | baseMapper.insert(oss); |
| 173 | SysOssVo sysOssVo = new SysOssVo(); | 210 | SysOssVo sysOssVo = new SysOssVo(); |
| 174 | BeanCopyUtils.copy(oss, sysOssVo); | 211 | BeanCopyUtils.copy(oss, sysOssVo); |
| 175 | return this.matchingUrl(sysOssVo); | 212 | SysOssVo vo = this.matchingUrl(sysOssVo); |
| 213 | repUrl(vo); | ||
| 214 | return vo; | ||
| 176 | } | 215 | } |
| 177 | 216 | ||
| 178 | private SysOssVo buildResultEntity(String originalfileName, String suffix, String configKey, UploadResult uploadResult) { | 217 | private SysOssVo buildResultEntity(String originalfileName, String suffix, String configKey, UploadResult uploadResult) { |
| ... | @@ -184,7 +223,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService { | ... | @@ -184,7 +223,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService { |
| 184 | oss.setService(configKey); | 223 | oss.setService(configKey); |
| 185 | baseMapper.insert(oss); | 224 | baseMapper.insert(oss); |
| 186 | SysOssVo sysOssVo = BeanUtil.toBean(oss, SysOssVo.class); | 225 | SysOssVo sysOssVo = BeanUtil.toBean(oss, SysOssVo.class); |
| 187 | return this.matchingUrl(sysOssVo); | 226 | SysOssVo vo = this.matchingUrl(sysOssVo); |
| 227 | repUrl(vo); | ||
| 228 | return vo; | ||
| 188 | } | 229 | } |
| 189 | 230 | ||
| 190 | @Override | 231 | @Override | ... | ... |
-
Please register or sign in to post a comment