2929
3030using System ;
3131using System . IO ;
32+ using System . Net . Http ;
3233using System . Threading ;
3334using System . Threading . Tasks ;
3435
@@ -38,35 +39,82 @@ partial class Downloader
3839{
3940 internal sealed class WebDownloader ( Fetcher . WebFetcher fetcher ) : Downloader
4041 {
42+ #region 私有字段
43+ private Func < HttpClient , Package , CancellationToken , ValueTask < Stream > > _downloader ;
44+ private readonly Func < HttpClient , Package , CancellationToken , ValueTask < Stream > > [ ] _downloaders =
45+ [
46+ Download1Async ,
47+ Download2Async ,
48+ Download3Async ,
49+ Download4Async ,
50+ Download5Async ,
51+ Download6Async ,
52+ ] ;
53+
4154 private readonly Fetcher . WebFetcher _fetcher = fetcher ?? throw new ArgumentNullException ( nameof ( fetcher ) ) ;
55+ #endregion
4256
57+ #region 重写方法
4358 protected override async ValueTask < Stream > DownloadAsync ( Package package , CancellationToken cancellation )
4459 {
4560 var client = _fetcher . Client ;
4661 if ( client == null )
4762 return null ;
4863
49- var response = await client . GetAsync ( $ "Download/{ package . Name } /{ package . GetRuntimeIdentifier ( ) } ", cancellation ) ;
50- if ( response != null && response . IsSuccessStatusCode )
51- return await response . Content . ReadAsStreamAsync ( cancellation ) ;
64+ if ( package . Properties . TryGetValue ( "url" , out var url ) && url != null )
65+ {
66+ var stream = await DownloadAsync ( client , url , cancellation ) ;
67+
68+ if ( stream != null )
69+ return stream ;
70+ }
71+
72+ if ( package . Properties . TryGetValue ( "download" , out url ) && url != null )
73+ {
74+ var stream = await DownloadAsync ( client , url , cancellation ) ;
5275
53- response = await client . GetAsync ( $ "packages/ { package . Name } / { package . Version } / { package . GetRuntimeIdentifier ( ) } / { Path . GetFileName ( package . Path ) } " , cancellation ) ;
54- if ( response != null && response . IsSuccessStatusCode )
55- return await response . Content . ReadAsStreamAsync ( cancellation ) ;
76+ if ( stream != null )
77+ return stream ;
78+ }
5679
57- response = await client . GetAsync ( $ "packages/{ package . Name } /{ package . Version } /{ Path . GetFileName ( package . Path ) } ", cancellation ) ;
58- if ( response != null && response . IsSuccessStatusCode )
59- return await response . Content . ReadAsStreamAsync ( cancellation ) ;
80+ if ( _downloader != null )
81+ return await _downloader ( client , package , cancellation ) ;
6082
61- response = await client . GetAsync ( $ "packages/ { Path . GetFileName ( package . Path ) } " , cancellation ) ;
62- if ( response != null && response . IsSuccessStatusCode )
63- return await response . Content . ReadAsStreamAsync ( cancellation ) ;
83+ for ( int i = 0 ; i < _downloaders . Length ; i ++ )
84+ {
85+ var stream = await _downloaders [ i ] ( client , package , cancellation ) ;
6486
65- response = await client . GetAsync ( $ "packages/{ package . Name } @{ package . Version } _{ package . GetRuntimeIdentifier ( ) } { Path . GetExtension ( package . Path ) } ", cancellation ) ;
66- if ( response != null && response . IsSuccessStatusCode )
67- return await response . Content . ReadAsStreamAsync ( cancellation ) ;
87+ if ( stream != null )
88+ {
89+ _downloader = _downloaders [ i ] ;
90+ return stream ;
91+ }
92+ }
6893
6994 return null ;
7095 }
96+ #endregion
97+
98+ #region 私有方法
99+ static ValueTask < Stream > Download1Async ( HttpClient client , Package package , CancellationToken cancellation ) => DownloadAsync ( client , $ "Download/{ package . Name } /{ package . GetRuntimeIdentifier ( ) } ", cancellation ) ;
100+ static ValueTask < Stream > Download2Async ( HttpClient client , Package package , CancellationToken cancellation ) => DownloadAsync ( client , $ "packages/{ package . Name } /{ package . Version } /{ package . GetRuntimeIdentifier ( ) } /{ Path . GetFileName ( package . Path ) } ", cancellation ) ;
101+ static ValueTask < Stream > Download3Async ( HttpClient client , Package package , CancellationToken cancellation ) => DownloadAsync ( client , $ "packages/{ package . Name } /{ package . Version } /{ Path . GetFileName ( package . Path ) } ", cancellation ) ;
102+ static ValueTask < Stream > Download4Async ( HttpClient client , Package package , CancellationToken cancellation ) => DownloadAsync ( client , $ "packages/{ Path . GetFileName ( package . Path ) } ", cancellation ) ;
103+ static ValueTask < Stream > Download5Async ( HttpClient client , Package package , CancellationToken cancellation ) => DownloadAsync ( client , $ "packages/{ package . Name } @{ package . Version } _{ package . GetRuntimeIdentifier ( ) } { Path . GetExtension ( package . Path ) } ", cancellation ) ;
104+ static ValueTask < Stream > Download6Async ( HttpClient client , Package package , CancellationToken cancellation )
105+ {
106+ var path = Path . IsPathFullyQualified ( package . Path ) ?
107+ Path . GetRelativePath ( Path . GetPathRoot ( package . Path ) , package . Path ) : package . Path ;
108+ return DownloadAsync ( client , path , cancellation ) ;
109+ }
110+
111+ static async ValueTask < Stream > DownloadAsync ( HttpClient client , string url , CancellationToken cancellation )
112+ {
113+ var response = await client . GetAsync ( url , cancellation ) ;
114+
115+ return response != null && response . IsSuccessStatusCode ?
116+ await response . Content . ReadAsStreamAsync ( cancellation ) : null ;
117+ }
118+ #endregion
71119 }
72120}
0 commit comments