Skip to content

Commit

Permalink
[IMP] 优化附件预览信息处理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
戴高阔 committed Jun 14, 2023
1 parent d582451 commit 62d45f6
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import org.hzero.boot.file.dto.FileDTO;
import org.hzero.core.base.BaseConstants;
import org.hzero.core.util.ResponseUtils;
import io.choerodon.core.exception.CommonException;
import io.choerodon.core.oauth.CustomUserDetails;
import io.choerodon.mybatis.domain.AuditDomain;
Expand All @@ -29,10 +32,6 @@
import io.choerodon.test.manager.infra.util.AttachmentUtils;
import io.choerodon.test.manager.infra.util.IAttachmentPreviewInfoUtil;

import org.hzero.boot.file.dto.FileDTO;
import org.hzero.core.base.BaseConstants;
import org.hzero.core.util.ResponseUtils;

/**
* @author zhaotianxin
* @since 2019/11/22
Expand Down Expand Up @@ -68,8 +67,6 @@ public class TestCaseAssembler {
private FileFeignClient fileFeignClient;
@Autowired
private ModelMapper modelMapper;
@Autowired
private AttachmentUtils attachmentUtils;

private static final String TYPE = "CYCLE_CASE";

Expand Down Expand Up @@ -162,9 +159,9 @@ public TestCaseInfoVO dtoToInfoVO(TestCaseDTO testCaseDTO) {
List<String> fileKeys = new ArrayList<>();
if (CollectionUtils.isNotEmpty(attachment)) {
Set<String> urls = attachment.stream().map(TestCaseAttachmentDTO::getUrl).collect(Collectors.toSet());
Map<String, String> stringStringMap = attachmentUtils.getFileKeys(urls);
Map<String, String> urlToFileKeyMap = AttachmentUtils.getFileKeys(urls);
for (TestCaseAttachmentDTO testCaseAttachment : attachment) {
String fileKey = stringStringMap.get(testCaseAttachment.getUrl());
String fileKey = urlToFileKeyMap.get(testCaseAttachment.getUrl());
if (!ObjectUtils.isEmpty(fileKey)) {
testCaseAttachment.setFileKey(fileKey);
fileKeys.add(fileKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import org.hzero.boot.file.FileClient;
import org.hzero.boot.file.dto.FileDTO;
import org.hzero.core.util.ResponseUtils;
import io.choerodon.core.exception.CommonException;
import io.choerodon.core.oauth.CustomUserDetails;
import io.choerodon.core.oauth.DetailsHelper;
Expand All @@ -42,10 +45,6 @@
import io.choerodon.test.manager.infra.util.AttachmentUtils;
import io.choerodon.test.manager.infra.util.IAttachmentPreviewInfoUtil;

import org.hzero.boot.file.FileClient;
import org.hzero.boot.file.dto.FileDTO;
import org.hzero.core.util.ResponseUtils;

/**
* @author zhaotianxin
* @since 2019/11/21
Expand Down Expand Up @@ -79,9 +78,6 @@ public class TestCaseAttachmentServiceImpl implements TestCaseAttachmentService
private FilePathService filePathService;
@Autowired
private FileClient fileClient;
@Autowired
private AttachmentUtils attachmentUtils;


@Override
public TestCaseAttachmentDTO dealIssue(Long projectId, Long issueId, String fileName, String url) {
Expand Down Expand Up @@ -286,7 +282,7 @@ public TestCaseAttachmentDTO attachmentCombineUpload(Long projectId, TestCaseAtt
String relativePath = filePathService.generateRelativePath(path);
TestCaseAttachmentDTO attachment = dealIssue(projectId, caseId, fileName, relativePath);
// 填充wps onlyOffice预览所需要的信息
String fileKey = attachmentUtils.getFileKey(attachment.getUrl());
String fileKey = AttachmentUtils.getFileKey(attachment.getUrl());
attachment.setFileKey(fileKey);
ProjectDTO project = remoteIamOperator.getProjectById(projectId);
if (project != null && StringUtils.isNotBlank(fileKey)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import org.hzero.boot.file.dto.FileDTO;
import org.hzero.core.util.ResponseUtils;
import io.choerodon.core.exception.CommonException;
import io.choerodon.core.oauth.CustomUserDetails;
import io.choerodon.core.oauth.DetailsHelper;
Expand All @@ -33,12 +35,8 @@
import io.choerodon.test.manager.infra.feign.operator.RemoteIamOperator;
import io.choerodon.test.manager.infra.mapper.TestAttachmentMapper;
import io.choerodon.test.manager.infra.mapper.TestCycleCaseAttachmentRelMapper;
import io.choerodon.test.manager.infra.util.AttachmentUtils;
import io.choerodon.test.manager.infra.util.IAttachmentPreviewInfoUtil;

import org.hzero.boot.file.dto.FileDTO;
import org.hzero.core.util.ResponseUtils;

/**
* Created by [email protected] on 6/11/18.
*/
Expand All @@ -62,8 +60,6 @@ public class TestCycleCaseAttachmentRelServiceImpl implements TestCycleCaseAttac
private TestCycleCaseAttachmentRelUploadService testCycleCaseAttachmentRelUploadService;
@Autowired
private FilePathService filePathService;
@Autowired
private AttachmentUtils attachmentUtils;

