From cf396d3076a064998fbac84f76861f5ed918c5e5 Mon Sep 17 00:00:00 2001
From: chenrui <1187576398@qq.com>
Date: 星期四, 03 四月 2025 15:18:34 +0800
Subject: [PATCH] minio修改

---
 basic-server/src/main/java/com/ruoyi/basic/service/impl/WorkShopServiceImpl.java |   84 ++++++++++++++++--------------------------
 1 files changed, 32 insertions(+), 52 deletions(-)

diff --git a/basic-server/src/main/java/com/ruoyi/basic/service/impl/WorkShopServiceImpl.java b/basic-server/src/main/java/com/ruoyi/basic/service/impl/WorkShopServiceImpl.java
index 7d006b6..8b99c7b 100644
--- a/basic-server/src/main/java/com/ruoyi/basic/service/impl/WorkShopServiceImpl.java
+++ b/basic-server/src/main/java/com/ruoyi/basic/service/impl/WorkShopServiceImpl.java
@@ -1,6 +1,5 @@
 package com.ruoyi.basic.service.impl;
 
-import cn.hutool.core.lang.UUID;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -10,16 +9,18 @@
 import com.ruoyi.basic.pojo.WorkShop;
 import com.ruoyi.basic.pojo.WorkShopFile;
 import com.ruoyi.basic.service.WorkShopService;
+import com.ruoyi.common.config.MinioConfig;
+import com.ruoyi.common.core.domain.MinioResult;
 import com.ruoyi.common.utils.QueryWrappers;
 import com.ruoyi.common.utils.SecurityUtils;
+import com.ruoyi.common.utils.file.MinioUtils;
+import com.ruoyi.framework.exception.ErrorException;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
-import java.io.File;
-import java.time.LocalDateTime;
-import java.time.format.DateTimeFormatter;
 
 /**
  * @description 閽堝琛ㄣ�恮ork_shop(杞﹂棿鍚嶇О)銆戠殑鏁版嵁搴撴搷浣淪ervice瀹炵幇
@@ -33,11 +34,11 @@
     @Resource
     private WorkShopFileMapper workShopFileMapper;
 
-    @Value("${wordUrl}")
-    private String wordUrl;
+    @Resource
+    private MinioUtils minioUtils;
 
-    @Value("${file.path}")
-    private String imgUrl;
+    @Autowired
+    private MinioConfig minioConfig;
 
 
     @Override
@@ -58,58 +59,37 @@
     public int delWorkShop(Integer id) {
         return workShopMapper.deleteById(id);
     }
-    
+
     @Override
-    public int uploadFile(Integer id, MultipartFile file) {
-        String username = SecurityUtils.getUsername();
-        String urlString;
-        String pathName;
-        String path;
-        String filename = file.getOriginalFilename();
-        String contentType = file.getContentType();
+    public int uploadFile(Integer id, MultipartFile file) throws Exception {
+        // 鍩虹鏍¢獙
+        if (file == null || file.isEmpty()) {
+            throw new ErrorException("涓婁紶鏂囦欢涓虹┖");
+        }
+
         WorkShopFile workShopFile = new WorkShopFile();
         workShopFile.setWorkShopId(id);
-        workShopFile.setFileName(filename);
-        if (contentType != null && contentType.startsWith("image/")) {
-            // 鏄浘鐗�
-            path = imgUrl;
-            workShopFile.setType(1);
-        } else {
-            // 鏄枃浠�
-            path = wordUrl;
-            workShopFile.setType(2);
-        }
+        workShopFile.setFileName(file.getOriginalFilename());
+        workShopFile.setName(SecurityUtils.getUsername());
+
         try {
-            // 1. 瑙f瀽缁濆璺緞锛堜紭鍏堜娇鐢ㄩ厤缃殑缁濆璺緞锛屽惁鍒欏熀浜庨」鐩牴鐩綍锛�
-            String basePath = imgUrl.startsWith(File.separator) ? imgUrl : System.getProperty("user.dir") + File.separator + imgUrl;
-            File realpath = new File(basePath);
-
-            // 2. 鍒涘缓鐩綍锛堣褰曟棩蹇楋紝妫�鏌ユ槸鍚︽垚鍔燂級
-            if (!realpath.exists()) {
-                if (!realpath.mkdirs()) {
-                    System.err.println("鐩綍鍒涘缓澶辫触锛歿}");
-                    return 0;
-                }
+            String contentType = file.getContentType();
+            String category = contentType != null && contentType.startsWith("image/") ? "images" : "docs";
+            MinioResult upload = minioUtils.upload(minioConfig.getBucketName(), file, true);
+            // 璁板綍瀛樺偍璺緞
+            workShopFile.setFileUrl(upload.getBucketFileName());
+            workShopFile.setFileMinioUrl(upload.getPreviewExpiry());
+            workShopFile.setType(category.equals("images") ? 1 : 2);
+            // 鏁版嵁搴撴搷浣�
+            int insertResult = workShopFileMapper.insert(workShopFile);
+            if (insertResult <= 0) {
+                throw new ErrorException("鏁版嵁搴撴彃鍏ュけ璐�");
             }
-
-            // 3. 鐢熸垚鍞竴鏂囦欢鍚�
-            String uuid = UUID.randomUUID().toString().replace("-", "");
-            String suffix = filename.substring(filename.lastIndexOf("."));
-            pathName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + "_" + uuid + suffix;
-            File targetFile = new File(realpath, pathName);
-
-            // 4. 淇濆瓨鏂囦欢
-            file.transferTo(targetFile);
-            workShopFile.setFileUrl(pathName);
-            workShopFile.setName(username);
-            return workShopFileMapper.insert(workShopFile);
+            return insertResult;
         } catch (Exception e) {
-            e.printStackTrace();
-            System.err.println("闄勪欢涓婁紶閿欒");
-            return 0;
+            throw new Exception("绯荤粺寮傚父: ", e);
         }
     }
-
 }
 
 

--
Gitblit v1.9.3