-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prerequisites for CLI filename template support (#886)
* Move FilenameService to Core * Reduce filename generator memory footprint * Fix game name not being sanitized * string.IsNullOrWhiteSpace -> string.IsNullOrEmpty * Support escaping with quote marks in both ReadOnlySpanExtensions.TryReplaceNonEscaped and TimeSpanHFormat * Rename TimeSpanExtensions to UrlTimeCode
- Loading branch information
Showing
11 changed files
with
270 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
|
||
namespace TwitchDownloaderCore.Extensions | ||
{ | ||
public static class StringExtensions | ||
{ | ||
public static string ReplaceAny(this string str, ReadOnlySpan<char> oldChars, char newChar) | ||
{ | ||
if (string.IsNullOrEmpty(str)) | ||
{ | ||
return str; | ||
} | ||
|
||
var index = str.AsSpan().IndexOfAny(oldChars); | ||
if (index == -1) | ||
{ | ||
return str; | ||
} | ||
|
||
const ushort MAX_STACK_SIZE = 512; | ||
var span = str.Length <= MAX_STACK_SIZE | ||
? stackalloc char[str.Length] | ||
: str.ToCharArray(); | ||
|
||
// Unfortunately this cannot be inlined with the previous statement because a ternary is required for the stackalloc to compile | ||
if (str.Length <= MAX_STACK_SIZE) | ||
str.CopyTo(span); | ||
|
||
var tempSpan = span; | ||
do | ||
{ | ||
tempSpan[index] = newChar; | ||
tempSpan = tempSpan[(index + 1)..]; | ||
|
||
index = tempSpan.IndexOfAny(oldChars); | ||
if (index == -1) | ||
break; | ||
} while (true); | ||
|
||
return span.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using TwitchDownloaderCore.Extensions; | ||
|
||
namespace TwitchDownloaderCore.Tools | ||
{ | ||
public static class FilenameService | ||
{ | ||
private static string[] GetTemplateSubfolders(ref string fullPath) | ||
{ | ||
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; | ||
} | ||
|
||
public static string GetFilename(string template, string title, string id, DateTime date, string channel, TimeSpan cropStart, TimeSpan cropEnd, string viewCount, string game) | ||
{ | ||
var videoLength = cropEnd - cropStart; | ||
|
||
var stringBuilder = new StringBuilder(template) | ||
.Replace("{title}", RemoveInvalidFilenameChars(title)) | ||
.Replace("{id}", id) | ||
.Replace("{channel}", RemoveInvalidFilenameChars(channel)) | ||
.Replace("{date}", date.ToString("Mdyy")) | ||
.Replace("{random_string}", Path.GetRandomFileName().Replace(".", "")) | ||
.Replace("{crop_start}", TimeSpanHFormat.ReusableInstance.Format(@"HH\-mm\-ss", cropStart)) | ||
.Replace("{crop_end}", TimeSpanHFormat.ReusableInstance.Format(@"HH\-mm\-ss", cropEnd)) | ||
.Replace("{length}", TimeSpanHFormat.ReusableInstance.Format(@"HH\-mm\-ss", videoLength)) | ||
.Replace("{views}", viewCount) | ||
.Replace("{game}", RemoveInvalidFilenameChars(game)); | ||
|
||
if (template.Contains("{date_custom=")) | ||
{ | ||
var dateRegex = new Regex("{date_custom=\"(.*)\"}"); | ||
ReplaceCustomWithFormattable(stringBuilder, dateRegex, date); | ||
} | ||
|
||
if (template.Contains("{crop_start_custom=")) | ||
{ | ||
var cropStartRegex = new Regex("{crop_start_custom=\"(.*)\"}"); | ||
ReplaceCustomWithFormattable(stringBuilder, cropStartRegex, cropStart); | ||
} | ||
|
||
if (template.Contains("{crop_end_custom=")) | ||
{ | ||
var cropEndRegex = new Regex("{crop_end_custom=\"(.*)\"}"); | ||
ReplaceCustomWithFormattable(stringBuilder, cropEndRegex, cropEnd); | ||
} | ||
|
||
if (template.Contains("{length_custom=")) | ||
{ | ||
var lengthRegex = new Regex("{length_custom=\"(.*)\"}"); | ||
ReplaceCustomWithFormattable(stringBuilder, lengthRegex, videoLength); | ||
} | ||
|
||
var fileName = stringBuilder.ToString(); | ||
var additionalSubfolders = GetTemplateSubfolders(ref fileName); | ||
return Path.Combine(Path.Combine(additionalSubfolders), RemoveInvalidFilenameChars(fileName)); | ||
} | ||
|
||
private static void ReplaceCustomWithFormattable(StringBuilder sb, Regex regex, IFormattable formattable, IFormatProvider formatProvider = null) | ||
{ | ||
do | ||
{ | ||
// There's probably a better way to do this that doesn't require calling ToString() | ||
// However we need .NET7+ for span support in the regex matcher. | ||
var match = regex.Match(sb.ToString()); | ||
if (!match.Success) | ||
break; | ||
|
||
var formatString = match.Groups[1].Value; | ||
sb.Remove(match.Groups[0].Index, match.Groups[0].Length); | ||
sb.Insert(match.Groups[0].Index, RemoveInvalidFilenameChars(formattable.ToString(formatString, formatProvider))); | ||
} while (true); | ||
} | ||
|
||
private static readonly char[] FilenameInvalidChars = Path.GetInvalidFileNameChars(); | ||
|
||
private static string RemoveInvalidFilenameChars(string filename) => filename.ReplaceAny(FilenameInvalidChars, '_'); | ||
} | ||
} |
Oops, something went wrong.