< Summary

Information
Class: Elsa.Http.HttpFile
Assembly: Elsa.Http
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Models/HttpFile.cs
Line coverage
34%
Covered lines: 10
Uncovered lines: 19
Coverable lines: 29
Total lines: 84
Line coverage: 34.4%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%210%
.ctor(...)100%11100%
get_Stream()100%11100%
get_Filename()100%11100%
get_ContentType()100%11100%
get_ETag()100%11100%
GetBytes()0%620%
GetStreamContent()0%7280%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Models/HttpFile.cs

#LineLine coverage
 1using System.Net.Http.Headers;
 2using System.Text.Json.Serialization;
 3
 4namespace Elsa.Http;
 5
 6/// <summary>
 7/// Represents a downloadable object.
 8/// </summary>
 9public class HttpFile
 10{
 11    /// <summary>
 12    /// Initializes a new instance of the <see cref="Downloadable"/> class.
 13    /// </summary>
 14    [JsonConstructor]
 015    public HttpFile()
 16    {
 017    }
 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>
 1626    public HttpFile(Stream stream, string? filename = default, string? contentType = default, string? eTag = default)
 27    {
 1628        Stream = stream;
 1629        Filename = filename;
 1630        ContentType = contentType;
 1631        ETag = eTag;
 1632    }
 33
 34    /// <summary>
 35    /// The file stream.
 36    /// </summary>
 3237    public Stream Stream { get; set; } = default!;
 38
 39    /// <summary>
 40    /// The filename.
 41    /// </summary>
 2242    public string? Filename { get; set; }
 43
 44    /// <summary>
 45    /// The content type.
 46    /// </summary>
 1747    public string? ContentType { get; set; }
 48
 49    /// <summary>
 50    /// The ETag.
 51    /// </summary>
 1652    public string? ETag { get; set; }
 53
 54    /// <summary>
 55    /// Gets the file bytes.
 56    /// </summary>
 57    public byte[] GetBytes()
 58    {
 059        using var memoryStream = new MemoryStream();
 060        if (Stream.CanSeek) Stream.Seek(0, SeekOrigin.Begin);
 061        Stream.CopyTo(memoryStream);
 062        return memoryStream.ToArray();
 063    }
 64
 65    public StreamContent GetStreamContent()
 66    {
 067        if (Stream.CanSeek) Stream.Seek(0, SeekOrigin.Begin);
 068        var content = new StreamContent(Stream);
 69
 070        if (!string.IsNullOrWhiteSpace(Filename))
 71        {
 072            content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
 073            {
 074                Name = Filename,
 075                FileName = Filename,
 076                FileNameStar = Filename
 077            };
 78        }
 79
 080        if (!string.IsNullOrWhiteSpace(ContentType)) content.Headers.ContentType = new MediaTypeHeaderValue(ContentType)
 081        if (!string.IsNullOrWhiteSpace(ETag)) content.Headers.TryAddWithoutValidation("ETag", ETag);
 082        return content;
 83    }
 84}