|
@@ -1,6 +1,8 @@
|
|
|
package vip.xiaonuo.disk.util;
|
|
package vip.xiaonuo.disk.util;
|
|
|
|
|
|
|
|
import io.minio.*;
|
|
import io.minio.*;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
@@ -13,28 +15,34 @@ import java.util.concurrent.TimeUnit;
|
|
|
public class OfficeConverter {
|
|
public class OfficeConverter {
|
|
|
|
|
|
|
|
|
|
|
|
|
-// @Value("${ufop.minio.endpoint}")
|
|
|
|
|
-// private String minioEndpoint;
|
|
|
|
|
-//
|
|
|
|
|
-// @Value("${ufop.minio.access-key}")
|
|
|
|
|
-// private String accessKey;
|
|
|
|
|
-//
|
|
|
|
|
-// @Value("${ufop.minio.secret-key}")
|
|
|
|
|
-// private String secretKey;
|
|
|
|
|
-//
|
|
|
|
|
-// @Value("${ufop.minio.bucket-name}")
|
|
|
|
|
-// private String bucketName;
|
|
|
|
|
-
|
|
|
|
|
- private static final MinioClient minioClient = MinioClient.builder()
|
|
|
|
|
- .endpoint("http:47.93.185.192:10005")
|
|
|
|
|
- .credentials("admin", "Aa123456")
|
|
|
|
|
- .build();
|
|
|
|
|
|
|
+ private final String minioEndpoint;
|
|
|
|
|
+ private final String accessKey;
|
|
|
|
|
+ private final String secretKey;
|
|
|
|
|
+ private final String bucketName;
|
|
|
|
|
+ private final MinioClient minioClient;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ public OfficeConverter(
|
|
|
|
|
+ @Value("${ufop.minio.endpoint}") String minioEndpoint,
|
|
|
|
|
+ @Value("${ufop.minio.access-key}") String accessKey,
|
|
|
|
|
+ @Value("${ufop.minio.secret-key}") String secretKey,
|
|
|
|
|
+ @Value("${ufop.minio.bucket-name}") String bucketName
|
|
|
|
|
+ ) {
|
|
|
|
|
+ this.minioEndpoint = minioEndpoint;
|
|
|
|
|
+ this.accessKey = accessKey;
|
|
|
|
|
+ this.secretKey = secretKey;
|
|
|
|
|
+ this.bucketName = bucketName;
|
|
|
|
|
+ this.minioClient = MinioClient.builder()
|
|
|
|
|
+ .endpoint(minioEndpoint)
|
|
|
|
|
+ .credentials(accessKey, secretKey)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
private static final String LIBRE_OFFICE_PATH = "soffice"; // Linux路径
|
|
private static final String LIBRE_OFFICE_PATH = "soffice"; // Linux路径
|
|
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
|
|
|
|
+ public void main(String[] args) {
|
|
|
try {
|
|
try {
|
|
|
- convertAndUpload("netdisk", "upload/20241010/fba210e119de04088439e64b062665da.doc", "netdisk", "665da.docx");
|
|
|
|
|
|
|
+ convertAndUpload("upload/20241010/fba210e119de04088439e64b062665da.doc", "665da.docx");
|
|
|
// convertAndUpload("netdisk", "upload/20240705/7faa77badef7a854e3bf664a2ddb1a4f.ppt", "netdisk", "b1a4f.pptx");
|
|
// convertAndUpload("netdisk", "upload/20240705/7faa77badef7a854e3bf664a2ddb1a4f.ppt", "netdisk", "b1a4f.pptx");
|
|
|
// convertAndUpload("netdisk", "upload/20250611/答辩会议室号.xls", "netdisk", "室号.xlsx");
|
|
// convertAndUpload("netdisk", "upload/20250611/答辩会议室号.xls", "netdisk", "室号.xlsx");
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -42,22 +50,21 @@ public class OfficeConverter {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static void convertAndUpload(String inputBucket, String inputFile,
|
|
|
|
|
- String outputBucket, String outputFile) throws Exception {
|
|
|
|
|
|
|
+ public void convertAndUpload(String inputFile, String outputFile) throws Exception {
|
|
|
// 1. 从MinIO下载文件
|
|
// 1. 从MinIO下载文件
|
|
|
- File tempInput = downloadFromMinio(inputBucket, inputFile);
|
|
|
|
|
|
|
+ File tempInput = downloadFromMinio(bucketName, inputFile);
|
|
|
|
|
|
|
|
// 2. 执行LibreOffice转换
|
|
// 2. 执行LibreOffice转换
|
|
|
File tempOutput = convertWithLibreOffice(tempInput, getOutputFormat(outputFile));
|
|
File tempOutput = convertWithLibreOffice(tempInput, getOutputFormat(outputFile));
|
|
|
|
|
|
|
|
// 3. 上传到MinIO
|
|
// 3. 上传到MinIO
|
|
|
- uploadToMinio(outputBucket, outputFile, tempOutput);
|
|
|
|
|
|
|
+ uploadToMinio(bucketName, outputFile, tempOutput);
|
|
|
|
|
|
|
|
// 4. 清理临时文件
|
|
// 4. 清理临时文件
|
|
|
deleteFiles(tempInput, tempOutput);
|
|
deleteFiles(tempInput, tempOutput);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static File downloadFromMinio(String bucket, String objectName) throws Exception {
|
|
|
|
|
|
|
+ private File downloadFromMinio(String bucket, String objectName) throws Exception {
|
|
|
// 使用UUID确保唯一性
|
|
// 使用UUID确保唯一性
|
|
|
String uniqueId = UUID.randomUUID().toString();
|
|
String uniqueId = UUID.randomUUID().toString();
|
|
|
Path tempDir = Files.createTempDirectory("minio-download-");
|
|
Path tempDir = Files.createTempDirectory("minio-download-");
|
|
@@ -66,7 +73,7 @@ public class OfficeConverter {
|
|
|
try {
|
|
try {
|
|
|
minioClient.downloadObject(
|
|
minioClient.downloadObject(
|
|
|
DownloadObjectArgs.builder()
|
|
DownloadObjectArgs.builder()
|
|
|
- .bucket(bucket)
|
|
|
|
|
|
|
+ .bucket(bucketName)
|
|
|
.object(objectName)
|
|
.object(objectName)
|
|
|
.filename(tempPath.toString())
|
|
.filename(tempPath.toString())
|
|
|
.build());
|
|
.build());
|
|
@@ -79,7 +86,7 @@ public class OfficeConverter {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static File convertWithLibreOffice(File inputFile, String outputFormat) throws Exception {
|
|
|
|
|
|
|
+ private File convertWithLibreOffice(File inputFile, String outputFormat) throws Exception {
|
|
|
File outputFile = File.createTempFile("converted-", "." + outputFormat);
|
|
File outputFile = File.createTempFile("converted-", "." + outputFormat);
|
|
|
|
|
|
|
|
ProcessBuilder pb = new ProcessBuilder(
|
|
ProcessBuilder pb = new ProcessBuilder(
|
|
@@ -112,17 +119,27 @@ public class OfficeConverter {
|
|
|
return convertedFile;
|
|
return convertedFile;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static void uploadToMinio(String bucket, String objectName, File file) throws Exception {
|
|
|
|
|
|
|
+// private void uploadToMinio(String bucket, String objectName, File file) throws Exception {
|
|
|
|
|
+// minioClient.uploadObject(
|
|
|
|
|
+// UploadObjectArgs.builder()
|
|
|
|
|
+// .bucket(bucketName)
|
|
|
|
|
+// .object(objectName)
|
|
|
|
|
+// .filename("/convert")
|
|
|
|
|
+// .contentType(getMimeType(objectName))
|
|
|
|
|
+// .build());
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+ private void uploadToMinio(String bucket, String objectName, File file) throws Exception {
|
|
|
minioClient.uploadObject(
|
|
minioClient.uploadObject(
|
|
|
UploadObjectArgs.builder()
|
|
UploadObjectArgs.builder()
|
|
|
- .bucket(bucket)
|
|
|
|
|
- .object(objectName)
|
|
|
|
|
|
|
+ .bucket(bucketName)
|
|
|
|
|
+ .object("converted/"+objectName)
|
|
|
.filename(file.getAbsolutePath())
|
|
.filename(file.getAbsolutePath())
|
|
|
.contentType(getMimeType(objectName))
|
|
.contentType(getMimeType(objectName))
|
|
|
.build());
|
|
.build());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static void deleteFiles(File... files) {
|
|
|
|
|
|
|
+ private void deleteFiles(File... files) {
|
|
|
for (File f : files) {
|
|
for (File f : files) {
|
|
|
if (f != null && f.exists()) {
|
|
if (f != null && f.exists()) {
|
|
|
f.delete();
|
|
f.delete();
|
|
@@ -130,7 +147,7 @@ public class OfficeConverter {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static String getOutputFormat(String outputFile) {
|
|
|
|
|
|
|
+ private String getOutputFormat(String outputFile) {
|
|
|
if (outputFile.endsWith(".docx")) {
|
|
if (outputFile.endsWith(".docx")) {
|
|
|
return "docx";
|
|
return "docx";
|
|
|
}
|
|
}
|
|
@@ -143,7 +160,7 @@ public class OfficeConverter {
|
|
|
throw new IllegalArgumentException("不支持的输出格式: " + outputFile);
|
|
throw new IllegalArgumentException("不支持的输出格式: " + outputFile);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static String getMimeType(String fileName) {
|
|
|
|
|
|
|
+ private String getMimeType(String fileName) {
|
|
|
return switch (fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase()) {
|
|
return switch (fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase()) {
|
|
|
case "docx" -> "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
case "docx" -> "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
|
case "pptx" -> "application/vnd.openxmlformats-officedocument.presentationml.presentation";
|
|
case "pptx" -> "application/vnd.openxmlformats-officedocument.presentationml.presentation";
|