| | | 1 | | using System.Net; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.Models; |
| | | 4 | | /// <summary> |
| | | 5 | | /// Options for Content-Disposition header. |
| | | 6 | | /// </summary> |
| | | 7 | | public class ContentDispositionOptions |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Initializes a new instance of the <see cref="ContentDispositionOptions"/> class. |
| | | 11 | | /// </summary> |
| | 103 | 12 | | public ContentDispositionOptions() |
| | | 13 | | { |
| | 103 | 14 | | FileName = null; |
| | 103 | 15 | | Type = ContentDispositionType.NoContentDisposition; |
| | 103 | 16 | | } |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Gets or sets the file name to use in the Content-Disposition header. |
| | | 20 | | /// </summary> |
| | 121 | 21 | | public string? FileName { get; set; } |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets or sets the type of Content-Disposition header to use. |
| | | 24 | | /// </summary> |
| | 142 | 25 | | public ContentDispositionType Type { get; set; } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Returns the Content-Disposition header value as a string, based on the type and file name. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <returns>The Content-Disposition header value, or an empty string if no disposition is set.</returns> |
| | | 31 | | public override string ToString() |
| | | 32 | | { |
| | 4 | 33 | | if (Type == ContentDispositionType.NoContentDisposition) |
| | | 34 | | { |
| | 1 | 35 | | return string.Empty; |
| | | 36 | | } |
| | | 37 | | |
| | 3 | 38 | | var disposition = Type == ContentDispositionType.Attachment ? "attachment" : "inline"; |
| | 3 | 39 | | if (string.IsNullOrEmpty(FileName)) |
| | | 40 | | { |
| | 2 | 41 | | return disposition; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | // Escape the filename to handle special characters |
| | 1 | 45 | | var escapedFileName = WebUtility.UrlEncode(FileName); |
| | 1 | 46 | | return $"{disposition}; filename=\"{escapedFileName}\""; |
| | | 47 | | } |
| | | 48 | | } |