| | |
| | | package org.dromara.system.service.impl; |
| | | |
| | | 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 com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.dromara.common.core.constant.CacheNames; |
| | | import org.dromara.common.core.domain.dto.OssDTO; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.service.OssService; |
| | | import org.dromara.common.core.utils.MapstructUtils; |
| | |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | |
| | | @Override |
| | | public List<SysOssVo> listByIds(Collection<Long> ossIds) { |
| | | List<SysOssVo> list = new ArrayList<>(); |
| | | SysOssServiceImpl ossService = SpringUtils.getAopProxy(this); |
| | | for (Long id : ossIds) { |
| | | SysOssVo vo = SpringUtils.getAopProxy(this).getById(id); |
| | | SysOssVo vo = ossService.getById(id); |
| | | if (ObjectUtil.isNotNull(vo)) { |
| | | try { |
| | | list.add(this.matchingUrl(vo)); |
| | |
| | | @Override |
| | | public String selectUrlByIds(String ossIds) { |
| | | List<String> list = new ArrayList<>(); |
| | | SysOssServiceImpl ossService = SpringUtils.getAopProxy(this); |
| | | for (Long id : StringUtils.splitTo(ossIds, Convert::toLong)) { |
| | | SysOssVo vo = SpringUtils.getAopProxy(this).getById(id); |
| | | SysOssVo vo = ossService.getById(id); |
| | | if (ObjectUtil.isNotNull(vo)) { |
| | | try { |
| | | list.add(this.matchingUrl(vo).getUrl()); |
| | |
| | | } |
| | | } |
| | | return String.join(StringUtils.SEPARATOR, list); |
| | | } |
| | | |
| | | @Override |
| | | public List<OssDTO> selectByIds(String ossIds) { |
| | | List<OssDTO> list = new ArrayList<>(); |
| | | for (Long id : StringUtils.splitTo(ossIds, Convert::toLong)) { |
| | | SysOssVo vo = SpringUtils.getAopProxy(this).getById(id); |
| | | if (ObjectUtil.isNotNull(vo)) { |
| | | try { |
| | | vo.setUrl(this.matchingUrl(vo).getUrl()); |
| | | list.add(BeanUtil.toBean(vo, OssDTO.class)); |
| | | } catch (Exception ignored) { |
| | | // 如果oss异常无法连接则将数据直接返回 |
| | | list.add(BeanUtil.toBean(vo, OssDTO.class)); |
| | | } |
| | | } |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | private LambdaQueryWrapper<SysOss> buildQueryWrapper(SysOssBo bo) { |
| | |
| | | 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())) { |
| | | int available = inputStream.available(); |
| | | IoUtil.copy(inputStream, response.getOutputStream(), available); |
| | | response.setContentLength(available); |
| | | } catch (Exception e) { |
| | | throw new ServiceException(e.getMessage()); |
| | | } |
| | | long contentLength = storage.download(sysOss.getFileName(), response.getOutputStream()); |
| | | response.setContentLengthLong(contentLength); |
| | | } |
| | | |
| | | /** |
| | |
| | | OssClient storage = OssFactory.instance(sysOss.getService()); |
| | | storage.delete(sysOss.getUrl()); |
| | | } |
| | | return baseMapper.deleteBatchIds(ids) > 0; |
| | | return baseMapper.deleteByIds(ids) > 0; |
| | | } |
| | | |
| | | /** |