AppFileServiceImpl.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package com.fdkankan.manage.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  6. import com.fdkankan.manage.common.ResultCode;
  7. import com.fdkankan.manage.exception.BusinessException;
  8. import com.fdkankan.manage.common.PageInfo;
  9. import com.fdkankan.common.util.FileMd5Util;
  10. import com.fdkankan.common.util.FileUtils;
  11. import com.fdkankan.manage.common.FilePath;
  12. import com.fdkankan.manage.entity.AppFile;
  13. import com.fdkankan.manage.mapper.IAppFileMapper;
  14. import com.fdkankan.manage.service.IAppFileService;
  15. import com.fdkankan.manage.vo.request.AppFileParam;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.springframework.beans.BeanUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.beans.factory.annotation.Value;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.util.ObjectUtils;
  23. import org.springframework.web.multipart.MultipartFile;
  24. import javax.annotation.Resource;
  25. import java.io.File;
  26. import java.util.*;
  27. import java.util.stream.Collectors;
  28. /**
  29. * <p>
  30. * 服务实现类
  31. * </p>
  32. *
  33. * @author
  34. * @since 2022-06-14
  35. */
  36. @Service
  37. @Slf4j
  38. public class AppFileServiceImpl extends ServiceImpl<IAppFileMapper, AppFile> implements IAppFileService {
  39. @Autowired
  40. private FYunFileServiceInterface fYunFileServiceInterface;
  41. @Value("${fyun.type}")
  42. private String ossType;
  43. @Value("${fyun.host}")
  44. private String prefixAli;
  45. @Override
  46. public Map<String, String> upload(MultipartFile file) {
  47. if (ObjectUtils.isEmpty(file) || file.isEmpty() || file.getSize() <= 0) {
  48. throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
  49. }
  50. try {
  51. Map<String,String> result = new HashMap<>(2);
  52. String filePath = FilePath.appLocalPath;
  53. FileUtils.createDir(filePath);
  54. filePath = filePath.concat(file.getOriginalFilename());
  55. // 删除旧文件
  56. FileUtils.deleteFile(filePath);
  57. File file1 = new File(filePath);
  58. file.transferTo(file1);
  59. // 写文件到本地
  60. String md5 = FileMd5Util.getFileMD5(filePath);
  61. result.put("md5",md5);
  62. result.put("fileName",file.getOriginalFilename());
  63. return result;
  64. }catch (Exception e){
  65. log.error("上传App出错{}",e);
  66. throw new BusinessException(ResultCode.UPLOAD_ERROR);
  67. }
  68. }
  69. @Override
  70. public PageInfo pageList(AppFileParam param) {
  71. LambdaQueryWrapper<AppFile> queryWrapper = new LambdaQueryWrapper<>();
  72. if(StringUtils.isNotBlank(param.getAgentName())){
  73. queryWrapper.like(AppFile::getAgent,param.getAgentName());
  74. }
  75. queryWrapper.orderByDesc(AppFile::getCreateTime);
  76. Page<AppFile> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), queryWrapper);
  77. return PageInfo.PageInfo(page);
  78. }
  79. @Override
  80. public Set<String> agentList(String agentName) {
  81. List<String> strings = fYunFileServiceInterface.listRemoteFiles(FilePath.appOssPath);
  82. Set<String> keys = new HashSet<>();
  83. for (String key : strings) {
  84. String agent = key.replace(FilePath.appOssPath,"");
  85. String[] split = agent.split("/");
  86. String res = split[0];
  87. if(StringUtils.isNotBlank(res)){
  88. keys.add(res);
  89. }
  90. }
  91. return keys;
  92. }
  93. @Override
  94. public void saveByParam(AppFile param) {
  95. if(ObjectUtils.isEmpty(param.getName())){
  96. throw new BusinessException(ResultCode.FILE_NAME_EMPTY);
  97. }
  98. if(!param.getFileServerType().equals(ossType)){
  99. throw new BusinessException(ResultCode.UPLOAD_YUN_TYPE_ERROR);
  100. }
  101. AppFile managerAPPEntity = new AppFile();
  102. BeanUtils.copyProperties(param, managerAPPEntity);
  103. String basePath = FilePath.appLocalPath ;
  104. String filePath = basePath.concat(param.getName());
  105. File file = new File(filePath);
  106. if (!file.exists()) {
  107. throw new BusinessException(ResultCode.APP_FILE_EMPTY);
  108. }
  109. String versionFilePath = basePath.concat("version.json");
  110. if (!new File(versionFilePath).exists()) {
  111. throw new BusinessException(ResultCode.VERSION_EMPTY);
  112. }
  113. String fileType = param.getName().substring(param.getName().lastIndexOf(".")+1);
  114. String ossPathPrefix = FilePath.appOssPath + param.getAgent() + "/"+param.getAppType()+"/";
  115. fYunFileServiceInterface.uploadFile(param.getFileServerType(),filePath, ossPathPrefix+"4dkankan."+fileType);
  116. // 上传到 历史记录文件夹目录
  117. fYunFileServiceInterface.uploadFile(param.getFileServerType(),filePath, ossPathPrefix+"oldapps/" + file.getName());
  118. // 上传到 version.json 文件
  119. fYunFileServiceInterface.uploadFile(param.getFileServerType(),versionFilePath, ossPathPrefix+"version/version.json");
  120. // 删除旧文件
  121. FileUtils.deleteFile(filePath);
  122. FileUtils.deleteFile(versionFilePath);
  123. managerAPPEntity.setUrl(prefixAli.concat(ossPathPrefix+"4dkankan."+fileType));
  124. managerAPPEntity.setVersion(prefixAli.concat(ossPathPrefix+"version/version.json"));
  125. managerAPPEntity.setFileServerType(ossType);
  126. this.save(managerAPPEntity);
  127. }
  128. }