diff --git a/ImmichFrame.Core/Logic/PooledImmichFrameLogic.cs b/ImmichFrame.Core/Logic/PooledImmichFrameLogic.cs index a13eead4..d3dbb705 100644 --- a/ImmichFrame.Core/Logic/PooledImmichFrameLogic.cs +++ b/ImmichFrame.Core/Logic/PooledImmichFrameLogic.cs @@ -162,27 +162,60 @@ public Task> GetAssets() private async Task<(string fileName, string ContentType, Stream fileStream)> GetVideoAsset(Guid id) { - var videoResponse = await _immichApi.PlayAssetVideoAsync(id, string.Empty); - - if (videoResponse == null) - throw new AssetNotFoundException($"Video asset {id} was not found!"); + var fileName = $"{id}.mp4"; - var contentType = ""; - if (videoResponse.Headers.ContainsKey("Content-Type")) + if (_generalSettings.DownloadImages) { - contentType = videoResponse.Headers["Content-Type"].FirstOrDefault() ?? ""; - } + if (!Directory.Exists(_downloadLocation)) + { + Directory.CreateDirectory(_downloadLocation); + } - if (string.IsNullOrWhiteSpace(contentType)) - { - contentType = "video/mp4"; + var filePath = Path.Combine(_downloadLocation, fileName); + + if (File.Exists(filePath)) + { + if (_generalSettings.RenewImagesDuration > (DateTime.UtcNow - File.GetCreationTimeUtc(filePath)).Days) + { + return (fileName, "video/mp4", File.OpenRead(filePath)); + } + File.Delete(filePath); + } + + using var videoResponse = await _immichApi.PlayAssetVideoAsync(id, string.Empty); + + if (videoResponse == null) + throw new AssetNotFoundException($"Video asset {id} was not found!"); + + var contentType = videoResponse.Headers.ContainsKey("Content-Type") + ? videoResponse.Headers["Content-Type"].FirstOrDefault() ?? "video/mp4" + : "video/mp4"; + + using (var fileStream = File.Create(filePath)) + { + await videoResponse.Stream.CopyToAsync(fileStream); + } + + return (fileName, contentType, File.OpenRead(filePath)); } + else + { + using var videoResponse = await _immichApi.PlayAssetVideoAsync(id, string.Empty); - var fileName = $"{id}.mp4"; + if (videoResponse == null) + throw new AssetNotFoundException($"Video asset {id} was not found!"); - return (fileName, contentType, videoResponse.Stream); - } + var contentType = videoResponse.Headers.ContainsKey("Content-Type") + ? videoResponse.Headers["Content-Type"].FirstOrDefault() ?? "video/mp4" + : "video/mp4"; + var memoryStream = new MemoryStream(); + await videoResponse.Stream.CopyToAsync(memoryStream); + memoryStream.Position = 0; + + return (fileName, contentType, memoryStream); + } + } public Task SendWebhookNotification(IWebhookNotification notification) => WebhookHelper.SendWebhookNotification(notification, _generalSettings.Webhook);