@Override
public void deleteAttachmentRel(Long projectId,Long attachId) {
Expand Down Expand Up @@ -201,7 +197,10 @@ private void handAttachment(ProjectDTO project, List<TestCycleCaseAttachmentRelV
if (CollectionUtils.isEmpty(testCycleCaseAttachmentRelVOS)) {
return;
}
List<String> fileUrls = testCycleCaseAttachmentRelVOS.stream().map(TestCycleCaseAttachmentRelVO::getUrl).collect(Collectors.toList());
List<String> fileUrls = testCycleCaseAttachmentRelVOS.stream()
.peek(testCycleCaseAttachmentRel -> testCycleCaseAttachmentRel.setFileKey(testCycleCaseAttachmentRel.getUrl()))
.map(TestCycleCaseAttachmentRelVO::getUrl)
.collect(Collectors.toList());
if(CollectionUtils.isEmpty(fileUrls)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.hzero.boot.file.dto.FileDTO;
import org.hzero.core.util.ResponseUtils;
import io.choerodon.core.domain.Page;
import io.choerodon.core.exception.CommonException;
import io.choerodon.core.oauth.CustomUserDetails;
Expand All @@ -29,14 +31,10 @@
import io.choerodon.test.manager.infra.feign.FileFeignClient;
import io.choerodon.test.manager.infra.feign.operator.RemoteIamOperator;
import io.choerodon.test.manager.infra.mapper.*;
import io.choerodon.test.manager.infra.util.AttachmentUtils;
import io.choerodon.test.manager.infra.util.ConvertUtils;
import io.choerodon.test.manager.infra.util.IAttachmentPreviewInfoUtil;
import io.choerodon.test.manager.infra.util.PageUtil;

import org.hzero.boot.file.dto.FileDTO;
import org.hzero.core.util.ResponseUtils;

/**
* Created by [email protected] on 6/11/18.
*/
Expand Down Expand Up @@ -66,14 +64,10 @@ public class TestCycleCaseStepServiceImpl implements TestCycleCaseStepService {
@Autowired
private TestCycleCaseMapper testCycleCaseMapper;
@Autowired
private FilePathService filePathService;
@Autowired
private RemoteIamOperator remoteIamOperator;
@Autowired
private FileFeignClient fileFeignClient;
@Autowired
private AttachmentUtils attachmentUtils;


@Override
public void update(TestCycleCaseStepVO testCycleCaseStepVO) {
TestCycleCaseStepDTO testCycleCaseStepDTO = modelMapper.map(testCycleCaseStepVO, TestCycleCaseStepDTO.class);
Expand Down Expand Up @@ -298,7 +292,10 @@ private void handAttachment(Long projectId, List<TestCycleCaseStepVO> testCycleC
if (CollectionUtils.isEmpty(testCycleCaseAttachmentRelVOS)) {
return;
}
List<String> fileUrls = testCycleCaseAttachmentRelVOS.stream().map(TestCycleCaseAttachmentRelVO::getUrl).collect(Collectors.toList());
List<String> fileUrls = testCycleCaseAttachmentRelVOS.stream()
.peek(testCycleCaseAttachmentRel -> testCycleCaseAttachmentRel.setFileKey(testCycleCaseAttachmentRel.getUrl()))
.map(TestCycleCaseAttachmentRelVO::getUrl)
.collect(Collectors.toList());
if(CollectionUtils.isEmpty(fileUrls)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import org.hzero.core.base.BaseConstants;

Expand All @@ -16,17 +15,17 @@
*
* @author 汪翔 2023-06-05
*/
@Component
public class AttachmentUtils {
public String getFileKey(String relativePath) {

public static String getFileKey(String relativePath) {
if (StringUtils.isNotEmpty(relativePath) && relativePath.startsWith(BaseConstants.Symbol.SLASH)) {
return relativePath.substring(1);
} else {
return relativePath;
}
}

public Map<String, String> getFileKeys(Set<String> relativePaths) {
public static Map<String, String> getFileKeys(Set<String> relativePaths) {
// 对于测试的附件
if (CollectionUtils.isEmpty(relativePaths)) {
return Collections.emptyMap();
Expand All @@ -36,15 +35,15 @@ public Map<String, String> getFileKeys(Set<String> relativePaths) {
return fileKeys;
}

public static String getFileType(String fileKey) {
if (StringUtils.isEmpty(fileKey)) {
return "";
public static String getFileType(String fileKeyOrUrl) {
if (StringUtils.isEmpty(fileKeyOrUrl)) {
return StringUtils.EMPTY;
}
int index = fileKey.lastIndexOf(".");
int index = fileKeyOrUrl.lastIndexOf(BaseConstants.Symbol.POINT);
if (index > -1) {
return fileKey.substring(index + 1);
return fileKeyOrUrl.substring(index + 1);
} else {
return "";
return StringUtils.EMPTY;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import io.choerodon.test.manager.api.vo.IAttachmentPreviewInfo;

import org.hzero.boot.file.dto.FileDTO;
import org.hzero.core.base.BaseConstants;
import io.choerodon.test.manager.api.vo.IAttachmentPreviewInfo;

/**
* 可预览附件处理工具
Expand All @@ -29,7 +28,7 @@ public class IAttachmentPreviewInfoUtil {
* @param fileInfos 文件信息
*/
public static void fillAttachmentFileAttributeByFileKey(Collection<? extends IAttachmentPreviewInfo> attachmentInfos, Collection<? extends FileDTO> fileInfos) {
fillAttachmentFileAttributeByFileKey(attachmentInfos, fileInfos, IAttachmentPreviewInfo::getFileKey, FileDTO::getFileKey);
fillAttachmentFileAttributeByFileIdentity(attachmentInfos, fileInfos, IAttachmentPreviewInfo::getFileKey, FileDTO::getFileKey);
}

/**
Expand All @@ -38,7 +37,7 @@ public static void fillAttachmentFileAttributeByFileKey(Collection<? extends IAt
* @param fileInfos 文件信息
*/
public static void fillAttachmentFileAttributeByFileUrl(Collection<? extends IAttachmentPreviewInfo> attachmentInfos, Collection<? extends FileDTO> fileInfos) {
fillAttachmentFileAttributeByFileKey(attachmentInfos, fileInfos, IAttachmentPreviewInfo::getUrl, FileDTO::getFileUrl);
fillAttachmentFileAttributeByFileIdentity(attachmentInfos, fileInfos, IAttachmentPreviewInfo::getUrl, FileDTO::getFileUrl);
}

/**
Expand All @@ -48,7 +47,7 @@ public static void fillAttachmentFileAttributeByFileUrl(Collection<? extends IAt
* @param attachmentIdentityExtractor 唯一键生成器
* @param fileIdentityExtractor 唯一键生成器
*/
private static <K> void fillAttachmentFileAttributeByFileKey(Collection<? extends IAttachmentPreviewInfo> attachmentInfos,
private static <K> void fillAttachmentFileAttributeByFileIdentity(Collection<? extends IAttachmentPreviewInfo> attachmentInfos,
Collection<? extends FileDTO> fileInfos,
Function<IAttachmentPreviewInfo, K> attachmentIdentityExtractor,
Function<FileDTO, K> fileIdentityExtractor) {
Expand Down

0 comments on commit 62d45f6

Please sign in to comment.