| | |
| | | import cn.hutool.core.lang.UUID; |
| | | import com.sandu.common.enums.FileStorageEnums; |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.file.FileUploadDto; |
| | | import com.sandu.common.file.IFileUpload; |
| | | import com.sandu.common.file.*; |
| | | import com.sandu.common.file.config.FileProperties; |
| | | import com.sandu.common.util.SpringContextHolder; |
| | | import lombok.AllArgsConstructor; |
| | |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.math.BigInteger; |
| | | import java.security.MessageDigest; |
| | | import java.time.LocalDate; |
| | | import java.util.List; |
| | | import java.util.Optional; |
| | |
| | | private static List<String> videoSuffixList = CollectionUtil.newArrayList("mp4", "mov", "avi", "mkv", "m4v", "wmv", |
| | | "asf", "asx", "rm", "rmvb", "3gp", "dat", "flv", "vob"); |
| | | |
| | | private final FileToThumbnail fileToThumbnail; |
| | | |
| | | @Override |
| | | public FileUploadDto uploadFile(MultipartFile multipartFile) { |
| | | FileProperties fileProperties = SpringContextHolder.getBean(FileProperties.class); |
| | |
| | | long size = multipartFile.getSize(); |
| | | String suffix = FileUtil.getSuffix(originalFilename); |
| | | String date = LocalDateTimeUtil.formatNormal(LocalDate.now()); |
| | | String fileName = UUID.fastUUID().toString() + "." + suffix; |
| | | String uuidStr = UUID.fastUUID().toString(); |
| | | String fileName = uuidStr + "." + suffix; |
| | | File dest; |
| | | try { |
| | | String type = Optional.ofNullable(FileTypeUtil.getType(multipartFile.getInputStream(), originalFilename)).orElse(""); |
| | |
| | | FileUploadDto uploadDto = new FileUploadDto(); |
| | | uploadDto.setOriginName(originalFilename); |
| | | uploadDto.setFileName(fileName); |
| | | uploadDto.setFileUrl(dest.getPath().replace(fileProperties.getUploadRootPath(), "")); |
| | | uploadDto.setRealUrl(dest.getPath()); |
| | | uploadDto.setFileUrl(fileProperties.getRealUrl() + dest.getPath()); |
| | | uploadDto.setFileType(FileUtil.getType(dest)); |
| | | uploadDto.setStorageType(FileStorageEnums.LOCAL.getCode()); |
| | | uploadDto.setFileSize(size); |
| | | |
| | | uploadDto.setMd5(getMD5(multipartFile) + "." + uploadDto.getFileType()); |
| | | |
| | | if (IMAGE_SUFFIX_LIST.contains(uploadDto.getFileType())) { |
| | | BufferedImage img = null; |
| | |
| | | uploadDto.setWidth(img.getWidth()); |
| | | uploadDto.setHeight(img.getHeight()); |
| | | } |
| | | } else if (FileService.videoSuffixList.contains(uploadDto.getFileType())) { |
| | | try { |
| | | long duration = VideoUtil.getDuration(uploadDto.getRealUrl()); |
| | | uploadDto.setDuration((int) (duration * 1000)); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | try { |
| | | InputStream inputStream = multipartFile.getInputStream(); |
| | | String screenShot = fileProperties.getUploadRootPath() + uploadDto.getFileType() |
| | | + File.separator + date + File.separator + "screenShot" + uuidStr + ".jpg"; |
| | | String thumbnailForVideo = fileToThumbnail.getThumbnailForVideo(inputStream, screenShot); |
| | | uploadDto.setScreenShot(fileProperties.getRealUrl() + thumbnailForVideo); |
| | | inputStream.close(); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | return uploadDto; |
| | | } |
| | | |
| | | public FileUploadDto uploadCert(MultipartFile multipartFile) { |
| | | String originalFilename = multipartFile.getOriginalFilename(); |
| | | long size = multipartFile.getSize(); |
| | | String suffix = FileUtil.getSuffix(originalFilename); |
| | | String date = LocalDateTimeUtil.formatNormal(LocalDate.now()); |
| | | String fileName = UUID.fastUUID().toString() + "." + suffix; |
| | | File dest; |
| | | |
| | | /** |
| | | * 获取上传文件的MD5值 |
| | | * |
| | | * @param file |
| | | * @return |
| | | */ |
| | | public String getMD5(MultipartFile file) { |
| | | |
| | | try { |
| | | String type = Optional.ofNullable(FileTypeUtil.getType(multipartFile.getInputStream(), originalFilename)).orElse(""); |
| | | // dest = new File(fileProperties.getUploadRootPath() + type + File.separator + date + File.separator + fileName).getCanonicalFile(); |
| | | |
| | | // if (!dest.getParentFile().exists()) { |
| | | // dest.getParentFile().mkdirs(); |
| | | // } |
| | | // multipartFile.transferTo(dest); |
| | | |
| | | byte[] uploadBytes = file.getBytes(); |
| | | MessageDigest md5 = MessageDigest.getInstance("MD5"); |
| | | byte[] digest = md5.digest(uploadBytes); |
| | | String hashString = new BigInteger(1, digest).toString(16); |
| | | if (hashString.length() % 2 != 0) { |
| | | hashString = "0" + hashString; |
| | | } |
| | | return hashString; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new BusinessException(e.getMessage()); |
| | | } |
| | | |
| | | FileUploadDto uploadDto = new FileUploadDto(); |
| | | uploadDto.setOriginName(originalFilename); |
| | | uploadDto.setFileName(fileName); |
| | | // uploadDto.setFileUrl(dest.getPath()); |
| | | // uploadDto.setFileType(FileUtil.getType(dest)); |
| | | uploadDto.setStorageType(FileStorageEnums.LOCAL.getCode()); |
| | | uploadDto.setFileSize(size); |
| | | return uploadDto; |
| | | return null; |
| | | } |
| | | } |