From d346846239a8a39246c464dc634f5fd72add99ab Mon Sep 17 00:00:00 2001 From: maven <2163098428@qq.com> Date: 星期二, 26 八月 2025 15:22:15 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/pim-jlmy' into pim-jlmy --- main-business/src/main/java/com/ruoyi/business/other/service/impl/TempFileServiceImpl.java | 183 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 183 insertions(+), 0 deletions(-) diff --git a/main-business/src/main/java/com/ruoyi/business/other/service/impl/TempFileServiceImpl.java b/main-business/src/main/java/com/ruoyi/business/other/service/impl/TempFileServiceImpl.java new file mode 100644 index 0000000..b024478 --- /dev/null +++ b/main-business/src/main/java/com/ruoyi/business/other/service/impl/TempFileServiceImpl.java @@ -0,0 +1,183 @@ +package com.ruoyi.business.other.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.ruoyi.business.other.mapper.CommonFileMapper; +import com.ruoyi.business.other.mapper.TempFileMapper; +import com.ruoyi.business.other.pojo.CommonFile; +import com.ruoyi.business.other.pojo.TempFile; +import com.ruoyi.business.other.service.TempFileService; +import com.ruoyi.common.utils.StringUtils; + +import lombok.extern.slf4j.Slf4j; +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.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.List; +import java.util.UUID; + +@Service +@Slf4j +public class TempFileServiceImpl extends ServiceImpl<TempFileMapper, TempFile> implements TempFileService { + + @Autowired + private TempFileMapper tempFileMapper; + + @Autowired + private CommonFileMapper commonFileMapper; + + @Value("${file.upload-dir}") + private String uploadDir; + + @Value("${file.temp-dir}") + private String tempDir; + + // 涓婁紶鍒颁复鏃剁洰褰� + @Override + public TempFile uploadFile(MultipartFile file,Integer type) throws IOException { + // 1. 鐢熸垚涓存椂鏂囦欢ID鍜岃矾寰� + String tempId = UUID.randomUUID().toString(); + String originalFilename = file.getOriginalFilename(); + if(originalFilename == null) throw new IOException("鏂囦欢鍚嶄笉鑳戒负绌�"); +// URLEncoder urlEncoder = new URLEncoder(); +// String encodedFilename = urlEncoder.encode(originalFilename, StandardCharsets.UTF_8); +// encodedFilename = encodedFilename.replaceAll("%2E","."); +// Path tempFilePath = Paths.get(tempDir, tempId + "_" + encodedFilename); + + Path tempFilePath = Paths.get(tempDir, tempId + "_" + file.getOriginalFilename()); + + // 2. 纭繚鐩綍瀛樺湪 + Path parentDir = tempFilePath.getParent(); + if (parentDir != null && !Files.exists(parentDir)) { + try { + // 閫掑綊鍒涘缓鐩綍骞舵鏌ョ粨鏋� + Files.createDirectories(parentDir); + } catch (IOException e) { + log.error("鍒涘缓鐩綍澶辫触: {}", parentDir, e); + throw new IOException("鏃犳硶鍒涘缓鐩綍: " + parentDir, e); + } + } +// if (parentDir != null) { +// Files.createDirectories(parentDir); // 閫掑綊鍒涘缓鐩綍 +// } + + // 3. 淇濆瓨鏂囦欢鍒颁复鏃剁洰褰� + file.transferTo(tempFilePath.toFile()); + + // 4. 淇濆瓨涓存椂鏂囦欢璁板綍 + TempFile tempFileRecord = new TempFile(); + tempFileRecord.setTempId(tempId); + tempFileRecord.setOriginalName(file.getOriginalFilename()); + tempFileRecord.setTempPath(tempFilePath.toString()); + tempFileRecord.setExpireTime(LocalDateTime.now().plusHours(2)); // 2灏忔椂鍚庤繃鏈� + tempFileRecord.setType(type); + tempFileMapper.insert(tempFileRecord); + + return tempFileRecord; + } + + /** + * 灏嗕复鏃舵枃浠惰縼绉诲埌姝e紡鐩綍 + * + * @param businessId 涓氬姟ID锛堥攢鍞彴璐D锛� + * @param tempFileIds 涓存椂鏂囦欢ID鍒楄〃 + * @param fileType 鏂囦欢绫诲瀷(鏉ヨ嚜FileNameType) + * @throws IOException 鏂囦欢鎿嶄綔寮傚父 + */ + public void migrateTempFilesToFormal(Long businessId, List<String> tempFileIds, Integer fileType) throws IOException { + if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(tempFileIds)) { + return; + } + + // 鏋勫缓姝e紡鐩綍璺緞锛堟寜涓氬姟绫诲瀷鍜屾棩鏈熷垎缁勶級 + String formalDir = uploadDir + LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE); + + Path formalDirPath = Paths.get(formalDir); + + // 纭繚姝e紡鐩綍瀛樺湪锛堥�掑綊鍒涘缓锛� + if (!Files.exists(formalDirPath)) { + Files.createDirectories(formalDirPath); + } + + for (String tempFileId : tempFileIds) { + // 鏌ヨ涓存椂鏂囦欢璁板綍 + TempFile tempFile = tempFileMapper.selectById(tempFileId); + if (tempFile == null) { + log.warn("涓存椂鏂囦欢涓嶅瓨鍦紝璺宠繃澶勭悊: {}", tempFileId); + continue; + } + + // 鏋勫缓姝e紡鏂囦欢鍚嶏紙鍖呭惈涓氬姟ID鍜屾椂闂存埑锛岄伩鍏嶅啿绐侊級 + String originalFilename = tempFile.getOriginalName(); + String fileExtension = FilenameUtils.getExtension(originalFilename); + String formalFilename = businessId + "_" + + System.currentTimeMillis() + "_" + + UUID.randomUUID().toString().substring(0, 8) + + (StringUtils.hasText(fileExtension) ? "." + fileExtension : ""); + + Path formalFilePath = formalDirPath.resolve(formalFilename); + + try { + // 鎵ц鏂囦欢杩佺Щ锛堜娇鐢ㄥ師瀛愭搷浣滅‘淇濆畨鍏ㄦ�э級 + Files.move( + Paths.get(tempFile.getTempPath()), + formalFilePath, + StandardCopyOption.REPLACE_EXISTING, + StandardCopyOption.ATOMIC_MOVE + ); + log.info("鏂囦欢杩佺Щ鎴愬姛: {} -> {}", tempFile.getTempPath(), formalFilePath); + + // 鏇存柊鏂囦欢璁板綍锛堝叧鑱斿埌涓氬姟ID锛� + CommonFile fileRecord = new CommonFile(); + fileRecord.setCommonId(businessId); + fileRecord.setName(originalFilename); + fileRecord.setUrl(formalFilePath.toString()); + fileRecord.setCreateTime(LocalDateTime.now()); + fileRecord.setType(fileType); + commonFileMapper.insert(fileRecord); + + // 鍒犻櫎涓存椂鏂囦欢璁板綍 + tempFileMapper.deleteById(tempFile); + + log.info("鏂囦欢杩佺Щ鎴愬姛: {} -> {}", tempFile.getTempPath(), formalFilePath); + } catch (IOException e) { + log.error("鏂囦欢杩佺Щ澶辫触: {}", tempFile.getTempPath(), e); + // 鍙�夋嫨鍥炴粴浜嬪姟鎴栬褰曞け璐ユ枃浠� + throw new IOException("鏂囦欢杩佺Щ寮傚父", e); + } + } + } + + @Scheduled(cron = "0 0 3 * * ?") // 姣忓ぉ鍑屾櫒3鐐规墽琛� + public void cleanupExpiredTempFiles() { + LambdaQueryWrapper<TempFile> wrapper = new LambdaQueryWrapper<>(); + wrapper.lt(TempFile::getExpireTime, LocalDateTime.now()); // expireTime < 褰撳墠鏃堕棿 + + List<TempFile> expiredFiles = tempFileMapper.selectList(wrapper); + for (TempFile file : expiredFiles) { + try { + // 鍒犻櫎鐗╃悊鏂囦欢 + Files.deleteIfExists(Paths.get(file.getTempPath())); + // 鍒犻櫎鏁版嵁搴撹褰� + tempFileMapper.deleteById(file); + log.info("宸叉竻鐞嗚繃鏈熶复鏃舵枃浠�: {}", file.getTempPath()); + } catch (IOException e) { + log.error("鍒犻櫎鏂囦欢澶辫触: {}", file.getTempPath(), e); + // 鍙�夋嫨璁板綍澶辫触鏃ュ織鎴栭噸璇� + } + } + log.info("杩囨湡涓存椂鏂囦欢娓呯悊瀹屾垚锛屽叡娓呯悊 {} 涓枃浠�", expiredFiles.size()); + } +} -- Gitblit v1.9.3