diff --git a/src/main/java/com/yunhai/homework/service/impl/FileService.java b/src/main/java/com/yunhai/homework/service/impl/FileService.java index 6f1c57e..ff71bd9 100644 --- a/src/main/java/com/yunhai/homework/service/impl/FileService.java +++ b/src/main/java/com/yunhai/homework/service/impl/FileService.java @@ -1,8 +1,8 @@ package com.yunhai.homework.service.impl; import com.yunhai.homework.entity.UpFile; -import com.yunhai.homework.model.homework.UpFileVo; import com.yunhai.homework.model.homework.UpFileResultVo; +import com.yunhai.homework.model.homework.UpFileVo; import com.yunhai.homework.repository.UpFileRepository; import com.yunhai.homework.service.IFileService; import com.yunhai.platform.apistd.BizException; @@ -18,7 +18,6 @@ import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; -import java.io.File; import java.io.FileInputStream; import java.io.OutputStream; import java.net.URLConnection; @@ -26,15 +25,7 @@ import java.net.URLEncoder; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.UUID; -import java.util.ArrayList; +import java.util.*; import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -53,11 +44,9 @@ public class FileService implements IFileService { @Autowired private UpFileRepository upFileRepository; - @Value("${file.upload-path}") + @Value("${FILE_PATH}") private String uploadPath; - private static final String DEFAULT_APP_TYPE = "homework"; - // ============================ 上传 ============================ @Override @@ -77,13 +66,10 @@ public class FileService implements IFileService { ext = originalName.substring(dotIdx + 1).toLowerCase(); } - // 构造存储子路径:{appType}/{yyyyMMdd}/{uuid}.{ext} - String dateDir = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); String savedName = UUID.randomUUID().toString().replace("-", "") + (ext.isEmpty() ? "" : "." + ext); - String relativePath = (appType == null ? DEFAULT_APP_TYPE : appType) + "/" + dateDir + "/" + savedName; // 物理写入 - Path fullPath = Paths.get(uploadPath, relativePath); + Path fullPath = Paths.get(uploadPath, savedName); try { Files.createDirectories(fullPath.getParent()); file.transferTo(fullPath.toFile()); @@ -94,10 +80,10 @@ public class FileService implements IFileService { // 入库 UpFile entity = new UpFile(); - entity.setTopicType(appType == null ? DEFAULT_APP_TYPE : appType); + entity.setTopicType(appType); entity.setTopicId(topicId); entity.setFileTrueName(originalName); - entity.setFileName(relativePath); + entity.setFileName(savedName); entity.setFileSize((int) file.getSize()); entity.setFileType(ext); upFileRepository.save(entity); @@ -125,8 +111,7 @@ public class FileService implements IFileService { @Override public List listByTopic(String appType, Integer topicId) { - String type = appType == null ? DEFAULT_APP_TYPE : appType; - return upFileRepository.findByTopicTypeAndTopicId(type, topicId).stream() + return upFileRepository.findByTopicTypeAndTopicId(appType, topicId).stream() .map(this::toVo) .collect(Collectors.toList()); } @@ -279,8 +264,7 @@ public class FileService implements IFileService { @Override public void downloadZipByTopic(String topicType, Integer topicId, String zipName, HttpServletResponse response) { - String type = topicType == null ? DEFAULT_APP_TYPE : topicType; - List files = upFileRepository.findByTopicTypeAndTopicId(type, topicId); + List files = upFileRepository.findByTopicTypeAndTopicId(topicType, topicId); downloadZip(files, zipName, response); } @@ -293,9 +277,8 @@ public class FileService implements IFileService { if (files == null || files.length == 0) { return results; } - String type = appType != null ? appType : DEFAULT_APP_TYPE; for (MultipartFile file : files) { - results.add(upload(file, type, null)); + results.add(upload(file, appType, null)); } return results; }