| | |
| | | |
| | | 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; |
| | | import com.ruoyi.sales.mapper.CommonFileMapper; |
| | | import com.ruoyi.sales.pojo.CommonFile; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.catalina.util.URLEncoder; |
| | | import org.apache.commons.io.FilenameUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.IOException; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | |
| | | 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; |
| | | } |
| | | } |