|
| 1 | +# FTP code for when the file transfer is complete. |
| 2 | +const complete_transfer_code = 226 |
| 3 | + |
1 | 4 | """ |
2 | 5 | FTP(; kwargs...) -> FTP |
3 | 6 |
|
@@ -110,6 +113,94 @@ function upload(ftp::FTP, local_file::IO, remote_name::AbstractString; mode::FTP |
110 | 113 | end |
111 | 114 |
|
112 | 115 |
|
| 116 | +""" |
| 117 | + upload( |
| 118 | + ftp::FTP, |
| 119 | + local_file_paths::Vector{<:AbstractString}, |
| 120 | + ftp_dir<:AbstractString; |
| 121 | + retry_callback::Function=(count, options) -> (count < 4, options), |
| 122 | + retry_wait_seconds::Integer = 5 |
| 123 | + ) |
| 124 | +
|
| 125 | +Uploads the files specified in local_file_paths to the directory specifed by |
| 126 | +ftp_dir. The files will have the same names. |
| 127 | +
|
| 128 | +By default, will try to deliver the files 4 times with a 5 second wait in between |
| 129 | +each failed attempt. |
| 130 | +
|
| 131 | +You can specify a function for retry_callback to change behaviour. This function must |
| 132 | +take as parameters the number of attempts that have been made so far, and the current |
| 133 | +ftp connection options as a FTPClient.ConnContext type. It must return a boolean |
| 134 | +that is true if another delivery attempt can be made, and a TPClient.ConnContext |
| 135 | +type that is the connection options to use for all future files to be delivered. This |
| 136 | +allows backup ftp directories to be used for example. |
| 137 | +
|
| 138 | +# Arguments |
| 139 | +`ftp::FTP`: The FTP to deliver to. See FTPClient.FTP for details. |
| 140 | +`file_paths::Vector{T}`: The file paths to the files we want to deliver. |
| 141 | +`ftp_dir`: The directory on the ftp server where we want to drop the files. |
| 142 | +`retry_callback::Function=(count, options) -> (count < 4, options)`: Function for retrying |
| 143 | + when delivery fails. |
| 144 | +`retry_wait_seconds::Integer = 5`: How many seconds to wait in between retries. |
| 145 | +
|
| 146 | +# Returns |
| 147 | +- `Array{Bool,1}`: Returns a vector of booleans with true for each successfully delivered |
| 148 | + file and false for any that failed to transfer. |
| 149 | +""" |
| 150 | +@compat function upload( |
| 151 | + ftp::FTP, |
| 152 | + local_file_paths::Vector{<:AbstractString}, |
| 153 | + ftp_dir::AbstractString; |
| 154 | + retry_callback::Function=(count, options) -> (count < 4, options), |
| 155 | + retry_wait_seconds::Integer = 5 |
| 156 | +) |
| 157 | + |
| 158 | + successful_delivery = Bool[] |
| 159 | + |
| 160 | + ftp_options = ftp.ctxt |
| 161 | + |
| 162 | + for single_file in local_file_paths |
| 163 | + # The location we are going to drop the file in the FTP server |
| 164 | + server_location = joinpath(ftp_dir, basename(single_file)) |
| 165 | + |
| 166 | + # ftp_put requires an io so open up our file. |
| 167 | + open(single_file) do single_file_io |
| 168 | + # Whether or not the current file was successfully delivered to the FTP |
| 169 | + file_delivery_success = false |
| 170 | + |
| 171 | + attempts = 1 |
| 172 | + # The loops should break after an appropriate amount of retries. |
| 173 | + # This way of doing retries makes testing easier. |
| 174 | + # Defaults to 4 attempts, waiting 5 seconds between each retry. |
| 175 | + while true |
| 176 | + try |
| 177 | + resp = ftp_put(ftp_options, server_location, single_file_io) |
| 178 | + file_delivery_success = resp.code == complete_transfer_code |
| 179 | + if file_delivery_success |
| 180 | + break |
| 181 | + end |
| 182 | + catch e |
| 183 | + warn(e) |
| 184 | + end |
| 185 | + sleep(retry_wait_seconds) |
| 186 | + # It returns ftp_options for testing purposes, where the ftp server |
| 187 | + # starts not existing then comes into existence during retries. |
| 188 | + do_retry, ftp_options = retry_callback(attempts, ftp_options) |
| 189 | + if !do_retry |
| 190 | + break |
| 191 | + end |
| 192 | + attempts += 1 |
| 193 | + end |
| 194 | + |
| 195 | + push!(successful_delivery, file_delivery_success) |
| 196 | + |
| 197 | + end |
| 198 | + end |
| 199 | + |
| 200 | + return successful_delivery |
| 201 | +end |
| 202 | + |
| 203 | + |
113 | 204 | """ |
114 | 205 | readdir(ftp::FTP) |
115 | 206 |
|
|
0 commit comments