Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WPF crash when getting filename for streams with missing info #1098

Merged
merged 3 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions TwitchDownloaderCore.Tests/ToolTests/FilenameServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using TwitchDownloaderCore.Extensions;
using TwitchDownloaderCore.Tools;
using TwitchDownloaderCore.Tools;

namespace TwitchDownloaderCore.Tests.ToolTests
{
Expand Down Expand Up @@ -148,6 +147,17 @@ public void DoesNotInterpretBogusTemplateParameter()
Assert.Equal(EXPECTED, result);
}

[Fact]
public void GetFilenameDoesNotThrow_WhenNullOrDefaultInput()
{
const string TEMPLATE = "{title}_{id}_{date}_{channel}_{trim_start}_{trim_end}_{length}_{views}_{game}_{date_custom=\"s\"}_{trim_start_custom=\"hh\\-mm\\-ss\"}_{trim_end_custom=\"hh\\-mm\\-ss\"}_{length_custom=\"hh\\-mm\\-ss\"}";
const string EXPECTED = "__1-1-01__00-00-00_00-00-00_00-00-00_0__0001-01-01T00_00_00_00-00-00_00-00-00_00-00-00";

var result = FilenameService.GetFilename(TEMPLATE, default, default, default, default, default, default, default, default);

Assert.Equal(EXPECTED, result);
}

[Theory]
[InlineData("\"", """)]
[InlineData("*", "*")]
Expand All @@ -166,6 +176,17 @@ public void CorrectlyReplacesInvalidFilenameCharacters(string str, string expect
Assert.Equal(expected, actual);
}

[Fact]
public void ReplaceInvalidFilenameCharactersDoesNotThrow_WhenNullInput()
{
const string? STR = null;
const string? EXPECTED = null;

var actual = FilenameService.ReplaceInvalidFilenameChars(STR);

Assert.Equal(EXPECTED, actual);
}

[Fact]
public void GetNonCollidingNameWorks_WhenNoCollisionExists()
{
Expand Down
11 changes: 9 additions & 2 deletions TwitchDownloaderCore/Tools/FilenameService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Text;
Expand All @@ -9,7 +10,7 @@ namespace TwitchDownloaderCore.Tools
{
public static class FilenameService
{
public static string GetFilename(string template, string title, string id, DateTime date, string channel, TimeSpan trimStart, TimeSpan trimEnd, long viewCount, string game)
public static string GetFilename(string template, [AllowNull] string title, [AllowNull] string id, DateTime date, [AllowNull] string channel, TimeSpan trimStart, TimeSpan trimEnd, long viewCount, [AllowNull] string game)
{
var videoLength = trimEnd - trimStart;

Expand Down Expand Up @@ -86,8 +87,14 @@ private static string[] GetTemplateSubfolders(ref string fullPath)

private static readonly char[] FilenameInvalidChars = Path.GetInvalidFileNameChars();

public static string ReplaceInvalidFilenameChars(string filename)
[return: NotNullIfNotNull(nameof(filename))]
public static string ReplaceInvalidFilenameChars([AllowNull] string filename)
{
if (string.IsNullOrEmpty(filename))
{
return filename;
}

const string TIMESTAMP_PATTERN = /*lang=regex*/ @"(?<=\d):(?=\d\d)";
var newName = Regex.Replace(filename, TIMESTAMP_PATTERN, "_");

Expand Down