Skip to content

Commit

Permalink
refactor: improve logging and exception handling in S3Service
Browse files Browse the repository at this point in the history
- Change log messages in `YoutubeDLHelper.cs` to eliminate `.exe` for `yt-dlp` and `ffmpeg`
- Add a new entry to user dictionary in `LivestreamRecorderService.sln.DotSettings`
- Refactor variable declarations and method calls in `PlatformService.cs` for clarity
- Expand file upload logging in `S3Service.cs` by adding size and etag information and warning for empty etag, indicating potential failed upload

Signed-off-by: 陳鈞 <[email protected]>
  • Loading branch information
jim60105 committed May 24, 2024
1 parent bc0deb3 commit 75a5e2c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Helper/YoutubeDLHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ from e in extensions
where File.Exists(path)
select path)?.FirstOrDefault();

Log.Debug("Found yt-dlp.exe at {YtdlpPath}", _YtdlpPath);
Log.Debug("Found ffmpeg.exe at {FFmpegPath}", _FFmpegPath);
Log.Debug("Found yt-dlp at {YtdlpPath}", _YtdlpPath);
Log.Debug("Found ffmpeg at {FFmpegPath}", _FFmpegPath);

return (_YtdlpPath, _FFmpegPath);
}
Expand Down
1 change: 1 addition & 0 deletions LivestreamRecorderService.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=libaom/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=sharedvolume/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=telop/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=twitcasting/@EntryIndexedValue">True</s:Boolean>
Expand Down
31 changes: 16 additions & 15 deletions ScopedServices/PlatformService/PlatformService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public bool StepInterval(int elapsedTime)
$"Failed to fetch video data from yt-dlp for URL: {url}. Errors: {string.Join(' ', res.ErrorOutput)}");
}

var videoData = res.Data;
YtdlpVideoData? videoData = res.Data;
return videoData;
}
catch (Exception e)
Expand Down Expand Up @@ -190,21 +190,21 @@ public bool StepInterval(int elapsedTime)
throw new ArgumentNullException(nameof(path));
}

string? extension, contentType, pathInStorage, tempPath;
string? contentType, pathInStorage, tempPath;
try
{
using var client = HttpClientFactory.CreateClient();
var response = await client.GetAsync(url, cancellation);
using HttpClient client = HttpClientFactory.CreateClient();
HttpResponseMessage response = await client.GetAsync(url, cancellation);
response.EnsureSuccessStatusCode();

contentType = response.Content.Headers.ContentType?.MediaType;
extension = MimeUtility.GetExtensions(contentType)?.FirstOrDefault();
string? extension = MimeUtility.GetExtensions(contentType)?.FirstOrDefault();
extension = extension == "jpeg" ? "jpg" : extension;
pathInStorage = $"{path}.{extension}";

tempPath = Path.GetTempFileName();
tempPath = Path.ChangeExtension(tempPath, extension);
await using var contentStream = await response.Content.ReadAsStreamAsync(cancellation);
await using Stream contentStream = await response.Content.ReadAsStreamAsync(cancellation);
await using var fileStream = new FileStream(tempPath, FileMode.Create);
await contentStream.CopyToAsync(fileStream, cancellation);
}
Expand All @@ -216,16 +216,16 @@ public bool StepInterval(int elapsedTime)

try
{
List<Task> tasks =
[
StorageService.UploadPublicFileAsync(contentType, pathInStorage, tempPath, cancellation),
StorageService.UploadPublicFileAsync(KnownMimeTypes.Avif,
$"{path}.avif",
await ImageHelper.ConvertToAvifAsync(tempPath),
cancellation)
];
await StorageService.UploadPublicFileAsync(contentType: contentType,
pathInStorage: pathInStorage,
filePathToUpload: tempPath,
cancellation: cancellation);

await StorageService.UploadPublicFileAsync(contentType: KnownMimeTypes.Avif,
pathInStorage: $"{path}.avif",
filePathToUpload: await ImageHelper.ConvertToAvifAsync(tempPath),
cancellation: cancellation);

await Task.WhenAll(tasks);
return pathInStorage;
}
catch (Exception e)
Expand All @@ -242,6 +242,7 @@ await ImageHelper.ConvertToAvifAsync(tempPath),
}
catch (IOException)
{
// ignored
}
}
}
Expand Down
25 changes: 18 additions & 7 deletions SingletonServices/S3Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Minio;
using Minio.DataModel;
using Minio.DataModel.Args;
using Minio.DataModel.Response;
using Minio.Exceptions;

namespace LivestreamRecorderService.SingletonServices;
Expand Down Expand Up @@ -71,14 +72,24 @@ public async Task UploadPublicFileAsync(string? contentType, string pathInStorag
{
try
{
await minioClient.PutObjectAsync(new PutObjectArgs()
.WithBucket(_options.BucketName_Public)
.WithObject(pathInStorage)
.WithFileName(tempPath)
.WithContentType(contentType),
cancellation);
string bucketNamePublic = _options.BucketName_Public;
PutObjectArgs putObjectArgs = new PutObjectArgs().WithBucket(bucketNamePublic)
.WithObject(pathInStorage)
.WithFileName(tempPath)
.WithContentType(contentType);

PutObjectResponse result = await minioClient.PutObjectAsync(putObjectArgs, cancellation);

logger.LogInformation("Public file uploaded to {bucket}/{filePath}, {size}, {etag}",
bucketNamePublic,
result.ObjectName,
result.Size,
result.Etag);

if (string.IsNullOrEmpty(result.Etag))
logger.LogWarning("The Etag is empty for the uploaded file at {filePath}.", pathInStorage);
}
catch (MinioException e)
catch (Exception e)
{
logger.LogError(e, "Failed to upload public file: {filePath}", pathInStorage);
}
Expand Down

0 comments on commit 75a5e2c

Please sign in to comment.