|
4 | 4 | import com.dataengine.common.domain.model.FileUploadResult; |
5 | 5 | import com.dataengine.common.domain.service.FileService; |
6 | 6 | import com.dataengine.common.domain.utils.AnalyzerUtils; |
| 7 | +import com.dataengine.common.infrastructure.exception.BusinessException; |
| 8 | +import com.dataengine.common.infrastructure.exception.SystemErrorCode; |
7 | 9 | import com.dataengine.datamanagement.domain.contants.DatasetConstant; |
8 | 10 | import com.dataengine.datamanagement.domain.model.dataset.Dataset; |
9 | 11 | import com.dataengine.datamanagement.domain.model.dataset.DatasetFile; |
|
16 | 18 | import com.dataengine.datamanagement.interfaces.dto.UploadFilesPreRequest; |
17 | 19 | import com.fasterxml.jackson.core.JsonProcessingException; |
18 | 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | +import jakarta.servlet.http.HttpServletResponse; |
19 | 22 | import lombok.extern.slf4j.Slf4j; |
20 | 23 | import org.apache.ibatis.session.RowBounds; |
21 | 24 | import org.springframework.beans.factory.annotation.Autowired; |
|
25 | 28 | import org.springframework.data.domain.Page; |
26 | 29 | import org.springframework.data.domain.PageImpl; |
27 | 30 | import org.springframework.data.domain.Pageable; |
| 31 | +import org.springframework.http.HttpHeaders; |
28 | 32 | import org.springframework.stereotype.Service; |
29 | 33 | import org.springframework.transaction.annotation.Transactional; |
30 | 34 | import org.springframework.web.multipart.MultipartFile; |
31 | 35 |
|
| 36 | +import java.io.BufferedInputStream; |
32 | 37 | import java.io.File; |
33 | 38 | import java.io.IOException; |
| 39 | +import java.io.InputStream; |
34 | 40 | import java.net.MalformedURLException; |
35 | 41 | import java.nio.file.Files; |
36 | 42 | import java.nio.file.Path; |
37 | 43 | import java.nio.file.Paths; |
38 | 44 | import java.nio.file.StandardCopyOption; |
39 | 45 | import java.time.LocalDateTime; |
| 46 | +import java.time.format.DateTimeFormatter; |
40 | 47 | import java.util.List; |
41 | 48 | import java.util.Objects; |
42 | 49 | import java.util.UUID; |
| 50 | +import java.util.zip.ZipEntry; |
| 51 | +import java.util.zip.ZipOutputStream; |
43 | 52 |
|
44 | 53 | /** |
45 | 54 | * 数据集文件应用服务 |
@@ -75,7 +84,7 @@ public DatasetFileApplicationService(DatasetFileRepository datasetFileRepository |
75 | 84 | /** |
76 | 85 | * 上传文件到数据集 |
77 | 86 | */ |
78 | | - public DatasetFile uploadFile(String datasetId, MultipartFile file, String description, String uploadedBy) { |
| 87 | + public DatasetFile uploadFile(String datasetId, MultipartFile file) { |
79 | 88 | Dataset dataset = datasetRepository.getById(datasetId); |
80 | 89 | if (dataset == null) { |
81 | 90 | throw new IllegalArgumentException("Dataset not found: " + datasetId); |
@@ -182,6 +191,44 @@ public Resource downloadFile(String datasetId, String fileId) { |
182 | 191 | } |
183 | 192 | } |
184 | 193 |
|
| 194 | + /** |
| 195 | + * 下载文件 |
| 196 | + */ |
| 197 | + @Transactional(readOnly = true) |
| 198 | + public void downloadDatasetFileAsZip(String datasetId, HttpServletResponse response) { |
| 199 | + List<DatasetFile> allByDatasetId = datasetFileRepository.findAllByDatasetId(datasetId); |
| 200 | + response.setContentType("application/zip"); |
| 201 | + String zipName = String.format("dataset_%s.zip", |
| 202 | + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))); |
| 203 | + response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + zipName); |
| 204 | + try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) { |
| 205 | + for (DatasetFile file : allByDatasetId) { |
| 206 | + addToZipFile(file, zos); |
| 207 | + } |
| 208 | + } catch (IOException e) { |
| 209 | + log.error("Failed to download files in batches.", e); |
| 210 | + throw BusinessException.of(SystemErrorCode.FILE_SYSTEM_ERROR); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + private void addToZipFile(DatasetFile file, ZipOutputStream zos) throws IOException { |
| 215 | + if (file.getFilePath() == null || !Files.exists(Paths.get(file.getFilePath()))) { |
| 216 | + log.warn("The file hasn't been found on filesystem, id: {}", file.getId()); |
| 217 | + return; |
| 218 | + } |
| 219 | + try (InputStream fis = Files.newInputStream(Paths.get(file.getFilePath())); |
| 220 | + BufferedInputStream bis = new BufferedInputStream(fis)) { |
| 221 | + ZipEntry zipEntry = new ZipEntry(file.getFileName()); |
| 222 | + zos.putNextEntry(zipEntry); |
| 223 | + byte[] buffer = new byte[8192]; |
| 224 | + int length; |
| 225 | + while ((length = bis.read(buffer)) >= 0) { |
| 226 | + zos.write(buffer, 0, length); |
| 227 | + } |
| 228 | + zos.closeEntry(); |
| 229 | + } |
| 230 | + } |
| 231 | + |
185 | 232 | private String getFileExtension(String fileName) { |
186 | 233 | if (fileName == null || fileName.isEmpty()) { |
187 | 234 | return null; |
|
0 commit comments