FileManager.java
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.bs.reception.manager;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.domain.FileEntity;
import com.bs.fileservice.feign.RemoteFileService;
import com.bs.reception.utils.FFmpegAudioCutterUtils;
import com.bs.reception.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class FileManager {
@Autowired
private Environment environment;
@Autowired
private RemoteFileService remoteFileService;
// 合并文件,返回本地文件路径
public String mergeFile(List<String> recordUrlList) {
List<String> localFiles = new ArrayList<>();
try {
String tempDir = environment.getProperty("reception.file.tmp", "/tmp");
tempDir = tempDir.endsWith("/") ? tempDir + "recordingTemp/" : tempDir + "/recordingTemp/";
for (String recordUrl : recordUrlList) {
recordUrl = FileUtils.replaceAliOssDomain(recordUrl, environment.getProperty("glsk.oss.aliyun.use.internal", "1"));
String filePath = FileUtils.getFileToLocal(recordUrl, tempDir);
log.info("recordUrl: {} filePath:{} 本地文件路径", recordUrl, filePath);
if (StringUtils.isEmpty(filePath)) {
throw new RuntimeException("拉取文件到本地出现异常 recordingUrl:" + recordUrl);
}
localFiles.add(filePath);
}
int size = localFiles.size();
if (size==0){
log.error("合并文件异常");
return null;
}
String finalFilePath = null;
if (size == 1) {
finalFilePath = localFiles.get(0);
localFiles.clear();
} else {
String s = localFiles.get(0);
String fileName = s.substring(s.lastIndexOf("/") + 1);
finalFilePath = tempDir + "merged_" + fileName;
FFmpegAudioCutterUtils.mergeAudioList(localFiles, finalFilePath);
}
return finalFilePath;
}catch (Exception e){
log.error("合并文件异常",e);
return null;
}finally {
if (!CollectionUtils.isEmpty(localFiles)) {
for (String localFile : localFiles) {
boolean removed = FileUtils.removeLocalFile(localFile);
if (!removed) {
log.error("删除本地临时文件失败 filePath:{}", localFile);
}
}
}
}
}
public String uploadFile(String localFilePath) {
try (InputStream inputStream = new FileInputStream(localFilePath)) {
String originalFilename = new File(localFilePath).getName();
String contentType = null;
try {
contentType = Files.probeContentType(Paths.get(localFilePath));
} catch (Exception ignore) {
}
if (contentType == null) {
contentType = "application/octet-stream";
}
MockMultipartFile file = new MockMultipartFile("file", originalFilename, contentType, inputStream);
// 调用文件上传的Feign客户端接口进行文件上传操作
AjaxResult<FileEntity> upload = remoteFileService.upload(file);
log.info("upload: {}", upload);
if (upload != null && upload.getData() != null) {
return upload.getData().getUrl();
}
log.error("文件上传失败");
} catch (Exception e) {
log.error("文件上传失败", e);
}
return null;
}
}