ClientEventLogger.cs 1.5 KB

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