1 | #Region "Microsoft.VisualBasic::3d1bbe2951d4f0384bac47ae53e926de, Microsoft.VisualBasic.Core\Net\MIME\MIME.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Module MIME |
35 | ' |
36 | ' Properties: ContentTypes, SuffixTable, UnknownType |
37 | ' |
38 | ' Constructor: (+1 Overloads) Sub New |
39 | ' Function: __loadContents |
40 | ' |
41 | ' |
42 | ' /********************************************************************************/ |
43 | |
44 | #End Region |
45 | |
46 | Imports System.Runtime.CompilerServices |
47 | |
48 | Namespace Net.Protocols.ContentTypes |
49 | |
50 | ''' <summary> |
51 | ''' MIME stands for "Multipurpose Internet Mail Extensions. It's a way of identifying files on the Internet according to their nature and format. |
52 | ''' For example, using the ``Content-type`` header value defined in a HTTP response, the browser can open the file with the proper extension/plugin. |
53 | ''' (http://www.freeformatter.com/mime-types-list.html) |
54 | ''' </summary> |
55 | Public Module MIME |
56 | |
57 | ''' <summary> |
58 | ''' 枚举出所有已知的文件拓展名列表,Key全部都是小写的 (格式: ``.ext``) |
59 | ''' </summary> |
60 | ''' <returns></returns> |
61 | Public ReadOnly Property SuffixTable As IReadOnlyDictionary(Of String, ContentType) |
62 | ''' <summary> |
63 | ''' 根据类型来枚举,Key全部都是小写的 |
64 | ''' </summary> |
65 | ''' <returns></returns> |
66 | Public ReadOnly Property ContentTypes As IReadOnlyDictionary(Of String, ContentType) |
67 | |
68 | ''' <summary> |
69 | ''' .*( 二进制流,不知道下载文件类型) |
70 | ''' </summary> |
71 | Public Const Unknown As String = "application/octet-stream" |
72 | Public Const MsDownload As String = "application/x-msdownload" |
73 | Public Const Json As String = "application/json" |
74 | Public Const ZIP As String = "application/zip" |
75 | Public Const Png As String = "image/png" |
76 | |
77 | Public ReadOnly Property UnknownType As New ContentType With { |
78 | .FileExt = "*.*", |
79 | .MIMEType = Unknown, |
80 | .Name = NameOf(Unknown) |
81 | } |
82 | |
83 | Sub New() |
84 | SuffixTable = My.Resources _ |
85 | .List_of_MIME_types___Internet_Media_Types_ _ |
86 | .LineTokens _ |
87 | .__loadContents _ |
88 | .Where(Function(x) Not x.IsEmpty) _ |
89 | .GroupBy(Function(x) x.FileExt.ToLower) _ |
90 | .ToDictionary(Function(x) x.Key, |
91 | Function(x) x.First) |
92 | ContentTypes = SuffixTable.Values _ |
93 | .ToDictionary(Function(x) x.MIMEType.ToLower) |
94 | End Sub |
95 | |
96 | <Extension> |
97 | Private Iterator Function __loadContents(lines As IEnumerable(Of String)) As IEnumerable(Of ContentType) |
98 | lines = From line As String |
99 | In lines.Skip(1) |
100 | Where Not String.IsNullOrWhiteSpace(line) |
101 | Select line |
102 | |
103 | For Each line$ In lines |
104 | Try |
105 | ' 2016-11-28 |
106 | ' Not sure why a bugs happed here, there is no bugs here before! |
107 | Yield ContentType.__createObject(line) |
108 | Catch ex As Exception |
109 | #If DEBUG Then |
110 | Call line.Warning |
111 | #End If |
112 | End Try |
113 | Next |
114 | End Function |
115 | End Module |
116 | End Namespace |