diff --git a/pom.xml b/pom.xml index 7af30d01ec67ffeb254e1936526796bb7d61320d..40a0e44f12ac21b33e5fd0154f6bb40ef176990c 100644 --- a/pom.xml +++ b/pom.xml @@ -29,13 +29,14 @@ 0.0.9 4.3.1 3.0.6 - 3.4.0 - 7.2.17 + 3.4.2 + 7.2.18 4.0.0 2.0.4 1.7 1.10 5.0.2.RELEASE + 1.26.5 Cairo-SR3 0.2.1.RELEASE 2.0.8.RELEASE @@ -285,6 +286,11 @@ sharding-jdbc-spring-boot-starter ${sharding-sphere.version} + + com.github.tobato + fastdfs-client + ${fastdfs-client.version} + org.springframework.boot diff --git a/zlt-business/file-center/pom.xml b/zlt-business/file-center/pom.xml index a5c542d94e64c8a8b2adf372fe07424472859dd5..532097e2e05da4ab8ff91d9ed8c5af326ba2dbe4 100644 --- a/zlt-business/file-center/pom.xml +++ b/zlt-business/file-center/pom.xml @@ -47,16 +47,6 @@ zlt-swagger2-spring-boot-starter - - com.aliyun.oss - aliyun-sdk-oss - - - - com.qiniu - qiniu-java-sdk - - org.springframework.boot spring-boot-starter-actuator @@ -71,6 +61,22 @@ io.micrometer micrometer-registry-prometheus + + + + com.aliyun.oss + aliyun-sdk-oss + + + + com.qiniu + qiniu-java-sdk + + + + com.github.tobato + fastdfs-client + diff --git a/zlt-business/file-center/src/main/java/com/central/FileCenterApp.java b/zlt-business/file-center/src/main/java/com/central/FileCenterApp.java index ce714a3d62f92e69fbeed0ee4ca7e11160ff8f69..836e96d1b3e0d40c2ae40c256ccca79d1e4aed05 100644 --- a/zlt-business/file-center/src/main/java/com/central/FileCenterApp.java +++ b/zlt-business/file-center/src/main/java/com/central/FileCenterApp.java @@ -1,7 +1,10 @@ package com.central; +import com.central.file.properties.FileServerProperties; +import com.central.file.properties.OssProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** @@ -9,11 +12,10 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient; * @author 作者 owen E-mail: 624191343@qq.com */ @EnableDiscoveryClient +@EnableConfigurationProperties(FileServerProperties.class) @SpringBootApplication public class FileCenterApp { public static void main(String[] args) { - // 固定端口 SpringApplication.run(FileCenterApp.class, args); } - } \ No newline at end of file diff --git a/zlt-business/file-center/src/main/java/com/central/file/config/AliyunOSSAutoConfigure.java b/zlt-business/file-center/src/main/java/com/central/file/config/AliyunOSSAutoConfigure.java new file mode 100644 index 0000000000000000000000000000000000000000..9531e08a04e187c1fe01b522a0051f0a2ce5eb16 --- /dev/null +++ b/zlt-business/file-center/src/main/java/com/central/file/config/AliyunOSSAutoConfigure.java @@ -0,0 +1,61 @@ +package com.central.file.config; + +import com.aliyun.oss.common.auth.DefaultCredentialProvider; +import com.central.file.model.FileInfo; +import com.central.file.properties.FileServerProperties; +import com.central.file.service.impl.AbstractIFileService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.aliyun.oss.OSSClient; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +/** + * 阿里云配置 + * + * @author 作者 owen E-mail: 624191343@qq.com + */ +@Configuration +@ConditionalOnProperty(name = "zlt.file-server.type", havingValue = "aliyun") +public class AliyunOSSAutoConfigure { + @Autowired + private FileServerProperties fileProperties; + + /** + * 阿里云文件存储client + * 只有配置了aliyun.oss.access-key才可以使用 + */ + @Bean + public OSSClient ossClient() { + OSSClient ossClient = new OSSClient(fileProperties.getOss().getEndpoint() + , new DefaultCredentialProvider(fileProperties.getOss().getAccessKey(), fileProperties.getOss().getAccessKeySecret()) + , null); + return ossClient; + } + + @Service + public class AliyunOssServiceImpl extends AbstractIFileService { + @Autowired + private OSSClient ossClient; + + @Override + protected String fileType() { + return fileProperties.getType(); + } + + @Override + protected void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception { + ossClient.putObject(fileProperties.getOss().getBucketName(), fileInfo.getName(), file.getInputStream()); + fileInfo.setUrl(fileProperties.getOss().getDomain() + "/" + fileInfo.getName()); + } + + @Override + protected boolean deleteFile(FileInfo fileInfo) { + ossClient.deleteObject(fileProperties.getOss().getBucketName(), fileInfo.getName()); + return true; + } + } +} diff --git a/zlt-business/file-center/src/main/java/com/central/file/config/AliyunOSSConfig.java b/zlt-business/file-center/src/main/java/com/central/file/config/AliyunOSSConfig.java deleted file mode 100644 index bc807f2a64c1e28a26c54eb832a60c307c7202a4..0000000000000000000000000000000000000000 --- a/zlt-business/file-center/src/main/java/com/central/file/config/AliyunOSSConfig.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.central.file.config; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import com.aliyun.oss.OSSClient; - -/** - * 阿里云配置 - * - * @author 作者 owen E-mail: 624191343@qq.com - */ -@Configuration -public class AliyunOSSConfig { - @Value("${aliyun.oss.endpoint:xxxxx}") - private String endpoint; - @Value("${aliyun.oss.access-key:xxxxx}") - private String accessKeyId; - @Value("${aliyun.oss.accessKeySecret:xxxxx}") - private String accessKeySecret; - - /** - * 阿里云文件存储client - * 只有配置了aliyun.oss.access-key才可以使用 - */ - @Bean - @ConditionalOnProperty(name = "aliyun.oss.access-key", matchIfMissing = true) - public OSSClient ossClient() { - OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret); - return ossClient; - } -} diff --git a/zlt-business/file-center/src/main/java/com/central/file/config/FastdfsAutoConfigure.java b/zlt-business/file-center/src/main/java/com/central/file/config/FastdfsAutoConfigure.java new file mode 100644 index 0000000000000000000000000000000000000000..b41879811851929e5d551329027d243ca5e2c6a8 --- /dev/null +++ b/zlt-business/file-center/src/main/java/com/central/file/config/FastdfsAutoConfigure.java @@ -0,0 +1,53 @@ +package com.central.file.config; + +import cn.hutool.core.util.StrUtil; +import com.central.file.model.FileInfo; +import com.central.file.properties.FileServerProperties; +import com.central.file.service.impl.AbstractIFileService; +import com.github.tobato.fastdfs.domain.fdfs.StorePath; +import com.github.tobato.fastdfs.service.FastFileStorageClient; +import org.apache.commons.io.FilenameUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +/** + * FastDFS配置 + * + * @author zlt + */ +@Configuration +@ConditionalOnProperty(name = "zlt.file-server.type", havingValue = "fastdfs") +public class FastdfsAutoConfigure { + @Autowired + private FileServerProperties fileProperties; + + @Service + public class FastdfsServiceImpl extends AbstractIFileService { + @Autowired + private FastFileStorageClient storageClient; + + @Override + protected String fileType() { + return fileProperties.getType(); + } + + @Override + protected void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception { + StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null); + fileInfo.setUrl(fileProperties.getFdfs().getWebUrl() + "/" + storePath.getFullPath()); + fileInfo.setPath(storePath.getFullPath()); + } + + @Override + protected boolean deleteFile(FileInfo fileInfo) { + if (fileInfo != null && StrUtil.isNotEmpty(fileInfo.getPath())) { + StorePath storePath = StorePath.parseFromUrl(fileInfo.getPath()); + storageClient.deleteFile(storePath.getGroup(), storePath.getPath()); + } + return true; + } + } +} diff --git a/zlt-business/file-center/src/main/java/com/central/file/config/OssServiceFactory.java b/zlt-business/file-center/src/main/java/com/central/file/config/OssServiceFactory.java deleted file mode 100644 index d857e18e4a5ba6f47c363d3c922f1398fc5a1ef8..0000000000000000000000000000000000000000 --- a/zlt-business/file-center/src/main/java/com/central/file/config/OssServiceFactory.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.central.file.config; - -import java.util.EnumMap; -import java.util.HashMap; -import java.util.Map; - -import javax.annotation.PostConstruct; - -import com.central.file.service.IFileService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; - -import com.central.file.model.FileType; - -/** - * FileService工厂
- * 将各个实现类放入map - * - * @author 作者 owen E-mail: 624191343@qq.com - */ -@Configuration -public class OssServiceFactory { - - private Map map = new EnumMap<>(FileType.class); - @Autowired - private IFileService aliyunOssServiceImpl; - - @Autowired - private IFileService qiniuOssServiceImpl; - - @PostConstruct - public void init() { - map.put(FileType.ALIYUN, aliyunOssServiceImpl); - map.put(FileType.QINIU, qiniuOssServiceImpl); - } - - public IFileService getFileService(String fileType) { - return map.get(FileType.valueOf(fileType)); - } -} diff --git a/zlt-business/file-center/src/main/java/com/central/file/config/PasswordConfig.java b/zlt-business/file-center/src/main/java/com/central/file/config/PasswordConfig.java deleted file mode 100644 index 7460a7903578e678990ed624c68c483a68b06740..0000000000000000000000000000000000000000 --- a/zlt-business/file-center/src/main/java/com/central/file/config/PasswordConfig.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.central.file.config; - -import com.central.common.config.DefaultPasswordConfig; -import org.springframework.context.annotation.Configuration; - -/** - * @author zlt - * @date 2019/1/2 - */ -@Configuration -public class PasswordConfig extends DefaultPasswordConfig { -} diff --git a/zlt-business/file-center/src/main/java/com/central/file/config/QiniuOSSAutoConfigure.java b/zlt-business/file-center/src/main/java/com/central/file/config/QiniuOSSAutoConfigure.java new file mode 100644 index 0000000000000000000000000000000000000000..849f928af9fb8c48aecf30c51cd6be1d699834e4 --- /dev/null +++ b/zlt-business/file-center/src/main/java/com/central/file/config/QiniuOSSAutoConfigure.java @@ -0,0 +1,101 @@ +package com.central.file.config; + +import com.central.file.model.FileInfo; +import com.central.file.properties.FileServerProperties; +import com.central.file.service.impl.AbstractIFileService; +import com.qiniu.common.QiniuException; +import com.qiniu.http.Response; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.qiniu.common.Zone; +import com.qiniu.storage.BucketManager; +import com.qiniu.storage.UploadManager; +import com.qiniu.util.Auth; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +/** + * 七牛云配置 + * + * @author 作者 owen E-mail: 624191343@qq.com + */ +@Configuration +@ConditionalOnProperty(name = "zlt.file-server.type", havingValue = "qiniu") +public class QiniuOSSAutoConfigure { + @Autowired + private FileServerProperties fileProperties; + + /** + * 华东机房 + */ + @Bean + public com.qiniu.storage.Configuration qiniuConfig() { + return new com.qiniu.storage.Configuration(Zone.zone2()); + } + + /** + * 构建一个七牛上传工具实例 + */ + @Bean + public UploadManager uploadManager() { + return new UploadManager(qiniuConfig()); + } + + /** + * 认证信息实例 + * + * @return + */ + @Bean + public Auth auth() { + return Auth.create(fileProperties.getOss().getAccessKey(), fileProperties.getOss().getAccessKeySecret()); + } + + /** + * 构建七牛空间管理实例 + */ + @Bean + public BucketManager bucketManager() { + return new BucketManager(auth(), qiniuConfig()); + } + + @Service + public class QiniuOssServiceImpl extends AbstractIFileService { + @Autowired + private UploadManager uploadManager; + @Autowired + private BucketManager bucketManager; + @Autowired + private Auth auth; + + @Override + protected String fileType() { + return fileProperties.getType(); + } + + @Override + protected void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception { + // 调用put方法上传 + uploadManager.put(file.getBytes(), fileInfo.getName(), auth.uploadToken(fileProperties.getOss().getBucketName())); + fileInfo.setUrl(fileProperties.getOss().getEndpoint() + "/" + fileInfo.getName()); + fileInfo.setPath(fileProperties.getOss().getEndpoint() + "/" + fileInfo.getName()); + } + + @Override + protected boolean deleteFile(FileInfo fileInfo) { + try { + Response response = bucketManager.delete(fileProperties.getOss().getBucketName(), fileInfo.getPath()); + int retry = 0; + while (response.needRetry() && retry++ < 3) { + response = bucketManager.delete(fileProperties.getOss().getBucketName(), fileInfo.getPath()); + } + } catch (QiniuException e) { + return false; + } + return true; + } + } +} diff --git a/zlt-business/file-center/src/main/java/com/central/file/config/QiniuOSSConfig.java b/zlt-business/file-center/src/main/java/com/central/file/config/QiniuOSSConfig.java deleted file mode 100644 index 3eada458d801e630000a2a365b780ffe717b6cf7..0000000000000000000000000000000000000000 --- a/zlt-business/file-center/src/main/java/com/central/file/config/QiniuOSSConfig.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.central.file.config; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import com.qiniu.common.Zone; -import com.qiniu.storage.BucketManager; -import com.qiniu.storage.UploadManager; -import com.qiniu.util.Auth; - -/** - * 七牛云配置 - * - * @author 作者 owen E-mail: 624191343@qq.com - */ -@Configuration -public class QiniuOSSConfig { - @Value("${qiniu.oss.access-key:xxxxx}") - private String accessKeyId; - @Value("${qiniu.oss.accessKeySecret:xxxxx}") - private String accessKeySecret; - - /** - * 华东机房 - */ - @Bean - public com.qiniu.storage.Configuration qiniuConfig() { - return new com.qiniu.storage.Configuration(Zone.zone2()); - } - - /** - * 构建一个七牛上传工具实例 - */ - @Bean - public UploadManager uploadManager() { - return new UploadManager(qiniuConfig()); - } - - /** - * 认证信息实例 - * - * @return - */ - @Bean - public Auth auth() { - return Auth.create(accessKeyId, accessKeySecret); - } - - /** - * 构建七牛空间管理实例 - */ - @Bean - public BucketManager bucketManager() { - return new BucketManager(auth(), qiniuConfig()); - } -} diff --git a/zlt-business/file-center/src/main/java/com/central/file/controller/FileController.java b/zlt-business/file-center/src/main/java/com/central/file/controller/FileController.java index 9423fde8995813cc27c3ef3846549886d502fc0f..93c784f921933459b0b8d53cc87d1ec24123f38c 100644 --- a/zlt-business/file-center/src/main/java/com/central/file/controller/FileController.java +++ b/zlt-business/file-center/src/main/java/com/central/file/controller/FileController.java @@ -4,7 +4,6 @@ import java.util.Map; import com.central.common.model.Result; import com.central.file.service.IFileService; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -14,21 +13,19 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.central.common.model.PageResult; -import com.central.file.config.OssServiceFactory; import com.central.file.model.FileInfo; -import com.central.file.model.FileType; + +import javax.annotation.Resource; /** - * 文件上传 同步oss db双写 目前仅实现了阿里云,七牛云 - * 参考src/main/view/upload.html + * 文件上传 * * @author 作者 owen E-mail: 624191343@qq.com */ @RestController public class FileController { - - @Autowired - private OssServiceFactory fileServiceFactory; + @Resource + private IFileService fileService; /** * 文件上传 @@ -40,8 +37,6 @@ public class FileController { */ @PostMapping("/files-anon") public FileInfo upload(@RequestParam("file") MultipartFile file) throws Exception { - String fileType = FileType.QINIU.toString(); - IFileService fileService = fileServiceFactory.getFileService(fileType); return fileService.upload(file); } @@ -53,11 +48,7 @@ public class FileController { @DeleteMapping("/files/{id}") public Result delete(@PathVariable String id) { try { - FileInfo fileInfo = fileServiceFactory.getFileService(FileType.QINIU.toString()).getById(id); - if (fileInfo != null) { - IFileService fileService = fileServiceFactory.getFileService(fileInfo.getSource()); - fileService.removeById(fileInfo); - } + fileService.delete(id); return Result.succeed("操作成功"); } catch (Exception ex) { return Result.failed("操作失败"); @@ -72,6 +63,6 @@ public class FileController { */ @GetMapping("/files") public PageResult findFiles(@RequestParam Map params) { - return fileServiceFactory.getFileService(FileType.QINIU.toString()).findList(params); + return fileService.findList(params); } } diff --git a/zlt-business/file-center/src/main/java/com/central/file/model/FileType.java b/zlt-business/file-center/src/main/java/com/central/file/model/FileType.java deleted file mode 100644 index 78c106a81decff6d8f8a10418091e52c6bde30d9..0000000000000000000000000000000000000000 --- a/zlt-business/file-center/src/main/java/com/central/file/model/FileType.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.central.file.model; - -/** - * 仅支持阿里云 oss ,七牛云等 - * - * @author 作者 owen E-mail: 624191343@qq.com - */ -public enum FileType { - //七牛 - QINIU, - //阿里云 - ALIYUN -} diff --git a/zlt-business/file-center/src/main/java/com/central/file/properties/FdfsProperties.java b/zlt-business/file-center/src/main/java/com/central/file/properties/FdfsProperties.java new file mode 100644 index 0000000000000000000000000000000000000000..f2a3c1726cf3e70f9d5a4c5f0c859e9027d6dc48 --- /dev/null +++ b/zlt-business/file-center/src/main/java/com/central/file/properties/FdfsProperties.java @@ -0,0 +1,16 @@ +package com.central.file.properties; + +import lombok.Getter; +import lombok.Setter; + +/** + * @author zlt + */ +@Setter +@Getter +public class FdfsProperties { + /** + * fastdfs的http访问地址 + */ + private String webUrl; +} diff --git a/zlt-business/file-center/src/main/java/com/central/file/properties/FileServerProperties.java b/zlt-business/file-center/src/main/java/com/central/file/properties/FileServerProperties.java new file mode 100644 index 0000000000000000000000000000000000000000..dca0c508267825dd16bdba413a636e3e4c7c34c4 --- /dev/null +++ b/zlt-business/file-center/src/main/java/com/central/file/properties/FileServerProperties.java @@ -0,0 +1,33 @@ +package com.central.file.properties; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.cloud.context.config.annotation.RefreshScope; + +/** + * @author zlt + */ +@Setter +@Getter +@ConfigurationProperties(prefix = "zlt.file-server") +@RefreshScope +public class FileServerProperties { + /** + * 为以下3个值,指定不同的自动化配置 + * qiniu:七牛oss + * aliyun:阿里云oss + * fastdfs:本地部署的fastDFS + */ + private String type; + + /** + * oss配置 + */ + OssProperties oss = new OssProperties(); + + /** + * fastDFS配置 + */ + FdfsProperties fdfs = new FdfsProperties(); +} diff --git a/zlt-business/file-center/src/main/java/com/central/file/properties/OssProperties.java b/zlt-business/file-center/src/main/java/com/central/file/properties/OssProperties.java new file mode 100644 index 0000000000000000000000000000000000000000..f3300b3ba42d2867a7dc593aeb1b58cb97e12016 --- /dev/null +++ b/zlt-business/file-center/src/main/java/com/central/file/properties/OssProperties.java @@ -0,0 +1,32 @@ +package com.central.file.properties; + +import lombok.Getter; +import lombok.Setter; + +/** + * @author zlt + */ +@Setter +@Getter +public class OssProperties { + /** + * 密钥key + */ + private String accessKey; + /** + * 密钥密码 + */ + private String accessKeySecret; + /** + * 端点 + */ + private String endpoint; + /** + * bucket名称 + */ + private String bucketName; + /** + * 说明 + */ + private String domain; +} diff --git a/zlt-business/file-center/src/main/java/com/central/file/service/IFileService.java b/zlt-business/file-center/src/main/java/com/central/file/service/IFileService.java index a67841ff8652c10d3cdfad17b3ebd7bbf2fea5fc..6f97a8614f469e8632d414f7d1f9fb3d2e35a4c1 100644 --- a/zlt-business/file-center/src/main/java/com/central/file/service/IFileService.java +++ b/zlt-business/file-center/src/main/java/com/central/file/service/IFileService.java @@ -9,7 +9,7 @@ import org.springframework.web.multipart.MultipartFile; import com.central.file.model.FileInfo; /** - * 文件service 目前仅支持阿里云oss,七牛云 + * 文件service * * @author 作者 owen E-mail: 624191343@qq.com */ @@ -17,4 +17,6 @@ public interface IFileService extends IService { FileInfo upload(MultipartFile file ) throws Exception; PageResult findList(Map params); + + void delete(String id); } diff --git a/zlt-business/file-center/src/main/java/com/central/file/service/impl/AbstractIFileService.java b/zlt-business/file-center/src/main/java/com/central/file/service/impl/AbstractIFileService.java index 42d0f64d297cfd5703f79f439188ac1394433706..caf622c85286a410d43ff15bb8f6b12491b88bf6 100644 --- a/zlt-business/file-center/src/main/java/com/central/file/service/impl/AbstractIFileService.java +++ b/zlt-business/file-center/src/main/java/com/central/file/service/impl/AbstractIFileService.java @@ -11,7 +11,6 @@ import org.springframework.web.multipart.MultipartFile; import com.central.common.model.PageResult; import com.central.file.mapper.FileMapper; import com.central.file.model.FileInfo; -import com.central.file.model.FileType; import com.central.file.service.IFileService; import com.central.file.utils.FileUtil; @@ -19,56 +18,73 @@ import lombok.extern.slf4j.Slf4j; /** * AbstractIFileService 抽取类 - * 根据filetype 实例化具体oss对象 + * 根据zlt.file-server.type 实例化具体对象 * * @author 作者 owen E-mail: 624191343@qq.com */ @Slf4j public abstract class AbstractIFileService extends ServiceImpl implements IFileService { - @Override - public FileInfo upload(MultipartFile file ) throws Exception { - FileInfo fileInfo = FileUtil.getFileInfo(file); - FileInfo oldFileInfo = baseMapper.selectById(fileInfo.getId()); - if (oldFileInfo != null) { - return oldFileInfo; - } - if (!fileInfo.getName().contains(".")) { - throw new IllegalArgumentException("缺少后缀名"); - } - uploadFile(file, fileInfo); - fileInfo.setSource(fileType().name());// 设置文件来源 - baseMapper.insert(fileInfo);// 将文件信息保存到数据库 + private static final String FILE_SPLIT = "."; - return fileInfo; - } + @Override + public FileInfo upload(MultipartFile file) throws Exception { + FileInfo fileInfo = FileUtil.getFileInfo(file); + FileInfo oldFileInfo = baseMapper.selectById(fileInfo.getId()); + if (oldFileInfo != null) { + return oldFileInfo; + } + if (!fileInfo.getName().contains(FILE_SPLIT)) { + throw new IllegalArgumentException("缺少后缀名"); + } + uploadFile(file, fileInfo); + // 设置文件来源 + fileInfo.setSource(fileType()); + // 将文件信息保存到数据库 + baseMapper.insert(fileInfo); - /** - * 文件来源 - * - * @return - */ - protected abstract FileType fileType(); + return fileInfo; + } - /** - * 上传文件 - * - * @param file - * @param fileInfo - */ - protected abstract void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception; + /** + * 文件来源 + * + * @return + */ + protected abstract String fileType(); - /** - * 删除文件资源 - * - * @param fileInfo - * @return - */ - protected abstract boolean deleteFile(FileInfo fileInfo); + /** + * 上传文件 + * + * @param file + * @param fileInfo + */ + protected abstract void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception; - @Override - public PageResult findList(Map params){ - Page page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit")); + /** + * 删除文件 + * @param id 文件id + */ + @Override + public void delete(String id) { + FileInfo fileInfo = baseMapper.selectById(id); + if (fileInfo != null) { + baseMapper.deleteById(fileInfo.getId()); + this.deleteFile(fileInfo); + } + } + + /** + * 删除文件资源 + * + * @param fileInfo + * @return + */ + protected abstract boolean deleteFile(FileInfo fileInfo); + + @Override + public PageResult findList(Map params) { + Page page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit")); List list = baseMapper.findList(page, params); - return PageResult.builder().data(list).code(0).count(page.getTotal()).build(); - } + return PageResult.builder().data(list).code(0).count(page.getTotal()).build(); + } } diff --git a/zlt-business/file-center/src/main/java/com/central/file/service/impl/AliyunOssServiceImplI.java b/zlt-business/file-center/src/main/java/com/central/file/service/impl/AliyunOssServiceImplI.java deleted file mode 100644 index 452b90610349c0fcb6edf107c8607bb8e4b962d0..0000000000000000000000000000000000000000 --- a/zlt-business/file-center/src/main/java/com/central/file/service/impl/AliyunOssServiceImplI.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.central.file.service.impl; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; - -import com.aliyun.oss.OSSClient; -import com.central.file.model.FileInfo; -import com.central.file.model.FileType; - -/** - * 阿里云oss存储文件 - * - * @author 作者 owen E-mail: 624191343@qq.com - */ -@Service("aliyunOssServiceImpl") -public class AliyunOssServiceImplI extends AbstractIFileService { - @Autowired - private OSSClient ossClient; - @Value("${aliyun.oss.bucketName:xxxxx}") - private String bucketName; - @Value("${aliyun.oss.domain:xxxxx}") - private String domain; - - @Override - protected FileType fileType() { - return FileType.ALIYUN; - } - - @Override - protected void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception { - ossClient.putObject(bucketName, fileInfo.getName(), file.getInputStream()); - fileInfo.setUrl(domain + "/" + fileInfo.getName()); - } - - @Override - protected boolean deleteFile(FileInfo fileInfo) { - ossClient.deleteObject(bucketName, fileInfo.getName()); - return true; - } -} diff --git a/zlt-business/file-center/src/main/java/com/central/file/service/impl/QiniuOssServiceImplI.java b/zlt-business/file-center/src/main/java/com/central/file/service/impl/QiniuOssServiceImplI.java deleted file mode 100644 index 3f35a01a5839709d63ce21299570e3b6bb6b4b84..0000000000000000000000000000000000000000 --- a/zlt-business/file-center/src/main/java/com/central/file/service/impl/QiniuOssServiceImplI.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.central.file.service.impl; - -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; - -import com.central.file.model.FileInfo; -import com.central.file.model.FileType; -import com.qiniu.common.QiniuException; -import com.qiniu.http.Response; -import com.qiniu.storage.BucketManager; -import com.qiniu.storage.UploadManager; -import com.qiniu.util.Auth; -import com.qiniu.util.StringMap; - -/** - * 七牛云oss存储文件 - * - * @author 作者 owen E-mail: 624191343@qq.com - */ -@Slf4j -@Service("qiniuOssServiceImpl") -public class QiniuOssServiceImplI extends AbstractIFileService implements InitializingBean { - @Autowired - private UploadManager uploadManager; - @Autowired - private BucketManager bucketManager; - @Autowired - private Auth auth; - @Value("${qiniu.oss.bucketName:xxxxx}") - private String bucket; - @Value("${qiniu.oss.endpoint:xxxxx}") - private String endpoint; - private StringMap putPolicy; - - @Override - protected FileType fileType() { - return FileType.QINIU; - } - - @Override - protected void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception { - // 调用put方法上传 - uploadManager.put(file.getBytes(), fileInfo.getName(), auth.uploadToken(bucket)); - fileInfo.setUrl(endpoint + "/" + fileInfo.getName()); - fileInfo.setPath(endpoint + "/" + fileInfo.getName()); - } - - @Override - protected boolean deleteFile(FileInfo fileInfo) { - try { - Response response = bucketManager.delete(this.bucket, fileInfo.getPath()); - int retry = 0; - while (response.needRetry() && retry++ < 3) { - response = bucketManager.delete(bucket, fileInfo.getPath()); - } - } catch (QiniuException e) { - return false; - } - return true; - } - - @Override - public void afterPropertiesSet() throws Exception { - this.putPolicy = new StringMap(); - putPolicy.put("returnBody", - "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"width\":$(imageInfo.width), \"height\":${imageInfo.height}}"); - } -} diff --git a/zlt-business/file-center/src/main/java/com/central/file/utils/FileUtil.java b/zlt-business/file-center/src/main/java/com/central/file/utils/FileUtil.java index 8efe3d7bc6312650e9c9a7b786fc4cc3649047db..c4f244296490a320fbf019b7fc5d75ca4d4fe357 100644 --- a/zlt-business/file-center/src/main/java/com/central/file/utils/FileUtil.java +++ b/zlt-business/file-center/src/main/java/com/central/file/utils/FileUtil.java @@ -24,7 +24,8 @@ public class FileUtil { public static FileInfo getFileInfo(MultipartFile file) throws Exception { String md5 = fileMd5(file.getInputStream()); FileInfo fileInfo = new FileInfo(); - fileInfo.setId(md5);// 将文件的md5设置为文件表的id + // 将文件的md5设置为文件表的id + fileInfo.setId(md5); fileInfo.setName(file.getOriginalFilename()); fileInfo.setContentType(file.getContentType()); fileInfo.setIsImg(fileInfo.getContentType().startsWith("image/")); diff --git a/zlt-business/file-center/src/main/resources/application.yml b/zlt-business/file-center/src/main/resources/application.yml index 47637e355e20f9f16e844645c5091322b72a2731..922b8d30fd5d67980db6506a92cd502f07cbb246 100644 --- a/zlt-business/file-center/src/main/resources/application.yml +++ b/zlt-business/file-center/src/main/resources/application.yml @@ -1,18 +1,3 @@ -#aliyun: -# oss: -# access-key: 你的密钥 -# accessKeySecret: 你的密钥 -# endpoint: 你的端点 -# bucketName: 你的名称 -# domain: 你的说明 - -qiniu: - oss: - access-key: tpi8mObnfzZi4ggBX8Bw7zydjoTQ0WeML3RkPKsX - accessKeySecret: HZBXmSyUTy-haYp0KbBTtsil-GoKjVS2kDKT8Yow - endpoint: http://pkqtmn0p1.bkt.clouddn.com - bucketName: public-oss - spring: datasource: url: jdbc:mysql://${zlt.datasource.ip}:3306/file_center?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull @@ -27,10 +12,28 @@ mybatis-plus: global-config: db-config: id-type: INPUT + zlt: + file-server: + type: fastdfs + fdfs: + web-url: ${zlt.fdfs.web-url} + #oss配置 + #oss: + # access-key: tpi8mObnfzZi4ggBX8Bw7zydjoTQ0WeML3RkPKsX + # accessKeySecret: HZBXmSyUTy-haYp0KbBTtsil-GoKjVS2kDKT8Yow + # endpoint: http://pkqtmn0p1.bkt.clouddn.com + # bucketName: public-oss + # domain: swagger: enabled: true title: 文件中心 description: 文件中心接口文档 version: 1.0 - base-package: com.central.file.controller \ No newline at end of file + base-package: com.central.file.controller + +#fastDFS配置 +fdfs: + soTimeout: 1500 + connectTimeout: 600 + trackerList: ${zlt.fdfs.trackerList} \ No newline at end of file diff --git a/zlt-config/src/main/resources/application-dev.properties b/zlt-config/src/main/resources/application-dev.properties index cbdd2f46d22a2b99edb3e6d0113c03ba42014c37..058aebe070666cc2cf2a31ecb4ce7983fca1a29d 100644 --- a/zlt-config/src/main/resources/application-dev.properties +++ b/zlt-config/src/main/resources/application-dev.properties @@ -14,4 +14,8 @@ zlt.elasticsearch.cluster-name=my-es zlt.elasticsearch.cluster-nodes=192.168.28.130:9300 ##### sentinel配置 -zlt.sentinel.dashboard=127.0.0.1:6999 \ No newline at end of file +zlt.sentinel.dashboard=127.0.0.1:6999 + +##### fastDFS配置 +zlt.fdfs.trackerList=192.168.28.130:22122 +zlt.fdfs.web-url=192.168.28.130 \ No newline at end of file diff --git a/zlt-config/src/main/resources/application-prod.properties b/zlt-config/src/main/resources/application-prod.properties index b18008792ec44eac4fc59d7b45ba710bc88f77b8..29818800d9652c49274171471a6d1675801d5801 100644 --- a/zlt-config/src/main/resources/application-prod.properties +++ b/zlt-config/src/main/resources/application-prod.properties @@ -14,4 +14,8 @@ zlt.elasticsearch.cluster-name=my-es zlt.elasticsearch.cluster-nodes=47.107.114.25:9300 ##### sentinel配置 -zlt.sentinel.dashboard=120.78.94.191:6999 \ No newline at end of file +zlt.sentinel.dashboard=120.78.94.191:6999 + +##### fastDFS配置 +zlt.fdfs.trackerList[0]=47.107.114.25:22122 +zlt.fdfs.web-url=47.107.114.25 \ No newline at end of file diff --git a/zlt-config/src/main/resources/application-test.properties b/zlt-config/src/main/resources/application-test.properties index e6da190032d613b003a7ff58d0f2dabe6a24ce59..ed22f796040d5aa9f99c56ddd893abc3b2dcd863 100644 --- a/zlt-config/src/main/resources/application-test.properties +++ b/zlt-config/src/main/resources/application-test.properties @@ -14,4 +14,8 @@ zlt.elasticsearch.cluster-name=my-es zlt.elasticsearch.cluster-nodes=47.107.114.25:9300 ##### sentinel配置 -zlt.sentinel.dashboard=120.78.94.191:6999 \ No newline at end of file +zlt.sentinel.dashboard=120.78.94.191:6999 + +##### fastDFS配置 +zlt.fdfs.trackerList[0]=47.107.114.25:22122 +zlt.fdfs.web-url=47.107.114.25 \ No newline at end of file