public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> implements FileInfoService { private final AppConfig appConfig; @Async public void transferFile(String fileId, String userId) { boolean transferSuccess = true; String targetFilePath = null; String cover = null; FileInfo fileInfo = getByMultiId(fileId, userId); if (fileInfo == null || !FileStatusEnums.TRANSFER.getStatus().equals(fileInfo.getStatus())) { return; } try { String tempFolderName = appConfig.getProjectFolder() + Constants.FILE_FOLDER_TEMP; String currentUserFolderName = userId + fileId; File fileFolder = new File(tempFolderName + currentUserFolderName); String fileSuffix = StringTools.getFileSuffix(fileInfo.getFilename()); String month = fileInfo.getCreateTime().format(DateTimeFormatter.ofPattern(DateTimePatternEnum.YYYYMM.getPattern())); String targetFolderName = appConfig.getProjectFolder() + Constants.FILE_FOLDER_FILE; File targetFolder = new File(targetFolderName + "/" + month); if (!targetFolder.exists()) { targetFolder.mkdirs(); } String realFilename = currentUserFolderName + fileSuffix; targetFilePath = targetFolder.getPath() + "/" + realFilename; union(fileFolder.getPath(), targetFilePath, fileInfo.getFilename(), true); Integer fileType = fileInfo.getFileType(); if (FileTypeEnums.VIDEO.getType().equals(fileType)) { cutFile4Video(fileId, targetFilePath); cover = month + "/" + currentUserFolderName + Constants.IMAGE_PNG_SUFFIX; String coverPath = targetFolderName + "/" + cover; FfmpegUtil.createTargetThumbnail(new File(targetFilePath), Constants.LENGTH_150, new File(coverPath)); } else if (FileTypeEnums.IMAGE.getType().equals(fileType)) { cover = month + "/" + realFilename.replace(".", "_."); String coverPath = targetFolderName + "/" + cover; Boolean created = FfmpegUtil.createThumbnailWidthFFmpeg(new File(targetFilePath), Constants.LENGTH_150, new File(coverPath), false); if (!created) { FileUtils.copyFile(new File(targetFilePath), new File(coverPath)); } } } catch (Exception e) { log.error("文件转码失败, 文件ID: {}, userId: {}", fileId, userId, e); transferSuccess = false; } finally { FileInfo updateInfo = new FileInfo(); if (targetFilePath == null) { updateInfo.setFileSize(0L); } else { updateInfo.setFileSize(new File(targetFilePath).length()); } updateInfo.setFileCover(cover); updateInfo.setStatus(transferSuccess ? FileStatusEnums.USING.getStatus() : FileStatusEnums.TRANSFER_FAIL.getStatus()); updateByMultiId(updateInfo, fileId, userId); } }
private Boolean updateByMultiId(FileInfo fileInfo, String id, String userId) { return update(fileInfo, new LambdaUpdateWrapper<FileInfo>().eq(FileInfo::getId, id).eq(FileInfo::getUserId, userId)); }
private FileInfo getByMultiId(String id, String userId) { return getOne(new LambdaQueryWrapper<FileInfo>().eq(FileInfo::getId, id).eq(FileInfo::getUserId, userId)); }
private void union(String dirPath, String toFilePath, String filename, Boolean delSource) { File dir = new File(dirPath); if (!dir.exists()) { throw new BizException("目录不存在"); } File[] files = dir.listFiles(); File targetFile = new File(toFilePath); RandomAccessFile writeFile = null; try { writeFile = new RandomAccessFile(targetFile, "rw"); byte[] b = new byte[1024 * 10]; for (int i = 0; i < files.length; i++) { int len = -1; File chunkFile = new File(dirPath + "/" + i); try (RandomAccessFile readFile = new RandomAccessFile(chunkFile, "r")) { while ((len = readFile.read(b)) != -1) { writeFile.write(b, 0, len); } } catch (Exception e) { log.error("合并分片失败", e); throw new BizException("合并分片失败"); } } } catch (Exception e) { log.error("合并文件:{}失败", filename, e); throw new BizException("合并文件" + filename + "出错了"); } finally { if (null != writeFile) { try { writeFile.close(); } catch (IOException e) { e.printStackTrace(); } } if (delSource && dir.exists()) { try { FileUtils.deleteDirectory(dir); } catch (IOException e) { e.printStackTrace(); } } } }
private void cutFile4Video(String fileId, String videoFilePath) { File tsFolder = new File(videoFilePath.substring(0, videoFilePath.lastIndexOf("."))); if (!tsFolder.exists()) { tsFolder.mkdirs(); } String tsPath = tsFolder + "/" + Constants.TS_NAME; FfmpegUtil.transfer2ts(videoFilePath, tsPath); String indexTs = tsFolder.getPath() + "/" + Constants.M3U8_NAME; FfmpegUtil.cutTs(tsPath, indexTs, tsFolder.getPath(), fileId); new File(tsPath).delete(); } }
|