PhysicalFileResult.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System;
  4. using System.IO;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore.Internal;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Microsoft.Extensions.Logging;
  9. namespace Microsoft.AspNetCore.Http.Result
  10. {
  11. /// <summary>
  12. /// A <see cref="PhysicalFileResult"/> on execution will write a file from disk to the response
  13. /// using mechanisms provided by the host.
  14. /// </summary>
  15. internal sealed partial class PhysicalFileResult : FileResult, IResult
  16. {
  17. /// <summary>
  18. /// Creates a new <see cref="PhysicalFileResult"/> instance with
  19. /// the provided <paramref name="fileName"/> and the provided <paramref name="contentType"/>.
  20. /// </summary>
  21. /// <param name="fileName">The path to the file. The path must be an absolute path.</param>
  22. /// <param name="contentType">The Content-Type header of the response.</param>
  23. public PhysicalFileResult(string fileName, string contentType)
  24. : base(contentType)
  25. {
  26. FileName = fileName;
  27. }
  28. /// <summary>
  29. /// Gets or sets the path to the file that will be sent back as the response.
  30. /// </summary>
  31. public string FileName { get; }
  32. // For testing
  33. public Func<string, FileInfoWrapper> GetFileInfoWrapper { get; init; } =
  34. static path => new FileInfoWrapper(path);
  35. public Task ExecuteAsync(HttpContext httpContext)
  36. {
  37. var fileInfo = GetFileInfoWrapper(FileName);
  38. if (!fileInfo.Exists)
  39. {
  40. throw new FileNotFoundException($"Could not find file: {FileName}", FileName);
  41. }
  42. var logger = httpContext.RequestServices.GetRequiredService<ILogger<PhysicalFileResult>>();
  43. Log.ExecutingFileResult(logger, this, FileName);
  44. var lastModified = LastModified ?? fileInfo.LastWriteTimeUtc;
  45. var fileResultInfo = new FileResultInfo
  46. {
  47. ContentType = ContentType,
  48. EnableRangeProcessing = EnableRangeProcessing,
  49. EntityTag = EntityTag,
  50. FileDownloadName = FileDownloadName,
  51. LastModified = lastModified,
  52. };
  53. var (range, rangeLength, serveBody) = FileResultHelper.SetHeadersAndLog(
  54. httpContext,
  55. fileResultInfo,
  56. fileInfo.Length,
  57. EnableRangeProcessing,
  58. lastModified,
  59. EntityTag,
  60. logger);
  61. if (!serveBody)
  62. {
  63. return Task.CompletedTask;
  64. }
  65. if (range != null && rangeLength == 0)
  66. {
  67. return Task.CompletedTask;
  68. }
  69. var response = httpContext.Response;
  70. if (!Path.IsPathRooted(FileName))
  71. {
  72. throw new NotSupportedException($"Path '{FileName}' was not rooted.");
  73. }
  74. if (range != null)
  75. {
  76. FileResultHelper.Log.WritingRangeToBody(logger);
  77. }
  78. var offset = 0L;
  79. var count = (long?)null;
  80. if (range != null)
  81. {
  82. offset = range.From ?? 0L;
  83. count = rangeLength;
  84. }
  85. return response.SendFileAsync(
  86. FileName,
  87. offset: offset,
  88. count: count);
  89. }
  90. internal readonly struct FileInfoWrapper
  91. {
  92. public FileInfoWrapper(string path)
  93. {
  94. var fileInfo = new FileInfo(path);
  95. Exists = fileInfo.Exists;
  96. Length = fileInfo.Length;
  97. LastWriteTimeUtc = fileInfo.LastWriteTimeUtc;
  98. }
  99. public bool Exists { get; init; }
  100. public long Length { get; init; }
  101. public DateTimeOffset LastWriteTimeUtc { get; init; }
  102. }
  103. }
  104. }