From dead56fc669312360dff68729021025ba8c8008c Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Wed, 7 Feb 2024 09:02:16 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=86=E9=A2=91=E7=BC=93=E5=AD=98=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=96=B9=E5=BC=8F=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/sip/service/VideoCacheManager.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/service/VideoCacheManager.java b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/service/VideoCacheManager.java index b621578..0ecbc04 100644 --- a/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/service/VideoCacheManager.java +++ b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/service/VideoCacheManager.java @@ -23,8 +23,6 @@ import org.springframework.stereotype.Service; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; -import java.nio.channels.FileChannel; -import java.nio.channels.FileLock; import java.nio.file.Paths; import java.util.Date; import java.util.concurrent.*; @@ -71,7 +69,15 @@ public class VideoCacheManager { public CompletableFuture> get(String deviceCode, Date startTime, Date endTime){ String name = fileName(deviceCode, startTime, endTime); - return tasks.get(name); + CompletableFuture> future = tasks.get(name); + if(future == null){ + File realFile = Paths.get(deviceProxyConfig.getPreDownloadForRecordInfo().getCachePath(),fileName(deviceCode, startTime, endTime) + ".mp4").toFile(); + if(realFile.exists()){ + log.info("文件 {} 已缓存, 直接返回", realFile.getAbsolutePath()); + return CompletableFuture.completedFuture(JsonResponse.success(realFile.getAbsolutePath())); + } + } + return future; } @SneakyThrows @@ -91,17 +97,14 @@ public class VideoCacheManager { .addQuery("useDownload", true).build(); File file = Paths.get(deviceProxyConfig.getPreDownloadForRecordInfo().getCachePath(),fileName(deviceCode, startTime, endTime) + ".mp4.tmp").toFile(); log.info("文件存储路径 => {}", file.getAbsolutePath()); - log.info("文件 {}, 是否存在: {}", file.getAbsolutePath(), file.exists()); + log.info("临时文件 {}, 是否存在: {}", file.getAbsolutePath(), file.exists()); if(file.exists()){ file.delete(); - log.info("删除已存但未完成下载的文件 => {}", file.getAbsolutePath()); + log.info("删除已存但未完成下载的临时文件 => {}", file.getAbsolutePath()); } try (FileOutputStream outputStream = new FileOutputStream(file)) { - FileChannel channel = outputStream.getChannel(); - FileLock lock = channel.lock(); - HttpClient client = HttpClients.custom() .setConnectionManager(manager) .setConnectionManagerShared(true) @@ -114,10 +117,10 @@ public class VideoCacheManager { return stream; }); execute.close(); - log.info("视频下载完成 => {}", file.getAbsolutePath()); - log.info("文件 {}, 是否存在: {}", file.getAbsolutePath(), file.exists()); + log.info("临时文件下载完成 => {}", file.getAbsolutePath()); + log.info("临时文件 {}, 是否存在: {}", file.getAbsolutePath(), file.exists()); file.renameTo(realFile); - lock.release(); + log.info("保存视频文件 => {}", realFile.getAbsolutePath()); return JsonResponse.success(realFile.getAbsolutePath()); } catch (Exception e) { log.error("视频下载失败 => {}", e.getMessage());