FileManager.java 4.09 KB
package com.bs.reception.manager;


import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.domain.FileEntity;
import com.bs.reception.feign.FileFeignClient;
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 FileFeignClient fileFeignClient;


    // 合并文件,返回本地文件路径
    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 = fileFeignClient.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;
    }
}