| | | 1 | | using System.Net.Http.Headers; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Http; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Represents a downloadable object. |
| | | 8 | | /// </summary> |
| | | 9 | | public class HttpFile |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Initializes a new instance of the <see cref="Downloadable"/> class. |
| | | 13 | | /// </summary> |
| | | 14 | | [JsonConstructor] |
| | 0 | 15 | | public HttpFile() |
| | | 16 | | { |
| | 0 | 17 | | } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new instance of the <see cref="HttpFile"/> class. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="stream">The stream to download.</param> |
| | | 23 | | /// <param name="filename">The filename to use when downloading the stream.</param> |
| | | 24 | | /// <param name="contentType">The content type to use when downloading the stream.</param> |
| | | 25 | | /// <param name="eTag">The ETag to use when downloading the stream.</param> |
| | 16 | 26 | | public HttpFile(Stream stream, string? filename = default, string? contentType = default, string? eTag = default) |
| | | 27 | | { |
| | 16 | 28 | | Stream = stream; |
| | 16 | 29 | | Filename = filename; |
| | 16 | 30 | | ContentType = contentType; |
| | 16 | 31 | | ETag = eTag; |
| | 16 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// The file stream. |
| | | 36 | | /// </summary> |
| | 32 | 37 | | public Stream Stream { get; set; } = default!; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// The filename. |
| | | 41 | | /// </summary> |
| | 22 | 42 | | public string? Filename { get; set; } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// The content type. |
| | | 46 | | /// </summary> |
| | 17 | 47 | | public string? ContentType { get; set; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// The ETag. |
| | | 51 | | /// </summary> |
| | 16 | 52 | | public string? ETag { get; set; } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets the file bytes. |
| | | 56 | | /// </summary> |
| | | 57 | | public byte[] GetBytes() |
| | | 58 | | { |
| | 0 | 59 | | using var memoryStream = new MemoryStream(); |
| | 0 | 60 | | if (Stream.CanSeek) Stream.Seek(0, SeekOrigin.Begin); |
| | 0 | 61 | | Stream.CopyTo(memoryStream); |
| | 0 | 62 | | return memoryStream.ToArray(); |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | public StreamContent GetStreamContent() |
| | | 66 | | { |
| | 0 | 67 | | if (Stream.CanSeek) Stream.Seek(0, SeekOrigin.Begin); |
| | 0 | 68 | | var content = new StreamContent(Stream); |
| | | 69 | | |
| | 0 | 70 | | if (!string.IsNullOrWhiteSpace(Filename)) |
| | | 71 | | { |
| | 0 | 72 | | content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") |
| | 0 | 73 | | { |
| | 0 | 74 | | Name = Filename, |
| | 0 | 75 | | FileName = Filename, |
| | 0 | 76 | | FileNameStar = Filename |
| | 0 | 77 | | }; |
| | | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | if (!string.IsNullOrWhiteSpace(ContentType)) content.Headers.ContentType = new MediaTypeHeaderValue(ContentType) |
| | 0 | 81 | | if (!string.IsNullOrWhiteSpace(ETag)) content.Headers.TryAddWithoutValidation("ETag", ETag); |
| | 0 | 82 | | return content; |
| | | 83 | | } |
| | | 84 | | } |