1 | #Region "Microsoft.VisualBasic::765d3616374e97c0d4ce0e6392c22f29, Microsoft.VisualBasic.Core\Extensions\IO\Extensions\NetFile.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 NetFile |
35 | ' |
36 | ' Function: GetMapPath, MapNetFile, NetFileExists, OpenNetStream |
37 | ' |
38 | ' |
39 | ' /********************************************************************************/ |
40 | |
41 | #End Region |
42 | |
43 | Imports System.IO |
44 | Imports System.Net |
45 | Imports System.Runtime.CompilerServices |
46 | Imports Microsoft.VisualBasic.CommandLine.Reflection |
47 | Imports Microsoft.VisualBasic.Scripting.MetaData |
48 | Imports Microsoft.VisualBasic.Text |
49 | |
50 | Namespace FileIO |
51 | |
52 | <Package("IO.NetFile")> |
53 | Public Module NetFile |
54 | |
55 | ''' <summary> |
56 | ''' 将网络文件映射为本地文件,这个可以同时兼容http或者本地文件路径 |
57 | ''' </summary> |
58 | ''' <param name="url"></param> |
59 | ''' <returns></returns> |
60 | <ExportAPI("MapNetFile")> |
61 | <Extension> |
62 | Public Function MapNetFile(url As String) As String |
63 | Dim path As String = url.GetMapPath |
64 | |
65 | If path.FileExists Then |
66 | Return path |
67 | Else ' 下载数据然后缓存 |
68 | Call DownloadFile(url, path) |
69 | Return path |
70 | End If |
71 | End Function |
72 | |
73 | <Extension> |
74 | Public Function OpenNetStream(url$, Optional encoding As Encodings = Encodings.Default) As StreamReader |
75 | Dim path$ = url.GetMapPath |
76 | Dim stream As Stream |
77 | |
78 | If path.FileExists Then |
79 | Dim file As New FileStream(path, FileMode.Open) |
80 | stream = file |
81 | Else ' 网络文件 |
82 | Dim WebRequest As HttpWebRequest = HttpWebRequest.Create(url) |
83 | Dim WebResponse As WebResponse = WebRequest.GetResponse |
84 | stream = WebResponse.GetResponseStream |
85 | End If |
86 | |
87 | Dim out As New StreamReader(stream, encoding.CodePage) |
88 | Return out |
89 | End Function |
90 | |
91 | <ExportAPI("NetFile.FileExists")> |
92 | <Extension> |
93 | Public Function NetFileExists(url As String) As Boolean |
94 | Return url.GetMapPath.FileExists |
95 | End Function |
96 | |
97 | ''' <summary> |
98 | ''' 网络文件转换为本地文件路径 |
99 | ''' </summary> |
100 | ''' <param name="url"></param> |
101 | ''' <returns></returns> |
102 | ''' |
103 | <ExportAPI("Map.Path")> |
104 | <Extension> |
105 | Public Function GetMapPath(url As String) As String |
106 | If InStr(url, "http://", CompareMethod.Text) + |
107 | InStr(url, "https://", CompareMethod.Text) > 0 Then |
108 | |
109 | url = Strings.Split(url, "//").Last |
110 | |
111 | Dim tokens$() = url.Split("/"c) |
112 | Dim folders As String = tokens.Take(tokens.Length - 1).JoinBy("/") |
113 | |
114 | url = tokens.Last.NormalizePathString |
115 | url = App.AppSystemTemp & "/" & folders & "/" & url |
116 | |
117 | Call folders.MkDIR |
118 | |
119 | Return url |
120 | ElseIf InStr(url, "file://", CompareMethod.Text) = 1 Then |
121 | url = Mid(url, 8) |
122 | Return url |
123 | Else |
124 | If url.FileExists Then |
125 | Return url |
126 | Else |
127 | Throw New Exception(url & " is a unrecognized url path!") |
128 | End If |
129 | End If |
130 | End Function |
131 | End Module |
132 | End Namespace |