Skip to content

Commit 39827ee

Browse files
committed
Control buffer size
1 parent 208756f commit 39827ee

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

AutoSSHApp.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Renci.SshNet;
1717
using Renci.SshNet.Common;
1818
using Renci.SshNet.Sftp;
19+
using System.Buffers;
1920

2021
#endregion Imports
2122

@@ -267,6 +268,30 @@ private static string BytesToString(long byteCount)
267268
return (Math.Sign(byteCount) * num).ToString() + suf[place];
268269
}
269270

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+
270295
private static long BackupFile(string root, string remotePath, SftpClient client)
271296
{
272297
string name = Path.GetFileName(remotePath);
@@ -394,13 +419,14 @@ private static long UploadFolder(HostEntry host, string pathInfo, SftpClient cli
394419

395420
try
396421
{
397-
using var stream = File.OpenRead(file);
422+
long prevProgress = 0;
423+
using var localStream = File.OpenRead(file);
398424
if (!client.Exists(remoteDir))
399425
{
400426
client.CreateDirectory(remoteDir);
401427
}
402-
long prevProgress = 0;
403-
client.UploadFile(stream, remoteFile, bytesUploaded =>
428+
using var remoteStream = client.OpenWrite(remoteFile);
429+
localStream.CopyTo(remoteStream, bytesUploaded =>
404430
{
405431
Interlocked.Add(ref AutoSSHApp.bytesUploaded, ((long)bytesUploaded - prevProgress));
406432
prevProgress = (long)bytesUploaded;

Properties/PublishProfiles/FolderProfile.pubxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
1111
<TargetFramework>net5.0</TargetFramework>
1212
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
1313
<SelfContained>true</SelfContained>
14-
<PublishSingleFile>False</PublishSingleFile>
14+
<PublishSingleFile>True</PublishSingleFile>
1515
<PublishReadyToRun>False</PublishReadyToRun>
1616
<PublishTrimmed>False</PublishTrimmed>
1717
</PropertyGroup>

0 commit comments

Comments
 (0)