From 77e4741346fb02d5a1f845b89f681a56d06dad0e Mon Sep 17 00:00:00 2001
From: 安瑾然 <107107765@qq.com>
Date: 星期二, 12 七月 2022 14:59:29 +0800
Subject: [PATCH] minio接口修正,宣传、常见问题接口开发

---
 src/main/java/com/example/jz/service/impl/MinIOServiceImpl.java |   68 +++++++++++++++++++++------------
 1 files changed, 43 insertions(+), 25 deletions(-)

diff --git a/src/main/java/com/example/jz/service/impl/MinIOServiceImpl.java b/src/main/java/com/example/jz/service/impl/MinIOServiceImpl.java
index 0d2ea6a..4c3966d 100644
--- a/src/main/java/com/example/jz/service/impl/MinIOServiceImpl.java
+++ b/src/main/java/com/example/jz/service/impl/MinIOServiceImpl.java
@@ -1,17 +1,24 @@
 package com.example.jz.service.impl;
 
+import cn.hutool.core.util.IdUtil;
 import com.example.jz.config.MinIOConfig;
+import com.example.jz.exception.BusinessException;
 import com.example.jz.service.MinIOService;
 import io.minio.GetPresignedObjectUrlArgs;
 import io.minio.MinioClient;
+import io.minio.PutObjectOptions;
+import io.minio.UploadObjectArgs;
 import io.minio.errors.*;
 import io.minio.http.Method;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
 import java.security.InvalidKeyException;
 import java.security.NoSuchAlgorithmException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import java.util.concurrent.TimeUnit;
 
 @Service
@@ -20,34 +27,45 @@
     MinioClient minioClient;
     @Autowired
     MinIOConfig minIOConfig;
+
     @Override
-    public String getPreviewFileUrl(String fileName){
+    public String getPreviewFileUrl(String fileName) {
         String res = null;
         try {
-            res = minioClient.presignedGetObject(minIOConfig.getBucketName(), fileName);
-        } catch (ErrorResponseException e) {
-            throw new RuntimeException(e);
-        } catch (InsufficientDataException e) {
-            throw new RuntimeException(e);
-        } catch (InternalException e) {
-            throw new RuntimeException(e);
-        } catch (InvalidBucketNameException e) {
-            throw new RuntimeException(e);
-        } catch (InvalidExpiresRangeException e) {
-            throw new RuntimeException(e);
-        } catch (InvalidKeyException e) {
-            throw new RuntimeException(e);
-        } catch (InvalidResponseException e) {
-            throw new RuntimeException(e);
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        } catch (NoSuchAlgorithmException e) {
-            throw new RuntimeException(e);
-        } catch (ServerException e) {
-            throw new RuntimeException(e);
-        } catch (XmlParserException e) {
-            throw new RuntimeException(e);
+            res = minioClient.getPresignedObjectUrl(
+                    GetPresignedObjectUrlArgs.builder()
+                            .method(Method.GET)
+                            .bucket(minIOConfig.getBucketName())
+                            .object(fileName)
+                            .build());
+        } catch (Exception e) {
+            throw new BusinessException("鑾峰彇鏂囦欢棰勮鍦板潃澶辫触");
         }
-        return  res;
+        return res;
+    }
+
+    @Override
+    public String upload(MultipartFile file) {
+        String objectName = null;
+        try {
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
+            objectName = sdf.format(new Date()) + "/" + IdUtil.simpleUUID() + "." + file.getContentType().split("/")[1];
+            PutObjectOptions putObjectOptions = new PutObjectOptions(file.getInputStream().available(), -1);
+            putObjectOptions.setContentType(file.getContentType());
+            minioClient.putObject(minIOConfig.getBucketName(), objectName, file.getInputStream(), putObjectOptions);
+        } catch (Exception e) {
+            throw new BusinessException("涓婁紶鏂囦欢澶辫触");
+        }
+        return objectName;
+    }
+
+    @Override
+    public Boolean delete(String fileName) {
+        try {
+            minioClient.removeObject(minIOConfig.getBucketName(), fileName);
+        } catch (Exception e) {
+            throw new BusinessException("鍒犻櫎鏂囦欢澶辫触");
+        }
+        return true;
     }
 }

--
Gitblit v1.8.0