< Summary

Information
Class: Elsa.Http.DownloadableContentHandlers.UrlDownloadableContentHandler
Assembly: Elsa.Http
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/DownloadableContentHandlers/UrlDownloadableContentHandler.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 27
Coverable lines: 27
Total lines: 62
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 18
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%
GetSupportsContent(...)0%2040%
GetDownloadableAsync(...)100%210%
DownloadAsync()0%110100%
GetFilename(...)0%620%
GetContentType(...)0%620%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/DownloadableContentHandlers/UrlDownloadableContentHandler.cs

#LineLine coverage
 1using Elsa.Http.Abstractions;
 2using Elsa.Http.Contexts;
 3using Elsa.Http.Options;
 4using Microsoft.AspNetCore.StaticFiles;
 5
 6namespace Elsa.Http.DownloadableContentHandlers;
 7
 8/// <summary>
 9/// Handles content that represents a downloadable URL.
 10/// </summary>
 11public class UrlDownloadableContentHandler : DownloadableContentHandlerBase
 12{
 13    private readonly IFileDownloader _fileDownloader;
 14    private readonly IContentTypeProvider _contentTypeProvider;
 15
 16    /// <inheritdoc />
 017    public UrlDownloadableContentHandler(IFileDownloader fileDownloader, IContentTypeProvider contentTypeProvider)
 18    {
 019        _fileDownloader = fileDownloader;
 020        _contentTypeProvider = contentTypeProvider;
 021    }
 22
 23    /// <inheritdoc />
 024    public override bool GetSupportsContent(object content) => (content is string url && url.StartsWith("http", StringCo
 25
 26    /// <inheritdoc />
 027    protected override Func<ValueTask<Downloadable>> GetDownloadableAsync(DownloadableContext context) => async () => aw
 28
 29    private async ValueTask<Downloadable> DownloadAsync(DownloadableContext context)
 30    {
 031        var url = context.Content is string s ? new Uri(s) : (Uri)context.Content;
 032        var cancellationToken = context.CancellationToken;
 033        var options = new FileDownloadOptions
 034        {
 035            // TODO: Uncomment the next two lines if we implement file caching for this handler.
 036            // ETag = context.Options.ETag,
 037            // Range = context.Options.Range
 038        };
 039        var response = await _fileDownloader.DownloadAsync(url, options, cancellationToken);
 040        var eTag = response.Headers.ETag?.Tag;
 041        var filename = GetFilename(response) ?? url.Segments.Last();
 042        var contentType = response.Content.Headers.ContentType?.MediaType ?? GetContentType(filename);
 043        var stream = await response.Content.ReadAsStreamAsync(cancellationToken);
 44
 045        return new Downloadable(stream, filename, contentType, eTag);
 046    }
 47
 48    private static string? GetFilename(HttpResponseMessage response)
 49    {
 050        if (!response.Content.Headers.TryGetValues("Content-Disposition", out var values))
 051            return null;
 52
 053        var contentDispositionString = string.Join("", values);
 054        var contentDisposition = new System.Net.Mime.ContentDisposition(contentDispositionString);
 055        return contentDisposition.FileName;
 56    }
 57
 58    private string GetContentType(string filename)
 59    {
 060        return _contentTypeProvider.TryGetContentType(filename, out var contentType) ? contentType : System.Net.Mime.Med
 61    }
 62}