123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- package com.fdkankan.fusion.service.impl;
- import com.fdkankan.fusion.common.FilePath;
- import com.fdkankan.fusion.common.OssPath;
- import com.fdkankan.fusion.common.ResultCode;
- import com.fdkankan.fusion.common.util.*;
- import com.fdkankan.fusion.common.util.LocalToOssUtil;
- import com.fdkankan.fusion.config.CacheUtil;
- import com.fdkankan.fusion.exception.BusinessException;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- 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.net.URLEncoder;
- import java.nio.file.FileStore;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.LinkedHashSet;
- import java.util.UUID;
- import java.util.regex.Matcher;
- @Service
- @Slf4j
- public class UploadService {
- @Autowired
- LocalToOssUtil localToOssUtil;
- @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);
- }
- FileWriterUtil.checkSpace(file.getSize(),1.1);
- //获取文件名
- 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();
- }
- 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);
- }
- FileWriterUtil.checkSpace(file.getSize(),1.1);
- //获取文件名
- 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("."));
- //重新生成文件名
- String uuid = UUID.randomUUID().toString().replace("-","") ;
- String ossPath = String.format(OssPath.MANAGE_FILE_PATH, uuid + suffixName);
- localFile = new File(OssPath.localPath + ossPath);
- if(!localFile.getParentFile().exists()){
- localFile.getParentFile().mkdirs();
- }
- file.transferTo(localFile);
- //localToOssUtil.uploadOss(localFile.getPath(),ossPath);
- return CacheUtil.mapping + ossPath;
- }catch (Exception e){
- log.info("upload-error:{}",e);
- throw new BusinessException(ResultCode.UPLOAD_ERROR.code,ResultCode.UPLOAD_ERROR.msg);
- }
- }
- public void deleteOssUrl(String path) {
- try {
- String replace = path.replace(queryPath, "");
- localToOssUtil.delete(replace);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
|