Skip to content

Commit

Permalink
chore: improve validation and error handling in audio processing
Browse files Browse the repository at this point in the history
- Added a check for a non-empty request clip in the ProcessAudio method
- Added ArgumentNullExceptions to the DownloadAudioAsync methods if the input is null or empty
- Implemented a check in the SoundButtons class to return a BadRequestResult if there source.VideoId, req.Form.GetFirstValue("clip") and req.Form.Files.Count are all empty
- Created a SourceCheck function to validate a Source object
- Refactored time invalidation check and error message to be more descriptive

Signed-off-by: 陳鈞 <jim60105@gmail.com>
  • Loading branch information
jim60105 committed Mar 26, 2024
1 parent 4242d9b commit 5a7d0eb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion SoundButtons/Functions/ProcessAudio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static async Task<string> ProcessAudioAsync(
await ProcessAudioHelper.DownloadAudioAsync(youtubeDLPath, tempPath, request.Source);
await ProcessAudioHelper.CutAudioAsync(tempPath, request.Source);
}
else
else if (!string.IsNullOrEmpty(request.Clip))
{
await ProcessAudioHelper.DownloadAudioAsync(youtubeDLPath, tempPath, request.Clip);
}
Expand Down
4 changes: 4 additions & 0 deletions SoundButtons/Helper/ProcessAudioHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ string UseBuiltInYtdlp()

internal static Task<int> DownloadAudioAsync(string youtubeDLPath, string tempPath, Source source)
{
if (string.IsNullOrEmpty(source.VideoId)) throw new ArgumentNullException(nameof(source));

OptionSet optionSet = new()
{
// 最佳音質
Expand Down Expand Up @@ -95,6 +97,8 @@ internal static Task<int> DownloadAudioAsync(string youtubeDLPath, string tempPa

internal static Task<int> DownloadAudioAsync(string youtubeDLPath, string tempPath, string url)
{
if (string.IsNullOrEmpty(url)) throw new ArgumentNullException(nameof(url));

OptionSet optionSet = new()
{
NoCheckCertificates = true,
Expand Down
32 changes: 25 additions & 7 deletions SoundButtons/SoundButtons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ public static async Task<IActionResult> HttpStart(
if (!req.ContentType.Contains("multipart/form-data;"))
return new BadRequestResult();

Source source = GetSourceInfo(req);
Source source = SourceCheck(GetSourceInfo(req));

if (string.IsNullOrEmpty(source.VideoId)
&& string.IsNullOrEmpty(req.Form.GetFirstValue("clip"))
&& req.Form.Files.Count == 0)
{
Logger.Error("No source found.");
return new BadRequestResult();
}

// 啟動長輪詢
return await StartOrchestrator(req: req,
Expand Down Expand Up @@ -213,17 +221,27 @@ private static async Task<string> ProcessAudioFileAsync(HttpRequest req, Source
{
return await ProcessAudioFromFileUpload(files);
}
// source檢核

if (string.IsNullOrEmpty(source.VideoId)
|| source.End - source.Start <= 0
return "";
}

private static Source SourceCheck(Source source)
{
if (string.IsNullOrEmpty(source.VideoId))
{
source.Start = 0;
source.End = 0;
return source;
}

if (source.End - source.Start <= 0
|| source.End - source.Start > 180)
{
Logger.Error("video time invalid: {start}, {end}", source.Start, source.End);
throw new Exception($"video time invalid: {source.Start}, {source.End}");
Logger.Error("Video time invalid: {start}, {end}", source.Start, source.End);
throw new Exception($"Video time invalid: {source.Start}, {source.End}");
}

return "";
return source;
}

private static async Task<string> ProcessAudioFromFileUpload(IFormFileCollection files)
Expand Down

0 comments on commit 5a7d0eb

Please sign in to comment.