From ff14f6f5737c45423a403b036d19e5dfb4a395d7 Mon Sep 17 00:00:00 2001 From: weiweiw <14335254+weiweiw22@user.noreply.gitee.com> Date: Mon, 17 Jun 2024 18:04:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9C=AC=E5=9C=B0=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=92=8C?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bonus-modules/bonus-file/pom.xml | 7 +++- .../file/controller/SysFileController.java | 37 +++++++++++++++++++ .../service/FastDfsSysFileServiceImpl.java | 13 +++++++ .../bonus/file/service/ISysFileService.java | 7 ++++ .../file/service/LocalSysFileServiceImpl.java | 31 ++++++++++++++++ .../file/service/MinioSysFileServiceImpl.java | 13 +++++++ 6 files changed, 107 insertions(+), 1 deletion(-) diff --git a/bonus-modules/bonus-file/pom.xml b/bonus-modules/bonus-file/pom.xml index f3b5647..8f4016c 100644 --- a/bonus-modules/bonus-file/pom.xml +++ b/bonus-modules/bonus-file/pom.xml @@ -65,7 +65,12 @@ com.bonus bonus-common-swagger - + + + commons-net + commons-net + 3.9.0 + diff --git a/bonus-modules/bonus-file/src/main/java/com/bonus/file/controller/SysFileController.java b/bonus-modules/bonus-file/src/main/java/com/bonus/file/controller/SysFileController.java index cc8b8ff..6729af6 100644 --- a/bonus-modules/bonus-file/src/main/java/com/bonus/file/controller/SysFileController.java +++ b/bonus-modules/bonus-file/src/main/java/com/bonus/file/controller/SysFileController.java @@ -1,9 +1,13 @@ package com.bonus.file.controller; +import com.bonus.common.core.utils.Base64Utils; +import com.bonus.file.utils.FileDownloadUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.bonus.common.core.domain.R; @@ -11,6 +15,8 @@ import com.bonus.common.core.utils.file.FileUtils; import com.bonus.file.service.ISysFileService; import com.bonus.system.api.domain.SysFile; +import java.io.IOException; + /** * 文件请求处理 * @@ -45,4 +51,35 @@ public class SysFileController return R.fail(e.getMessage()); } } + + @GetMapping("/download") + public R downloadFile(String url, String destination) { + try { + String fileUrl = Base64Utils.decodeUrl(url); + if (fileUrl != null) { + String fileName = Base64Utils.getFileNameFromURL(fileUrl); + sysFileService.downloadFile(fileUrl, destination + "/" + fileName); + return R.ok(); + } + } catch (Exception e) { + log.error("下载文件失败", e); + return R.fail(e.getMessage()); + } + return R.fail("下载文件失败"); + } + + @GetMapping("/delete") + public R deleteFile(String url) { + try { + String fileUrl = Base64Utils.decodeUrl(url); + if (fileUrl != null) { + sysFileService.deleteFile(fileUrl); + return R.ok(); + } + } catch (Exception e) { + log.error("delete文件失败", e); + return R.fail(e.getMessage()); + } + return R.fail("传入参数不满足要求"); + } } \ No newline at end of file diff --git a/bonus-modules/bonus-file/src/main/java/com/bonus/file/service/FastDfsSysFileServiceImpl.java b/bonus-modules/bonus-file/src/main/java/com/bonus/file/service/FastDfsSysFileServiceImpl.java index 4672ae9..8d626a5 100644 --- a/bonus-modules/bonus-file/src/main/java/com/bonus/file/service/FastDfsSysFileServiceImpl.java +++ b/bonus-modules/bonus-file/src/main/java/com/bonus/file/service/FastDfsSysFileServiceImpl.java @@ -43,4 +43,17 @@ public class FastDfsSysFileServiceImpl implements ISysFileService IoUtils.closeQuietly(inputStream); return domain + "/" + storePath.getFullPath(); } + + @Override + public boolean downloadFile(String urlStr, String destination) throws Exception + { + return false; + } + + + @Override + public boolean deleteFile(String urlStr) throws Exception + { + return false; + } } diff --git a/bonus-modules/bonus-file/src/main/java/com/bonus/file/service/ISysFileService.java b/bonus-modules/bonus-file/src/main/java/com/bonus/file/service/ISysFileService.java index ee70146..a5c7d7d 100644 --- a/bonus-modules/bonus-file/src/main/java/com/bonus/file/service/ISysFileService.java +++ b/bonus-modules/bonus-file/src/main/java/com/bonus/file/service/ISysFileService.java @@ -17,4 +17,11 @@ public interface ISysFileService * @throws Exception */ public String uploadFile(MultipartFile file) throws Exception; + + + public boolean downloadFile(String urlStr, String destination) throws Exception; + + + public boolean deleteFile(String urlStr) throws Exception; + } diff --git a/bonus-modules/bonus-file/src/main/java/com/bonus/file/service/LocalSysFileServiceImpl.java b/bonus-modules/bonus-file/src/main/java/com/bonus/file/service/LocalSysFileServiceImpl.java index 7e3595b..abe1e07 100644 --- a/bonus-modules/bonus-file/src/main/java/com/bonus/file/service/LocalSysFileServiceImpl.java +++ b/bonus-modules/bonus-file/src/main/java/com/bonus/file/service/LocalSysFileServiceImpl.java @@ -1,11 +1,17 @@ package com.bonus.file.service; +import com.bonus.file.utils.FileDownloadUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import com.bonus.file.utils.FileUploadUtils; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + /** * 本地文件存储 * @@ -47,4 +53,29 @@ public class LocalSysFileServiceImpl implements ISysFileService String url = domain + localFilePrefix + name; return url; } + + @Override + public boolean downloadFile(String urlStr, String destination) throws Exception + { + return FileDownloadUtils.downloadFile(urlStr, destination); + } + + + @Override + public boolean deleteFile(String urlStr) throws Exception + { + String regex = String.format("^(.*?%s)", localFilePrefix); + String updatePath = urlStr.replaceFirst(regex, localFilePath); + Path path = Paths.get(updatePath); + if (Files.exists(path)){ + try { + Files.deleteIfExists(path); + }catch (IOException e){ + throw new Exception(e.getMessage(), e); + } + }else { + throw new Exception("删除文件时文件不存在"); + } + return true; + } } diff --git a/bonus-modules/bonus-file/src/main/java/com/bonus/file/service/MinioSysFileServiceImpl.java b/bonus-modules/bonus-file/src/main/java/com/bonus/file/service/MinioSysFileServiceImpl.java index 6374369..b540455 100644 --- a/bonus-modules/bonus-file/src/main/java/com/bonus/file/service/MinioSysFileServiceImpl.java +++ b/bonus-modules/bonus-file/src/main/java/com/bonus/file/service/MinioSysFileServiceImpl.java @@ -46,4 +46,17 @@ public class MinioSysFileServiceImpl implements ISysFileService IoUtils.closeQuietly(inputStream); return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName; } + + @Override + public boolean downloadFile(String urlStr, String destination) throws Exception + { + return false; + } + + + @Override + public boolean deleteFile(String urlStr) throws Exception + { + return false; + } }