From a8b2acaad62a60a125980f27355338b3e10ec39f Mon Sep 17 00:00:00 2001 From: whimsical-c4lic0 Date: Mon, 9 Feb 2026 04:31:27 -0600 Subject: [PATCH] Fix custom filename format configs --- OF DL/Models/Config.cs | 39 ++++++++------ OF DL/Models/Dtos/Common/FilesDto.cs | 2 +- OF DL/Services/FileNameService.cs | 78 +++++++++++++++------------- OF DL/Services/IFileNameService.cs | 5 +- 4 files changed, 70 insertions(+), 54 deletions(-) diff --git a/OF DL/Models/Config.cs b/OF DL/Models/Config.cs index 4e45631..7e83618 100644 --- a/OF DL/Models/Config.cs +++ b/OF DL/Models/Config.cs @@ -105,28 +105,35 @@ public class Config : IFileNameFormatConfig public IFileNameFormatConfig GetCreatorFileNameFormatConfig(string username) { - FileNameFormatConfig combinedFilenameFormatConfig = new(); + FileNameFormatConfig combinedFilenameFormatConfig = new() + { + PaidPostFileNameFormat = PaidPostFileNameFormat, + PostFileNameFormat = PostFileNameFormat, + PaidMessageFileNameFormat = PaidMessageFileNameFormat, + MessageFileNameFormat = MessageFileNameFormat + }; if (CreatorConfigs.TryGetValue(username, out CreatorConfig? creatorConfig)) { - combinedFilenameFormatConfig.PaidMessageFileNameFormat = - !string.IsNullOrEmpty(creatorConfig.PaidMessageFileNameFormat) - ? creatorConfig.PaidMessageFileNameFormat - : PaidMessageFileNameFormat; + if (creatorConfig?.PaidPostFileNameFormat != null) + { + combinedFilenameFormatConfig.PaidPostFileNameFormat = creatorConfig.PaidPostFileNameFormat; + } - combinedFilenameFormatConfig.PostFileNameFormat = !string.IsNullOrEmpty(creatorConfig.PostFileNameFormat) - ? creatorConfig.PostFileNameFormat - : PostFileNameFormat; + if (creatorConfig?.PostFileNameFormat != null) + { + combinedFilenameFormatConfig.PostFileNameFormat = creatorConfig.PostFileNameFormat; + } - combinedFilenameFormatConfig.MessageFileNameFormat = - !string.IsNullOrEmpty(creatorConfig.MessageFileNameFormat) - ? creatorConfig.MessageFileNameFormat - : MessageFileNameFormat; + if (creatorConfig?.PaidMessageFileNameFormat != null) + { + combinedFilenameFormatConfig.PaidMessageFileNameFormat = creatorConfig.PaidMessageFileNameFormat; + } - combinedFilenameFormatConfig.PaidPostFileNameFormat = - !string.IsNullOrEmpty(creatorConfig.PaidPostFileNameFormat) - ? creatorConfig.PaidPostFileNameFormat - : PaidPostFileNameFormat; + if (creatorConfig?.MessageFileNameFormat != null) + { + combinedFilenameFormatConfig.MessageFileNameFormat = creatorConfig.MessageFileNameFormat; + } } Log.Debug("PaidMessageFilenameFormat: {CombinedConfigPaidMessageFileNameFormat}", diff --git a/OF DL/Models/Dtos/Common/FilesDto.cs b/OF DL/Models/Dtos/Common/FilesDto.cs index 13692a8..c7b9056 100644 --- a/OF DL/Models/Dtos/Common/FilesDto.cs +++ b/OF DL/Models/Dtos/Common/FilesDto.cs @@ -13,5 +13,5 @@ public class FilesDto [JsonProperty("squarePreview")] public SquarePreviewDto SquarePreview { get; set; } = new(); - [JsonProperty("drm")] public DrmDto Drm { get; set; } = new(); + [JsonProperty("drm")] public DrmDto? Drm { get; set; } } diff --git a/OF DL/Services/FileNameService.cs b/OF DL/Services/FileNameService.cs index a692874..4643637 100644 --- a/OF DL/Services/FileNameService.cs +++ b/OF DL/Services/FileNameService.cs @@ -5,12 +5,12 @@ namespace OF_DL.Services; public class FileNameService(IAuthService authService) : IFileNameService { - public async Task> GetFilename(object obj1, object obj2, object obj3, - List selectedProperties, string username, Dictionary users = null) + public async Task> GetFilename(object info, object media, object author, + List selectedProperties, string username, Dictionary? users = null) { Dictionary values = new(); - Type type1 = obj1.GetType(); - Type type2 = obj2.GetType(); + Type type1 = info.GetType(); + Type type2 = media.GetType(); PropertyInfo[] properties1 = type1.GetProperties(); PropertyInfo[] properties2 = type2.GetProperties(); @@ -18,19 +18,30 @@ public class FileNameService(IAuthService authService) : IFileNameService { if (propertyName.Contains("media")) { - object drmProperty = null; - object fileProperty = GetNestedPropertyValue(obj2, "files"); + object? drmProperty = null; + object? fileProperty = GetNestedPropertyValue(media, "Files"); if (fileProperty != null) { - drmProperty = GetNestedPropertyValue(obj2, "files.drm"); + drmProperty = GetNestedPropertyValue(media, "Files.Drm"); } if (fileProperty != null && drmProperty != null && propertyName == "mediaCreatedAt") { - object mpdurl = GetNestedPropertyValue(obj2, "files.drm.manifest.dash"); - object policy = GetNestedPropertyValue(obj2, "files.drm.signature.dash.CloudFrontPolicy"); - object signature = GetNestedPropertyValue(obj2, "files.drm.signature.dash.CloudFrontSignature"); - object kvp = GetNestedPropertyValue(obj2, "files.drm.signature.dash.CloudFrontKeyPairId"); + string? mpdurl = GetNestedPropertyValue(media, "Files.Drm.Manifest.Dash") as string; + string? policy = + GetNestedPropertyValue(media, "Files.Drm.Signature.Dash.CloudFrontPolicy") as string; + string? signature = + GetNestedPropertyValue(media, "Files.Drm.Signature.Dash.CloudFrontSignature") as string; + string? kvp = + GetNestedPropertyValue(media, "Files.Drm.Signature.Dash.CloudFrontKeyPairId") as string; + + if (string.IsNullOrEmpty(mpdurl) || string.IsNullOrEmpty(policy) || + string.IsNullOrEmpty(signature) || string.IsNullOrEmpty(kvp) || + authService.CurrentAuth == null) + { + continue; + } + DateTime lastModified = await DownloadService.GetDRMVideoLastModified(string.Join(",", mpdurl, policy, signature, kvp), authService.CurrentAuth); @@ -40,18 +51,18 @@ public class FileNameService(IAuthService authService) : IFileNameService if ((fileProperty == null || drmProperty == null) && propertyName == "mediaCreatedAt") { - object source = GetNestedPropertyValue(obj2, "files.full.url"); + object? source = GetNestedPropertyValue(media, "Files.Full.Url"); if (source != null) { - DateTime lastModified = await DownloadService.GetMediaLastModified(source.ToString()); + DateTime lastModified = await DownloadService.GetMediaLastModified(source.ToString() ?? ""); values.Add(propertyName, lastModified.ToString("yyyy-MM-dd")); continue; } - object preview = GetNestedPropertyValue(obj2, "preview"); + object? preview = GetNestedPropertyValue(media, "Preview"); if (preview != null) { - DateTime lastModified = await DownloadService.GetMediaLastModified(preview.ToString()); + DateTime lastModified = await DownloadService.GetMediaLastModified(preview.ToString() ?? ""); values.Add(propertyName, lastModified.ToString("yyyy-MM-dd")); continue; } @@ -61,7 +72,7 @@ public class FileNameService(IAuthService authService) : IFileNameService p => p.Name.Equals(propertyName.Replace("media", ""), StringComparison.OrdinalIgnoreCase)); if (property != null) { - object? propertyValue = property.GetValue(obj2); + object? propertyValue = property.GetValue(media); if (propertyValue != null) { if (propertyValue is DateTime dateTimeValue) @@ -70,28 +81,26 @@ public class FileNameService(IAuthService authService) : IFileNameService } else { - values.Add(propertyName, propertyValue.ToString()); + values.Add(propertyName, propertyValue.ToString() ?? ""); } } } } else if (propertyName.Contains("filename")) { - string sourcePropertyPath = "files.full.url"; - object sourcePropertyValue = GetNestedPropertyValue(obj2, sourcePropertyPath); + object? sourcePropertyValue = GetNestedPropertyValue(media, "Files.Full.Url"); if (sourcePropertyValue != null) { - Uri uri = new(sourcePropertyValue.ToString()); + Uri uri = new(sourcePropertyValue.ToString() ?? ""); string filename = Path.GetFileName(uri.LocalPath); values.Add(propertyName, filename.Split(".")[0]); } else { - string propertyPath = "files.drm.manifest.dash"; - object nestedPropertyValue = GetNestedPropertyValue(obj2, propertyPath); + object? nestedPropertyValue = GetNestedPropertyValue(media, "Files.Drm.Manifest.Dash"); if (nestedPropertyValue != null) { - Uri uri = new(nestedPropertyValue.ToString()); + Uri uri = new(nestedPropertyValue.ToString() ?? ""); string filename = Path.GetFileName(uri.LocalPath); values.Add(propertyName, filename.Split(".")[0] + "_source"); } @@ -105,8 +114,7 @@ public class FileNameService(IAuthService authService) : IFileNameService } else { - string propertyPath = "id"; - object nestedPropertyValue = GetNestedPropertyValue(obj3, propertyPath); + object? nestedPropertyValue = GetNestedPropertyValue(author, "Id"); if (nestedPropertyValue != null) { values.Add(propertyName, @@ -116,11 +124,11 @@ public class FileNameService(IAuthService authService) : IFileNameService } else if (propertyName.Contains("text", StringComparison.OrdinalIgnoreCase)) { - PropertyInfo property = Array.Find(properties1, + PropertyInfo? property = Array.Find(properties1, p => p.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase)); if (property != null) { - object propertyValue = property.GetValue(obj1); + object? propertyValue = property.GetValue(info); if (propertyValue != null) { HtmlDocument pageDoc = new(); @@ -137,11 +145,11 @@ public class FileNameService(IAuthService authService) : IFileNameService } else { - PropertyInfo property = Array.Find(properties1, + PropertyInfo? property = Array.Find(properties1, p => p.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase)); if (property != null) { - object propertyValue = property.GetValue(obj1); + object? propertyValue = property.GetValue(info); if (propertyValue != null) { if (propertyValue is DateTime dateTimeValue) @@ -150,7 +158,7 @@ public class FileNameService(IAuthService authService) : IFileNameService } else { - values.Add(propertyName, propertyValue.ToString()); + values.Add(propertyName, propertyValue.ToString() ?? ""); } } } @@ -160,7 +168,7 @@ public class FileNameService(IAuthService authService) : IFileNameService return values; } - public async Task BuildFilename(string fileFormat, Dictionary values) + public Task BuildFilename(string fileFormat, Dictionary values) { foreach (KeyValuePair kvp in values) { @@ -168,15 +176,15 @@ public class FileNameService(IAuthService authService) : IFileNameService fileFormat = fileFormat.Replace(placeholder, kvp.Value); } - return RemoveInvalidFileNameChars($"{fileFormat}"); + return Task.FromResult(RemoveInvalidFileNameChars($"{fileFormat}")); } - private static object GetNestedPropertyValue(object source, string propertyPath) + private static object? GetNestedPropertyValue(object source, string propertyPath) { - object value = source; + object? value = source; foreach (string propertyName in propertyPath.Split('.')) { - PropertyInfo property = value.GetType().GetProperty(propertyName) ?? + PropertyInfo property = value?.GetType().GetProperty(propertyName) ?? throw new ArgumentException($"Property '{propertyName}' not found."); value = property.GetValue(value); } diff --git a/OF DL/Services/IFileNameService.cs b/OF DL/Services/IFileNameService.cs index 4e4a026..35d5c2f 100644 --- a/OF DL/Services/IFileNameService.cs +++ b/OF DL/Services/IFileNameService.cs @@ -4,6 +4,7 @@ public interface IFileNameService { Task BuildFilename(string fileFormat, Dictionary values); - Task> GetFilename(object obj1, object obj2, object obj3, List selectedProperties, - string username, Dictionary users = null); + Task> GetFilename(object info, object media, object author, + List selectedProperties, + string username, Dictionary? users = null); }