修复 视频格式 转换模式和预览模式 特征码通用的问题 修复压缩包创建带文件名的文件夹

This commit is contained in:
gaoxiongzaq 2023-10-25 10:05:51 +08:00
parent a057808624
commit be4080c80d
3 changed files with 18 additions and 13 deletions

View File

@ -19,6 +19,7 @@ public enum FileType {
CODE("codeFilePreviewImpl"),
OTHER("otherFilePreviewImpl"),
MEDIA("mediaFilePreviewImpl"),
MEDIACONVERT("mediaFilePreviewImpl"),
MARKDOWN("markdownFilePreviewImpl"),
XML("xmlFilePreviewImpl"),
CAD("cadFilePreviewImpl"),
@ -50,7 +51,7 @@ public enum FileType {
private static final String[] SSIM_TEXT_TYPES = ConfigConstants.getSimText();
private static final String[] CODES = {"java", "c", "php", "go", "python", "py", "js", "html", "ftl", "css", "lua", "sh", "rb", "yaml", "yml", "json", "h", "cpp", "cs", "aspx", "jsp", "sql"};
private static final String[] MEDIA_TYPES = ConfigConstants.getMedia();
public static final String[] MEDIA_TYPES_CONVERT = ConfigConstants.getConvertMedias();
public static final String[] MEDIACONVERT_TYPES_CONVERT = ConfigConstants.getConvertMedias();
private static final Map<String, FileType> FILE_TYPE_MAPPER = new HashMap<>();
static {
@ -69,8 +70,8 @@ public enum FileType {
for (String media : MEDIA_TYPES) {
FILE_TYPE_MAPPER.put(media, FileType.MEDIA);
}
for (String media : MEDIA_TYPES_CONVERT) {
FILE_TYPE_MAPPER.put(media, FileType.MEDIA);
for (String MEDIACONVERT : MEDIACONVERT_TYPES_CONVERT) {
FILE_TYPE_MAPPER.put( MEDIACONVERT, FileType. MEDIACONVERT);
}
for (String tif : TIFF_TYPES) {
FILE_TYPE_MAPPER.put(tif, FileType.TIFF);

View File

@ -475,7 +475,7 @@ public class FileHandlerService implements InitializingBean {
cacheName = cacheUnifyName +(isHtml ? "html" : "pdf"); //生成文件添加类型后缀 防止同名文件
}else if(type.equals(FileType.PDF)){
cacheName = fileName;
}else if(type.equals(FileType.MEDIA)){
}else if(type.equals(FileType.MEDIACONVERT)){
cacheName = cacheUnifyName +"mp4" ;
}else if(type.equals(FileType.CAD)){
String cadPreviewType = ConfigConstants.getCadPreviewType();

View File

@ -12,6 +12,7 @@ import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import java.io.File;
@ -38,8 +39,9 @@ public class MediaFilePreviewImpl implements FilePreview {
String cacheName = fileAttribute.getcacheName();
String outFilePath = fileAttribute.getoutFilePath();
boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
String fileKey = fileAttribute.getFileKey();
FileType type = fileAttribute.getType();
String[] mediaTypesConvert = FileType.MEDIA_TYPES_CONVERT; //获取支持的转换格式
String[] mediaTypesConvert = FileType.MEDIACONVERT_TYPES_CONVERT; //获取支持的转换格式
boolean mediaTypes = false;
for(String temp : mediaTypesConvert){
if (suffix.equals(temp)) {
@ -57,7 +59,7 @@ public class MediaFilePreviewImpl implements FilePreview {
String convertedUrl = null;
try {
if(mediaTypes){
convertedUrl=convertToMp4(filePath,outFilePath);
convertedUrl=convertToMp4(filePath,outFilePath,fileKey);
}else {
convertedUrl =outFilePath; //其他协议的 不需要转换方式的文件 直接输出
}
@ -94,7 +96,7 @@ public class MediaFilePreviewImpl implements FilePreview {
}
return false;
}
private static String convertToMp4(String filePath,String outFilePath)throws Exception {
private static String convertToMp4(String filePath,String outFilePath,String fileKey)throws Exception {
FFmpegFrameGrabber frameGrabber = FFmpegFrameGrabber.createDefault(filePath);
Frame captured_frame;
FFmpegFrameRecorder recorder = null;
@ -104,12 +106,14 @@ public class MediaFilePreviewImpl implements FilePreview {
if(desFile.exists()){
return outFilePath;
}
int index = outFilePath.lastIndexOf(".");
String folder = outFilePath.substring(0, index);
File path = new File(folder);
//目录不存在 创建新的目录
if (!path.exists()) {
path.mkdirs();
if (!ObjectUtils.isEmpty(fileKey)) { //判断 是压缩包的创建新的目录
int index = outFilePath.lastIndexOf("/"); //截取最后一个斜杠的前面的内容
String folder = outFilePath.substring(0, index);
File path = new File(folder);
//目录不存在 创建新的目录
if (!path.exists()) {
path.mkdirs();
}
}
frameGrabber.start();
recorder = new FFmpegFrameRecorder(outFilePath, frameGrabber.getImageWidth(), frameGrabber.getImageHeight(), frameGrabber.getAudioChannels());