Fix custom filename format configs

This commit is contained in:
whimsical-c4lic0 2026-02-09 04:31:27 -06:00
parent a57af4042f
commit a8b2acaad6
4 changed files with 70 additions and 54 deletions

View File

@ -105,28 +105,35 @@ public class Config : IFileNameFormatConfig
public IFileNameFormatConfig GetCreatorFileNameFormatConfig(string username) 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)) if (CreatorConfigs.TryGetValue(username, out CreatorConfig? creatorConfig))
{ {
combinedFilenameFormatConfig.PaidMessageFileNameFormat = if (creatorConfig?.PaidPostFileNameFormat != null)
!string.IsNullOrEmpty(creatorConfig.PaidMessageFileNameFormat) {
? creatorConfig.PaidMessageFileNameFormat combinedFilenameFormatConfig.PaidPostFileNameFormat = creatorConfig.PaidPostFileNameFormat;
: PaidMessageFileNameFormat; }
combinedFilenameFormatConfig.PostFileNameFormat = !string.IsNullOrEmpty(creatorConfig.PostFileNameFormat) if (creatorConfig?.PostFileNameFormat != null)
? creatorConfig.PostFileNameFormat {
: PostFileNameFormat; combinedFilenameFormatConfig.PostFileNameFormat = creatorConfig.PostFileNameFormat;
}
combinedFilenameFormatConfig.MessageFileNameFormat = if (creatorConfig?.PaidMessageFileNameFormat != null)
!string.IsNullOrEmpty(creatorConfig.MessageFileNameFormat) {
? creatorConfig.MessageFileNameFormat combinedFilenameFormatConfig.PaidMessageFileNameFormat = creatorConfig.PaidMessageFileNameFormat;
: MessageFileNameFormat; }
combinedFilenameFormatConfig.PaidPostFileNameFormat = if (creatorConfig?.MessageFileNameFormat != null)
!string.IsNullOrEmpty(creatorConfig.PaidPostFileNameFormat) {
? creatorConfig.PaidPostFileNameFormat combinedFilenameFormatConfig.MessageFileNameFormat = creatorConfig.MessageFileNameFormat;
: PaidPostFileNameFormat; }
} }
Log.Debug("PaidMessageFilenameFormat: {CombinedConfigPaidMessageFileNameFormat}", Log.Debug("PaidMessageFilenameFormat: {CombinedConfigPaidMessageFileNameFormat}",

View File

@ -13,5 +13,5 @@ public class FilesDto
[JsonProperty("squarePreview")] public SquarePreviewDto SquarePreview { get; set; } = new(); [JsonProperty("squarePreview")] public SquarePreviewDto SquarePreview { get; set; } = new();
[JsonProperty("drm")] public DrmDto Drm { get; set; } = new(); [JsonProperty("drm")] public DrmDto? Drm { get; set; }
} }

View File

