|
7 | 7 | // File name: LogStream.cs |
8 | 8 | // Repository: https://github.com/sisk-http/core |
9 | 9 |
|
| 10 | +using System.Globalization; |
10 | 11 | using System.Text; |
11 | 12 | using System.Threading.Channels; |
12 | 13 | using Sisk.Core.Entity; |
| 14 | +using Sisk.Core.Helpers; |
13 | 15 |
|
14 | 16 | namespace Sisk.Core.Http { |
15 | 17 | /// <summary> |
@@ -37,6 +39,22 @@ public class LogStream : IDisposable, IAsyncDisposable { |
37 | 39 | private static readonly Lazy<LogStream> _consoleOutputLazy = new Lazy<LogStream> ( () => new LogStream ( Console.Out ) ); |
38 | 40 | private static readonly Lazy<LogStream> _emptyLazy = new Lazy<LogStream> ( () => new LogStream () ); |
39 | 41 |
|
| 42 | + /// <summary> |
| 43 | + /// Converts the specified <see cref="DateTime"/> to a file-name-safe string representation |
| 44 | + /// formatted as "yyyy-MM-dd_HH-mm-ss". |
| 45 | + /// </summary> |
| 46 | + /// <param name="dt">The date and time to convert.</param> |
| 47 | + /// <param name="provider">An object that supplies culture-specific formatting information. Can be <see langword="null"/>.</param> |
| 48 | + /// <returns>A string that is safe for use in file names.</returns> |
| 49 | + public static string EscapeSafeDateTime ( DateTime dt, IFormatProvider? provider = null ) { |
| 50 | + return dt.ToString ( "yyyy-MM-dd_HH-mm-ss", provider ); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Gets the current local date and time as a file-name-safe string formatted as "yyyy-MM-dd_HH-mm-ss". |
| 55 | + /// </summary> |
| 56 | + public static string SafeTimestamp => EscapeSafeDateTime ( DateTime.Now, CultureInfo.InvariantCulture ); |
| 57 | + |
40 | 58 | /// <summary> |
41 | 59 | /// Gets a shared <see cref="LogStream"/> that writes its output to the <see cref="Console.Out"/> stream. |
42 | 60 | /// </summary> |
@@ -86,6 +104,10 @@ public string? FilePath { |
86 | 104 | if (value is not null) { |
87 | 105 | _filePath = Path.GetFullPath ( value ); |
88 | 106 |
|
| 107 | + if (!PathHelper.IsPathAllowed ( _filePath )) { |
| 108 | + throw new ArgumentException ( SR.Format ( SR.LogStream_InvalidFilePath, value ), nameof ( value ) ); |
| 109 | + } |
| 110 | + |
89 | 111 | string? dirPath = Path.GetDirectoryName ( _filePath ); |
90 | 112 | if (dirPath is not null) |
91 | 113 | Directory.CreateDirectory ( dirPath ); |
|
0 commit comments