From 5d2e6ca50091f405dfda9e287b367dd295dc6cf8 Mon Sep 17 00:00:00 2001
From: Xiajijian <1176061145@qq.com>
Date: Mon, 09 Feb 2026 17:39:17 +0800
Subject: [PATCH] 1. 航线任务增加校验,只有下发人和登陆人是一个人才可以删除修改操作。 2. 增加校验航线任务是否可以创建航线接口。
---
ard-oss/src/main/java/com/ard/oss/service/impl/OssServiceContext.java | 78 ++++++++++++++++++++++++++++++--------
1 files changed, 61 insertions(+), 17 deletions(-)
diff --git a/ard-oss/src/main/java/com/ard/oss/service/impl/OssServiceContext.java b/ard-oss/src/main/java/com/ard/oss/service/impl/OssServiceContext.java
index c3f7a89..2ee3fdc 100644
--- a/ard-oss/src/main/java/com/ard/oss/service/impl/OssServiceContext.java
+++ b/ard-oss/src/main/java/com/ard/oss/service/impl/OssServiceContext.java
@@ -10,7 +10,9 @@
import com.ard.oss.model.enums.OssTypeEnum;
import com.ard.oss.service.IOssService;
import io.minio.PutObjectArgs;
+import lombok.Cleanup;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
@@ -87,7 +89,7 @@
*/
public String upload(MultipartFile file) throws IOException {
try {
- return upload(OssConfiguration.bucket, null, file);
+ return upload(OssConfiguration.bucket, file);
} catch (Exception e) {
throw new IOException(e.getMessage(), e);
}
@@ -130,16 +132,15 @@
? extractFilename(file)
: object;
+ // 通过文件扩展名获取 MIME 类型
+ String contentType = getMimeTypeByExtension(file.getOriginalFilename());
+
+ @Cleanup
+ InputStream inputStream = file.getInputStream();
// 上传文件到 Minio
- this.ossService.putObject(bucket, objectName, file.getInputStream());
+ this.ossService.putObject(bucket, objectName, inputStream, contentType);
- // ✅ 拼接永久 URL(不带 presigned 过期参数)
- String baseUrl = OssConfiguration.endpoint;
- if (!baseUrl.endsWith("/")) {
- baseUrl += "/";
- }
-
- return baseUrl + bucket + "/" + objectName;
+ return buildUrl(bucket, objectName);
} catch (Exception e) {
throw new IOException("Upload failed: " + e.getMessage(), e);
}
@@ -147,19 +148,62 @@
public String upload(String bucket, String objectKey, InputStream input) {
try {
-
// 上传文件
this.ossService.putObject(bucket, objectKey, input);
-
- // ✅ 构建永久访问 URL
- String baseUrl = OssConfiguration.endpoint;
- if (!baseUrl.endsWith("/")) {
- baseUrl += "/";
- }
- return baseUrl + bucket + "/" + objectKey;
+ return buildUrl(bucket, objectKey);
} catch (Exception e) {
throw new RuntimeException("Upload failed: " + e.getMessage(), e);
}
}
+ public String upload(String bucket, String objectKey, InputStream input, String contentType) {
+ try {
+ // 上传文件时指定 Content-Type
+ this.ossService.putObject(bucket, objectKey, input, contentType);
+ return buildUrl(bucket, objectKey);
+ } catch (Exception e) {
+ throw new RuntimeException("Upload failed: " + e.getMessage(), e);
+ }
+ }
+ // 获取文件 MIME 类型
+ public String getMimeTypeByExtension(String fileName) {
+ // 获取文件扩展名
+ String extension = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
+
+ // 根据文件扩展名返回 MIME 类型
+ switch (extension) {
+ case "jpg":
+ case "jpeg":
+ return "image/jpeg";
+ case "png":
+ return "image/png";
+ case "gif":
+ return "image/gif";
+ case "pdf":
+ return "application/pdf";
+ case "txt":
+ return "text/plain";
+ case "html":
+ return "text/html";
+ case "json":
+ return "application/json";
+ case "csv":
+ return "text/csv";
+ case "zip":
+ return "application/zip";
+ // 添加更多的扩展名映射
+ default:
+ return "application/octet-stream"; // 默认 MIME 类型
+ }
+ }
+
+ // 构建url
+ private String buildUrl(String bucket, String key) {
+ String base = OssConfiguration.endpoint;
+ if (!base.endsWith("/")) {
+ base += "/";
+ }
+ return base + bucket + "/" + key;
+ }
+
}
--
Gitblit v1.9.3