Skip to content

Commit

Permalink
escape invalid chars for path in GetFriendlyName by @pkuislm Fix #104
Browse files Browse the repository at this point in the history
  • Loading branch information
UlyssesWu committed Mar 26, 2023
1 parent 5bb6cce commit 91f573f
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions FreeMote.Psb/Resources/ImageMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,18 @@ public override string ToString()
return $"{Part}/{Name}";
}

/// <summary>
/// Remove invalid chars in string for path
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private static string EscapeStringForPath(string name)
{
var invalidChars = Path.GetInvalidFileNameChars();
var n = name.IndexOfAny(invalidChars) != -1 ? string.Join("_", name.Split(invalidChars)) : name;
return n.StartsWith("..") ? n.TrimStart('.') : n;
}

/// <summary>
/// Name for export and import
/// </summary>
Expand All @@ -321,7 +333,7 @@ public string GetFriendlyName(PsbType type)
{
if (type == PsbType.Pimg && !string.IsNullOrWhiteSpace(Name))
{
return Path.GetFileNameWithoutExtension(Name);
return Path.GetFileNameWithoutExtension(EscapeStringForPath(Name));
}

if (string.IsNullOrWhiteSpace(Name) && string.IsNullOrWhiteSpace(Part))
Expand All @@ -334,7 +346,7 @@ public string GetFriendlyName(PsbType type)
return "";
}

return $"{Part}{Consts.ResourceNameDelimiter}{Name}";
return $"{EscapeStringForPath(Part)}{Consts.ResourceNameDelimiter}{EscapeStringForPath(Name)}";
}

internal byte[] LoadImageBytes(string path, FreeMountContext context,
Expand Down

0 comments on commit 91f573f

Please sign in to comment.