From 5f8c569a7084dc732cf17350d0ca3a771f60ffcf Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 29 Nov 2022 22:16:10 +0800 Subject: [PATCH] =?UTF-8?q?!313=20=E4=BF=AE=E5=A4=8D=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E4=B8=83=E7=89=9B=E4=BA=91=E6=97=A0mime=20type=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yudao/framework/file/core/client/FileClient.java | 2 +- .../yudao/framework/file/core/client/db/DBFileClient.java | 2 +- .../yudao/framework/file/core/client/ftp/FtpFileClient.java | 3 +-- .../framework/file/core/client/local/LocalFileClient.java | 2 +- .../yudao/framework/file/core/client/s3/S3FileClient.java | 3 ++- .../framework/file/core/client/sftp/SftpFileClient.java | 2 +- .../yudao/framework/file/core/utils/FileTypeUtils.java | 4 ++-- .../framework/file/core/client/ftp/FtpFileClientTest.java | 2 +- .../file/core/client/local/LocalFileClientTest.java | 2 +- .../framework/file/core/client/s3/S3FileClientTest.java | 2 +- .../framework/file/core/client/sftp/SftpFileClientTest.java | 2 +- .../module/infra/service/file/FileConfigServiceImpl.java | 6 +++--- .../yudao/module/infra/service/file/FileServiceImpl.java | 2 +- 13 files changed, 17 insertions(+), 17 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/FileClient.java b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/FileClient.java index 178c27d4c..fb576d508 100644 --- a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/FileClient.java +++ b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/FileClient.java @@ -22,7 +22,7 @@ public interface FileClient { * @return 完整路径,即 HTTP 访问地址 * @throws Exception 上传文件时,抛出 Exception 异常 */ - String upload(byte[] content, String path) throws Exception; + String upload(byte[] content, String path, String type) throws Exception; /** * 删除文件 diff --git a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/db/DBFileClient.java b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/db/DBFileClient.java index a227cc314..1e2c082fe 100644 --- a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/db/DBFileClient.java +++ b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/db/DBFileClient.java @@ -21,7 +21,7 @@ public class DBFileClient extends AbstractFileClient { } @Override - public String upload(byte[] content, String path) { + public String upload(byte[] content, String path, String type) { getDao().insert(getId(), path, content); // 拼接返回路径 return super.formatFileUrl(config.getDomain(), path); diff --git a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/ftp/FtpFileClient.java b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/ftp/FtpFileClient.java index 4ad345bdc..796044f3f 100644 --- a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/ftp/FtpFileClient.java +++ b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/ftp/FtpFileClient.java @@ -10,7 +10,6 @@ import cn.iocoder.yudao.framework.file.core.client.AbstractFileClient; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.File; /** * Ftp 文件客户端 @@ -39,7 +38,7 @@ public class FtpFileClient extends AbstractFileClient { } @Override - public String upload(byte[] content, String path) { + public String upload(byte[] content, String path, String type) { // 执行写入 String filePath = getFilePath(path); String fileName = FileUtil.getName(filePath); diff --git a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/local/LocalFileClient.java b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/local/LocalFileClient.java index 1c79f8999..cac13f1fb 100644 --- a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/local/LocalFileClient.java +++ b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/local/LocalFileClient.java @@ -25,7 +25,7 @@ public class LocalFileClient extends AbstractFileClient { } @Override - public String upload(byte[] content, String path) { + public String upload(byte[] content, String path, String type) { // 执行写入 String filePath = getFilePath(path); FileUtil.writeBytes(content, filePath); diff --git a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClient.java b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClient.java index ab95d4bda..a2b11b1dc 100644 --- a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClient.java +++ b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClient.java @@ -82,10 +82,11 @@ public class S3FileClient extends AbstractFileClient { } @Override - public String upload(byte[] content, String path) throws Exception { + public String upload(byte[] content, String path, String type) throws Exception { // 执行上传 client.putObject(PutObjectArgs.builder() .bucket(config.getBucket()) // bucket 必须传递 + .contentType(type) .object(path) // 相对路径作为 key .stream(new ByteArrayInputStream(content), content.length, -1) // 文件内容 .build()); diff --git a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/sftp/SftpFileClient.java b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/sftp/SftpFileClient.java index 3e18e888d..facddcea0 100644 --- a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/sftp/SftpFileClient.java +++ b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/sftp/SftpFileClient.java @@ -31,7 +31,7 @@ public class SftpFileClient extends AbstractFileClient { } @Override - public String upload(byte[] content, String path) { + public String upload(byte[] content, String path, String type) { // 执行写入 String filePath = getFilePath(path); File file = FileUtils.createTempFile(content); diff --git a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/utils/FileTypeUtils.java b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/utils/FileTypeUtils.java index b623d364a..8b99227b1 100644 --- a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/utils/FileTypeUtils.java +++ b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/utils/FileTypeUtils.java @@ -16,7 +16,7 @@ public class FileTypeUtils { /** * 获得文件的 mineType,对于doc,jar等文件会有误差 * - * @param data 包含文件开头几千个字节的字节数组 + * @param data 文件内容 * @return mineType 无法识别时会返回“application/octet-stream” */ @SneakyThrows @@ -37,7 +37,7 @@ public class FileTypeUtils { /** * 在拥有文件和数据的情况下,最好使用此方法,最为准确 * - * @param data 包含文件开头几千个字节的字节数组 + * @param data 文件内容 * @param name 文件名 * @return mineType 无法识别时会返回“application/octet-stream” */ diff --git a/yudao-framework/yudao-spring-boot-starter-file/src/test/java/cn/iocoder/yudao/framework/file/core/client/ftp/FtpFileClientTest.java b/yudao-framework/yudao-spring-boot-starter-file/src/test/java/cn/iocoder/yudao/framework/file/core/client/ftp/FtpFileClientTest.java index 00a3a268e..619e52db8 100644 --- a/yudao-framework/yudao-spring-boot-starter-file/src/test/java/cn/iocoder/yudao/framework/file/core/client/ftp/FtpFileClientTest.java +++ b/yudao-framework/yudao-spring-boot-starter-file/src/test/java/cn/iocoder/yudao/framework/file/core/client/ftp/FtpFileClientTest.java @@ -25,7 +25,7 @@ public class FtpFileClientTest { // 上传文件 String path = IdUtil.fastSimpleUUID() + ".jpg"; byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); - String fullPath = client.upload(content, path); + String fullPath = client.upload(content, path, "image/jpeg"); System.out.println("访问地址:" + fullPath); if (false) { byte[] bytes = client.getContent(path); diff --git a/yudao-framework/yudao-spring-boot-starter-file/src/test/java/cn/iocoder/yudao/framework/file/core/client/local/LocalFileClientTest.java b/yudao-framework/yudao-spring-boot-starter-file/src/test/java/cn/iocoder/yudao/framework/file/core/client/local/LocalFileClientTest.java index 2062d63d7..d48609bc6 100644 --- a/yudao-framework/yudao-spring-boot-starter-file/src/test/java/cn/iocoder/yudao/framework/file/core/client/local/LocalFileClientTest.java +++ b/yudao-framework/yudao-spring-boot-starter-file/src/test/java/cn/iocoder/yudao/framework/file/core/client/local/LocalFileClientTest.java @@ -19,7 +19,7 @@ public class LocalFileClientTest { // 上传文件 String path = IdUtil.fastSimpleUUID() + ".jpg"; byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); - String fullPath = client.upload(content, path); + String fullPath = client.upload(content, path, "image/jpeg"); System.out.println("访问地址:" + fullPath); client.delete(path); } diff --git a/yudao-framework/yudao-spring-boot-starter-file/src/test/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClientTest.java b/yudao-framework/yudao-spring-boot-starter-file/src/test/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClientTest.java index de77477cd..1d0ed2091 100644 --- a/yudao-framework/yudao-spring-boot-starter-file/src/test/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClientTest.java +++ b/yudao-framework/yudao-spring-boot-starter-file/src/test/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClientTest.java @@ -101,7 +101,7 @@ public class S3FileClientTest { // 上传文件 String path = IdUtil.fastSimpleUUID() + ".jpg"; byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); - String fullPath = client.upload(content, path); + String fullPath = client.upload(content, path, "image/jpeg"); System.out.println("访问地址:" + fullPath); // 读取文件 if (true) { diff --git a/yudao-framework/yudao-spring-boot-starter-file/src/test/java/cn/iocoder/yudao/framework/file/core/client/sftp/SftpFileClientTest.java b/yudao-framework/yudao-spring-boot-starter-file/src/test/java/cn/iocoder/yudao/framework/file/core/client/sftp/SftpFileClientTest.java index 412df1ea8..4785c0d89 100644 --- a/yudao-framework/yudao-spring-boot-starter-file/src/test/java/cn/iocoder/yudao/framework/file/core/client/sftp/SftpFileClientTest.java +++ b/yudao-framework/yudao-spring-boot-starter-file/src/test/java/cn/iocoder/yudao/framework/file/core/client/sftp/SftpFileClientTest.java @@ -23,7 +23,7 @@ public class SftpFileClientTest { // 上传文件 String path = IdUtil.fastSimpleUUID() + ".jpg"; byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); - String fullPath = client.upload(content, path); + String fullPath = client.upload(content, path, "image/jpeg"); System.out.println("访问地址:" + fullPath); if (false) { byte[] bytes = client.getContent(path); diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileConfigServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileConfigServiceImpl.java index 129889ef0..e39f4028a 100755 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileConfigServiceImpl.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileConfigServiceImpl.java @@ -31,8 +31,8 @@ import org.springframework.validation.annotation.Validated; import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax.validation.Validator; -import java.util.Collection; import java.time.LocalDateTime; +import java.util.Collection; import java.util.List; import java.util.Map; @@ -193,7 +193,7 @@ public class FileConfigServiceImpl implements FileConfigService { // 校验存在 FileConfigDO config = this.validateFileConfigExists(id); if (Boolean.TRUE.equals(config.getMaster())) { - throw exception(FILE_CONFIG_DELETE_FAIL_MASTER); + throw exception(FILE_CONFIG_DELETE_FAIL_MASTER); } // 删除 fileConfigMapper.deleteById(id); @@ -230,7 +230,7 @@ public class FileConfigServiceImpl implements FileConfigService { this.validateFileConfigExists(id); // 上传文件 byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); - return fileClientFactory.getFileClient(id).upload(content, IdUtil.fastSimpleUUID() + ".jpg"); + return fileClientFactory.getFileClient(id).upload(content, IdUtil.fastSimpleUUID() + ".jpg", "image/jpeg"); } @Override diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java index 493a9ed1f..2226e7bae 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java @@ -52,7 +52,7 @@ public class FileServiceImpl implements FileService { // 上传到文件存储器 FileClient client = fileConfigService.getMasterFileClient(); Assert.notNull(client, "客户端(master) 不能为空"); - String url = client.upload(content, path); + String url = client.upload(content, path, type); // 保存到数据库 FileDO file = new FileDO();