按编号导出参赛选手文件
This commit is contained in:
@@ -479,6 +479,109 @@ public class GlobalAwardServiceImpl implements GlobalAwardService {
|
||||
// 这里推送消息是在接受到视频生成结束后发生的,所以UserContext中没有用户信息
|
||||
messageCenterService.pushMessage("system", userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int exportContestantFiles(Integer minContestantNumber, Integer maxContestantNumber) throws Exception {
|
||||
if (minContestantNumber == null || maxContestantNumber == null) {
|
||||
throw new BusinessException("minContestantNumber and maxContestantNumber are required.");
|
||||
}
|
||||
if (minContestantNumber > maxContestantNumber) {
|
||||
throw new BusinessException("minContestantNumber cannot be greater than maxContestantNumber.");
|
||||
}
|
||||
|
||||
// 1. 根据contestantNumber范围查询参赛者
|
||||
QueryWrapper<Contestant> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.lambda()
|
||||
.ge(Contestant::getContestantNumber, minContestantNumber)
|
||||
.le(Contestant::getContestantNumber, maxContestantNumber)
|
||||
.orderByAsc(Contestant::getContestantNumber);
|
||||
List<Contestant> contestants = contestantMapper.selectList(queryWrapper);
|
||||
|
||||
if (contestants.isEmpty()) {
|
||||
log.info("No contestants found in range [{}, {}]", minContestantNumber, maxContestantNumber);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 2. 创建基础目录
|
||||
String baseDir = uploadDir + "/contestants";
|
||||
Path basePath = Paths.get(baseDir).toAbsolutePath();
|
||||
Files.createDirectories(basePath);
|
||||
log.info("Base directory created: {}", basePath);
|
||||
|
||||
int exportedCount = 0;
|
||||
|
||||
// 3. 遍历每个参赛者,下载文件
|
||||
for (Contestant contestant : contestants) {
|
||||
Integer contestantNumber = contestant.getContestantNumber();
|
||||
if (contestantNumber == null) {
|
||||
log.warn("Contestant {} has no contestantNumber, skipping", contestant.getId());
|
||||
continue;
|
||||
}
|
||||
|
||||
// 创建参赛者文件夹
|
||||
String contestantDir = baseDir + "/" + contestantNumber;
|
||||
Path contestantPath = Paths.get(contestantDir);
|
||||
Files.createDirectories(contestantPath);
|
||||
|
||||
// 下载PDF文件
|
||||
String pdfPath = contestant.getPdfPath();
|
||||
if (StringUtils.isNotBlank(pdfPath)) {
|
||||
try {
|
||||
downloadFileFromMinio(pdfPath, contestantPath.toString(), "design.pdf");
|
||||
log.info("Downloaded PDF for contestant {}", contestantNumber);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to download PDF for contestant {}: {}", contestantNumber, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 下载视频文件
|
||||
String videoPath = contestant.getVideoPath();
|
||||
if (StringUtils.isNotBlank(videoPath)) {
|
||||
try {
|
||||
// 根据路径判断视频格式
|
||||
String fileName = videoPath.contains("/") ?
|
||||
videoPath.substring(videoPath.lastIndexOf("/") + 1) : "video.mp4";
|
||||
downloadFileFromMinio(videoPath, contestantPath.toString(), fileName);
|
||||
log.info("Downloaded video for contestant {}", contestantNumber);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to download video for contestant {}: {}", contestantNumber, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
exportedCount++;
|
||||
}
|
||||
|
||||
log.info("Exported {} contestants' files to {}", exportedCount, basePath);
|
||||
return exportedCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从MinIO下载文件到本地
|
||||
* @param minioPath MinIO路径 (格式: bucketName/objectPath)
|
||||
* @param localDir 本地目录
|
||||
* @param fileName 本地文件名
|
||||
*/
|
||||
private void downloadFileFromMinio(String minioPath, String localDir, String fileName) {
|
||||
if (StringUtils.isBlank(minioPath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 从路径中提取bucket名称和对象名称
|
||||
int index = minioPath.indexOf("/");
|
||||
if (index == -1) {
|
||||
log.warn("Invalid MinIO path: {}", minioPath);
|
||||
return;
|
||||
}
|
||||
|
||||
String bucketName = minioPath.substring(0, index);
|
||||
String objectName = minioPath.substring(index + 1);
|
||||
|
||||
// 构建本地文件完整路径
|
||||
Path localFilePath = Paths.get(localDir, fileName);
|
||||
|
||||
// 下载文件
|
||||
minioUtil.downloadMinioObjectToLocal(bucketName, objectName, localFilePath.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user