|
|
@@ -2,11 +2,25 @@ package org.springblade.resource.feign;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import net.coobird.thumbnailator.Thumbnails;
|
|
|
+import org.springblade.common.enums.UrlEnum;
|
|
|
+import org.springblade.core.oss.model.BladeFile;
|
|
|
import org.springblade.core.tenant.annotation.NonDS;
|
|
|
import org.springblade.core.tool.api.R;
|
|
|
+import org.springblade.resource.builder.oss.OssBuilder;
|
|
|
import org.springblade.resource.entity.Oss;
|
|
|
import org.springblade.resource.mapper.OssMapper;
|
|
|
+import org.springframework.mock.web.MockMultipartFile;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
|
|
|
@NonDS
|
|
|
@RestController
|
|
|
@@ -15,6 +29,16 @@ public class OssClient implements IOssClient {
|
|
|
|
|
|
private final OssMapper ossMapper;
|
|
|
|
|
|
+ /**
|
|
|
+ * 对象存储构建类
|
|
|
+ */
|
|
|
+ private final OssBuilder ossBuilder;
|
|
|
+
|
|
|
+ private final List<String> CONTENT_TYPE = Arrays.asList("image/jpeg", "image/png", "image/jpg", "image/bmp");
|
|
|
+
|
|
|
+ private final long MAX_IMAGE_SIZE = 204800;
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public R addOss(String tenantId) {
|
|
|
Oss oss = ossMapper.selectOne(Wrappers.<Oss>query().lambda()
|
|
|
@@ -30,4 +54,67 @@ public class OssClient implements IOssClient {
|
|
|
return R.data(oss);
|
|
|
}
|
|
|
|
|
|
+ @SneakyThrows
|
|
|
+ @Override
|
|
|
+ public R<String> putFileFeign(@RequestParam("fileUrl") String fileUrl) {
|
|
|
+ String replace;
|
|
|
+ File file = new File(fileUrl);
|
|
|
+ // 检查文件是否存在
|
|
|
+ if (!file.exists()) {
|
|
|
+ System.out.println("文件不存在: " + fileUrl);
|
|
|
+ }
|
|
|
+ // 读取临时文件并转换为MultipartFile
|
|
|
+ try (FileInputStream fileInputStream = new FileInputStream(file)) {
|
|
|
+ MultipartFile multipartFile = new MockMultipartFile(
|
|
|
+ file.getName(),
|
|
|
+ file.getName(),
|
|
|
+ getContentType(file),
|
|
|
+ fileInputStream
|
|
|
+ );
|
|
|
+ String contentType = multipartFile.getContentType();
|
|
|
+ BladeFile bladeFile;
|
|
|
+ if (this.CONTENT_TYPE.contains(contentType) && multipartFile.getSize() > this.MAX_IMAGE_SIZE) {
|
|
|
+ // 使用Thumbnails压缩图片
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
+ Thumbnails.of(multipartFile.getInputStream()).size(350, 350).outputQuality(1).toOutputStream(outputStream);
|
|
|
+ MultipartFile compressedImage = new MockMultipartFile(multipartFile.getName(), multipartFile.getOriginalFilename(), multipartFile.getContentType(), outputStream.toByteArray());
|
|
|
+ bladeFile = ossBuilder.template().putFile(compressedImage.getOriginalFilename(), compressedImage.getInputStream());
|
|
|
+ } else {
|
|
|
+ bladeFile = ossBuilder.template().putFile(multipartFile.getOriginalFilename(), multipartFile.getInputStream());
|
|
|
+ }
|
|
|
+ String link = bladeFile.getLink();
|
|
|
+ replace = link.replace(UrlEnum.INTERNAL_NETWORK_ADDRESS.url, UrlEnum.EXTERNAL_NETWORK_ADDRESS.url);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e.getMessage());
|
|
|
+ }
|
|
|
+ boolean status = file.delete();
|
|
|
+ if (status) {
|
|
|
+ System.out.println("文件删除成功!");
|
|
|
+ } else {
|
|
|
+ System.out.println("文件删除失败!");
|
|
|
+ }
|
|
|
+ return R.data(replace);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据文件扩展名获取Content-Type
|
|
|
+ *
|
|
|
+ * @param file 文件对象
|
|
|
+ * @return 对应的Content-Type
|
|
|
+ */
|
|
|
+ private static String getContentType(File file) {
|
|
|
+ String fileName = file.getName().toLowerCase();
|
|
|
+ if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")) {
|
|
|
+ return "image/jpeg";
|
|
|
+ } else if (fileName.endsWith(".png")) {
|
|
|
+ return "image/png";
|
|
|
+ } else if (fileName.endsWith(".gif")) {
|
|
|
+ return "image/gif";
|
|
|
+ } else if (fileName.endsWith(".pdf")) {
|
|
|
+ return "application/pdf";
|
|
|
+ } else {
|
|
|
+ return "application/octet-stream";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|