1
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2
2
3
- # file downloading
4
-
5
- if Sys. iswindows ()
6
- function download_powershell (url:: AbstractString , filename:: AbstractString )
7
- ps = joinpath (get (ENV , " SYSTEMROOT" , " C:\\ Windows" ), " System32\\ WindowsPowerShell\\ v1.0\\ powershell.exe" )
8
- tls12 = " [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12"
9
- client = " New-Object System.Net.Webclient"
10
- # in the following we escape ' with '' (see https://ss64.com/ps/syntax-esc.html)
11
- downloadfile = " ($client ).DownloadFile('$(replace (url, " '" => " ''" )) ', '$(replace (filename, " '" => " ''" )) ')"
12
- # PowerShell v3 or later is required for Tls12
13
- proc = run (pipeline (` $ps -Version 3 -NoProfile -Command "$tls12 ; $downloadfile "` ; stderr = stderr ); wait= false )
14
- if ! success (proc)
15
- if proc. exitcode % Int32 == - 393216
16
- # appears to be "wrong version" exit code, based on
17
- # https://docs.microsoft.com/en-us/azure/cloud-services/cloud-services-startup-tasks-common
18
- @error " Downloading files requires Windows Management Framework 3.0 or later."
19
- end
20
- pipeline_error (proc)
21
- end
22
- return filename
23
- end
24
- end
25
-
26
- function find_curl ()
27
- if Sys. isapple () && Sys. isexecutable (" /usr/bin/curl" )
28
- " /usr/bin/curl"
29
- elseif Sys. iswindows () && Sys. isexecutable (joinpath (get (ENV , " SYSTEMROOT" , " C:\\ Windows" ), " System32\\ curl.exe" ))
30
- joinpath (get (ENV , " SYSTEMROOT" , " C:\\ Windows" ), " System32\\ curl.exe" )
31
- elseif ! Sys. iswindows () && Sys. which (" curl" ) != = nothing
32
- " curl"
33
- else
34
- nothing
35
- end
36
- end
37
-
38
- function download_curl (curl_exe:: AbstractString , url:: AbstractString , filename:: AbstractString )
39
- err = PipeBuffer ()
40
- process = run (pipeline (` $curl_exe -s -S -g -L -f -o $filename $url ` , stderr = err), wait= false )
41
- if ! success (process)
42
- error_msg = readline (err)
43
- @error " Download failed: $error_msg "
44
- pipeline_error (process)
45
- end
46
- return filename
47
- end
48
-
49
3
const DOWNLOAD_HOOKS = Callable[]
50
4
51
5
function download_url (url:: AbstractString )
@@ -55,50 +9,25 @@ function download_url(url::AbstractString)
55
9
return url
56
10
end
57
11
58
- function download (url:: AbstractString , filename:: AbstractString )
59
- url = download_url (url)
60
- curl_exe = find_curl ()
61
- if curl_exe != = nothing
62
- return download_curl (curl_exe, url, filename)
63
- elseif Sys. iswindows ()
64
- return download_powershell (url, filename)
65
- elseif Sys. which (" wget" ) != = nothing
66
- try
67
- run (` wget -O $filename $url ` )
68
- catch
69
- rm (filename, force= true ) # wget always creates a file
70
- rethrow ()
71
- end
72
- elseif Sys. which (" busybox" ) != = nothing
73
- try
74
- run (` busybox wget -O $filename $url ` )
75
- catch
76
- rm (filename, force= true ) # wget always creates a file
77
- rethrow ()
78
- end
79
- elseif Sys. which (" fetch" ) != = nothing
80
- run (` fetch -f $filename $url ` )
81
- else
82
- error (" No download agent available; install curl, wget, busybox or fetch." )
83
- end
84
- return filename
85
- end
86
-
87
- function download (url:: AbstractString )
88
- filename = tempname ()
89
- download (url, filename)
90
- end
12
+ Downloads () = require (PkgId (
13
+ UUID ((0xf43a241f_c20a_4ad4 , 0x852c_f6b1247861c6 )),
14
+ " Downloads" ,
15
+ ))
91
16
92
17
"""
93
- download(url::AbstractString, [localfile ::AbstractString])
18
+ download(url::AbstractString, [path ::AbstractString = tempname()]) -> path
94
19
95
- Download a file from the given url, optionally renaming it to the given local file name. If
96
- no filename is given this will download into a randomly-named file in your temp directory.
97
- Note that this function relies on the availability of external tools such as `curl`, `wget`
98
- or `fetch` to download the file and is provided for convenience. For production use or
99
- situations in which more options are needed, please use a package that provides the desired
100
- functionality instead.
20
+ Download a file from the given url, saving it to the location `path`, or if not
21
+ specified, a temporary path. Returns the path of the downloaded file.
101
22
102
- Returns the filename of the downloaded file.
23
+ !!! note
24
+ Since Julia 1.6, this function is deprecated and is just a thin wrapper
25
+ around `Downloads.download`. In new code, you should use that function
26
+ directly instead of calling this.
103
27
"""
104
- download (url, filename)
28
+ function download (url:: AbstractString , path:: AbstractString )
29
+ invokelatest (Downloads (). download, download_url (url), path)
30
+ end
31
+ function download (url:: AbstractString )
32
+ invokelatest (Downloads (). download, download_url (url))
33
+ end
0 commit comments