Skip to content

Commit

Permalink
Merge pull request #146 from jamebal/develop
Browse files Browse the repository at this point in the history
perf: 优化heic文件处理
  • Loading branch information
jamebal authored Jul 31, 2024
2 parents 9433041 + c131923 commit 5502a6a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
55 changes: 51 additions & 4 deletions src/main/java/com/jmal/clouddisk/media/HeifUtils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.jmal.clouddisk.media;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.PathUtil;
import com.jmal.clouddisk.service.Constants;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.jmal.clouddisk.util.FFMPEGUtils.getWaitingForResults;

Expand All @@ -16,18 +22,31 @@ public class HeifUtils {
/**
* 将heic转换为jpg
*/
public static String heifConvert(String inputPath) {
public static String heifConvert(String inputPath, Path outputParentPath) {
if (!checkHeif()) {
return null;
}
String outputPath = inputPath + ".jpg";
Path filepath = Paths.get(inputPath + ".jpg");
String outputPath = Paths.get(outputParentPath.toString(), filepath.getFileName().toString()).toString();
if (FileUtil.exist(outputPath)) {
return outputPath;
}
try {
ProcessBuilder processBuilder = heifConvert(inputPath, outputPath);
Process process = processBuilder.start();
return getWaitingForResults(outputPath, processBuilder, process);
StringBuilder output = printProcessInfo(process);
outputPath = getWaitingForResults(outputPath, processBuilder, process);

delAuxiliaryImagePath(output);

// 打印命令 用空格连接
String command = String.join(" ", processBuilder.command());
log.info("heif-convert 执行成功, command: \r\n{}", command);

if (outputPath != null) {
PathUtil.move(Path.of(outputPath), filepath, true);
return filepath.toString();
}
} catch (InterruptedException e) {
log.error(e.getMessage(), e);
return null;
Expand All @@ -37,6 +56,23 @@ public static String heifConvert(String inputPath) {
return null;
}

/**
* 删除辅助图像文件
* @param output process输出
*/
private static void delAuxiliaryImagePath(StringBuilder output) {
// 解析输出,查找辅助图像文件名, 并删除
String auxiliaryImagePath = null;
Pattern pattern = Pattern.compile("Auxiliary image written to (.+)");
Matcher matcher = pattern.matcher(output.toString());
if (matcher.find()) {
auxiliaryImagePath = matcher.group(1);
}
if (auxiliaryImagePath != null) {
FileUtil.del(auxiliaryImagePath);
}
}

/**
* 将heic转换为jpg
* @param filepath heic文件绝对路径
Expand All @@ -52,11 +88,22 @@ private static ProcessBuilder heifConvert(String filepath, String outputPath) {
return processBuilder;
}

private static StringBuilder printProcessInfo(Process process) throws IOException {
StringBuilder output = new StringBuilder();
try (InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
}
return output;
}

/**
* 检查是否安装了heif-convert
*/
public static boolean checkHeif() {
private static boolean checkHeif() {
try {
Process process = Runtime.getRuntime().exec(Constants.HEIF_CONVERT);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public String createFile(String username, File file, String userId, Boolean isPu
String contentType = getContentType(file, FileContentTypeUtils.getContentType(suffix));
if (contentType.startsWith(Constants.CONTENT_TYPE_IMAGE)) {
// 换成webp格式的图片
file = replaceWebp(userId, file);
file = replaceWebp(userId, file, username);
if (file == null) {
return null;
}
Expand Down Expand Up @@ -478,11 +478,11 @@ public FileDocument getFileDocument(String userId, String fileName, String relat
return getFileDocument(userId, fileName, relativePath, query);
}

private File replaceWebp(String userId, File file) {
private File replaceWebp(String userId, File file, String username) {
String suffix = FileUtil.getSuffix(file).toLowerCase();
// 判断是否为heic格式
if ("heic".equals(suffix)) {
String output = HeifUtils.heifConvert(file.getAbsolutePath());
String output = HeifUtils.heifConvert(file.getAbsolutePath(), Paths.get(fileProperties.getRootDir(), fileProperties.getChunkFileDir(), username));
if (output != null) {
FileUtil.del(file);
return null;
Expand Down

0 comments on commit 5502a6a

Please sign in to comment.