From c1daa359ac79f4673463b7662b794b8da4c86e11 Mon Sep 17 00:00:00 2001
From: liuhaonan <31457034@qq.com>
Date: 星期二, 10 五月 2022 17:13:58 +0800
Subject: [PATCH] Changes

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/service/IpVolumeFileService.java |   96 ++++++++++++++++++++++++++++++++++++------------
 1 files changed, 72 insertions(+), 24 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..607aa49 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());
@@ -47,40 +53,82 @@
 
     public boolean deleteFile(Long fileId) {
         IpVolumeFile byId = getById(fileId);
+        String bucketName;
+        if (SecurityUtils.getClientId() == null) {
+            bucketName = MinIoConstant.ADMIN_BROADCAST_FILE.getBucketName();
+        } else {
+            bucketName = MinIoConstant.BROADCAST_FILE.getBucketName() + SecurityUtils.getUserId();
+        }
         if (byId == null) {
             throw new BusinessException("鎵句笉鍒板搴旀枃浠�");
         }
+        boolean b = minIoUtil.deleteFile(bucketName, byId.getFileName());
+//        if (!b) {
+//            throw new BusinessException("鏈嶅姟鍣ㄥ垹闄ゅけ璐�");
+//        }
+
+
         return removeById(fileId);
     }
 
     public LambdaQueryWrapper<IpVolumeFile> listFile(BaseConditionVO baseConditionVO, String keyword) {
         PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize());
-        LambdaQueryWrapper<IpVolumeFile> wrapper = Wrappers.lambdaQuery(IpVolumeFile.class).eq(IpVolumeFile::getClientId, clientService.getClientId());
-        if (keyword.isEmpty()) {
-            return wrapper;
-        } else {
-          return   wrapper.like(IpVolumeFile::getFileName,keyword);
+        LambdaQueryWrapper<IpVolumeFile> wrapper = Wrappers.lambdaQuery(IpVolumeFile.class);
+
+        //涓嶆槸瓒呯
+        if (SecurityUtils.getClientId() != null) {
+            wrapper = wrapper.eq(IpVolumeFile::getClientId, SecurityUtils.getUserId())
+                    .or(file -> {
+                        file.eq(IpVolumeFile::getUserId, SecurityUtils.getUserId());
+                    });
         }
+
+        if (keyword != null && !keyword.isEmpty()) {
+            wrapper.like(IpVolumeFile::getFileName, keyword);
+        }
+        return wrapper;
     }
 
-    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
+     * @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