@@ -13,7 +13,7 @@ internal static class IOManager
1313 /// <summary>
1414 /// Array of characters that are not allowed in file names.
1515 /// </summary>
16- public static char [ ] InvalidFileNameChars = new char [ ]
16+ public static char [ ] InvalidFileNameCharsWindows = new char [ ]
1717 {
1818 '\" ' , '<' , '>' , '|' , '\0 ' ,
1919 ( char ) 1 , ( char ) 2 , ( char ) 3 , ( char ) 4 , ( char ) 5 , ( char ) 6 , ( char ) 7 , ( char ) 8 , ( char ) 9 , ( char ) 10 ,
@@ -22,6 +22,8 @@ internal static class IOManager
2222 ( char ) 31 , ':' , '?' , '\\ ' , '/'
2323 } ;
2424
25+ public static char [ ] InvalidFileNameCharsUnix = new char [ ] { '/' , '\0 ' } ;
26+
2527 /// <summary>
2628 /// Converts bytes to megabytes.
2729 /// </summary>
@@ -40,8 +42,12 @@ internal static class IOManager
4042 public static string RemoveInvalidFileNameChars ( string name )
4143 {
4244 StringBuilder fileBuilder = new ( name ) ;
43- foreach ( char c in InvalidFileNameChars )
44- fileBuilder . Replace ( c . ToString ( ) , string . Empty ) ;
45+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
46+ foreach ( char c in InvalidFileNameCharsWindows )
47+ fileBuilder . Replace ( c . ToString ( ) , string . Empty ) ;
48+ else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) || RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
49+ foreach ( char c in InvalidFileNameCharsUnix )
50+ fileBuilder . Replace ( c . ToString ( ) , string . Empty ) ;
4551 return fileBuilder . ToString ( ) ;
4652 }
4753
@@ -58,24 +64,32 @@ public static string RemoveInvalidFileNameChars(string name)
5864 public static bool IsValidPath ( string path ) => TryGetFullPath ( path , out _ ) ;
5965
6066
61- /// <summary>
62- /// Tries to get the absolute path for the specified path string.
63- /// </summary>
64- /// <param name="path">The file or directory for which to obtain absolute path information.</param>
65- /// <param name="result">
66- /// When this method returns, contains the absolute path representation of <paramref name="path"/>,
67- /// if the conversion succeeded, or <see cref="string.Empty"/> if the conversion failed.
68- /// </param>
69- /// <returns>
70- /// <c>true</c> if <paramref name="path"/> was converted to an absolute path successfully; otherwise, <c>false</c>.
71- /// </returns>
7267 public static bool TryGetFullPath ( string path , out string result )
7368 {
7469 result = string . Empty ;
75- if ( string . IsNullOrWhiteSpace ( path ) || path [ 1 ] != ':' )
70+
71+ if ( string . IsNullOrWhiteSpace ( path ) )
7672 return false ;
77- bool status = false ;
7873
74+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
75+ {
76+ if ( path . Length < 2 || path [ 1 ] != ':' )
77+ return false ;
78+ }
79+ else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) || RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
80+ {
81+ char [ ] invalidChars = Path . GetInvalidPathChars ( ) ;
82+ if ( path . IndexOfAny ( invalidChars ) >= 0 )
83+ return false ;
84+
85+ if ( Encoding . UTF8 . GetByteCount ( path ) > 4096 )
86+ return false ;
87+
88+ if ( ! path . StartsWith ( "/" ) && ! path . StartsWith ( "./" ) && ! path . StartsWith ( "../" ) )
89+ return false ;
90+ }
91+
92+ bool status = false ;
7993 try
8094 {
8195 result = Path . GetFullPath ( path ) ;
@@ -97,9 +111,11 @@ public static bool TryGetFullPath(string path, out string result)
97111 /// <exception cref="SecurityException">Thrown when the caller does not have the required permission to perform this operation.</exception>
98112 public static string ? GetHomePath ( )
99113 {
100- if ( Environment . OSVersion . Platform == PlatformID . Unix )
114+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
115+ return Environment . ExpandEnvironmentVariables ( "%HOMEDRIVE%%HOMEPATH%" ) ;
116+ else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) || RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
101117 return Environment . GetEnvironmentVariable ( "HOME" ) ;
102- return Environment . ExpandEnvironmentVariables ( "%HOMEDRIVE%%HOMEPATH% ") ;
118+ throw new PlatformNotSupportedException ( "The platform is not supported. ") ;
103119 }
104120
105121 /// <summary>
@@ -112,14 +128,7 @@ public static bool TryGetFullPath(string path, out string result)
112128 /// <exception cref="SecurityException">Thrown when the caller does not have the required permission to perform this operation.</exception>
113129 public static string ? GetDownloadFolderPath ( )
114130 {
115- if ( Environment . OSVersion . Platform == PlatformID . Unix )
116- {
117- string ? homePath = GetHomePath ( ) ;
118- if ( string . IsNullOrEmpty ( homePath ) )
119- return null ;
120- return Path . Combine ( homePath , "Downloads" ) ;
121- }
122- else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
131+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
123132 {
124133 string path = SHGetKnownFolderPath ( new ( "374DE290-123F-4565-9164-39C4925E467B" ) , 0 ) ;
125134 if ( string . IsNullOrEmpty ( path ) )
@@ -130,6 +139,13 @@ public static bool TryGetFullPath(string path, out string result)
130139 , string . Empty ) ) ;
131140 else return path ;
132141 }
142+ else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) || RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
143+ {
144+ string ? homePath = Environment . GetEnvironmentVariable ( "HOME" ) ;
145+ if ( string . IsNullOrEmpty ( homePath ) )
146+ return null ;
147+ return Path . Combine ( homePath , "Downloads" ) ;
148+ }
133149 else return null ;
134150 }
135151
0 commit comments