From 70a29ae28b85643e00a4b7fbf111a95646a24dc2 Mon Sep 17 00:00:00 2001
From: lin <sbla5888@163.com>
Date: Wed, 20 Mar 2024 17:20:38 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 src/main/java/org/springblade/modules/resource/endpoint/OssEndpoint.java |   69 ++++++++++++++++++++++++++++++++++
 1 files changed, 68 insertions(+), 1 deletions(-)

diff --git a/src/main/java/org/springblade/modules/resource/endpoint/OssEndpoint.java b/src/main/java/org/springblade/modules/resource/endpoint/OssEndpoint.java
index 473107a..413f2f0 100644
--- a/src/main/java/org/springblade/modules/resource/endpoint/OssEndpoint.java
+++ b/src/main/java/org/springblade/modules/resource/endpoint/OssEndpoint.java
@@ -19,6 +19,7 @@
 import io.swagger.annotations.Api;
 import lombok.AllArgsConstructor;
 import lombok.SneakyThrows;
+import org.springblade.common.utils.ImageUtils;
 import org.springblade.core.launch.constant.AppConstant;
 import org.springblade.core.oss.model.BladeFile;
 import org.springblade.core.oss.model.OssFile;
@@ -28,8 +29,11 @@
 import org.springblade.core.tool.constant.RoleConstant;
 import org.springblade.core.tool.utils.FileUtil;
 import org.springblade.core.tool.utils.Func;
+import org.springblade.core.tool.utils.ImageUtil;
 import org.springblade.modules.resource.builder.oss.OssBuilder;
 import org.springblade.modules.resource.entity.Attach;
+import org.springblade.modules.resource.entity.AttachData;
+import org.springblade.modules.resource.service.IAttachDataService;
 import org.springblade.modules.resource.service.IAttachService;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
@@ -55,6 +59,11 @@
 	 * 附件表服务
 	 */
 	private final IAttachService attachService;
+
+	/**
+	 * 附件数据表服务
+	 */
+	private final IAttachDataService attachDataService;
 
 	/**
 	 * 创建存储桶
@@ -206,10 +215,46 @@
 	 */
 	@SneakyThrows
 	@PostMapping("/put-file-attach-by-name")
-	public R<BladeFile> putFileAttach(@RequestParam String fileName, @RequestParam MultipartFile file) {
+	public R<BladeFile> putFileAttach(@RequestParam(required = false) String fileName, @RequestParam MultipartFile file) {
 		BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
 		Long attachId = buildAttach(fileName, file.getSize(), bladeFile);
 		bladeFile.setAttachId(attachId);
+		return R.data(bladeFile);
+	}
+
+	/**
+	 * 自定义前缀上传文件
+	 *
+	 * @param file 文件
+	 * @param prefixPath 文件
+	 * @return ObjectStat
+	 */
+	@SneakyThrows
+	@PostMapping("/put-file-by-prefix-path")
+	public R<BladeFile> putFileByPrefixPath(@RequestParam MultipartFile file,@RequestParam(required = false) String prefixPath) {
+		BladeFile bladeFile = ossBuilder.templateByPrefixPath(prefixPath).putFile(file.getOriginalFilename(), file.getInputStream());
+		// 修改link
+		changeLink(bladeFile);
+		return R.data(bladeFile);
+	}
+
+	/**
+	 * 自定义前缀上传文件并保存至附件表
+	 *
+	 * @param file 文件
+	 * @param prefixPath 文件
+	 * @return ObjectStat
+	 */
+	@SneakyThrows
+	@PostMapping("/put-file-attach-by-prefix-path")
+	public R<BladeFile> putFileAttachByPrefixPath(@RequestParam MultipartFile file,@RequestParam(required = false) String prefixPath) {
+		String fileName = file.getOriginalFilename();
+		BladeFile bladeFile = ossBuilder.templateByPrefixPath(prefixPath).putFile(file.getOriginalFilename(), file.getInputStream());
+		Long attachId = buildAttach(fileName, file.getSize(), bladeFile);
+		buildAttachData(attachId,fileName, file.getSize(), bladeFile,file);
+		bladeFile.setAttachId(attachId);
+		// 修改link
+		changeLink(bladeFile);
 		return R.data(bladeFile);
 	}
 
@@ -235,6 +280,28 @@
 	}
 
 	/**
+	 * 构建附件数据表
+	 *
+	 * @param attachId 附件id
+	 * @param fileName  文件名
+	 * @param fileSize  文件大小
+	 * @param bladeFile 对象存储文件
+	 * @return attachId
+	 */
+	private Long buildAttachData(Long attachId,String fileName, Long fileSize, BladeFile bladeFile,MultipartFile file) {
+		String fileExtension = FileUtil.getFileExtension(fileName);
+		AttachData attach = new AttachData();
+		attach.setAttachId(attachId);
+		attach.setName(bladeFile.getName());
+		attach.setOriginalName(bladeFile.getOriginalName());
+		attach.setSize(fileSize);
+		attach.setExtension(fileExtension);
+		attach.setData(ImageUtils.mulToBase64(file));
+		attachDataService.save(attach);
+		return attach.getId();
+	}
+
+	/**
 	 * 删除文件
 	 *
 	 * @param fileName 存储桶对象名称

--
Gitblit v1.9.3