From 7a5cdb971f5b98763837f07921969cbb431a9bea Mon Sep 17 00:00:00 2001 From: Kieran Date: Thu, 7 Mar 2024 12:54:08 +0000 Subject: [PATCH] try fix delete --- NostrStreamer/Extensions.cs | 2 +- NostrStreamer/Services/Dvr/S3DvrStore.cs | 29 +++++++++---------- .../Services/Thumbnail/S3ThumbnailService.cs | 14 +++------ 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/NostrStreamer/Extensions.cs b/NostrStreamer/Extensions.cs index 96d5c32..3ded084 100644 --- a/NostrStreamer/Extensions.cs +++ b/NostrStreamer/Extensions.cs @@ -45,7 +45,7 @@ public static class Extensions RegionEndpoint = !string.IsNullOrEmpty(c.Region) ? RegionEndpoint.GetBySystemName(c.Region) : null, ServiceURL = c.ServiceUrl.ToString(), UseHttp = c.ServiceUrl.Scheme == "http", - ForcePathStyle = true + ForcePathStyle = true, }); } diff --git a/NostrStreamer/Services/Dvr/S3DvrStore.cs b/NostrStreamer/Services/Dvr/S3DvrStore.cs index 1ee499c..1ecb778 100644 --- a/NostrStreamer/Services/Dvr/S3DvrStore.cs +++ b/NostrStreamer/Services/Dvr/S3DvrStore.cs @@ -6,20 +6,11 @@ using NostrStreamer.Database; namespace NostrStreamer.Services.Dvr; -public class S3DvrStore : IDvrStore +public class S3DvrStore(Config config, HttpClient httpClient, ILogger logger) + : IDvrStore { - private readonly AmazonS3Client _client; - private readonly S3BlobConfig _config; - private readonly HttpClient _httpClient; - private readonly ILogger _logger; - - public S3DvrStore(Config config, HttpClient httpClient, ILogger logger) - { - _httpClient = httpClient; - _logger = logger; - _config = config.S3Store; - _client = config.S3Store.CreateClient(); - } + private readonly AmazonS3Client _client = config.S3Store.CreateClient(); + private readonly S3BlobConfig _config = config.S3Store; public async Task UploadRecording(UserStream stream, Uri source) { @@ -43,7 +34,7 @@ public class S3DvrStore : IDvrStore sw.Restart(); await using var fs = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite); - var dl = await _httpClient.GetStreamAsync(source); + var dl = await httpClient.GetStreamAsync(source); await dl.CopyToAsync(fs); await fs.FlushAsync(); fs.Seek(0, SeekOrigin.Begin); @@ -87,7 +78,7 @@ public class S3DvrStore : IDvrStore fs.Close(); File.Delete(tmpFile); - _logger.LogInformation("download={tc:#,##0}ms, probe={pc:#,##0}ms, upload={uc:#,##0}ms", + logger.LogInformation("download={tc:#,##0}ms, probe={pc:#,##0}ms, upload={uc:#,##0}ms", tsDownload.TotalMilliseconds, tsProbe.TotalMilliseconds, tsUpload.TotalMilliseconds); @@ -97,7 +88,7 @@ public class S3DvrStore : IDvrStore public async Task> DeleteRecordings(UserStream stream) { var deleted = new List(); - foreach (var batch in stream.Recordings.Select((a, i) => (Batch: i / 1000, Item: a)).GroupBy(a => a.Batch)) + foreach (var batch in stream.Recordings.Select((a, i) => (Batch: i / 100, Item: a)).GroupBy(a => a.Batch)) { var res = await _client.DeleteObjectsAsync(new() { @@ -110,6 +101,12 @@ public class S3DvrStore : IDvrStore deleted.AddRange(res.DeletedObjects.Select(a => Guid.Parse(Path.GetFileNameWithoutExtension(a.Key)))); } + await _client.DeleteObjectAsync(new() + { + BucketName = _config.BucketName, + Key = $"{stream.Id}/" + }); + return deleted; } } \ No newline at end of file diff --git a/NostrStreamer/Services/Thumbnail/S3ThumbnailService.cs b/NostrStreamer/Services/Thumbnail/S3ThumbnailService.cs index c67cae3..38e95d9 100644 --- a/NostrStreamer/Services/Thumbnail/S3ThumbnailService.cs +++ b/NostrStreamer/Services/Thumbnail/S3ThumbnailService.cs @@ -5,16 +5,10 @@ using NostrStreamer.Database; namespace NostrStreamer.Services.Thumbnail; -public class S3ThumbnailService : BaseThumbnailService, IThumbnailService +public class S3ThumbnailService(Config config, ILogger logger, StreamerContext context) + : BaseThumbnailService(config, logger), IThumbnailService { - private readonly AmazonS3Client _client; - private readonly StreamerContext _context; - - public S3ThumbnailService(Config config, ILogger logger, StreamerContext context) : base(config, logger) - { - _client = config.S3Store.CreateClient(); - _context = context; - } + private readonly AmazonS3Client _client = config.S3Store.CreateClient(); public async Task GenerateThumb(UserStream stream) { @@ -54,7 +48,7 @@ public class S3ThumbnailService : BaseThumbnailService, IThumbnailService var tUpload = sw.Elapsed; sw.Restart(); - await _context.Streams.Where(a => a.Id == stream.Id) + await context.Streams.Where(a => a.Id == stream.Id) .ExecuteUpdateAsync(o => o.SetProperty(v => v.Thumbnail, ub.Uri.ToString())); var tDbUpdate = sw.Elapsed;