@@ -8,15 +8,16 @@ namespace dotnet_unpkg
88{
99 public static class Download
1010 {
11+ private static readonly char [ ] SplitChar = { '/' } ;
1112 private static readonly HttpClient Client = new HttpClient
1213 {
1314 BaseAddress = new Uri ( "https://unpkg.com" )
1415 } ;
1516 private static readonly string BaseDirectory = Path . Combine ( "wwwroot" , "lib" ) ;
1617
17- public static async Task < string > DistFile ( string package , string path )
18+ public static async Task < ( string , string ) > DistFile ( string package , string path )
1819 {
19- var target = TargetFile ( path ) ;
20+ var target = TargetFile ( package , path ) ;
2021 using ( var response = await Client . GetAsync ( path ) )
2122 {
2223 if ( response . IsSuccessStatusCode )
@@ -36,23 +37,82 @@ public static async Task<string> DistFile(string package, string path)
3637 }
3738
3839 Console . WriteLine ( $ "{ response . RequestMessage . RequestUri } ... OK") ;
39- return localPath ;
40+ return ( response . RequestMessage . RequestUri . ToString ( ) , localPath ) ;
4041 }
4142 else
4243 {
4344 Console . WriteLine ( $ "{ response . RequestMessage . RequestUri } ... failed ({ ( int ) response . StatusCode } )") ;
44- return null ;
45+ return default ;
4546 }
4647 }
4748 }
4849
49- private static string TargetFile ( string path ) => path . Contains ( "/dist/" )
50- ? string . Join ( Path . DirectorySeparatorChar ,
51- path . Split ( new [ ] { '/' } , StringSplitOptions . RemoveEmptyEntries )
52- . SkipWhile ( s => ! s . Equals ( "dist" , StringComparison . OrdinalIgnoreCase ) )
53- . Skip ( 1 ) )
54- : string . Join ( Path . DirectorySeparatorChar ,
55- path . Split ( new [ ] { '/' } , StringSplitOptions . RemoveEmptyEntries )
56- . Skip ( 1 ) ) ;
50+ public static async Task RestoreDistFile ( string url , string path )
51+ {
52+ using ( var response = await Client . GetAsync ( url ) )
53+ {
54+ if ( response . IsSuccessStatusCode )
55+ {
56+ path = path . Replace ( '/' , Path . DirectorySeparatorChar ) ;
57+ var directory = Path . GetDirectoryName ( path ) ;
58+ if ( ! Directory . Exists ( directory ) )
59+ {
60+ Directory . CreateDirectory ( directory ) ;
61+ }
62+
63+ using ( var fileStream = File . Create ( path ) )
64+ {
65+ await response . Content . CopyToAsync ( fileStream ) ;
66+ }
67+
68+ Console . WriteLine ( $ "{ response . RequestMessage . RequestUri } ... OK") ;
69+ }
70+ else
71+ {
72+ Console . WriteLine ( $ "{ response . RequestMessage . RequestUri } ... failed ({ ( int ) response . StatusCode } )") ;
73+ }
74+ }
75+ }
76+
77+ private static string TargetFile ( string package , string path )
78+ {
79+ var packageEnd = package . Split ( SplitChar , StringSplitOptions . RemoveEmptyEntries ) . Last ( ) ;
80+ var pathParts = path . Split ( new [ ] { '/' } , StringSplitOptions . RemoveEmptyEntries ) . ToArray ( ) ;
81+
82+ if ( pathParts . Contains ( "dist" ) )
83+ {
84+ pathParts = pathParts
85+ . SkipWhile ( s => ! s . Equals ( "dist" , StringComparison . OrdinalIgnoreCase ) )
86+ . Skip ( 1 ) . ToArray ( ) ;
87+
88+ switch ( pathParts . Length )
89+ {
90+ case 0 :
91+ return path ;
92+ case 1 :
93+ return pathParts [ 0 ] ;
94+ default :
95+ if ( pathParts [ 0 ] . Equals ( packageEnd , StringComparison . OrdinalIgnoreCase ) )
96+ {
97+ pathParts = pathParts . Skip ( 1 ) . ToArray ( ) ;
98+ }
99+
100+ break ;
101+ }
102+ // return string.Join(Path.DirectorySeparatorChar,
103+ // pathParts
104+ // .SkipWhile(s => !s.Equals("dist", StringComparison.OrdinalIgnoreCase))
105+ // .Skip(1));
106+ }
107+ else
108+ {
109+ pathParts = pathParts . Skip ( 1 ) . ToArray ( ) ;
110+ // return string.Join(Path.DirectorySeparatorChar,
111+ // pathParts
112+ // .Skip(1));
113+ }
114+
115+ return string . Join ( Path . DirectorySeparatorChar , pathParts ) ;
116+ }
57117 }
58118}
0 commit comments