Commit 44249e43dd9a3049b2fd8e176b62be392b04831a

Authored by 芋道源码
Committed by Gitee
2 parents 391fec37 7f9fc415

!313 修复上传七牛云无mime type问题

Merge pull request !313 from 石溪/master
Showing 14 changed files with 15 additions and 14 deletions
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 {
22 22 * @return 完整路径,即 HTTP 访问地址
23 23 * @throws Exception 上传文件时,抛出 Exception 异常
24 24 */
25   - String upload(byte[] content, String path) throws Exception;
  25 + String upload(byte[] content, String path, String type) throws Exception;
26 26  
27 27 /**
28 28 * 删除文件
... ...
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<DBFileClientConfig> {
21 21 }
22 22  
23 23 @Override
24   - public String upload(byte[] content, String path) {
  24 + public String upload(byte[] content, String path, String type) {
25 25 getDao().insert(getId(), path, content);
26 26 // 拼接返回路径
27 27 return super.formatFileUrl(config.getDomain(), path);
... ...
yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/ftp/FtpFileClient.java
... ... @@ -38,7 +38,7 @@ public class FtpFileClient extends AbstractFileClient<FtpFileClientConfig> {
38 38 }
39 39  
40 40 @Override
41   - public String upload(byte[] content, String path) {
  41 + public String upload(byte[] content, String path, String type) {
42 42 // 执行写入
43 43 String filePath = getFilePath(path);
44 44 String fileName = FileUtil.getName(filePath);
... ...
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<LocalFileClientConfig> {
25 25 }
26 26  
27 27 @Override
28   - public String upload(byte[] content, String path) {
  28 + public String upload(byte[] content, String path, String type) {
29 29 // 执行写入
30 30 String filePath = getFilePath(path);
31 31 FileUtil.writeBytes(content, filePath);
... ...
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<S3FileClientConfig> {
82 82 }
83 83  
84 84 @Override
85   - public String upload(byte[] content, String path) throws Exception {
  85 + public String upload(byte[] content, String path, String type) throws Exception {
86 86 // 执行上传
87 87 client.putObject(PutObjectArgs.builder()
88 88 .bucket(config.getBucket()) // bucket 必须传递
  89 + .contentType(type)
89 90 .object(path) // 相对路径作为 key
90 91 .stream(new ByteArrayInputStream(content), content.length, -1) // 文件内容
91 92 .build());
... ...
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<SftpFileClientConfig> {
31 31 }
32 32  
33 33 @Override
34   - public String upload(byte[] content, String path) {
  34 + public String upload(byte[] content, String path, String type) {
35 35 // 执行写入
36 36 String filePath = getFilePath(path);
37 37 File file = FileUtils.createTempFile(content);
... ...
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 {
25 25 // 上传文件
26 26 String path = IdUtil.fastSimpleUUID() + ".jpg";
27 27 byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
28   - String fullPath = client.upload(content, path);
  28 + String fullPath = client.upload(content, path, "image/jpeg");
29 29 System.out.println("访问地址:" + fullPath);
30 30 if (false) {
31 31 byte[] bytes = client.getContent(path);
... ...
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 {
19 19 // 上传文件
20 20 String path = IdUtil.fastSimpleUUID() + ".jpg";
21 21 byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
22   - String fullPath = client.upload(content, path);
  22 + String fullPath = client.upload(content, path, "image/jpeg");
23 23 System.out.println("访问地址:" + fullPath);
24 24 client.delete(path);
25 25 }
... ...
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 {
101 101 // 上传文件
102 102 String path = IdUtil.fastSimpleUUID() + ".jpg";
103 103 byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
104   - String fullPath = client.upload(content, path);
  104 + String fullPath = client.upload(content, path, "image/jpeg");
105 105 System.out.println("访问地址:" + fullPath);
106 106 // 读取文件
107 107 if (true) {
... ...
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 {
23 23 // 上传文件
24 24 String path = IdUtil.fastSimpleUUID() + ".jpg";
25 25 byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
26   - String fullPath = client.upload(content, path);
  26 + String fullPath = client.upload(content, path, "image/jpeg");
27 27 System.out.println("访问地址:" + fullPath);
28 28 if (false) {
29 29 byte[] bytes = client.getContent(path);
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileConfigServiceImpl.java
... ... @@ -230,7 +230,7 @@ public class FileConfigServiceImpl implements FileConfigService {
230 230 this.validateFileConfigExists(id);
231 231 // 上传文件
232 232 byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
233   - return fileClientFactory.getFileClient(id).upload(content, IdUtil.fastSimpleUUID() + ".jpg");
  233 + return fileClientFactory.getFileClient(id).upload(content, IdUtil.fastSimpleUUID() + ".jpg", "image/jpeg");
234 234 }
235 235  
236 236 @Override
... ...
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 {
52 52 // 上传到文件存储器
53 53 FileClient client = fileConfigService.getMasterFileClient();
54 54 Assert.notNull(client, "客户端(master) 不能为空");
55   - String url = client.upload(content, path);
  55 + String url = client.upload(content, path, type);
56 56  
57 57 // 保存到数据库
58 58 FileDO file = new FileDO();
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileConfigServiceImplTest.java
... ... @@ -240,7 +240,7 @@ public class FileConfigServiceImplTest extends BaseDbUnitTest {
240 240 // mock 获得 Client
241 241 FileClient fileClient = mock(FileClient.class);
242 242 when(fileClientFactory.getFileClient(eq(id))).thenReturn(fileClient);
243   - when(fileClient.upload(any(), any())).thenReturn("https://www.iocoder.cn");
  243 + when(fileClient.upload(any(), any(), any())).thenReturn("https://www.iocoder.cn");
244 244  
245 245 // 调用,并断言
246 246 assertEquals("https://www.iocoder.cn", fileConfigService.testFileConfig(id));
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceTest.java
... ... @@ -79,7 +79,7 @@ public class FileServiceTest extends BaseDbUnitTest {
79 79 FileClient client = mock(FileClient.class);
80 80 when(fileConfigService.getMasterFileClient()).thenReturn(client);
81 81 String url = randomString();
82   - when(client.upload(same(content), same(path))).thenReturn(url);
  82 + when(client.upload(same(content), same(path), same("image/jpeg"))).thenReturn(url);
83 83 when(client.getId()).thenReturn(10L);
84 84 String name = "单测文件名";
85 85 // 调用
... ...