UploadService.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package com.fdkankan.fusion.service.impl;
  2. import com.fdkankan.fusion.common.FilePath;
  3. import com.fdkankan.fusion.common.OssPath;
  4. import com.fdkankan.fusion.common.ResultCode;
  5. import com.fdkankan.fusion.common.enums.FileTypeEnum;
  6. import com.fdkankan.fusion.common.util.*;
  7. import com.fdkankan.fusion.common.util.LocalToOssUtil;
  8. import com.fdkankan.fusion.config.CacheUtil;
  9. import com.fdkankan.fusion.entity.CommonUpload;
  10. import com.fdkankan.fusion.exception.BusinessException;
  11. import com.fdkankan.fusion.service.ICommonUploadService;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.apache.commons.codec.language.Nysiis;
  14. import org.apache.commons.lang3.StringUtils;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.web.multipart.MultipartFile;
  19. import javax.annotation.Resource;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.net.URLEncoder;
  23. import java.nio.file.FileStore;
  24. import java.nio.file.Files;
  25. import java.nio.file.Path;
  26. import java.nio.file.Paths;
  27. import java.util.LinkedHashSet;
  28. import java.util.UUID;
  29. import java.util.regex.Matcher;
  30. @Service
  31. @Slf4j
  32. public class UploadService {
  33. @Autowired
  34. LocalToOssUtil localToOssUtil;
  35. @Value("${upload.query-path}")
  36. private String queryPath;
  37. @Value("${spring.profiles.active}")
  38. private String environment;
  39. @Autowired
  40. ICommonUploadService commonUploadService;
  41. public String uploadFile(MultipartFile file, Integer videoFolderId) {
  42. if(file.isEmpty()){
  43. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  44. }
  45. if(file.getSize()>10 * 1024 * 1024 * 100){
  46. System.out.println(file.getSize());
  47. throw new BusinessException(ResultCode.UPLOAD_FILE_TO_LONG);
  48. }
  49. FileWriterUtil.checkSpace(file.getSize(),1.1);
  50. //获取文件名
  51. String fileName = file.getOriginalFilename();
  52. if(StringUtils.isEmpty(fileName)){
  53. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  54. }
  55. File localFile = null;
  56. try {
  57. //获取文件后缀名
  58. String suffixName = fileName.substring(fileName.lastIndexOf("."));
  59. //重新生成文件名
  60. fileName = UUID.randomUUID().toString().replace("-","") ;
  61. localFile = new File(String.format(FilePath.VIDEO_LOCAL_PATH,environment) +"/"+videoFolderId +"/"+fileName + suffixName );
  62. if(!localFile.getParentFile().exists()){
  63. localFile.getParentFile().mkdirs();
  64. }
  65. file.transferTo(localFile);
  66. return localFile.getPath();
  67. }catch (Exception e){
  68. e.printStackTrace();
  69. }
  70. return null;
  71. }
  72. public String uploadFile(MultipartFile file, Boolean toMedia) {
  73. if(file.isEmpty()){
  74. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  75. }
  76. if(file.getSize()>10 * 1024 * 1024 * 100){
  77. throw new BusinessException(ResultCode.UPLOAD_FILE_TO_LONG);
  78. }
  79. FileWriterUtil.checkSpace(file.getSize(),1.1);
  80. //获取文件名
  81. String fileName = file.getOriginalFilename();
  82. if(StringUtils.isEmpty(fileName)){
  83. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  84. }
  85. File localFile = null;
  86. try {
  87. //获取文件后缀名
  88. String suffixName = fileName.substring(fileName.lastIndexOf("."));
  89. //重新生成文件名
  90. String uuid = UUID.randomUUID().toString().replace("-","") ;
  91. String ossPath = String.format(OssPath.MANAGE_FILE_PATH, uuid + suffixName);
  92. localFile = new File( CacheUtil.settingEntity.getProfilePath() + ossPath);
  93. if(!localFile.getParentFile().exists()){
  94. localFile.getParentFile().mkdirs();
  95. }
  96. file.transferTo(localFile);
  97. //localToOssUtil.uploadOss(localFile.getPath(),ossPath);
  98. String format = suffixName.replace(".", "");
  99. FileTypeEnum fileTypeEnum = FileTypeEnum.getByType(format);
  100. if( toMedia){
  101. commonUploadService.add(fileName.replace(suffixName, ""), MyFileUtils.replaceSeparator(CacheUtil.mapping + ossPath),
  102. String.valueOf(file.getSize()), uuid, fileTypeEnum, format,format,1,localFile.getParentFile().getPath(), null);
  103. }
  104. return MyFileUtils.replaceSeparator(CacheUtil.mapping + ossPath);
  105. }catch (Exception e){
  106. log.info("upload-error:{}",e);
  107. throw new BusinessException(ResultCode.UPLOAD_ERROR.code,ResultCode.UPLOAD_ERROR.msg);
  108. }
  109. }
  110. public void deleteOssUrl(String path) {
  111. try {
  112. String replace = path.replace(queryPath, "");
  113. localToOssUtil.delete(replace);
  114. }catch (Exception e){
  115. e.printStackTrace();
  116. }
  117. }
  118. }