Debian Codegraph @

man/multi_download.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/multi_download.R
3 \name{multi_download}
4 \alias{multi_download}
5 \title{Advanced download interface}
6 \usage{
7 multi_download(
8 urls,
9 destfiles = NULL,
10 resume = FALSE,
11 progress = TRUE,
12 multi_timeout = Inf,
13 multiplex = TRUE,
14 ...
15 )
16 }
17 \arguments{
18 \item{urls}{vector with URLs to download. Alternatively it may also be a
19 list of \link[=new_handle]{handle} objects that have the \code{url} option already set.}
20
21 \item{destfiles}{vector (of equal length as \code{urls}) with paths of output files,
22 or \code{NULL} to use \link{basename} of urls.}
23
24 \item{resume}{if the file already exists, resume the download. Note that this may
25 change server responses, see details.}
26
27 \item{progress}{print download progress information}
28
29 \item{multi_timeout}{in seconds, passed to \link{multi_run}}
30
31 \item{multiplex}{passed to \link{new_pool}}
32
33 \item{...}{extra handle options passed to each request \link{new_handle}}
34 }
35 \value{
36 The function returns a data frame with one row for each downloaded file and
37 the following columns:
38 \itemize{
39 \item \code{success} if the HTTP request was successfully performed, regardless of the
40 response status code. This is \code{FALSE} in case of a network error, or in case
41 you tried to resume from a server that did not support this. A value of \code{NA}
42 means the download was interrupted while in progress.
43 \item \code{status_code} the HTTP status code from the request. A successful download is
44 usually \code{200} for full requests or \code{206} for resumed requests. Anything else
45 could indicate that the downloaded file contains an error page instead of the
46 requested content.
47 \item \code{resumefrom} the file size before the request, in case a download was resumed.
48 \item \code{url} final url (after redirects) of the request.
49 \item \code{destfile} downloaded file on disk.
50 \item \code{error} if \code{success == FALSE} this column contains an error message.
51 \item \code{type} the \code{Content-Type} response header value.
52 \item \code{modified} the \code{Last-Modified} response header value.
53 \item \code{time} total elapsed download time for this file in seconds.
54 \item \code{headers} vector with http response headers for the request.
55 }
56 }
57 \description{
58 Download multiple files concurrently, with support for resuming large files.
59 This function is based on \code{\link[=multi_run]{multi_run()}} and hence does not error in case any
60 of the individual requests fail; you should inspect the return value to find
61 out which of the downloads were completed successfully.
62 }
63 \details{
64 Upon completion of all requests, this function returns a data frame with results.
65 The \code{success} column indicates if a request was successfully completed (regardless
66 of the HTTP status code). If it failed, e.g. due to a networking issue, the error
67 message is in the \code{error} column. A \code{success} value \code{NA} indicates that the request
68 was still in progress when the function was interrupted or reached the elapsed
69 \code{multi_timeout} and perhaps the download can be resumed if the server supports it.
70
71 It is also important to inspect the \code{status_code} column to see if any of the
72 requests were successful but had a non-success HTTP code, and hence the downloaded
73 file probably contains an error page instead of the requested content.
74
75 Note that when you set \code{resume = TRUE} you should expect HTTP-206 or HTTP-416
76 responses. The latter could indicate that the file was already complete, hence
77 there was no content left to resume from the server. If you try to resume a file
78 download but the server does not support this, success if \code{FALSE} and the file
79 will not be touched. In fact, if we request to a download to be resumed and the
80 server responds \verb{HTTP 200} instead of \verb{HTTP 206}, libcurl will error and not
81 download anything, because this probably means the server did not respect our
82 range request and is sending us the full file.
83 \subsection{About HTTP/2}{
84
85 Availability of HTTP/2 can increase the performance when making many parallel
86 requests to a server, because HTTP/2 can multiplex many requests over a single
87 TCP connection. Support for HTTP/2 depends on the version of \code{libcurl} that
88 your system has, and the TLS back-end that is in use, check \link{curl_version}.
89 For clients or servers without HTTP/2, curl makes at most 6 connections per
90 host over which it distributes the queued downloads.
91
92 On Windows and MacOS you can switch the active TLS backend by setting an
93 environment variable \href{https://curl.se/libcurl/c/libcurl-env.html}{\code{CURL_SSL_BACKEND}}
94 in your \verb{~/.Renviron} file. On Windows you can switch between \code{SecureChannel}
95 (default) and \code{OpenSSL} where only the latter supports HTTP/2. On MacOS you
96 can use either \code{SecureTransport} or \code{LibreSSL}, the default varies by MacOS
97 version.
98 }
99 }
100 \examples{
101 \dontrun{
102 # Example: some large files
103 urls <- sprintf(
104 "https://d37ci6vzurychx.cloudfront.net/trip-data/yellow_tripdata_2021-\%02d.parquet", 1:12)
105 res <- multi_download(urls, resume = TRUE) # You can interrupt (ESC) and resume
106
107 # Example: revdep checker
108 # Download all reverse dependencies for the 'curl' package from CRAN:
109 pkg <- 'curl'
110 mirror <- 'https://cloud.r-project.org'
111 db <- available.packages(repos = mirror)
112 packages <- c(pkg, tools::package_dependencies(pkg, db = db, reverse = TRUE)[[pkg]])
113 versions <- db[packages,'Version']
114 urls <- sprintf("\%s/src/contrib/\%s_\%s.tar.gz", mirror, packages, versions)
115 res <- multi_download(urls)
116 all.equal(unname(tools::md5sum(res$destfile)), unname(db[packages, 'MD5sum']))
117 # And then you could use e.g.: tools:::check_packages_in_dir()
118
119 # Example: URL checker
120 pkg_url_checker <- function(dir){
121 db <- tools:::url_db_from_package_sources(dir)
122 res <- multi_download(db$URL, rep('/dev/null', nrow(db)), nobody=TRUE)
123 db$OK <- res$status_code == 200
124 db
125 }
126
127 # Use a local package source directory
128 pkg_url_checker(".")
129
130 }
131 }