///////////////////////////////////////////////////////
| 299 | |
| 300 | //////////////////////////////////////////////////////////// |
| 301 | Ftp::Response Ftp::download(const std::filesystem::path& remoteFile, const std::filesystem::path& localPath, TransferMode mode) |
| 302 | { |
| 303 | // Open a data channel using the given transfer mode |
| 304 | DataChannel data(*this); |
| 305 | Response response = data.open(mode); |
| 306 | if (response.isOk()) |
| 307 | { |
| 308 | // Tell the server to start the transfer |
| 309 | response = sendCommand("RETR", remoteFile.string()); |
| 310 | if (response.isOk()) |
| 311 | { |
| 312 | // Create the file and truncate it if necessary |
| 313 | const std::filesystem::path filepath = localPath / remoteFile.filename(); |
| 314 | std::ofstream file(filepath, std::ios_base::binary | std::ios_base::trunc); |
| 315 | if (!file) |
| 316 | return Response(Response::Status::InvalidFile); |
| 317 | |
| 318 | // Receive the file data |
| 319 | data.receive(file); |
| 320 | |
| 321 | // Close the file |
| 322 | file.close(); |
| 323 | |
| 324 | // Get the response from the server |
| 325 | response = getResponse(); |
| 326 | |
| 327 | // If the download was unsuccessful, delete the partial file |
| 328 | if (!response.isOk()) |
| 329 | std::filesystem::remove(filepath); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | return response; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | //////////////////////////////////////////////////////////// |