| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | using System.Threading.Tasks; |
| | | 4 | | using Jellyfin.Extensions; |
| | | 5 | | |
| | | 6 | | namespace MediaBrowser.Controller.ClientEvent |
| | | 7 | | { |
| | | 8 | | /// <inheritdoc /> |
| | | 9 | | public class ClientEventLogger : IClientEventLogger |
| | | 10 | | { |
| | | 11 | | private readonly IServerApplicationPaths _applicationPaths; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Initializes a new instance of the <see cref="ClientEventLogger"/> class. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="applicationPaths">Instance of the <see cref="IServerApplicationPaths"/> interface.</param> |
| | | 17 | | public ClientEventLogger(IServerApplicationPaths applicationPaths) |
| | | 18 | | { |
| | 4 | 19 | | _applicationPaths = applicationPaths; |
| | 4 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public async Task<string> WriteDocumentAsync(string clientName, string clientVersion, Stream fileContents) |
| | | 24 | | { |
| | 4 | 25 | | var safeClientName = PathHelper.GetSafeLeafFileName(clientName) ?? "unknown-client"; |
| | 4 | 26 | | var safeClientVersion = PathHelper.GetSafeLeafFileName(clientVersion) ?? "unknown-version"; |
| | 4 | 27 | | var fileName = $"upload_{safeClientName}_{safeClientVersion}_{DateTime.UtcNow:yyyyMMddHHmmss}_{Guid.NewGuid( |
| | 4 | 28 | | var logFilePath = Path.Combine(_applicationPaths.LogDirectoryPath, fileName); |
| | 4 | 29 | | if (!PathHelper.IsContainedIn(_applicationPaths.LogDirectoryPath, logFilePath)) |
| | | 30 | | { |
| | 0 | 31 | | throw new ArgumentException("Path resolved to filename not in log directory"); |
| | | 32 | | } |
| | | 33 | | |
| | 4 | 34 | | var fileStream = new FileStream(logFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.None); |
| | 4 | 35 | | await using (fileStream.ConfigureAwait(false)) |
| | | 36 | | { |
| | 4 | 37 | | await fileContents.CopyToAsync(fileStream).ConfigureAwait(false); |
| | 4 | 38 | | return fileName; |
| | | 39 | | } |
| | 4 | 40 | | } |
| | | 41 | | } |
| | | 42 | | } |