package com.fdkankan.scene.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.StrUtil; import com.fdkankan.common.constant.ConstantFilePath; import com.fdkankan.common.constant.ErrorCode; import com.fdkankan.common.constant.UploadFilePath; import com.fdkankan.common.exception.BusinessException; import com.fdkankan.common.util.BASE64DecodedMultipartFile; import com.fdkankan.common.util.FdfsUtil; import com.fdkankan.redis.constant.RedisKey; import com.fdkankan.scene.bean.ResultData; import com.fdkankan.scene.entity.Scene; import com.fdkankan.scene.entity.SceneFileMapping; import com.fdkankan.scene.service.FYunFileService; import com.fdkankan.scene.service.ISceneUploadService; import com.fdkankan.scene.service.SceneFileMappingService; import com.fdkankan.scene.service.SceneService; import com.fdkankan.scene.vo.DeleteFileParamVO; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.File; import java.util.*; /** *

* 服务实现类 *

* * @author * @since 2022-01-19 */ @Slf4j @Service public class SceneUploadServiceImpl implements ISceneUploadService { @Autowired private SceneService sceneService; @Autowired private SceneFileMappingService sceneFileMappingService; // @Resource // private FdfsUtil fdfsUtil; @Resource FYunFileService fYunFileService; @Override public String uploads(String imgData,String fileName,String blzType,MultipartFile[] files, String sceneCode,Integer type, String uploadPath, Integer subgroup) throws Exception{ List multipartFiles = new ArrayList<>(); if(StringUtils.isNotBlank(imgData)){ MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(imgData); multipartFiles.add(file); } if(files !=null && files.length >0){ multipartFiles.addAll(Arrays.asList(files)); } return this.uploadFiles(fileName,blzType,multipartFiles,sceneCode,type,uploadPath, subgroup); } @Override public ResultData delete(DeleteFileParamVO param) throws Exception { Scene scenePlus = sceneService.getByNum(param.getNum(), param.getSubgroup()); if(Objects.isNull(scenePlus)){ throw new BusinessException(ErrorCode.FAILURE_CODE_5005); } String userEditPath = String.format(UploadFilePath.USER_VIEW_PATH, param.getNum()); List filePaths = new ArrayList<>(); Set hasDelete = new HashSet<>(); for (String fileName : param.getFileNames()) { if(hasDelete.contains(fileName)){ continue; } hasDelete.add(fileName); String key = userEditPath + fileName; filePaths.add(key); fYunFileService.deleteFile(param.getNum(), param.getSubgroup(), key); } return ResultData.ok(); } public String uploadFiles(String sendFileName,String bizType,List files, String num, Integer type, String uploadPath, Integer subgroup) throws Exception{ if (StrUtil.isEmpty(num)) throw new BusinessException(ErrorCode.PARAM_REQUIRED, "num"); if(CollUtil.isEmpty(files)) throw new BusinessException(ErrorCode.PARAM_REQUIRED, "files"); if(StrUtil.isEmpty(bizType)) throw new BusinessException(ErrorCode.PARAM_REQUIRED, "bizType"); Scene scene = sceneService.getByNum(num, subgroup); if(Objects.isNull(scene)) throw new BusinessException(ErrorCode.FAILURE_CODE_5005); List urlList = new ArrayList<>(); for (MultipartFile file : files) { String fileName = file.getOriginalFilename(); // 获取文件后缀 String prefix = fileName.substring(fileName.lastIndexOf(".")); // String path = String.format(ConstantFilePath.SCENE_TMP_PATH_V4, num).concat(UUID.randomUUID().toString()).concat(prefix); File tempFile = File.createTempFile(UUID.randomUUID().toString() ,prefix); // File tempFile = FileUtil.createTempFile(prefix, true); String path = tempFile.getAbsolutePath(); // FileUtil.mkParentDirs(path); file.transferTo(tempFile); String realFileName = fileName; if(files.size() ==1 && StringUtils.isNotBlank(sendFileName)){ realFileName = sendFileName ; } String oldExtName = cn.hutool.core.io.FileUtil.extName(realFileName); String newExtName = oldExtName.toLowerCase(); realFileName = realFileName.substring(0, realFileName.lastIndexOf(oldExtName)) + newExtName; String key = StrUtil.isNotBlank(uploadPath) ? uploadPath : (String.format(UploadFilePath.USER_VIEW_PATH , RedisKey.getNumStr(num, subgroup)) + realFileName); fYunFileService.uploadFile(num, subgroup, path, key); urlList.add(realFileName); FileUtil.del(path); //添加记录 } StringBuilder returnString = new StringBuilder(); for (String res : urlList) { if(StringUtils.isNotBlank(returnString)){ returnString.append(","); } returnString.append(res); } return returnString.toString(); } // private void updateFileByPreFix(String sceneCode, String blzType, String bucket) { // LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); // queryWrapper.eq(SceneUpload::getNum,sceneCode) // .eq(SceneUpload::getBizType,blzType); // List list = this.list(queryWrapper); // // if(list != null && list.size() >0){ // for (SceneUpload sceneUpload : list) { // try { // fYunFileService.deleteFile(bucket, sceneUpload.getFilePath()); // this.removeEntity(sceneUpload); // }catch (Exception e){ // log.error(sceneUpload.getFilePath()+"删除oss文件失败",e); // } // } // } // } // // private void removeEntity(SceneUpload sceneUpload) { // sceneUpload.setRecStatus(RecStatus.VALID.code()); // this.updateById(sceneUpload); // } // // private void saveData(String sceneCode, String ossPath, String bizType,Long userId) { // SceneUpload sceneUpload = new SceneUpload(); // sceneUpload.setNum(sceneCode); // sceneUpload.setFilePath(ossPath); // sceneUpload.setBizType(bizType); // sceneUpload.setUploadUser(userId); // this.save(sceneUpload); // } // // @Override // public String uploadContent(UploadContentParamVO param) throws Exception { // // String ossPath = param.getOssPath(); // String fileName = ossPath.substring(ossPath.lastIndexOf("/") + 1); // if(StrUtil.isEmpty(fileName)){ // throw new BusinessException(ErrorCode.FAILURE_CODE_7012); // } // String suffix = fileName.substring(fileName.lastIndexOf(".")); // if(StrUtil.isEmpty(suffix)){ // throw new BusinessException(ErrorCode.FAILURE_CODE_7012); // } // // File tempFile = File.createTempFile(UUID.randomUUID().toString(), suffix); // cn.hutool.core.io.FileUtil.writeString(param.getContent(), tempFile, StandardCharsets.UTF_8); // fYunFileService.uploadFile(param.getBucket(), tempFile.getPath(), param.getOssPath()); // tempFile.deleteOnExit(); // // return this.ossUrlPrefix + param.getOssPath(); // } }