You've already forked DataMate
feat: Enhance dataset file management with improved file copying
This commit is contained in:
@@ -21,7 +21,6 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.io.FileUtils;
|
|
||||||
import org.apache.ibatis.session.RowBounds;
|
import org.apache.ibatis.session.RowBounds;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@@ -45,6 +44,7 @@ import java.nio.file.Paths;
|
|||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
@@ -76,7 +76,7 @@ public class DatasetFileApplicationService {
|
|||||||
*/
|
*/
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Page<DatasetFile> getDatasetFiles(String datasetId, String fileType,
|
public Page<DatasetFile> getDatasetFiles(String datasetId, String fileType,
|
||||||
String status, Pageable pageable) {
|
String status, Pageable pageable) {
|
||||||
RowBounds bounds = new RowBounds(pageable.getPageNumber() * pageable.getPageSize(), pageable.getPageSize());
|
RowBounds bounds = new RowBounds(pageable.getPageNumber() * pageable.getPageSize(), pageable.getPageSize());
|
||||||
List<DatasetFile> content = datasetFileRepository.findByCriteria(datasetId, fileType, status, bounds);
|
List<DatasetFile> content = datasetFileRepository.findByCriteria(datasetId, fileType, status, bounds);
|
||||||
long total = content.size() < pageable.getPageSize() && pageable.getPageNumber() == 0 ? content.size() : content.size() + (long) pageable.getPageNumber() * pageable.getPageSize();
|
long total = content.size() < pageable.getPageSize() && pageable.getPageNumber() == 0 ? content.size() : content.size() + (long) pageable.getPageNumber() * pageable.getPageSize();
|
||||||
@@ -148,7 +148,7 @@ public class DatasetFileApplicationService {
|
|||||||
fileRename(allByDatasetId);
|
fileRename(allByDatasetId);
|
||||||
response.setContentType("application/zip");
|
response.setContentType("application/zip");
|
||||||
String zipName = String.format("dataset_%s.zip",
|
String zipName = String.format("dataset_%s.zip",
|
||||||
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
|
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
|
||||||
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + zipName);
|
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + zipName);
|
||||||
try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
|
try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
|
||||||
for (DatasetFile file : allByDatasetId) {
|
for (DatasetFile file : allByDatasetId) {
|
||||||
@@ -203,7 +203,7 @@ public class DatasetFileApplicationService {
|
|||||||
* 预上传
|
* 预上传
|
||||||
*
|
*
|
||||||
* @param chunkUploadRequest 上传请求
|
* @param chunkUploadRequest 上传请求
|
||||||
* @param datasetId 数据集id
|
* @param datasetId 数据集id
|
||||||
* @return 请求id
|
* @return 请求id
|
||||||
*/
|
*/
|
||||||
@Transactional
|
@Transactional
|
||||||
@@ -245,15 +245,15 @@ public class DatasetFileApplicationService {
|
|||||||
File savedFile = fileUploadResult.getSavedFile();
|
File savedFile = fileUploadResult.getSavedFile();
|
||||||
LocalDateTime currentTime = LocalDateTime.now();
|
LocalDateTime currentTime = LocalDateTime.now();
|
||||||
DatasetFile datasetFile = DatasetFile.builder()
|
DatasetFile datasetFile = DatasetFile.builder()
|
||||||
.id(UUID.randomUUID().toString())
|
.id(UUID.randomUUID().toString())
|
||||||
.datasetId(datasetId)
|
.datasetId(datasetId)
|
||||||
.fileSize(savedFile.length())
|
.fileSize(savedFile.length())
|
||||||
.uploadTime(currentTime)
|
.uploadTime(currentTime)
|
||||||
.lastAccessTime(currentTime)
|
.lastAccessTime(currentTime)
|
||||||
.fileName(uploadFile.getFileName())
|
.fileName(uploadFile.getFileName())
|
||||||
.filePath(savedFile.getPath())
|
.filePath(savedFile.getPath())
|
||||||
.fileType(AnalyzerUtils.getExtension(uploadFile.getFileName()))
|
.fileType(AnalyzerUtils.getExtension(uploadFile.getFileName()))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
datasetFileRepository.save(datasetFile);
|
datasetFileRepository.save(datasetFile);
|
||||||
dataset.addFile(datasetFile);
|
dataset.addFile(datasetFile);
|
||||||
@@ -265,7 +265,7 @@ public class DatasetFileApplicationService {
|
|||||||
* 复制文件到数据集目录
|
* 复制文件到数据集目录
|
||||||
*
|
*
|
||||||
* @param datasetId 数据集id
|
* @param datasetId 数据集id
|
||||||
* @param req 复制文件请求
|
* @param req 复制文件请求
|
||||||
* @return 复制的文件列表
|
* @return 复制的文件列表
|
||||||
*/
|
*/
|
||||||
@Transactional
|
@Transactional
|
||||||
@@ -280,31 +280,38 @@ public class DatasetFileApplicationService {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
String fileName = sourcePath.getFileName().toString();
|
String fileName = sourcePath.getFileName().toString();
|
||||||
File targetFile = new File(dataset.getPath(), fileName);
|
File sourceFile = sourcePath.toFile();
|
||||||
try {
|
|
||||||
FileUtils.copyInputStreamToFile(Files.newInputStream(sourcePath), targetFile);
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("Failed to copy file: {}", sourceFilePath, e);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
LocalDateTime currentTime = LocalDateTime.now();
|
LocalDateTime currentTime = LocalDateTime.now();
|
||||||
DatasetFile datasetFile = DatasetFile.builder()
|
DatasetFile datasetFile = DatasetFile.builder()
|
||||||
.id(UUID.randomUUID().toString())
|
.id(UUID.randomUUID().toString())
|
||||||
.datasetId(datasetId)
|
.datasetId(datasetId)
|
||||||
.fileName(fileName)
|
.fileName(fileName)
|
||||||
.fileType(AnalyzerUtils.getExtension(fileName))
|
.fileType(AnalyzerUtils.getExtension(fileName))
|
||||||
.fileSize(targetFile.length())
|
.fileSize(sourceFile.length())
|
||||||
.filePath(targetFile.getPath())
|
.filePath(Paths.get(dataset.getPath(), fileName).toString())
|
||||||
.uploadTime(currentTime)
|
.uploadTime(currentTime)
|
||||||
.lastAccessTime(currentTime)
|
.lastAccessTime(currentTime)
|
||||||
.build();
|
.build();
|
||||||
datasetFileRepository.save(datasetFile);
|
|
||||||
dataset.addFile(datasetFile);
|
dataset.addFile(datasetFile);
|
||||||
copiedFiles.add(datasetFile);
|
copiedFiles.add(datasetFile);
|
||||||
}
|
}
|
||||||
|
datasetFileRepository.saveBatch(copiedFiles, 100);
|
||||||
dataset.active();
|
dataset.active();
|
||||||
datasetRepository.updateById(dataset);
|
datasetRepository.updateById(dataset);
|
||||||
|
CompletableFuture.runAsync(() -> copyFilesToDatasetDir(req.sourcePaths(), dataset));
|
||||||
return copiedFiles;
|
return copiedFiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void copyFilesToDatasetDir(List<String> sourcePaths, Dataset dataset) {
|
||||||
|
for (String sourcePath : sourcePaths) {
|
||||||
|
Path sourceFilePath = Paths.get(sourcePath);
|
||||||
|
Path targetFilePath = Paths.get(dataset.getPath(), sourceFilePath.getFileName().toString());
|
||||||
|
try {
|
||||||
|
Files.createDirectories(Path.of(dataset.getPath()));
|
||||||
|
Files.copy(sourceFilePath, targetFilePath);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Failed to copy file from {} to {}", sourcePath, targetFilePath, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -162,7 +162,7 @@
|
|||||||
<version>${spring-boot.version}</version>
|
<version>${spring-boot.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<finalName>data-mate</finalName>
|
<finalName>data-mate</finalName>
|
||||||
<mainClass>com.datamate.main.DataMatePlatformApplication</mainClass>
|
<mainClass>com.datamate.main.DataMateApplication</mainClass>
|
||||||
</configuration>
|
</configuration>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|||||||
@EnableAsync
|
@EnableAsync
|
||||||
@EnableScheduling
|
@EnableScheduling
|
||||||
@EnableCaching
|
@EnableCaching
|
||||||
public class DataMatePlatformApplication {
|
public class DataMateApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(DataMatePlatformApplication.class, args);
|
SpringApplication.run(DataMateApplication.class, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user