增加附件SDK工程

This commit is contained in:
ajaxfan
2020-12-21 16:12:53 +08:00
parent 472224b2ff
commit 2d83008262
18 changed files with 529 additions and 53 deletions

View File

@ -0,0 +1,64 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.chinaunicom.ebtp</groupId>
<artifactId>mall-ebtp-cloud-attachment-sdk</artifactId>
<version>0.0.1</version>
<parent>
<groupId>com.chinaunicom.ebtp</groupId>
<artifactId>mall-ebtp-cloud-parent</artifactId>
<version>0.0.1</version>
<relativePath>../mall-ebtp-cloud-parent</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.chinaunicom.ebtp</groupId>
<artifactId>mall-ebtp-cloud-mvc-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>ettp-host-release</id>
<name>Install to ettp center</name>
<url>http://zentao.jlcucc.com:60000/repository/ettp-releases/</url>
</repository>
</distributionManagement>
</project>

View File

@ -0,0 +1,65 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.api;
import java.io.File;
import java.util.List;
import java.util.Optional;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentDetail;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentEntity;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.UploadObject;
/**
* 附件客户端工具类
*
* @author Ajaxfan
*/
public interface AttachmentClient {
/**
* @return 创建业务ID
*/
Optional<Long> getBusinessId();
/**
* 查询业务ID下的所有附件信息
*
* @param bid
* @return
*/
Optional<AttachmentDetail> findByBusinessId(List<String> businessIdList);
/**
* 查询指定Id的对象信息
*
* @param oid
* @return
*/
Optional<AttachmentEntity> findByObjectId(String objectId);
/**
* 下载业务ID下的所有资源
*
* @param bid
* @return
*/
Optional<byte[]> downloadFilesByBusinessId(String businessId);
/**
* 下载指定的资源数据
*
* @param bid
* @param oid
* @return
*/
Optional<byte[]> downloadFileByObjectId(String objectId);
/**
* 上传资源文件
*
* @param bid
* @param file
* @return
*/
Optional<UploadObject> upload(Long businessId, File file);
}

View File

@ -0,0 +1,105 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.api;
import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.convertor.FileConvertor;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentDetail;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentEntity;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.FeedbackMessage;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.Snowflake;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.UploadObject;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.param.QueryParameter;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.service.QueryService;
/**
* 附件工具客户端默认实现
*
* @author Ajaxfan
*/
@Primary
@Component
public class DefaultAttachmentClient implements AttachmentClient {
/* 附件工具类服务 */
private @Autowired QueryService queryService;
/**
* 获取业务ID
*/
@Override
public Optional<Long> getBusinessId() {
Snowflake snowflake = queryService.getSnokflakId();
if (Objects.isNull(snowflake)) {
return Optional.empty();
}
return Optional.of(snowflake.getId());
}
/**
* 查询指定Id文件的详细信息
*
* @param objectId
* @return
*/
@Override
public Optional<AttachmentEntity> findByObjectId(String objectId) {
AttachmentEntity entity = queryService.getObjectDetail(objectId);
return Optional.ofNullable(entity);
}
/**
* 通过业务ID查找附件信息
*
* @param businessIdList 业务ID列表
* @return
*/
@Override
public Optional<AttachmentDetail> findByBusinessId(List<String> businessIdList) {
QueryParameter param = new QueryParameter(businessIdList);
AttachmentDetail attachemtnDetail = queryService.getAttachmentDetails(param);
return Optional.ofNullable(attachemtnDetail);
}
/**
* @param businessId
* @return
*/
@Override
public Optional<byte[]> downloadFilesByBusinessId(String businessId) {
return Optional.ofNullable(queryService.downloadByBid(businessId));
}
/**
* @param objectId
* @return
*/
@Override
public Optional<byte[]> downloadFileByObjectId(String objectId) {
return Optional.ofNullable(queryService.downloadByOid(objectId));
}
/**
* @param businessId
* @param file
* @return
*/
@Override
public Optional<UploadObject> upload(Long businessId, File file) {
Objects.requireNonNull(businessId);
Objects.requireNonNull(file);
FeedbackMessage feedback = queryService.handleFileUpload(businessId, FileConvertor.toMultipartFile(file));
return Optional.ofNullable(new UploadObject().setId(feedback.getOid()));
}
}

View File

@ -0,0 +1,8 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.config;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignSupportConfig {
}

View File

@ -0,0 +1,12 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.config;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableFeignClients(basePackages = "com.chinaunicom.ebtp.mall.cloud.attachment.sdk")
@ComponentScan(basePackages = "com.chinaunicom.ebtp.mall.cloud.attachment.sdk")
public class SDKAutoConfiguration {
}

View File