@ -5,12 +5,12 @@ namespace OF_DL.Services;
public class FileNameService(IAuthService authService) : IFileNameService public class FileNameService(IAuthService authService) : IFileNameService
{ {
public async Task<Dictionary<string, string>> GetFilename(object obj1, object obj2, object obj3, public async Task<Dictionary<string, string>> GetFilename(object info, object media, object author,
List<string> selectedProperties, string username, Dictionary<string, long> users = null) List<string> selectedProperties, string username, Dictionary<string, long>? users = null)
{ {
Dictionary<string, string> values = new(); Dictionary<string, string> values = new();
Type type1 = obj1.GetType(); Type type1 = info.GetType();
Type type2 = obj2.GetType(); Type type2 = media.GetType();
PropertyInfo[] properties1 = type1.GetProperties(); PropertyInfo[] properties1 = type1.GetProperties();
PropertyInfo[] properties2 = type2.GetProperties(); PropertyInfo[] properties2 = type2.GetProperties();
@ -18,19 +18,30 @@ public class FileNameService(IAuthService authService) : IFileNameService
{ {
if (propertyName.Contains("media")) if (propertyName.Contains("media"))
{ {
object drmProperty = null; object? drmProperty = null;
object fileProperty = GetNestedPropertyValue(obj2, "files"); object? fileProperty = GetNestedPropertyValue(media, "Files");
if (fileProperty != null) if (fileProperty != null)
{ {
drmProperty = GetNestedPropertyValue(obj2, "files.drm"); drmProperty = GetNestedPropertyValue(media, "Files.Drm");
} }
if (fileProperty != null && drmProperty != null && propertyName == "mediaCreatedAt") if (fileProperty != null && drmProperty != null && propertyName == "mediaCreatedAt")
{ {
object mpdurl = GetNestedPropertyValue(obj2, "files.drm.manifest.dash"); string? mpdurl = GetNestedPropertyValue(media, "Files.Drm.Manifest.Dash") as string;
object policy = GetNestedPropertyValue(obj2, "files.drm.signature.dash.CloudFrontPolicy"); string? policy =
object signature = GetNestedPropertyValue(obj2, "files.drm.signature.dash.CloudFrontSignature"); GetNestedPropertyValue(media, "Files.Drm.Signature.Dash.CloudFrontPolicy") as string;
object kvp = GetNestedPropertyValue(obj2, "files.drm.signature.dash.CloudFrontKeyPairId"); 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 = DateTime lastModified =
await DownloadService.GetDRMVideoLastModified(string.Join(",", mpdurl, policy, signature, kvp), await DownloadService.GetDRMVideoLastModified(string.Join(",", mpdurl, policy, signature, kvp),
authService.CurrentAuth); authService.CurrentAuth);
@ -40,18 +51,18 @@ public class FileNameService(IAuthService authService) : IFileNameService
if ((fileProperty == null || drmProperty == null) && propertyName == "mediaCreatedAt") if ((fileProperty == null || drmProperty == null) && propertyName == "mediaCreatedAt")
{ {
object source = GetNestedPropertyValue(obj2, "files.full.url"); object? source = GetNestedPropertyValue(media, "Files.Full.Url");
if (source != null) 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")); values.Add(propertyName, lastModified.ToString("yyyy-MM-dd"));
continue; continue;
} }
object preview = GetNestedPropertyValue(obj2, "preview"); object? preview = GetNestedPropertyValue(media, "Preview");
if (preview != null) 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")); values.Add(propertyName, lastModified.ToString("yyyy-MM-dd"));
continue; continue;
} }
@ -61,7 +72,7 @@ public class FileNameService(IAuthService authService) : IFileNameService
p => p.Name.Equals(propertyName.Replace("media", ""), StringComparison.OrdinalIgnoreCase)); p => p.Name.Equals(propertyName.Replace("media", ""), StringComparison.OrdinalIgnoreCase));
if (property != null) if (property != null)
{ {
object? propertyValue = property.GetValue(obj2); object? propertyValue = property.GetValue(media);
if (propertyValue != null) if (propertyValue != null)
{ {
if (propertyValue is DateTime dateTimeValue) if (propertyValue is DateTime dateTimeValue)
@ -70,28 +81,26 @@ public class FileNameService(IAuthService authService) : IFileNameService
} }
else else
{ {
values.Add(propertyName, propertyValue.ToString()); values.Add(propertyName, propertyValue.ToString() ?? "");
} }
} }
} }
} }
else if (propertyName.Contains("filename")) else if (propertyName.Contains("filename"))
{ {
string sourcePropertyPath = "files.full.url"; object? sourcePropertyValue = GetNestedPropertyValue(media, "Files.Full.Url");
object sourcePropertyValue = GetNestedPropertyValue(obj2, sourcePropertyPath);
if (sourcePropertyValue != null) if (sourcePropertyValue != null)
{ {
Uri uri = new(sourcePropertyValue.ToString()); Uri uri = new(sourcePropertyValue.ToString() ?? "");
string filename = Path.GetFileName(uri.LocalPath); string filename = Path.GetFileName(uri.LocalPath);
values.Add(propertyName, filename.Split(".")[0]); values.Add(propertyName, filename.Split(".")[0]);
} }
else else
{ {
string propertyPath = "files.drm.manifest.dash"; object? nestedPropertyValue = GetNestedPropertyValue(media, "Files.Drm.Manifest.Dash");
object nestedPropertyValue = GetNestedPropertyValue(obj2, propertyPath);
if (nestedPropertyValue != null) if (nestedPropertyValue != null)
{ {
Uri uri = new(nestedPropertyValue.ToString()); Uri uri = new(nestedPropertyValue.ToString() ?? "");
string filename = Path.GetFileName(uri.LocalPath); string filename = Path.GetFileName(uri.LocalPath);
values.Add(propertyName, filename.Split(".")[0] + "_source"); values.Add(propertyName, filename.Split(".")[0] + "_source");
} }
@ -105,8 +114,7 @@ public class FileNameService(IAuthService authService) : IFileNameService
} }
else else
{ {
string propertyPath = "id"; object? nestedPropertyValue = GetNestedPropertyValue(author, "Id");
object nestedPropertyValue = GetNestedPropertyValue(obj3, propertyPath);
if (nestedPropertyValue != null) if (nestedPropertyValue != null)
{ {
values.Add(propertyName, values.Add(propertyName,
@ -116,11 +124,11 @@ public class FileNameService(IAuthService authService) : IFileNameService
} }
else if (propertyName.Contains("text", StringComparison.OrdinalIgnoreCase)) else if (propertyName.Contains("text", StringComparison.OrdinalIgnoreCase))
{ {
PropertyInfo property = Array.Find(properties1, PropertyInfo? property = Array.Find(properties1,
p => p.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase)); p => p.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase));
if (property != null) if (property != null)
{ {
object propertyValue = property.GetValue(obj1); object? propertyValue = property.GetValue(info);
if (propertyValue != null) if (propertyValue != null)
{ {
HtmlDocument pageDoc = new(); HtmlDocument pageDoc = new();
@ -137,11 +145,11 @@ public class FileNameService(IAuthService authService) : IFileNameService
} }
else else
{ {
PropertyInfo property = Array.Find(properties1, PropertyInfo? property = Array.Find(properties1,
p => p.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase)); p => p.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase));
if (property != null) if (property != null)
{ {
object propertyValue = property.GetValue(obj1); object? propertyValue = property.GetValue(info);
if (propertyValue != null) if (propertyValue != null)
{ {
if (propertyValue is DateTime dateTimeValue) if (propertyValue is DateTime dateTimeValue)
@ -150,7 +158,7 @@ public class FileNameService(IAuthService authService) : IFileNameService
} }
else else
{ {
values.Add(propertyName, propertyValue.ToString()); values.Add(propertyName, propertyValue.ToString() ?? "");
} }
} }
} }
@ -160,7 +168,7 @@ public class FileNameService(IAuthService authService) : IFileNameService
return values; return values;
} }
public async Task<string> BuildFilename(string fileFormat, Dictionary<string, string> values) public Task<string> BuildFilename(string fileFormat, Dictionary<string, string> values)
{ {
foreach (KeyValuePair<string, string> kvp in values) foreach (KeyValuePair<string, string> kvp in values)
{ {
@ -168,15 +176,15 @@ public class FileNameService(IAuthService authService) : IFileNameService
fileFormat = fileFormat.Replace(placeholder, kvp.Value); 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('.')) 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."); throw new ArgumentException($"Property '{propertyName}' not found.");
value = property.GetValue(value); value = property.GetValue(value);
} }

View File

@ -4,6 +4,7 @@ public interface IFileNameService
{ {
Task<string> BuildFilename(string fileFormat, Dictionary<string, string> values); Task<string> BuildFilename(string fileFormat, Dictionary<string, string> values);
Task<Dictionary<string, string>> GetFilename(object obj1, object obj2, object obj3, List<string> selectedProperties, Task<Dictionary<string, string>> GetFilename(object info, object media, object author,
string username, Dictionary<string, long> users = null); List<string> selectedProperties,
string username, Dictionary<string, long>? users = null);
} }