| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.constant.Constants; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.framework.config.RuoYiConfig; |
| | | import com.ruoyi.other.mapper.TempFileMapper; |
| | | import com.ruoyi.other.pojo.TempFile; |
| | | import com.ruoyi.other.service.TempFileService; |
| | |
| | | } |
| | | |
| | | // 构建正式目录路径(按业务类型和日期分组) |
| | | Path formalDirPath = Paths.get(uploadDir, LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE)); |
| | | String formalDir = uploadDir + LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE); |
| | | |
| | | Path formalDirPath = Paths.get(formalDir); |
| | | |
| | | // 确保正式目录存在(递归创建) |
| | | if (!Files.exists(formalDirPath)) { |
| | |
| | | fileRecord.setCommonId(businessId); |
| | | fileRecord.setName(originalFilename); |
| | | fileRecord.setUrl(formalFilePath.toString()); |
| | | fileRecord.setLink(buildAccessLink(formalFilePath)); |
| | | fileRecord.setCreateTime(LocalDateTime.now()); |
| | | fileRecord.setType(fileType); |
| | | commonFileMapper.insert(fileRecord); |
| | |
| | | } |
| | | } |
| | | |
| | | // @Scheduled(cron = "0 0 3 * * ?") // 每天凌晨3点执行 |
| | | // @Scheduled(cron = "0 0 3 * * ?") // 每天凌晨3点执行 |
| | | public void cleanupExpiredTempFiles() { |
| | | LambdaQueryWrapper<TempFile> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.lt(TempFile::getExpireTime, LocalDateTime.now()); // expireTime < 当前时间 |
| | |
| | | } |
| | | log.info("过期临时文件清理完成,共清理 {} 个文件", expiredFiles.size()); |
| | | } |
| | | |
| | | private String buildAccessLink(Path formalFilePath) { |
| | | String normalizedPath = formalFilePath.toString().replace("\\", "/"); |
| | | String profile = RuoYiConfig.getProfile(); |
| | | String normalizedProfile = profile == null ? "" : profile.replace("\\", "/"); |
| | | |
| | | String relativePath = normalizedPath; |
| | | if (StringUtils.hasText(normalizedProfile) && normalizedPath.startsWith(normalizedProfile)) { |
| | | relativePath = normalizedPath.substring(normalizedProfile.length()); |
| | | } |
| | | if (!relativePath.startsWith("/")) { |
| | | relativePath = "/" + relativePath; |
| | | } |
| | | |
| | | return Constants.RESOURCE_PREFIX + relativePath; |
| | | } |
| | | } |