@ -0,0 +1,44 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.convertor;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
/**
* 文件转换器
* @author Ajaxfan
*/
public final class FileConvertor {
/**
* 将 File 对象转换为 MultipartFile 对象
* @param file 原始文件对象
* @return
*/
public static MultipartFile toMultipartFile(File file) {
FileItemFactory factory = new DiskFileItemFactory(16, null);
FileItem item = factory.createItem("file", "text/plain", true, file.getName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
try (FileInputStream fis = new FileInputStream(file)) {
try (OutputStream os = item.getOutputStream()) {
while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return new CommonsMultipartFile(item);
}
}

View File

@ -0,0 +1,46 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.fallback;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentDetail;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentEntity;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.FeedbackMessage;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.Snowflake;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.param.QueryParameter;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.service.QueryService;
@Component
public class QueryServiceFallback implements QueryService {
@Override
public Snowflake getSnokflakId() {
return null;
}
@Override
public AttachmentEntity getObjectDetail(String oid) {
return null;
}
@Override
public AttachmentDetail getAttachmentDetails(QueryParameter param) {
return null;
}
@Override
public FeedbackMessage handleFileUpload(Long businessId, MultipartFile file) {
return null;
}
@Override
public byte[] downloadByBid(String bid) {
return null;
}
@Override
public byte[] downloadByOid(String oid) {
return null;
}
}

View File

@ -0,0 +1,14 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model;
import java.util.Hashtable;
import java.util.List;
/**
* 附件信息表单
*
* @author Ajaxfan
*/
public class AttachmentDetail extends Hashtable<String, List<AttachmentEntity>> {
private static final long serialVersionUID = -6579549900874220624L;
}

View File

@ -0,0 +1,19 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class AttachmentEntity {
/* 附件ID */
private String id;
/* 业务ID */
private String bid;
/* 附件名称 */
private String filename;
}

View File

@ -0,0 +1,14 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class FeedbackMessage {
private boolean success;
private String oid;
private String message;
}

View File

@ -0,0 +1,10 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model;
import lombok.Data;
@Data
public class Snowflake {
private Long id;
}

View File

@ -0,0 +1,12 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class UploadObject {
private String id;
}

View File

@ -0,0 +1,18 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.param;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class QueryParameter {
private List<String> bidList;
}

View File

@ -0,0 +1,43 @@
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.config.FeignSupportConfig;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.fallback.QueryServiceFallback;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentDetail;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentEntity;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.FeedbackMessage;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.Snowflake;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.param.QueryParameter;
@FeignClient(value = "core-service-ebtp-updownload", configuration = FeignSupportConfig.class, fallback = QueryServiceFallback.class)
public interface QueryService {
@RequestMapping(method = RequestMethod.GET, value = "v1/business/id")
Snowflake getSnokflakId();
@RequestMapping(method = RequestMethod.GET, value = "v1/attachment/find/oid/{oid}")
AttachmentEntity getObjectDetail(@PathVariable("oid") String oid);
@RequestMapping(method = RequestMethod.POST, value = "v1/attachment/find")
AttachmentDetail getAttachmentDetails(@RequestBody QueryParameter param);
@RequestMapping(value = "/v1/attachment/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
FeedbackMessage handleFileUpload(@RequestPart(value = "businessId") Long businessId,
@RequestPart(value = "file") MultipartFile file);
@GetMapping("/v1/attachment/download/bid/{bid}")
byte[] downloadByBid(@PathVariable("bid") String bid);
@GetMapping("/v1/attachment/download/oid/{oid}")
byte[] downloadByOid(@PathVariable("oid") String oid);
}

View File

@ -0,0 +1,3 @@
# AutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.chinaunicom.ebtp.mall.cloud.attachment.sdk.config.SDKAutoConfiguration

View File

@ -78,6 +78,11 @@
<artifactId>mall-ebtp-cloud-seata-starter</artifactId> <artifactId>mall-ebtp-cloud-seata-starter</artifactId>
<version>0.0.1</version> <version>0.0.1</version>
</dependency> </dependency>
<dependency>
<groupId>com.chinaunicom.ebtp</groupId>
<artifactId>mall-ebtp-cloud-attachment-sdk</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>

20
pom.xml
View File

@ -1,7 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
@ -350,22 +348,17 @@
</goals> </goals>
<configuration> <configuration>
<transformers> <transformers>
<transformer <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource> <resource>META-INF/spring.handlers</resource>
</transformer> </transformer>
<transformer <transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
<resource>META-INF/spring.factories</resource> <resource>META-INF/spring.factories</resource>
</transformer> </transformer>
<transformer <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource> <resource>META-INF/spring.schemas</resource>
</transformer> </transformer>
<transformer <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" /> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${start-class}</mainClass> <mainClass>${start-class}</mainClass>
</transformer> </transformer>
</transformers> </transformers>
@ -394,6 +387,7 @@
<module>mall-ebtp-cloud-seata-starter</module> <module>mall-ebtp-cloud-seata-starter</module>
<module>uboot-common</module> <module>uboot-common</module>
<module>uboot-core</module> <module>uboot-core</module>
<module>mall-ebtp-cloud-attachment-sdk</module>
</modules> </modules>
<organization> <organization>