1 #Region "Microsoft.VisualBasic::ceb6354319261aae8d03df0bc8bc2aac, Microsoft.VisualBasic.Core\Net\HTTP\DataURI.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     '     Class DataURI
35     
36     '         Properties: base64, chartSet, mime
37     
38     '         Constructor: (+3 OverloadsSub New
39     '         Function: FromFile, SVGImage, ToStream, ToString, URIParser
40     
41     
42     ' /********************************************************************************/
43
44 #End Region
45
46 Imports System.Drawing
47 Imports System.IO
48 Imports System.Runtime.CompilerServices
49 Imports Microsoft.VisualBasic.ApplicationServices
50 Imports Microsoft.VisualBasic.Net.Protocols
51 Imports Microsoft.VisualBasic.Text
52
53 Namespace Net.Http
54
55     ''' <summary>
56     ''' Data URI scheme
57     ''' </summary>
58     Public Class DataURI
59
60         ''' <summary>
61         ''' File mime type
62         ''' </summary>
63         Public ReadOnly Property mime As String
64         ''' <summary>
65         ''' The base64 string
66         ''' </summary>
67         Public ReadOnly Property base64 As String
68         Public ReadOnly Property chartSet As String
69
70         ''' <summary>
71         ''' 
72         ''' </summary>
73         ''' <param name="file"></param>
74         ''' <param name="codepage$">
75         ''' The chartset codepage name, by default is ``ASCII``.
76         ''' </param>
77         Sub New(file$, Optional codepage$ = Nothing)
78             mime = Strings.LCase(file.FileMimeType.MIMEType)
79             base64 = file.ReadBinary.ToBase64String
80             codepage = codepage
81         End Sub
82
83         <MethodImpl(MethodImplOptions.AggressiveInlining)>
84         Sub New(image As Image)
85             Call Me.New(image.ToBase64String, ContentTypes.MIME.Png, Nothing)
86         End Sub
87
88         Public Sub New(base64$, mine$, Optional charset$ = Nothing)
89             Me.base64 = base64
90             Me.mime = mime
91             Me.chartSet = charset
92         End Sub
93
94         ''' <summary>
95         ''' <see cref="Convert.FromBase64String"/>
96         ''' </summary>
97         ''' <returns></returns>
98         Public Function ToStream() As Stream
99             Return New MemoryStream(Convert.FromBase64String(base64))
100         End Function
101
102         <MethodImpl(MethodImplOptions.AggressiveInlining)>
103         Public Shared Function FromFile(file As StringAs DataURI
104             Return New DataURI(file)
105         End Function
106
107         <MethodImpl(MethodImplOptions.AggressiveInlining)>
108         Public Shared Function SVGImage(svg As StringAs DataURI
109             Return New DataURI(base64:=TextEncodings.UTF8WithoutBOM.GetBytes(svg).ToBase64String, mine:="image/svg+xml")
110         End Function
111
112         Public Shared Function URIParser(uri As StringAs DataURI
113             Dim t = uri.Split(";"c) _
114                 .Select(Function(p) p.StringSplit("[:=,]")) _
115                 .ToDictionary(Function(k) k(0).ToLower,
116                               Function(value) value(1))
117
118             Return New DataURI(
119                 base64:=t.TryGetValue("base64"),
120                 charset:=t.TryGetValue("charset"),
121                 mine:=t.TryGetValue("data")
122             )
123         End Function
124
125         ''' <summary>
126         ''' Output this data uri string text
127         ''' </summary>
128         ''' <returns></returns>
129         <MethodImpl(MethodImplOptions.AggressiveInlining)>
130         Public Overrides Function ToString() As String
131             If chartSet.StringEmpty Then
132                 Return $"data:{mime};base64,{base64}"
133             Else
134                 Return $"data:{mime};charset={chartSet};base64,{base64}"
135             End If
136         End Function
137     End Class
138 End Namespace