Skip to content

Commit

Permalink
Fix vod titles with slashes being interpretted as paths, fix other po…
Browse files Browse the repository at this point in the history
…tential issues with invalid filename chars, fix potential crop issue with chatupdater GUI
  • Loading branch information
ScrubN committed Apr 5, 2023
1 parent 4f72ccf commit eb299c1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
7 changes: 5 additions & 2 deletions TwitchDownloaderWPF/PageChatUpdate.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,11 @@ private async void btnBrowse_Click(object sender, RoutedEventArgs e)
}
else
{
numStartHour.Maximum = 0;
numEndHour.Maximum = 0;
if (VideoId != "-1")
{
numStartHour.Maximum = 0;
numEndHour.Maximum = 0;
}
GqlClipResponse videoInfo = await TwitchHelper.GetClipInfo(VideoId);
if (videoInfo.data.clip.video == null)
{
Expand Down
30 changes: 23 additions & 7 deletions TwitchDownloaderWPF/Services/FilenameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ public static class FilenameService
{
private static string[] GetTemplateSubfolders(ref string fullPath)
{
string[] returnString = fullPath.Split(new[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries);
var returnString = fullPath.Split(new[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries);
fullPath = returnString[^1];
Array.Resize(ref returnString, returnString.Length - 1);

for (var i = 0; i < returnString.Length; i++)
{
returnString[i] = RemoveInvalidFilenameChars(returnString[i]);
}

return returnString;
}

internal static string GetFilename(string template, string title, string id, DateTime date, string channel, TimeSpan cropStart, TimeSpan cropEnd)
{
var stringBuilder = new StringBuilder(template)
.Replace("{title}", title)
.Replace("{title}", RemoveInvalidFilenameChars(title))
.Replace("{id}", id)
.Replace("{channel}", channel)
.Replace("{channel}", RemoveInvalidFilenameChars(channel))
.Replace("{date}", date.ToString("Mdyy"))
.Replace("{random_string}", Path.GetFileNameWithoutExtension(Path.GetRandomFileName()))
.Replace("{crop_start}", string.Format(new TimeSpanHFormat(), @"{0:HH\-mm\-ss}", cropStart))
Expand All @@ -38,7 +44,7 @@ internal static string GetFilename(string template, string title, string id, Dat
{
var formatString = dateMatch.Groups[1].Value;
stringBuilder.Remove(dateMatch.Groups[0].Index, dateMatch.Groups[0].Length);
stringBuilder.Insert(dateMatch.Groups[0].Index, date.ToString(formatString));
stringBuilder.Insert(dateMatch.Groups[0].Index, RemoveInvalidFilenameChars(date.ToString(formatString)));
}
else
{
Expand All @@ -58,7 +64,7 @@ internal static string GetFilename(string template, string title, string id, Dat
{
var formatString = cropStartMatch.Groups[1].Value;
stringBuilder.Remove(cropStartMatch.Groups[0].Index, cropStartMatch.Groups[0].Length);
stringBuilder.Insert(cropStartMatch.Groups[0].Index, cropStart.ToString(formatString));
stringBuilder.Insert(cropStartMatch.Groups[0].Index, RemoveInvalidFilenameChars(cropStart.ToString(formatString)));
}
else
{
Expand All @@ -78,7 +84,7 @@ internal static string GetFilename(string template, string title, string id, Dat
{
var formatString = cropEndMatch.Groups[1].Value;
stringBuilder.Remove(cropEndMatch.Groups[0].Index, cropEndMatch.Groups[0].Length);
stringBuilder.Insert(cropEndMatch.Groups[0].Index, cropEnd.ToString(formatString));
stringBuilder.Insert(cropEndMatch.Groups[0].Index, RemoveInvalidFilenameChars(cropEnd.ToString(formatString)));
}
else
{
Expand All @@ -94,7 +100,17 @@ internal static string GetFilename(string template, string title, string id, Dat

private static string RemoveInvalidFilenameChars(string filename)
{
return string.Concat(filename.Split(Path.GetInvalidFileNameChars()));
if (string.IsNullOrWhiteSpace(filename))
{
return filename;
}

if (filename.IndexOfAny(Path.GetInvalidFileNameChars()) == -1)
{
return filename;
}

return string.Join('_', filename.Split(Path.GetInvalidFileNameChars()));
}
}
}

0 comments on commit eb299c1

Please sign in to comment.