Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MultipartSectionConverterExtensions.cs « src « WebUtilities « Http « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e690160ee68e92e0bb958433fa2bbdf90ecc7656 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNetCore.WebUtilities
{
    /// <summary>
    /// Various extensions for converting multipart sections
    /// </summary>
    public static class MultipartSectionConverterExtensions
    {
        /// <summary>
        /// Converts the section to a file section
        /// </summary>
        /// <param name="section">The section to convert</param>
        /// <returns>A file section</returns>
        public static FileMultipartSection? AsFileSection(this MultipartSection section)
        {
            if (section == null)
            {
                throw new ArgumentNullException(nameof(section));
            }

            try
            {
                return new FileMultipartSection(section);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Converts the section to a form section
        /// </summary>
        /// <param name="section">The section to convert</param>
        /// <returns>A form section</returns>
        public static FormMultipartSection? AsFormDataSection(this MultipartSection section)
        {
            if (section == null)
            {
                throw new ArgumentNullException(nameof(section));
            }

            try
            {
                return new FormMultipartSection(section);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Retrieves and parses the content disposition header from a section
        /// </summary>
        /// <param name="section">The section from which to retrieve</param>
        /// <returns>A <see cref="ContentDispositionHeaderValue"/> if the header was found, null otherwise</returns>
        public static ContentDispositionHeaderValue? GetContentDispositionHeader(this MultipartSection section)
        {
            if (!ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var header))
            {
                return null;
            }

            return header;
        }
    }
}