修改sdk文档中心接口,增加基于对象id的查询和下载
This commit is contained in:
@ -56,6 +56,17 @@ public class DefaultAttachmentClient implements AttachmentClient {
|
|||||||
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文件的详细信息
|
||||||
|
*
|
||||||
|
* @param objectId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Optional<AttachmentEntity> findByObjectId(String objectId) {
|
||||||
|
return modelConvertor.toAttachmentEntity(documentCenterService.getObjectDetail(objectId));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据业务id列表查询详单
|
* 根据业务id列表查询详单
|
||||||
*
|
*
|
||||||
@ -64,9 +75,9 @@ public class DefaultAttachmentClient implements AttachmentClient {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Optional<AttachmentDetail> findByBusinessId(List<String> businessIdList) {
|
public Optional<AttachmentDetail> findByBusinessId(List<String> businessIdList) {
|
||||||
log.debug("query bids: {}", businessIdList);
|
log.debug("query by bids: {}", businessIdList);
|
||||||
String json = documentCenterService.fetchDetails(businessIdList);
|
String json = documentCenterService.fetchDetails(businessIdList);
|
||||||
log.debug("fetch content: {}", json);
|
log.debug("document center return: {}", json);
|
||||||
|
|
||||||
AttachmentDetail detail = new AttachmentDetail();
|
AttachmentDetail detail = new AttachmentDetail();
|
||||||
|
|
||||||
@ -82,17 +93,6 @@ public class DefaultAttachmentClient implements AttachmentClient {
|
|||||||
return Optional.of(detail);
|
return Optional.of(detail);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询指定Id文件的详细信息
|
|
||||||
*
|
|
||||||
* @param objectId
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Optional<AttachmentEntity> findByObjectId(String objectId) {
|
|
||||||
return modelConvertor.toAttachmentEntity(documentCenterService.getObjectDetail(objectId));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过对象id下载附件
|
* 通过对象id下载附件
|
||||||
*
|
*
|
||||||
@ -101,7 +101,7 @@ public class DefaultAttachmentClient implements AttachmentClient {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Optional<byte[]> downloadFileByObjectId(String objectId) {
|
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
|
@Override
|
||||||
public Optional<byte[]> downloadFilesByBusinessId(String businessId) {
|
public Optional<byte[]> downloadFilesByBusinessId(String businessId) {
|
||||||
|
/** 文档中心不提提供通过业务id下载。因此,需要先通过业务id获取对象列表,然后下载每一个对象资源 */
|
||||||
Optional<AttachmentDetail> op = findByBusinessId(Arrays.asList(businessId));
|
Optional<AttachmentDetail> op = findByBusinessId(Arrays.asList(businessId));
|
||||||
|
|
||||||
if (op.isPresent()) {
|
if (op.isPresent()) {
|
||||||
@ -121,19 +122,12 @@ public class DefaultAttachmentClient implements AttachmentClient {
|
|||||||
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()) {
|
||||||
try (ZipOutputStream zout = new ZipOutputStream(out)) {
|
// 创建压缩文件流
|
||||||
list.stream().forEach(data -> {
|
createCompressionStream(list, out);
|
||||||
try {
|
|
||||||
zout.putNextEntry(new ZipEntry(data.getFilename()));// 文件项名称
|
|
||||||
zout.write(downloadFileByObjectId(data.getId()).get());// 文件数据流
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error(e.getMessage());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return Optional.ofNullable(out.toByteArray());
|
return Optional.ofNullable(out.toByteArray());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error(e.getMessage());
|
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
|
* @param businessId
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@ -158,19 +171,12 @@ public class DefaultAttachmentClient implements AttachmentClient {
|
|||||||
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()) {
|
||||||
try (ZipOutputStream zout = new ZipOutputStream(out)) {
|
// 创建压缩文件流
|
||||||
list.stream().forEach(data -> {
|
createCompressionStream(list, out);
|
||||||
try {
|
|
||||||
zout.putNextEntry(new ZipEntry(data.getFilename()));// 文件项名称
|
|
||||||
zout.write(downloadFileByObjectId(data.getId()).get());// 文件数据流
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error(e.getMessage());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return Optional.ofNullable(new DownloadEntity()
|
return Optional.ofNullable(new DownloadEntity()
|
||||||
.setFilename(String.format("%s.zip", UUID.randomUUID())).setStream(out.toByteArray()));
|
.setFilename(String.format("%s.zip", UUID.randomUUID())).setStream(out.toByteArray()));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -181,11 +187,6 @@ public class DefaultAttachmentClient implements AttachmentClient {
|
|||||||
return Optional.empty();
|
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());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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
|
* @param json
|
||||||
* @return
|
* @return
|
||||||
@ -111,8 +91,7 @@ public class ModelConvertor {
|
|||||||
if (op.isPresent()) {
|
if (op.isPresent()) {
|
||||||
SysStorageVO vo = op.get();
|
SysStorageVO vo = op.get();
|
||||||
entity = new DownloadEntity();
|
entity = new DownloadEntity();
|
||||||
entity.setFilename(URLEncoder.DEFAULT.encode(vo.getOriginalName(), Charset.forName("UTF-8")))
|
entity.setFilename(URLEncoder.DEFAULT.encode(vo.getOriginalName(), Charset.forName("UTF-8")));
|
||||||
.setStream(vo.getFileStream());
|
|
||||||
}
|
}
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
log.error(json);
|
log.error(json);
|
||||||
|
@ -30,4 +30,9 @@ public class DocumentCenterServiceFallback implements DocumentCenterService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] download(String fileId) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,16 @@ public interface DocumentCenterService {
|
|||||||
* @param fileId
|
* @param fileId
|
||||||
* @return
|
* @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);
|
String getObjectDetail(@RequestParam("fileId") String fileId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -14,9 +14,6 @@ public class SysStorageVO {
|
|||||||
/* 业务id */
|
/* 业务id */
|
||||||
private String objectId;
|
private String objectId;
|
||||||
|
|
||||||
/* 文件流 */
|
|
||||||
private byte[] fileStream;
|
|
||||||
|
|
||||||
/* 文件唯一标识 */
|
/* 文件唯一标识 */
|
||||||
private String fileName;
|
private String fileName;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user