Commit 44249e43dd9a3049b2fd8e176b62be392b04831a
Committed by
Gitee
!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,7 +22,7 @@ public interface FileClient { | ||
| 22 | * @return 完整路径,即 HTTP 访问地址 | 22 | * @return 完整路径,即 HTTP 访问地址 |
| 23 | * @throws Exception 上传文件时,抛出 Exception 异常 | 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,7 +21,7 @@ public class DBFileClient extends AbstractFileClient<DBFileClientConfig> { | ||
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | @Override | 23 | @Override |
| 24 | - public String upload(byte[] content, String path) { | 24 | + public String upload(byte[] content, String path, String type) { |
| 25 | getDao().insert(getId(), path, content); | 25 | getDao().insert(getId(), path, content); |
| 26 | // 拼接返回路径 | 26 | // 拼接返回路径 |
| 27 | return super.formatFileUrl(config.getDomain(), path); | 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,7 +38,7 @@ public class FtpFileClient extends AbstractFileClient<FtpFileClientConfig> { | ||
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | @Override | 40 | @Override |
| 41 | - public String upload(byte[] content, String path) { | 41 | + public String upload(byte[] content, String path, String type) { |
| 42 | // 执行写入 | 42 | // 执行写入 |
| 43 | String filePath = getFilePath(path); | 43 | String filePath = getFilePath(path); |
| 44 | String fileName = FileUtil.getName(filePath); | 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,7 +25,7 @@ public class LocalFileClient extends AbstractFileClient<LocalFileClientConfig> { | ||
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | @Override | 27 | @Override |
| 28 | - public String upload(byte[] content, String path) { | 28 | + public String upload(byte[] content, String path, String type) { |
| 29 | // 执行写入 | 29 | // 执行写入 |
| 30 | String filePath = getFilePath(path); | 30 | String filePath = getFilePath(path); |
| 31 | FileUtil.writeBytes(content, filePath); | 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,10 +82,11 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> { | ||
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | @Override | 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 | client.putObject(PutObjectArgs.builder() | 87 | client.putObject(PutObjectArgs.builder() |
| 88 | .bucket(config.getBucket()) // bucket 必须传递 | 88 | .bucket(config.getBucket()) // bucket 必须传递 |
| 89 | + .contentType(type) | ||
| 89 | .object(path) // 相对路径作为 key | 90 | .object(path) // 相对路径作为 key |
| 90 | .stream(new ByteArrayInputStream(content), content.length, -1) // 文件内容 | 91 | .stream(new ByteArrayInputStream(content), content.length, -1) // 文件内容 |
| 91 | .build()); | 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,7 +31,7 @@ public class SftpFileClient extends AbstractFileClient<SftpFileClientConfig> { | ||
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | @Override | 33 | @Override |
| 34 | - public String upload(byte[] content, String path) { | 34 | + public String upload(byte[] content, String path, String type) { |
| 35 | // 执行写入 | 35 | // 执行写入 |
| 36 | String filePath = getFilePath(path); | 36 | String filePath = getFilePath(path); |
| 37 | File file = FileUtils.createTempFile(content); | 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,7 +25,7 @@ public class FtpFileClientTest { | ||
| 25 | // 上传文件 | 25 | // 上传文件 |
| 26 | String path = IdUtil.fastSimpleUUID() + ".jpg"; | 26 | String path = IdUtil.fastSimpleUUID() + ".jpg"; |
| 27 | byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); | 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 | System.out.println("访问地址:" + fullPath); | 29 | System.out.println("访问地址:" + fullPath); |
| 30 | if (false) { | 30 | if (false) { |
| 31 | byte[] bytes = client.getContent(path); | 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,7 +19,7 @@ public class LocalFileClientTest { | ||
| 19 | // 上传文件 | 19 | // 上传文件 |
| 20 | String path = IdUtil.fastSimpleUUID() + ".jpg"; | 20 | String path = IdUtil.fastSimpleUUID() + ".jpg"; |
| 21 | byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); | 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 | System.out.println("访问地址:" + fullPath); | 23 | System.out.println("访问地址:" + fullPath); |
| 24 | client.delete(path); | 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,7 +101,7 @@ public class S3FileClientTest { | ||
| 101 | // 上传文件 | 101 | // 上传文件 |
| 102 | String path = IdUtil.fastSimpleUUID() + ".jpg"; | 102 | String path = IdUtil.fastSimpleUUID() + ".jpg"; |
| 103 | byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); | 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 | System.out.println("访问地址:" + fullPath); | 105 | System.out.println("访问地址:" + fullPath); |
| 106 | // 读取文件 | 106 | // 读取文件 |
| 107 | if (true) { | 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,7 +23,7 @@ public class SftpFileClientTest { | ||
| 23 | // 上传文件 | 23 | // 上传文件 |
| 24 | String path = IdUtil.fastSimpleUUID() + ".jpg"; | 24 | String path = IdUtil.fastSimpleUUID() + ".jpg"; |
| 25 | byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); | 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 | System.out.println("访问地址:" + fullPath); | 27 | System.out.println("访问地址:" + fullPath); |
| 28 | if (false) { | 28 | if (false) { |
| 29 | byte[] bytes = client.getContent(path); | 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,7 +230,7 @@ public class FileConfigServiceImpl implements FileConfigService { | ||
| 230 | this.validateFileConfigExists(id); | 230 | this.validateFileConfigExists(id); |
| 231 | // 上传文件 | 231 | // 上传文件 |
| 232 | byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); | 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 | @Override | 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,7 +52,7 @@ public class FileServiceImpl implements FileService { | ||
| 52 | // 上传到文件存储器 | 52 | // 上传到文件存储器 |
| 53 | FileClient client = fileConfigService.getMasterFileClient(); | 53 | FileClient client = fileConfigService.getMasterFileClient(); |
| 54 | Assert.notNull(client, "客户端(master) 不能为空"); | 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 | FileDO file = new FileDO(); | 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,7 +240,7 @@ public class FileConfigServiceImplTest extends BaseDbUnitTest { | ||
| 240 | // mock 获得 Client | 240 | // mock 获得 Client |
| 241 | FileClient fileClient = mock(FileClient.class); | 241 | FileClient fileClient = mock(FileClient.class); |
| 242 | when(fileClientFactory.getFileClient(eq(id))).thenReturn(fileClient); | 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 | assertEquals("https://www.iocoder.cn", fileConfigService.testFileConfig(id)); | 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,7 +79,7 @@ public class FileServiceTest extends BaseDbUnitTest { | ||
| 79 | FileClient client = mock(FileClient.class); | 79 | FileClient client = mock(FileClient.class); |
| 80 | when(fileConfigService.getMasterFileClient()).thenReturn(client); | 80 | when(fileConfigService.getMasterFileClient()).thenReturn(client); |
| 81 | String url = randomString(); | 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 | when(client.getId()).thenReturn(10L); | 83 | when(client.getId()).thenReturn(10L); |
| 84 | String name = "单测文件名"; | 84 | String name = "单测文件名"; |
| 85 | // 调用 | 85 | // 调用 |