11using System ;
2+ using System . Collections . Generic ;
3+ using System . Diagnostics ;
24using System . IO ;
3- using System . Reflection ;
5+ using System . IO . Compression ;
6+ using System . Net ;
47using System . Runtime . InteropServices ;
8+ using System . Threading . Tasks ;
59
610namespace PuppeteerSharp
711{
812 public class Downloader
913 {
10- private string _downloadsFolder ;
14+ private readonly string _downloadsFolder ;
1115 private const string DefaultDownloadHost = "https://storage.googleapis.com" ;
16+ private static readonly Dictionary < Platform , string > _downloadUrls = new Dictionary < Platform , string > {
17+ { Platform . Linux , "{0}/chromium-browser-snapshots/Linux_x64/{1}/chrome-linux.zip" } ,
18+ { Platform . MacOS , "{0}/chromium-browser-snapshots/Mac/{1}/chrome-mac.zip" } ,
19+ { Platform . Win32 , "{0}/chromium-browser-snapshots/Win/{1}/chrome-win32.zip" } ,
20+ { Platform . Win64 , "{0}/chromium-browser-snapshots/Win_x64/{1}/chrome-win32.zip" }
21+ } ;
1222 private string _downloadHost ;
1323
1424 public Downloader ( string downloadsFolder )
@@ -17,24 +27,29 @@ public Downloader(string downloadsFolder)
1727 _downloadHost = DefaultDownloadHost ;
1828 }
1929
30+ #region Public Methods
31+
2032 public static Downloader CreateDefault ( )
2133 {
2234 var downloadsFolder = Path . Combine ( Directory . GetCurrentDirectory ( ) , ".local-chromium" ) ;
2335 return new Downloader ( downloadsFolder ) ;
2436 }
2537
26- internal static Platform CurrentPlatform ( )
38+ internal static Platform CurrentPlatform
2739 {
28- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
29- return Platform . MacOS ;
30- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
31- return Platform . Linux ;
32- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
33- return IntPtr . Size == 8 ? Platform . Win64 : Platform . Win32 ;
34- return Platform . Unknown ;
40+ get
41+ {
42+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
43+ return Platform . MacOS ;
44+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
45+ return Platform . Linux ;
46+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
47+ return IntPtr . Size == 8 ? Platform . Win64 : Platform . Win32 ;
48+ return Platform . Unknown ;
49+ }
3550 }
3651
37- internal RevisionInfo RevisionInfo ( Platform platform , string revision )
52+ internal RevisionInfo RevisionInfo ( Platform platform , int revision )
3853 {
3954 var result = new RevisionInfo
4055 {
@@ -45,7 +60,47 @@ internal RevisionInfo RevisionInfo(Platform platform, string revision)
4560 return result ;
4661 }
4762
48- private static string GetExecutablePath ( Platform platform , string folderPath )
63+ public async Task DownloadRevisionAsync ( int revision )
64+ {
65+ var url = string . Format ( _downloadUrls [ CurrentPlatform ] , _downloadHost , revision ) ;
66+ var zipPath = Path . Combine ( _downloadsFolder , $ "download-{ CurrentPlatform . ToString ( ) } -{ revision } .zip") ;
67+ var folderPath = GetFolderPath ( CurrentPlatform , revision ) ;
68+
69+ if ( new DirectoryInfo ( folderPath ) . Exists )
70+ {
71+ return ;
72+ }
73+
74+ var downloadFolder = new DirectoryInfo ( _downloadsFolder ) ;
75+ if ( ! downloadFolder . Exists )
76+ {
77+ downloadFolder . Create ( ) ;
78+ }
79+
80+ await new WebClient ( ) . DownloadFileTaskAsync ( new Uri ( url ) , zipPath ) ;
81+
82+ if ( CurrentPlatform == Platform . MacOS )
83+ {
84+ //ZipFile and many others unzip libraries have issues extracting .app files
85+ //Until we have a clear solution we'll call the native unzip tool
86+ //https://github.com/dotnet/corefx/issues/15516
87+ NativeExtractToDirectory ( zipPath , folderPath ) ;
88+ }
89+ else
90+ {
91+ ZipFile . ExtractToDirectory ( zipPath , folderPath ) ;
92+ }
93+
94+ new FileInfo ( zipPath ) . Delete ( ) ;
95+
96+ }
97+
98+ public string GetExecutablePath ( int revision )
99+ {
100+ return GetExecutablePath ( CurrentPlatform , GetFolderPath ( CurrentPlatform , revision ) ) ;
101+ }
102+
103+ public static string GetExecutablePath ( Platform platform , string folderPath )
49104 {
50105 switch ( platform )
51106 {
@@ -62,9 +117,24 @@ private static string GetExecutablePath(Platform platform, string folderPath)
62117 }
63118 }
64119
65- private string GetFolderPath ( Platform platform , string revision )
120+ #endregion
121+
122+ #region Private Methods
123+
124+ private string GetFolderPath ( Platform platform , int revision )
66125 {
67126 return Path . Combine ( _downloadsFolder , $ "{ platform . ToString ( ) } -{ revision } ") ;
68127 }
128+
129+ private void NativeExtractToDirectory ( string zipPath , string folderPath )
130+ {
131+ var process = new Process ( ) ;
132+ process . StartInfo . FileName = "unzip" ;
133+ process . StartInfo . Arguments = $ "{ zipPath } -d { folderPath } ";
134+ process . Start ( ) ;
135+ process . WaitForExit ( ) ;
136+ }
137+
138+ #endregion
69139 }
70140}
0 commit comments