1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| @Service public class MediaFilesServiceImpl implements MediaFilesService { @Autowired private MediaFilesMapper mediaFilesMapper; /** * 分块文件上传 * <br/> * 分块文件不存放mysql信息,同时文件名不含后缀,只有md5 * @param file 文件 * @param md5 md5 * @return {@link Boolean} */ @Override public Boolean handleChunkUpload(MultipartFile file, String md5) { //只上传至minio OssClient storage = OssFactory.instance(); String path = getPathByMD5(md5, ""); try { storage.upload(file.getInputStream(), path, file.getContentType(), minioProperties.getVideoBucket()); } catch (IOException e) { throw new RuntimeException(e); } return true; } @Override public Boolean isChunkExist(String md5) { OssClient storage = OssFactory.instance(); String path = getPathByMD5(md5, ""); return storage.doesFileExist(minioProperties.getVideoBucket(), path); }
@Override public Boolean mergeChunks(MediaVideoMergeBo bo) { OssClient storage = OssFactory.instance(); String originalfileName = bo.getVideoName(); String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length()); //创建临时文件,用来存放合并文件 String tmpDir = System.getProperty("java.io.tmpdir"); String tmpFileName = UUID.randomUUID().toString() + ".tmp"; File tmpFile = new File(tmpDir, tmpFileName);
try( FileOutputStream fOut = new FileOutputStream(tmpFile); ) { //将分块文件以流的形式copy到临时文件 List<String> chunksMd5 = bo.getChunksMd5(); chunksMd5.forEach(chunkMd5 -> { String chunkPath = getPathByMD5(chunkMd5, ""); InputStream chunkIn = storage.getObjectContent(minioProperties.getVideoBucket(), chunkPath); IoUtil.copy(chunkIn, fOut); }); //合并文件上传到minio String videoMd5 = bo.getVideoMd5(); String path = getPathByMD5(videoMd5, suffix); storage.upload(tmpFile, path, minioProperties.getVideoBucket()); //删除分块文件 chunksMd5.forEach(chunkMd5->{ String chunkPath = getPathByMD5(chunkMd5, ""); storage.delete(chunkPath, minioProperties.getVideoBucket()); }); } catch (Exception e) { throw new RuntimeException(e); }finally { if (tmpFile.exists()){ tmpFile.delete(); } } //上传信息到mysql MediaFiles mediaFiles = new MediaFiles(); mediaFiles.setId(bo.getVideoMd5()); mediaFiles.setCompanyId(bo.getCompanyId()); mediaFiles.setOriginalName(originalfileName); mediaFiles.setFileSuffix(suffix); mediaFiles.setSize(bo.getVideoSize()); mediaFiles.setPath(getPathByMD5(bo.getVideoMd5(), suffix)); mediaFiles.setRemark(bo.getRemark()); mediaFiles.setAuditStatus(MediaStatusEnum.UNREVIEWED.getValue()); return mediaFilesMapper.insert(mediaFiles) > 0; } /** * 通过md5生成文件路径 * <br/> * 比如 * md5 = 6c4acb01320a21ccdbec089f6a9b7ca3 * <br/> * path = 6/c/md5 + suffix * @param prefix 前缀 * @param suffix 后缀 * @return {@link String} */ public String getPathByMD5(String md5, String suffix) { // 文件路径 String path = md5.charAt(0) + "/" + md5.charAt(1) + "/" + md5; return path + suffix; }
}
|