| | |
| | | package org.dromara.system.sync; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.Wrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.http.HttpResponse; |
| | | import org.dromara.common.mybatis.core.domain.BaseEntity; |
| | | import org.dromara.common.oss.core.OssClient; |
| | | import org.dromara.common.oss.factory.OssFactory; |
| | | import org.dromara.system.domain.SysOss; |
| | |
| | | @Scheduled(cron = "0 */10 * * * ?") |
| | | public void remove() throws IOException { |
| | | log.error("同步删除......"); |
| | | List<Long> ids = baseMapper.getWillDeleted(LocalDateTime.now().minusDays(1)); |
| | | |
| | | if (ids.size() == 0) { |
| | | List<SysOss> list = new LambdaQueryChainWrapper<>(baseMapper) |
| | | .le(SysOss::getCreateTime, LocalDateTime.now().minusDays(1L)) |
| | | .list(); |
| | | if (list.size() == 0) |
| | | return; |
| | | } |
| | | OssClient ossClient = OssFactory.instance(); |
| | | for (Long ossId : ids) { |
| | | SysOss sysOss = baseMapper.selectById(ossId); |
| | | if (Objects.nonNull(sysOss)) { |
| | | ossClient.delete(sysOss.getFileName()); |
| | | |
| | | for (SysOss vo : list) { |
| | | OssClient storage = OssFactory.instance(vo.getService()); |
| | | storage.delete(vo.getUrl()); |
| | | HttpResponse httpResponse = HttpUtils.sendDeleteRequest(this.boundary.getVideo() + "/resource/synchronization/delete", |
| | | String.valueOf(vo.getOssId())); |
| | | if (httpResponse.getStatusLine().getStatusCode() == 200) { |
| | | baseMapper.deleteId(vo.getOssId()); |
| | | } |
| | | HttpUtils.sendDeleteRequest(boundary.getVideo() + "/resource/synchronization/delete", |
| | | String.valueOf(ossId)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 每小时删除2天之前的文件信息,因为上面的同步删除并不能保证事务 |
| | | * |
| | | * @throws IOException |
| | | */ |
| | | @Scheduled(cron = "0 0 * * * ?") |
| | | public void removeOssTable() throws IOException { |
| | | log.error("每小时定时删除超过2天的数据库文件信息......"); |
| | | List<Long> ids = baseMapper.getWillDeleted(LocalDateTime.now().minusDays(2)); |
| | | if (ids.size() == 0) { |
| | | return; |
| | | } |
| | | // 删除对应文件 |
| | | OssClient ossClient = OssFactory.instance(); |
| | | for (Long ossId : ids) { |
| | | SysOss sysOss = baseMapper.selectById(ossId); |
| | | if (Objects.nonNull(sysOss)) { |
| | | ossClient.delete(sysOss.getFileName()); |
| | | } |
| | | } |
| | | baseMapper.deleteIds(ids); |
| | | } |
| | | } |