package com.fdkankan.fusion.service.impl; import com.fdkankan.fusion.common.FilePath; import com.fdkankan.fusion.common.ResultCode; import com.fdkankan.fusion.common.util.ShellUtil; import com.fdkankan.fusion.common.util.UploadToOssUtil; import com.fdkankan.fusion.common.util.VideoUtil; import com.fdkankan.fusion.exception.BusinessException; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import sun.rmi.runtime.Log; import javax.annotation.Resource; import java.io.File; import java.io.IOException; import java.util.LinkedHashSet; import java.util.UUID; @Service @Slf4j public class UploadService { @Resource private UploadToOssUtil uploadToOssUtil; @Value("${upload.query-path}") private String queryPath; @Value("${spring.profiles.active}") private String environment; public String uploadFile(MultipartFile file, Integer videoFolderId) { if(file.isEmpty()){ throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST); } if(file.getSize()>10 * 1024 * 1024 * 100){ System.out.println(file.getSize()); throw new BusinessException(ResultCode.UPLOAD_FILE_TO_LONG); } //获取文件名 String fileName = file.getOriginalFilename(); if(StringUtils.isEmpty(fileName)){ throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST); } File localFile = null; try { //获取文件后缀名 String suffixName = fileName.substring(fileName.lastIndexOf(".")); //重新生成文件名 fileName = UUID.randomUUID().toString().replace("-","") ; localFile = new File(String.format(FilePath.VIDEO_LOCAL_PATH,environment) +"/"+videoFolderId +"/"+fileName + suffixName ); if(!localFile.getParentFile().exists()){ localFile.getParentFile().mkdirs(); } file.transferTo(localFile); return localFile.getPath(); }catch (Exception e){ e.printStackTrace(); }finally { if(localFile!=null){ localFile.deleteOnExit(); //退出删除,后续需要使用文件 } } return null; } public String uploadFile(MultipartFile file, boolean newName, String filePathAdd) { if(file.isEmpty()){ throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST); } if(file.getSize()>10 * 1024 * 1024 * 100){ System.out.println(file.getSize()); throw new BusinessException(ResultCode.UPLOAD_FILE_TO_LONG); } //获取文件名 String fileName = file.getOriginalFilename(); if(StringUtils.isEmpty(fileName)){ throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST); } File localFile = null; try { //获取文件后缀名 String suffixName = fileName.substring(fileName.lastIndexOf(".")); //重新生成文件名 if(newName){ fileName = UUID.randomUUID().toString().replace("-","") ; }else { fileName= fileName.substring(0,fileName.lastIndexOf(".")); } localFile = File.createTempFile(fileName + suffixName,suffixName); file.transferTo(localFile); String path = localFile.getPath(); uploadToOssUtil.uploadOss(path,filePathAdd+ fileName + suffixName); if(!uploadToOssUtil.existKey(filePathAdd + fileName + suffixName)){ throw new BusinessException(ResultCode.UPLOAD_ERROR.code,ResultCode.UPLOAD_ERROR.msg); } return queryPath +filePathAdd+ fileName + suffixName; }catch (Exception e){ log.info("upload-error:{}",e); throw new BusinessException(ResultCode.UPLOAD_ERROR.code,ResultCode.UPLOAD_ERROR.msg); }finally { if(localFile!=null){ localFile.delete(); } } } public void deleteOssUrl(String path) { try { String replace = path.replace(queryPath, ""); uploadToOssUtil.delete(replace); }catch (Exception e){ e.printStackTrace(); } } }