From aa1c7114578981463e226d4b2d7773bc1b7994da Mon Sep 17 00:00:00 2001
From: liuhaonan <konodioda2333@vip.qq.com>
Date: 星期五, 08 四月 2022 16:57:14 +0800
Subject: [PATCH] ip音柱文件上传

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/service/IpVolumeFileService.java |   68 ++++++++++++++++++++++++----------
 1 files changed, 48 insertions(+), 20 deletions(-)

diff --git a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/IpVolumeFileService.java b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/IpVolumeFileService.java
index e2a44a3..1e87569 100644
--- a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/IpVolumeFileService.java
+++ b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/IpVolumeFileService.java
@@ -4,25 +4,33 @@
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.github.pagehelper.PageHelper;
 import com.sandu.common.execption.BusinessException;
+import com.sandu.common.file.FileUploadDto;
 import com.sandu.common.object.BaseConditionVO;
 import com.sandu.common.service.impl.BaseServiceImpl;
+import com.sandu.ximon.admin.minio.BroadcastFileUtils;
+import com.sandu.ximon.admin.minio.entity.FileSuffix;
+import com.sandu.ximon.admin.minio.entity.MinIoConstant;
+import com.sandu.ximon.admin.minio.utils.MinIoUtil;
 import com.sandu.ximon.admin.param.IpVolumeFileParam;
 import com.sandu.ximon.admin.security.SecurityUtils;
 import com.sandu.ximon.dao.domain.IpVolumeFile;
 import com.sandu.ximon.dao.mapper.IpVolumeFileMapper;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
 
 /**
  * @Author liuhaonan
  * @Date 2021/12/16 10:48
  * @Version 1.0
  */
+
 @Service
 @AllArgsConstructor
 public class IpVolumeFileService extends BaseServiceImpl<IpVolumeFileMapper, IpVolumeFile> {
     private final IpVolumeFileMapper ipVolumeFileMapper;
     private final ClientService clientService;
+    private MinIoUtil minIoUtil;
 
 
     public boolean addFile(IpVolumeFileParam fileParam) {
@@ -31,14 +39,12 @@
 //            file.setUserName(SecurityUtils.getUsername());
 //        }
 //
-        if(SecurityUtils.getClientId()!=null){
+        if (SecurityUtils.getClientId() != null) {
             file.setUserName(SecurityUtils.getUsername());
             file.setUserId(SecurityUtils.getClientId());
-        }
-
-        if(clientService.getClientId()!=null){
             file.setClientId(clientService.getClientId());
         }
+
         file.setFileName(fileParam.getFileName());
         file.setFileUrl(fileParam.getFileUrl());
         file.setOriginSize(fileParam.getOriginSize());
@@ -59,28 +65,50 @@
         if (keyword.isEmpty()) {
             return wrapper;
         } else {
-          return   wrapper.like(IpVolumeFile::getFileName,keyword);
+            return wrapper.like(IpVolumeFile::getFileName, keyword);
         }
     }
 
-    public boolean upload(IpVolumeFileParam fileParam) {
-        IpVolumeFile file = new IpVolumeFile();
-//        if(SecurityUtils.getClientId()!=null){
-//            file.setUserName(SecurityUtils.getUsername());
-//        }
-//
-        if(SecurityUtils.getClientId()!=null){
-            file.setUserName(SecurityUtils.getUsername());
-            file.setUserId(SecurityUtils.getClientId());
+
+    /**
+     * 鏂囦欢涓婁紶
+     *
+     * @param file
+     * @param userId
+     * @return
+     */
+    public FileUploadDto upload(MultipartFile file) {
+        log.error("闊虫煴鏂囦欢涓婁紶锛�(filename:" + file.getOriginalFilename() + "),(size: " + file.getSize() + "),鐢ㄦ埛id:" + SecurityUtils.getUserId());
+        //  鏍¢獙鏄惁涓簃p3鏂囦欢
+        if (!BroadcastFileUtils.isMp3File(file.getOriginalFilename(), FileSuffix.MP3)) {
+            log.error("闊虫煴鏂囦欢涓婁紶锛�(涓婁紶澶辫触, 闈濵P3鏂囦欢) (filename:" + file.getOriginalFilename() + "),(size: " + file.getSize() + "),鐢ㄦ埛id:" + SecurityUtils.getUserId());
+            throw new BusinessException("涓婁紶澶辫触, 闈濵P3鏂囦欢");
+        }
+        //  鍒ゆ柇璇ユ枃浠舵槸鍚﹀瓨鍦�
+        String bucketName;
+        if (SecurityUtils.getClientId() == null) {
+            bucketName = MinIoConstant.ADMIN_BROADCAST_FILE.getBucketName();
+        } else {
+            bucketName = MinIoConstant.BROADCAST_FILE.getBucketName() + SecurityUtils.getUserId();
+        }
+        if (minIoUtil.objectExists(bucketName, file.getOriginalFilename())) {
+            log.error("闊虫煴鏂囦欢涓婁紶锛�(涓婁紶澶辫触, 鍚屽悕鏂囦欢宸插瓨鍦�) (filename:" + file.getOriginalFilename() + "),(size: " + file.getSize() + "),鐢ㄦ埛id:" + SecurityUtils.getUserId());
+            throw new BusinessException("涓婁紶澶辫触, 鍚屽悕鏂囦欢宸插瓨鍦�");
         }
 
-        if(clientService.getClientId()!=null){
-            file.setClientId(clientService.getClientId());
+
+        String upload = minIoUtil.upload(bucketName, file);
+
+        if (null == upload) {
+            log.error("闊虫煴鏂囦欢涓婁紶锛�(涓婁紶澶辫触, 鏂囦欢鏈嶅姟鍣ㄥ嚭閿�) (filename:" + file.getOriginalFilename() + "),(size: " + file.getSize() + "),鐢ㄦ埛id:" + SecurityUtils.getUserId());
+            throw new BusinessException("涓婁紶澶辫触, 鏂囦欢鏈嶅姟鍣ㄥ嚭閿�");
         }
-        file.setFileName(fileParam.getFileName());
-        file.setFileUrl(fileParam.getFileUrl());
-        file.setOriginSize(fileParam.getOriginSize());
-        return save(file);
+        FileUploadDto fileUploadDto = new FileUploadDto();
+        fileUploadDto.setFileUrl(upload);
+        fileUploadDto.setFileSize(file.getSize());
+        fileUploadDto.setFileName(file.getOriginalFilename());
+        fileUploadDto.setFileType(file.getContentType());
+        return fileUploadDto;
     }
 
 }

--
Gitblit v1.9.3