|
16 | 16 | using Renci.SshNet; |
17 | 17 | using Renci.SshNet.Common; |
18 | 18 | using Renci.SshNet.Sftp; |
| 19 | +using System.Buffers; |
19 | 20 |
|
20 | 21 | #endregion Imports |
21 | 22 |
|
@@ -267,6 +268,30 @@ private static string BytesToString(long byteCount) |
267 | 268 | return (Math.Sign(byteCount) * num).ToString() + suf[place]; |
268 | 269 | } |
269 | 270 |
|
| 271 | + private static void CopyTo(this Stream source, Stream destination, Action<ulong> progress = null) |
| 272 | + { |
| 273 | + ulong totalBytes = 0; |
| 274 | + var buffer = ArrayPool<byte>.Shared.Rent(ushort.MaxValue); |
| 275 | + try |
| 276 | + { |
| 277 | + int read; |
| 278 | + while ((read = source.Read(buffer, 0, buffer.Length)) != 0) |
| 279 | + { |
| 280 | + if (progress != null) |
| 281 | + { |
| 282 | + totalBytes += (ulong)read; |
| 283 | + progress(totalBytes); |
| 284 | + } |
| 285 | + |
| 286 | + destination.Write(buffer, 0, read); |
| 287 | + } |
| 288 | + } |
| 289 | + finally |
| 290 | + { |
| 291 | + ArrayPool<byte>.Shared.Return(buffer); |
| 292 | + } |
| 293 | + } |
| 294 | + |
270 | 295 | private static long BackupFile(string root, string remotePath, SftpClient client) |
271 | 296 | { |
272 | 297 | string name = Path.GetFileName(remotePath); |
@@ -394,13 +419,14 @@ private static long UploadFolder(HostEntry host, string pathInfo, SftpClient cli |
394 | 419 |
|
395 | 420 | try |
396 | 421 | { |
397 | | - using var stream = File.OpenRead(file); |
| 422 | + long prevProgress = 0; |
| 423 | + using var localStream = File.OpenRead(file); |
398 | 424 | if (!client.Exists(remoteDir)) |
399 | 425 | { |
400 | 426 | client.CreateDirectory(remoteDir); |
401 | 427 | } |
402 | | - long prevProgress = 0; |
403 | | - client.UploadFile(stream, remoteFile, bytesUploaded => |
| 428 | + using var remoteStream = client.OpenWrite(remoteFile); |
| 429 | + localStream.CopyTo(remoteStream, bytesUploaded => |
404 | 430 | { |
405 | 431 | Interlocked.Add(ref AutoSSHApp.bytesUploaded, ((long)bytesUploaded - prevProgress)); |
406 | 432 | prevProgress = (long)bytesUploaded; |
|
0 commit comments