sdk 新增文件复制方法
This commit is contained in:
@ -1,106 +1,122 @@
|
|||||||
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.api;
|
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.AttachmentDetail;
|
||||||
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentEntity;
|
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentEntity;
|
||||||
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.DownloadEntity;
|
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.DownloadEntity;
|
||||||
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.UploadObject;
|
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.UploadObject;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 附件客户端工具类
|
* 附件客户端工具类
|
||||||
*
|
*
|
||||||
* @author Ajaxfan
|
* @author Ajaxfan
|
||||||
*/
|
*/
|
||||||
public interface AttachmentClient {
|
public interface AttachmentClient {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return 创建业务ID
|
* @return 创建业务ID
|
||||||
*/
|
*/
|
||||||
Optional<String> getBusinessId();
|
Optional<String> getBusinessId();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询业务ID下的所有附件信息
|
* 查询业务ID下的所有附件信息
|
||||||
*
|
*
|
||||||
* @param bid
|
* @param bid
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
Optional<AttachmentDetail> findByBusinessId(List<String> businessIdList);
|
Optional<AttachmentDetail> findByBusinessId(List<String> businessIdList);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询指定Id的对象信息
|
* 查询指定Id的对象信息
|
||||||
*
|
*
|
||||||
* @param oid
|
* @param oid
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
Optional<AttachmentEntity> findByObjectId(String objectId);
|
Optional<AttachmentEntity> findByObjectId(String objectId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载业务ID下的所有资源
|
* 下载业务ID下的所有资源
|
||||||
*
|
*
|
||||||
* @param bid
|
* @param bid
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
Optional<byte[]> downloadFilesByBusinessId(String businessId);
|
Optional<byte[]> downloadFilesByBusinessId(String businessId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载业务ID下的所有资源
|
* 下载业务ID下的所有资源
|
||||||
*
|
*
|
||||||
* @param bid
|
* @param bid
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
Optional<DownloadEntity> downloadOriginalFilesByBusinessId(String businessId);
|
Optional<DownloadEntity> downloadOriginalFilesByBusinessId(String businessId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载指定的资源数据
|
* 下载指定的资源数据
|
||||||
*
|
*
|
||||||
* @param bid
|
* @param bid
|
||||||
* @param oid
|
* @param oid
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
Optional<byte[]> downloadFileByObjectId(String objectId);
|
Optional<byte[]> downloadFileByObjectId(String objectId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载指定的资源数据
|
* 下载指定的资源数据
|
||||||
*
|
*
|
||||||
* @param bid
|
* @param bid
|
||||||
* @param oid
|
* @param oid
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
Optional<DownloadEntity> downloadOriginalFileByObjectId(String objectId);
|
Optional<DownloadEntity> downloadOriginalFileByObjectId(String objectId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传资源文件
|
* 上传资源文件
|
||||||
*
|
*
|
||||||
* @param bid
|
* @param bid
|
||||||
* @param file
|
* @param file
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
Optional<UploadObject> upload(String businessId, File file);
|
Optional<UploadObject> upload(String businessId, File file);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传资源文件
|
* 上传资源文件
|
||||||
*
|
*
|
||||||
* @param bid
|
* @param bid
|
||||||
* @param file
|
* @param file
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
Optional<UploadObject> upload(String businessId, String filename, byte[] array);
|
Optional<UploadObject> upload(String businessId, String filename, byte[] array);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据业务id删除附件
|
* 根据业务id删除附件
|
||||||
*
|
*
|
||||||
* @param businessId
|
* @param businessId
|
||||||
*/
|
*/
|
||||||
boolean deleteByBid(String businessId);
|
boolean deleteByBid(String businessId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据对象id删除附件
|
* 根据对象id删除附件
|
||||||
*
|
*
|
||||||
* @param objectId
|
* @param objectId
|
||||||
*/
|
*/
|
||||||
String deleteByOid(String objectId);
|
String deleteByOid(String objectId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据业务id复制附件
|
||||||
|
*
|
||||||
|
* @param oldBusinessId
|
||||||
|
* @param newBusinessId
|
||||||
|
*/
|
||||||
|
AttachmentDetail copyByBid(String oldBusinessId, String newBusinessId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据对象id复制附件
|
||||||
|
*
|
||||||
|
* @param objectId
|
||||||
|
* @param newBusinessId
|
||||||
|
*/
|
||||||
|
Optional<AttachmentEntity> copyByOid(String objectId, String newBusinessId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,6 @@
|
|||||||
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.api;
|
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.api;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import cn.hutool.core.util.IdUtil;
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.zip.ZipEntry;
|
|
||||||
import java.util.zip.ZipOutputStream;
|
|
||||||
|
|
||||||
import javax.validation.constraints.NotNull;
|
|
||||||
|
|
||||||
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.convertor.FileConvertor;
|
||||||
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.convertor.ModelConvertor;
|
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.convertor.ModelConvertor;
|
||||||
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentDetail;
|
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentDetail;
|
||||||
@ -23,14 +8,24 @@ import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentEntity;
|
|||||||
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.DownloadEntity;
|
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.DownloadEntity;
|
||||||
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.UploadObject;
|
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.UploadObject;
|
||||||
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.service.DocumentCenterService;
|
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.service.DocumentCenterService;
|
||||||
|
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo.SysStorageVO;
|
||||||
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo.query.QueryResult;
|
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo.query.QueryResult;
|
||||||
|
|
||||||
import cn.hutool.core.util.IdUtil;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 附件工具客户端默认实现
|
* 附件工具客户端默认实现
|
||||||
*
|
*
|
||||||
* @author Ajaxfan
|
* @author Ajaxfan
|
||||||
*/
|
*/
|
||||||
@Primary
|
@Primary
|
||||||
@ -38,259 +33,304 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class DefaultAttachmentClient implements AttachmentClient {
|
public class DefaultAttachmentClient implements AttachmentClient {
|
||||||
|
|
||||||
/* 附件工具类服务 */
|
/* 附件工具类服务 */
|
||||||
private @Autowired DocumentCenterService documentCenterService;
|
private @Autowired
|
||||||
|
DocumentCenterService documentCenterService;
|
||||||
|
|
||||||
/* 模型转换器 */
|
/* 模型转换器 */
|
||||||
private @Autowired ModelConvertor modelConvertor;
|
private @Autowired
|
||||||
|
ModelConvertor modelConvertor;
|
||||||
|
|
||||||
/* 文件转换器 */
|
/* 文件转换器 */
|
||||||
private @Autowired FileConvertor fileConvertor;
|
private @Autowired
|
||||||
|
FileConvertor fileConvertor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成业务id
|
* 生成业务id
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Optional<String> getBusinessId() {
|
public Optional<String> getBusinessId() {
|
||||||
return Optional.of(String.format("{\"id\":\"%s\"}", IdUtil.getSnowflake(23, 16).nextIdStr()));
|
return Optional.of(String.format("{\"id\":\"%s\"}", IdUtil.getSnowflake(23, 16).nextIdStr()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询指定Id文件的详细信息
|
* 查询指定Id文件的详细信息
|
||||||
*
|
*
|
||||||
* @param objectId
|
* @param objectId
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Optional<AttachmentEntity> findByObjectId(String objectId) {
|
public Optional<AttachmentEntity> findByObjectId(String objectId) {
|
||||||
return modelConvertor.toAttachmentEntity(documentCenterService.getObjectDetail(objectId));
|
return modelConvertor.toAttachmentEntity(documentCenterService.getObjectDetail(objectId));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据业务id列表查询详单
|
* 根据业务id列表查询详单
|
||||||
*
|
*
|
||||||
* @param businessIdList
|
* @param businessIdList
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Optional<AttachmentDetail> findByBusinessId(List<String> businessIdList) {
|
public Optional<AttachmentDetail> findByBusinessId(List<String> businessIdList) {
|
||||||
log.debug("query by bids: {}", businessIdList);
|
log.debug("query by bids: {}", businessIdList);
|
||||||
String json = documentCenterService.fetchDetails(businessIdList);
|
String json = documentCenterService.fetchDetails(businessIdList);
|
||||||
log.debug("document center return: {}", json);
|
log.debug("document center return: {}", json);
|
||||||
|
|
||||||
Optional<QueryResult> op = modelConvertor.toQueryResult(json);
|
Optional<QueryResult> op = modelConvertor.toQueryResult(json);
|
||||||
|
|
||||||
if (op.isPresent()) {
|
if (op.isPresent()) {
|
||||||
AttachmentDetail detail = new AttachmentDetail();
|
AttachmentDetail detail = new AttachmentDetail();
|
||||||
|
|
||||||
Optional.ofNullable(op.get().getData()).ifPresent(data -> {
|
Optional.ofNullable(op.get().getData()).ifPresent(data -> {
|
||||||
data.forEach(item -> {
|
data.forEach(item -> {
|
||||||
detail.add(new AttachmentEntity().setBid(item.getObjectId()).setId(item.getFileId())
|
detail.add(new AttachmentEntity().setBid(item.getObjectId()).setId(item.getFileId())
|
||||||
.setFilename(item.getOriginalName()).setKey(item.getFileName()));
|
.setFilename(item.getOriginalName()).setKey(item.getFileName()));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return Optional.of(detail);
|
return Optional.of(detail);
|
||||||
}
|
}
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过对象id下载附件
|
* 通过对象id下载附件
|
||||||
*
|
*
|
||||||
* @param objectId
|
* @param objectId
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Optional<byte[]> downloadFileByObjectId(String objectId) {
|
public Optional<byte[]> downloadFileByObjectId(String objectId) {
|
||||||
return Optional.ofNullable(documentCenterService.download(objectId));
|
return Optional.ofNullable(documentCenterService.download(objectId));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据业务id下载文件
|
* 根据业务id下载文件
|
||||||
*
|
*
|
||||||
* @param businessId
|
* @param businessId
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Optional<byte[]> downloadFilesByBusinessId(String businessId) {
|
public Optional<byte[]> downloadFilesByBusinessId(String businessId) {
|
||||||
/** 文档中心不提提供通过业务id下载。因此,需要先通过业务id获取对象列表,然后下载每一个对象资源 */
|
/** 文档中心不提提供通过业务id下载。因此,需要先通过业务id获取对象列表,然后下载每一个对象资源 */
|
||||||
Optional<AttachmentDetail> op = findByBusinessId(Arrays.asList(businessId));
|
Optional<AttachmentDetail> op = findByBusinessId(Arrays.asList(businessId));
|
||||||
|
|
||||||
if (op.isPresent()) {
|
if (op.isPresent()) {
|
||||||
List<AttachmentEntity> list = op.get().get(businessId);
|
List<AttachmentEntity> list = op.get().get(businessId);
|
||||||
|
|
||||||
// 如果业务id下只包含单独一个文件,则直接返回该文件数据
|
// 如果业务id下只包含单独一个文件,则直接返回该文件数据
|
||||||
if (list.size() == 1) {
|
if (list.size() == 1) {
|
||||||
return downloadFileByObjectId(list.get(0).getId());
|
return downloadFileByObjectId(list.get(0).getId());
|
||||||
}
|
}
|
||||||
// 如果大于1个则返回压缩包数据流
|
// 如果大于1个则返回压缩包数据流
|
||||||
else if (list.size() > 1) {
|
else if (list.size() > 1) {
|
||||||
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
|
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
|
||||||
// 创建压缩文件流
|
// 创建压缩文件流
|
||||||
createCompressionStream(list, out);
|
createCompressionStream(list, out);
|
||||||
|
|
||||||
return Optional.of(out.toByteArray());
|
return Optional.of(out.toByteArray());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error(e.getMessage());
|
log.error(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载原始对象格式(包含原始文件的基本信息)
|
* 下载原始对象格式(包含原始文件的基本信息)
|
||||||
*
|
*
|
||||||
* @param objectId
|
* @param objectId
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Optional<DownloadEntity> downloadOriginalFileByObjectId(String objectId) {
|
public Optional<DownloadEntity> downloadOriginalFileByObjectId(String objectId) {
|
||||||
// 获取对象文件原数据
|
// 获取对象文件原数据
|
||||||
Optional<DownloadEntity> op = modelConvertor.toDownloadEntity(documentCenterService.getObjectDetail(objectId));
|
Optional<DownloadEntity> op = modelConvertor.toDownloadEntity(documentCenterService.getObjectDetail(objectId));
|
||||||
// 设置文件流
|
// 设置文件流
|
||||||
op.ifPresent(entity -> {
|
op.ifPresent(entity -> {
|
||||||
entity.setStream(documentCenterService.download(objectId));
|
entity.setStream(documentCenterService.download(objectId));
|
||||||
});
|
});
|
||||||
return op;
|
return op;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载业务资源
|
* 下载业务资源
|
||||||
*
|
*
|
||||||
* @param businessId
|
* @param businessId
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Optional<DownloadEntity> downloadOriginalFilesByBusinessId(String businessId) {
|
public Optional<DownloadEntity> downloadOriginalFilesByBusinessId(String businessId) {
|
||||||
Optional<AttachmentDetail> op = findByBusinessId(Arrays.asList(businessId));
|
Optional<AttachmentDetail> op = findByBusinessId(Arrays.asList(businessId));
|
||||||
|
|
||||||
if (op.isPresent()) {
|
if (op.isPresent()) {
|
||||||
List<AttachmentEntity> list = op.get().get(businessId);
|
List<AttachmentEntity> list = op.get().get(businessId);
|
||||||
|
|
||||||
// 如果业务id下只包含单独一个文件,则直接返回该文件数据
|
// 如果业务id下只包含单独一个文件,则直接返回该文件数据
|
||||||
if (list.size() == 1) {
|
if (list.size() == 1) {
|
||||||
return downloadOriginalFileByObjectId(list.get(0).getId());
|
return downloadOriginalFileByObjectId(list.get(0).getId());
|
||||||
}
|
}
|
||||||
// 如果大于1个则返回压缩包数据流
|
// 如果大于1个则返回压缩包数据流
|
||||||
else if (list.size() > 1) {
|
else if (list.size() > 1) {
|
||||||
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
|
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
|
||||||
// 创建压缩文件流
|
// 创建压缩文件流
|
||||||
createCompressionStream(list, out);
|
createCompressionStream(list, out);
|
||||||
|
|
||||||
return Optional.of(new DownloadEntity().setFilename(String.format("%s.zip", UUID.randomUUID()))
|
return Optional.of(new DownloadEntity().setFilename(String.format("%s.zip", UUID.randomUUID()))
|
||||||
.setStream(out.toByteArray()));
|
.setStream(out.toByteArray()));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error(e.getMessage());
|
log.error(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件上传
|
* 文件上传
|
||||||
*
|
*
|
||||||
* @param businessId
|
* @param businessId
|
||||||
* @param file
|
* @param file
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Optional<UploadObject> upload(@NotNull String businessId, @NotNull File file) {
|
public Optional<UploadObject> upload(@NotNull String businessId, @NotNull File file) {
|
||||||
removeDuplicateObject(businessId, file.getName());
|
removeDuplicateObject(businessId, file.getName());
|
||||||
|
|
||||||
String res = documentCenterService.upload("ebtp-mall-cloud", businessId, fileConvertor.toMultipartFile(file));
|
String res = documentCenterService.upload("ebtp-mall-cloud", businessId, fileConvertor.toMultipartFile(file));
|
||||||
|
|
||||||
log.info("document center upload result: {}", res);
|
log.info("document center upload result: {}", res);
|
||||||
|
|
||||||
return modelConvertor.toUploadObject(res);
|
return modelConvertor.toUploadObject(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param businessId
|
* @param businessId
|
||||||
* @param filename
|
* @param filename
|
||||||
* @param array
|
* @param array
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Optional<UploadObject> upload(@NotNull String businessId, @NotNull String filename, @NotNull byte[] array) {
|
public Optional<UploadObject> upload(@NotNull String businessId, @NotNull String filename, @NotNull byte[] array) {
|
||||||
removeDuplicateObject(businessId, filename);
|
removeDuplicateObject(businessId, filename);
|
||||||
|
|
||||||
String res = documentCenterService.upload("ebtp-mall-cloud", businessId,
|
String res = documentCenterService.upload("ebtp-mall-cloud", businessId,
|
||||||
fileConvertor.toMultipartFile(filename, array));
|
fileConvertor.toMultipartFile(filename, array));
|
||||||
|
|
||||||
log.info("document center upload result: {}", res);
|
log.info("document center upload result: {}", res);
|
||||||
|
|
||||||
return modelConvertor.toUploadObject(res);
|
return modelConvertor.toUploadObject(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param businessId
|
* @param businessId
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteByBid(String businessId) {
|
public boolean deleteByBid(String businessId) {
|
||||||
Optional<AttachmentDetail> op = findByBusinessId(Arrays.asList(businessId));
|
Optional<AttachmentDetail> op = findByBusinessId(Arrays.asList(businessId));
|
||||||
|
|
||||||
op.ifPresent(attachmentDetail -> {
|
op.ifPresent(attachmentDetail -> {
|
||||||
attachmentDetail.values().forEach(collection -> {
|
attachmentDetail.values().forEach(collection -> {
|
||||||
collection.forEach(attachmentEntity -> {
|
collection.forEach(attachmentEntity -> {
|
||||||
documentCenterService.deleteByOid(attachmentEntity.getId());
|
documentCenterService.deleteByOid(attachmentEntity.getId());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return op.map(detail -> detail.values().size() > 0).orElseGet(() -> false);
|
return op.map(detail -> detail.values().size() > 0).orElseGet(() -> false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param objectId
|
* @param objectId
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String deleteByOid(String objectId) {
|
public String deleteByOid(String objectId) {
|
||||||
return documentCenterService.deleteByOid(objectId);
|
return documentCenterService.deleteByOid(objectId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////////////////////////////////// Private Method
|
/**
|
||||||
/**
|
* 根据业务id复制附件
|
||||||
* 根据业务要求, bid下不能挂在同名文件,需要去重
|
*
|
||||||
*
|
* @param businessId
|
||||||
* @param businessId
|
*/
|
||||||
* @param filename
|
@Override
|
||||||
*/
|
public AttachmentDetail copyByBid(String businessId, String newBusinessId) {
|
||||||
private void removeDuplicateObject(String businessId, String filename) {
|
AttachmentDetail result = new AttachmentDetail();
|
||||||
log.info("remove files {} in business: {} if exists", filename, businessId);
|
|
||||||
Optional<AttachmentDetail> op = findByBusinessId(Arrays.asList(businessId));
|
|
||||||
|
|
||||||
op.ifPresent(detail -> {
|
this.findByBusinessId(Collections.singletonList(businessId))
|
||||||
detail.get(businessId).stream().filter(obj -> obj.getFilename().equals(filename)).forEach(obj -> {
|
.ifPresent(attachmentDetail ->
|
||||||
deleteByOid(obj.getId());
|
attachmentDetail.get(businessId).forEach(
|
||||||
});
|
o -> this.copyByOid(o.getId(), newBusinessId).ifPresent(result::add)));
|
||||||
});
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建压缩文件流
|
* 根据对象id复制附件
|
||||||
*
|
*
|
||||||
* @param list
|
* @param objectId
|
||||||
* @param out
|
*/
|
||||||
* @throws IOException
|
@Override
|
||||||
*/
|
public Optional<AttachmentEntity> copyByOid(String objectId, String newBusinessId) {
|
||||||
private void createCompressionStream(List<AttachmentEntity> list, ByteArrayOutputStream out) throws IOException {
|
//复制
|
||||||
try (ZipOutputStream zout = new ZipOutputStream(out)) {
|
Optional<UploadObject> opt = modelConvertor.toUploadObject(documentCenterService.copyByOid(objectId));
|
||||||
list.forEach(data -> {
|
//修改bid
|
||||||
downloadFileByObjectId(data.getId()).ifPresent(buffer -> {
|
if (opt.isPresent()) {
|
||||||
try {
|
UploadObject uploadObject = opt.get();
|
||||||
zout.putNextEntry(new ZipEntry(data.getFilename()));// 文件项名称
|
|
||||||
zout.write(buffer);// 文件数据流
|
String json = documentCenterService.updateByBid(Collections.singletonList(new SysStorageVO().setFileId(uploadObject.getId()).setObjectId(newBusinessId)));
|
||||||
} catch (IOException e) {
|
Optional<UploadObject> updateOtp = modelConvertor.toUploadObject(json);
|
||||||
log.error(e.getMessage());
|
if (updateOtp.isPresent()) {
|
||||||
}
|
UploadObject uo = updateOtp.get();
|
||||||
});
|
if (uo.getId() != null && uo.getId().equals(uploadObject.getId())) {
|
||||||
});
|
return Optional.of(new AttachmentEntity().setId(uo.getId()).setBid(newBusinessId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/////////////////////////////////////// Private Method
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据业务要求, bid下不能挂在同名文件,需要去重
|
||||||
|
*
|
||||||
|
* @param businessId
|
||||||
|
* @param filename
|
||||||
|
*/
|
||||||
|
private void removeDuplicateObject(String businessId, String filename) {
|
||||||
|
log.info("remove files {} in business: {} if exists", filename, businessId);
|
||||||
|
Optional<AttachmentDetail> op = findByBusinessId(Arrays.asList(businessId));
|
||||||
|
|
||||||
|
op.ifPresent(detail -> {
|
||||||
|
detail.get(businessId).stream().filter(obj -> obj.getFilename().equals(filename)).forEach(obj -> {
|
||||||
|
deleteByOid(obj.getId());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建压缩文件流
|
||||||
|
*
|
||||||
|
* @param list
|
||||||
|
* @param out
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private void createCompressionStream(List<AttachmentEntity> list, ByteArrayOutputStream out) throws IOException {
|
||||||
|
try (ZipOutputStream zout = new ZipOutputStream(out)) {
|
||||||
|
list.forEach(data -> {
|
||||||
|
downloadFileByObjectId(data.getId()).ifPresent(buffer -> {
|
||||||
|
try {
|
||||||
|
zout.putNextEntry(new ZipEntry(data.getFilename()));// 文件项名称
|
||||||
|
zout.write(buffer);// 文件数据流
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error(e.getMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.fallback;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo.SysStorageVO;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@ -35,4 +36,13 @@ public class DocumentCenterServiceFallback implements DocumentCenterService {
|
|||||||
return new byte[0];
|
return new byte[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String updateByBid(List<SysStorageVO> sysStorageVO) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String copyByOid(String fileId) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,70 +1,84 @@
|
|||||||
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.service;
|
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.service;
|
||||||
|
|
||||||
|
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.fallback.DocumentCenterServiceFallback;
|
||||||
|
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo.SysStorageVO;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
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.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
|
|
||||||
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.fallback.DocumentCenterServiceFallback;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 连接山分的文档中心服务
|
* 连接山分的文档中心服务
|
||||||
*
|
*
|
||||||
* @author Ajaxfan
|
* @author Ajaxfan
|
||||||
*/
|
*/
|
||||||
//@FeignClient(name = "DocumentCenterService", url = "${document.center.ip-address}", fallback = DocumentCenterServiceFallback.class)
|
@FeignClient(name = "DocumentCenterService", url = "${document.center.ip-address}", fallback = DocumentCenterServiceFallback.class)
|
||||||
@FeignClient(name = "${document.center.service.id}", fallback = DocumentCenterServiceFallback.class)
|
//@FeignClient(name = "${document.center.service.id}", fallback = DocumentCenterServiceFallback.class)
|
||||||
public interface DocumentCenterService {
|
public interface DocumentCenterService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过附件id查询明细
|
* 通过附件id查询明细
|
||||||
*
|
*
|
||||||
* @param fileId
|
* @param fileId
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "v1.0/files/download")
|
@RequestMapping(method = RequestMethod.GET, value = "v1.0/files/download")
|
||||||
byte[] download(@RequestParam("fileId") String fileId);
|
byte[] download(@RequestParam("fileId") String fileId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过附件id查询明细
|
* 通过附件id查询明细
|
||||||
*
|
*
|
||||||
* @param fileId
|
* @param fileId
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@RequestMapping(method = RequestMethod.POST, value = "v1.0/files/findById")
|
@RequestMapping(method = RequestMethod.POST, value = "v1.0/files/findById")
|
||||||
String getObjectDetail(@RequestParam("fileId") String fileId);
|
String getObjectDetail(@RequestParam("fileId") String fileId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过业务id列表获取明细
|
* 通过业务id列表获取明细
|
||||||
*
|
*
|
||||||
* @param bids
|
* @param bids
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@RequestMapping(method = RequestMethod.POST, value = "v1.0/files/queryReturn")
|
@RequestMapping(method = RequestMethod.POST, value = "v1.0/files/queryReturn")
|
||||||
String fetchDetails(@RequestBody List<String> bids);
|
String fetchDetails(@RequestBody List<String> bids);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传文件
|
* 上传文件
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@RequestMapping(method = RequestMethod.POST, value = "/v1.0/files/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
@RequestMapping(method = RequestMethod.POST, value = "/v1.0/files/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||||
String upload(@RequestPart("appCode") String appCode, @RequestPart("objectId") String objectId,
|
String upload(@RequestPart("appCode") String appCode, @RequestPart("objectId") String objectId,
|
||||||
@RequestPart("multipartFiles") MultipartFile multipartFiles);
|
@RequestPart("multipartFiles") MultipartFile multipartFiles);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除附件信息
|
* 删除附件信息
|
||||||
*
|
*
|
||||||
* @param fileId
|
* @param fileId
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@RequestMapping(method = RequestMethod.POST, value = "v1.0/files/disk")
|
@RequestMapping(method = RequestMethod.POST, value = "v1.0/files/disk")
|
||||||
String deleteByOid(@RequestParam("fileId") String fileId);
|
String deleteByOid(@RequestParam("fileId") String fileId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据fileid进行附件的复制
|
||||||
|
*
|
||||||
|
* @param fileId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@RequestMapping(method = RequestMethod.POST, value = "v1.0/files/copy/")
|
||||||
|
String copyByOid(@RequestParam("fileId") String fileId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据fileid更新附件表信息
|
||||||
|
*
|
||||||
|
* @param sysStorageVO
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@RequestMapping(method = RequestMethod.POST, value = "v1.0/files/update/")
|
||||||
|
String updateByBid(@RequestBody List<SysStorageVO> sysStorageVO);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo;
|
package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.vo;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
|
@Accessors(chain = true)
|
||||||
public class SysStorageVO {
|
public class SysStorageVO {
|
||||||
|
|
||||||
/* 文件原始名称 */
|
/* 文件原始名称 */
|
||||||
|
Reference in New Issue
Block a user