123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- package com.fdkankan.manage.service.impl;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.fdkankan.fyun.face.FYunFileServiceInterface;
- import com.fdkankan.manage.common.ResultCode;
- import com.fdkankan.manage.exception.BusinessException;
- import com.fdkankan.manage.common.PageInfo;
- import com.fdkankan.common.util.FileMd5Util;
- import com.fdkankan.common.util.FileUtils;
- import com.fdkankan.manage.common.FilePath;
- import com.fdkankan.manage.entity.AppFile;
- import com.fdkankan.manage.mapper.IAppFileMapper;
- import com.fdkankan.manage.service.IAppFileService;
- import com.fdkankan.manage.vo.request.AppFileParam;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.springframework.util.ObjectUtils;
- import org.springframework.web.multipart.MultipartFile;
- import javax.annotation.Resource;
- import java.io.File;
- import java.util.*;
- import java.util.stream.Collectors;
- /**
- * <p>
- * 服务实现类
- * </p>
- *
- * @author
- * @since 2022-06-14
- */
- @Service
- @Slf4j
- public class AppFileServiceImpl extends ServiceImpl<IAppFileMapper, AppFile> implements IAppFileService {
- @Autowired
- private FYunFileServiceInterface fYunFileServiceInterface;
- @Value("${fyun.type}")
- private String ossType;
- @Value("${fyun.host}")
- private String prefixAli;
- @Override
- public Map<String, String> upload(MultipartFile file) {
- if (ObjectUtils.isEmpty(file) || file.isEmpty() || file.getSize() <= 0) {
- throw new BusinessException(ResultCode.UPLOAD_FILE_ERROR);
- }
- try {
- Map<String,String> result = new HashMap<>(2);
- String filePath = FilePath.appLocalPath;
- FileUtils.createDir(filePath);
- filePath = filePath.concat(file.getOriginalFilename());
- // 删除旧文件
- FileUtils.deleteFile(filePath);
- File file1 = new File(filePath);
- file.transferTo(file1);
- // 写文件到本地
- String md5 = FileMd5Util.getFileMD5(filePath);
- result.put("md5",md5);
- result.put("fileName",file.getOriginalFilename());
- return result;
- }catch (Exception e){
- log.error("上传App出错{}",e);
- throw new BusinessException(ResultCode.UPLOAD_ERROR);
- }
- }
- @Override
- public PageInfo pageList(AppFileParam param) {
- LambdaQueryWrapper<AppFile> queryWrapper = new LambdaQueryWrapper<>();
- if(StringUtils.isNotBlank(param.getAgentName())){
- queryWrapper.like(AppFile::getAgent,param.getAgentName());
- }
- queryWrapper.orderByDesc(AppFile::getCreateTime);
- Page<AppFile> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), queryWrapper);
- return PageInfo.PageInfo(page);
- }
- @Override
- public Set<String> agentList(String agentName) {
- List<String> strings = fYunFileServiceInterface.listRemoteFiles(FilePath.appOssPath);
- Set<String> keys = new HashSet<>();
- for (String key : strings) {
- String agent = key.replace(FilePath.appOssPath,"");
- String[] split = agent.split("/");
- String res = split[0];
- if(StringUtils.isNotBlank(res)){
- keys.add(res);
- }
- }
- return keys;
- }
- @Override
- public void saveByParam(AppFile param) {
- if(ObjectUtils.isEmpty(param.getName())){
- throw new BusinessException(ResultCode.FILE_NAME_EMPTY);
- }
- if(!param.getFileServerType().equals(ossType)){
- throw new BusinessException(ResultCode.UPLOAD_YUN_TYPE_ERROR);
- }
- AppFile managerAPPEntity = new AppFile();
- BeanUtils.copyProperties(param, managerAPPEntity);
- String basePath = FilePath.appLocalPath ;
- String filePath = basePath.concat(param.getName());
- File file = new File(filePath);
- if (!file.exists()) {
- throw new BusinessException(ResultCode.APP_FILE_EMPTY);
- }
- String versionFilePath = basePath.concat("version.json");
- if (!new File(versionFilePath).exists()) {
- throw new BusinessException(ResultCode.VERSION_EMPTY);
- }
- String fileType = param.getName().substring(param.getName().lastIndexOf(".")+1);
- String ossPathPrefix = FilePath.appOssPath + param.getAgent() + "/"+param.getAppType()+"/";
- fYunFileServiceInterface.uploadFile(param.getFileServerType(),filePath, ossPathPrefix+"4dkankan."+fileType);
- // 上传到 历史记录文件夹目录
- fYunFileServiceInterface.uploadFile(param.getFileServerType(),filePath, ossPathPrefix+"oldapps/" + file.getName());
- // 上传到 version.json 文件
- fYunFileServiceInterface.uploadFile(param.getFileServerType(),versionFilePath, ossPathPrefix+"version/version.json");
- // 删除旧文件
- FileUtils.deleteFile(filePath);
- FileUtils.deleteFile(versionFilePath);
- managerAPPEntity.setUrl(prefixAli.concat(ossPathPrefix+"4dkankan."+fileType));
- managerAPPEntity.setVersion(prefixAli.concat(ossPathPrefix+"version/version.json"));
- managerAPPEntity.setFileServerType(ossType);
- this.save(managerAPPEntity);
- }
- }
|