From c86bfa92432bde3c1e3ea6891cf5c43c2e99cd3d Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: Fri, 06 Jun 2025 19:36:36 +0800
Subject: [PATCH] 用户头像更换后移除旧头像文件

---
 ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/FastDfsSysFileServiceImpl.java |   46 ++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/FastDfsSysFileServiceImpl.java b/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/FastDfsSysFileServiceImpl.java
index deee22e..456c697 100644
--- a/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/FastDfsSysFileServiceImpl.java
+++ b/ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/service/FastDfsSysFileServiceImpl.java
@@ -1,16 +1,18 @@
 package com.ruoyi.file.service;
 
-import org.apache.commons.io.FilenameUtils;
+import java.io.InputStream;
 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 com.alibaba.nacos.common.utils.IoUtils;
 import com.github.tobato.fastdfs.domain.fdfs.StorePath;
 import com.github.tobato.fastdfs.service.FastFileStorageClient;
+import com.ruoyi.common.core.utils.file.FileTypeUtils;
 
 /**
  * FastDFS 文件存储
- * 
+ *
  * @author ruoyi
  */
 @Service
@@ -27,7 +29,7 @@
 
     /**
      * FastDfs文件上传接口
-     * 
+     *
      * @param file 上传的文件
      * @return 访问地址
      * @throws Exception
@@ -35,8 +37,40 @@
     @Override
     public String uploadFile(MultipartFile file) throws Exception
     {
-        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
-                FilenameUtils.getExtension(file.getOriginalFilename()), null);
-        return domain + "/" + storePath.getFullPath();
+        InputStream inputStream = null;
+        try
+        {
+            inputStream = file.getInputStream();
+            StorePath storePath = storageClient.uploadFile(inputStream, file.getSize(), FileTypeUtils.getExtension(file), null);
+            return domain + "/" + storePath.getFullPath();
+        }
+        catch (Exception e)
+        {
+            throw new RuntimeException("FastDfs Failed to upload file", e);
+        }
+        finally
+        {
+            IoUtils.closeQuietly(inputStream);
+        }
+    }
+
+    /**
+     * FastDFS文件删除接口
+     * 
+     * @param fileUrl 文件访问URL
+     * @throws Exception
+     */
+    @Override
+    public void deleteFile(String fileUrl) throws Exception
+    {
+        try
+        {
+            StorePath storePath = StorePath.parseFromUrl(fileUrl);
+            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
+        }
+        catch (Exception e)
+        {
+            throw new RuntimeException("FastDfs Failed to delete file: ", e);
+        }
     }
 }

--
Gitblit v1.9.3