修改sdk文档中心接口,增加基于对象id的查询和下载

This commit is contained in:
ajaxfan
2021-04-08 17:19:45 +08:00
parent 9e359c63fb
commit 02bc8014c8
5 changed files with 80 additions and 67 deletions

View File

@ -56,6 +56,17 @@ public class DefaultAttachmentClient implements AttachmentClient {
return Optional.of(String.format("{\"id\":\"%s\"}", IdUtil.getSnowflake(23, 16).nextIdStr()));
}
/**
* 查询指定Id文件的详细信息
*
* @param objectId
* @return
*/
@Override
public Optional<AttachmentEntity> findByObjectId(String objectId) {
return modelConvertor.toAttachmentEntity(documentCenterService.getObjectDetail(objectId));
}
/**
* 根据业务id列表查询详单
*
@ -64,9 +75,9 @@ public class DefaultAttachmentClient implements AttachmentClient {
*/
@Override
public Optional<AttachmentDetail> findByBusinessId(List<String> businessIdList) {
log.debug("query bids: {}", businessIdList);
log.debug("query by bids: {}", businessIdList);
String json = documentCenterService.fetchDetails(businessIdList);
log.debug("fetch content: {}", json);
log.debug("document center return: {}", json);
AttachmentDetail detail = new AttachmentDetail();
@ -82,17 +93,6 @@ public class DefaultAttachmentClient implements AttachmentClient {
return Optional.of(detail);
}
/**
* 查询指定Id文件的详细信息
*
* @param objectId
* @return
*/
@Override
public Optional<AttachmentEntity> findByObjectId(String objectId) {
return modelConvertor.toAttachmentEntity(documentCenterService.getObjectDetail(objectId));
}
/**
* 通过对象id下载附件
*
@ -101,7 +101,7 @@ public class DefaultAttachmentClient implements AttachmentClient {
*/
@Override
public Optional<byte[]> downloadFileByObjectId(String objectId) {
return modelConvertor.toByteArray(documentCenterService.getObjectDetail(objectId));
return Optional.ofNullable(documentCenterService.download(objectId));
}
/**
@ -112,6 +112,7 @@ public class DefaultAttachmentClient implements AttachmentClient {
*/
@Override
public Optional<byte[]> downloadFilesByBusinessId(String businessId) {
/** 文档中心不提提供通过业务id下载。因此需要先通过业务id获取对象列表然后下载每一个对象资源 */
Optional<AttachmentDetail> op = findByBusinessId(Arrays.asList(businessId));
if (op.isPresent()) {
@ -121,19 +122,12 @@ public class DefaultAttachmentClient implements AttachmentClient {
if (list.size() == 1) {
return downloadFileByObjectId(list.get(0).getId());
}
// 如果大于1个则下载返回压缩包数据流
// 如果大于1个则返回压缩包数据流
else if (list.size() > 1) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
try (ZipOutputStream zout = new ZipOutputStream(out)) {
list.stream().forEach(data -> {
try {
zout.putNextEntry(new ZipEntry(data.getFilename()));// 文件项名称
zout.write(downloadFileByObjectId(data.getId()).get());// 文件数据流
} catch (IOException e) {
log.error(e.getMessage());
}
});
}
// 创建压缩文件流
createCompressionStream(list, out);
return Optional.ofNullable(out.toByteArray());
} catch (IOException e) {
log.error(e.getMessage());
@ -144,6 +138,25 @@ public class DefaultAttachmentClient implements AttachmentClient {
}
/**
* 下载原始对象格式(包含原始文件的基本信息)
*
* @param objectId
* @return
*/
@Override
public Optional<DownloadEntity> downloadOriginalFileByObjectId(String objectId) {
// 获取对象文件原数据
Optional<DownloadEntity> op = modelConvertor.toDownloadEntity(documentCenterService.getObjectDetail(objectId));
// 设置文件流
op.ifPresent(entity -> {
entity.setStream(documentCenterService.download(objectId));
});
return op;
}
/**
* 下载业务资源
*
* @param businessId
* @return
*/
@ -158,19 +171,12 @@ public class DefaultAttachmentClient implements AttachmentClient {
if (list.size() == 1) {
return downloadOriginalFileByObjectId(list.get(0).getId());
}
// 如果大于1个则下载返回压缩包数据流
// 如果大于1个则返回压缩包数据流
else if (list.size() > 1) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
try (ZipOutputStream zout = new ZipOutputStream(out)) {
list.stream().forEach(data -> {
try {
zout.putNextEntry(new ZipEntry(data.getFilename()));// 文件项名称
zout.write(downloadFileByObjectId(data.getId()).get());// 文件数据流
} catch (IOException e) {
log.error(e.getMessage());
}
});
}
// 创建压缩文件流
createCompressionStream(list, out);
return Optional.ofNullable(new DownloadEntity()
.setFilename(String.format("%s.zip", UUID.randomUUID())).setStream(out.toByteArray()));
} catch (IOException e) {
@ -181,11 +187,6 @@ public class DefaultAttachmentClient implements AttachmentClient {
return Optional.empty();
}
@Override
public Optional<DownloadEntity> downloadOriginalFileByObjectId(String objectId) {
return modelConvertor.toDownloadEntity(documentCenterService.getObjectDetail(objectId));
}
/**
* 文件上传
*
@ -267,4 +268,26 @@ public class DefaultAttachmentClient implements AttachmentClient {
});
}
/**
* 创建压缩文件流
*
* @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());
}
});
});
}
}
}

View File

@ -78,26 +78,6 @@ public class ModelConvertor {
});
}
/**
* @param json
* @return
*/
public Optional<byte[]> toByteArray(String json) {
return Optional.ofNullable(json).map(content -> {
try {
Optional<SysStorageVO> op = tpDownPO(content);
if (op.isPresent()) {
return op.get().getFileStream();
}
} catch (JsonProcessingException e) {
log.error(json);
log.error(e.getMessage());
}
return null;
});
}
/**
* @param json
* @return
@ -111,8 +91,7 @@ public class ModelConvertor {
if (op.isPresent()) {
SysStorageVO vo = op.get();
entity = new DownloadEntity();
entity.setFilename(URLEncoder.DEFAULT.encode(vo.getOriginalName(), Charset.forName("UTF-8")))
.setStream(vo.getFileStream());
entity.setFilename(URLEncoder.DEFAULT.encode(vo.getOriginalName(), Charset.forName("UTF-8")));
}
} catch (JsonProcessingException e) {
log.error(json);

View File

@ -30,4 +30,9 @@ public class DocumentCenterServiceFallback implements DocumentCenterService {
return null;
}
@Override
public byte[] download(String fileId) {
return null;
}
}

View File

@ -28,7 +28,16 @@ public interface DocumentCenterService {
* @param fileId
* @return
*/
@RequestMapping(method = RequestMethod.POST, value = "v1.0/files/downloadFileAllStream")
@RequestMapping(method = RequestMethod.GET, value = "v1.0/files/download")
byte[] download(@RequestParam("fileId") String fileId);
/**
* 通过附件id查询明细
*
* @param fileId
* @return
*/
@RequestMapping(method = RequestMethod.POST, value = "v1.0/files/findById")
String getObjectDetail(@RequestParam("fileId") String fileId);
/**

View File

@ -14,9 +14,6 @@ public class SysStorageVO {
/* 业务id */
private String objectId;
/* 文件流 */
private byte[] fileStream;
/* 文件唯一标识 */
private String fileName;