Skip to content
This repository was archived by the owner on Oct 14, 2021. It is now read-only.

Commit adbc275

Browse files
authored
Close file stream on put and get sftp functions (#60)
1 parent 6caddc7 commit adbc275

File tree

2 files changed

+64
-61
lines changed

2 files changed

+64
-61
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,4 @@ dotnet/dotnetframework/GeneXusFtps/bin/
6262
dotnet/dotnetframework/GeneXusFtps/packages.config
6363
dotnet/dotnetcore/GeneXusFtpsNetCore/obj/
6464
dotnet/resources/key.snk
65+
dotnet/dotnetcore/GeneXusFtpsNetCore/bin/

dotnet/dotnetframework/GeneXusSftp/Sftp/SftpClient.cs

Lines changed: 63 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -138,77 +138,78 @@ public override bool Put(String localPath, String remoteDir)
138138
}
139139
}
140140

141-
FileStream stream = null;
142141
try
143142
{
144-
stream = File.OpenRead(local_path);
145-
}
146-
catch (Exception e)
147-
{
148-
this.error.setError("SF011", e.Message);
149-
return false;
150-
}
151-
152-
string rDir = "";
153-
bool control = false;
154-
string dirRemote = this.channel.WorkingDirectory;
155-
156-
try
157-
{
158-
control = this.channel.WorkingDirectory.Contains("/");
159-
160-
}
161-
catch (Exception e)
162-
{
163-
this.error.setError("SF018", e.Message);
164-
return false;
165-
}
166-
if (control)
167-
{
168-
remoteDir = $"/{remoteDir.Replace(@"\", "/")}";
169-
rDir = SecurityUtils.compareStrings(remoteDir, "/") ? remoteDir : remoteDir + "/";
170-
}
171-
else
172-
{
173-
rDir = SecurityUtils.compareStrings(remoteDir, "\\") ? remoteDir : remoteDir + "\\";
174-
}
175-
rDir += GetFileNamne(localPath);
176-
if (rDir.Length > 1)
177-
{
178-
if (rDir.StartsWith("\\") || rDir.StartsWith("/"))
143+
using (FileStream stream = File.OpenRead(local_path))
179144
{
180-
rDir = rDir.Substring(1, rDir.Length - 1);
181-
}
182-
}
145+
string rDir = "";
146+
bool control = false;
147+
string dirRemote = this.channel.WorkingDirectory;
183148

184-
try
185-
{
186-
this.channel.UploadFile(stream, rDir, true, null);
187-
}
188-
catch (Exception e)
189-
{
190-
if (SecurityUtils.compareStrings(remoteDir, "/") || SecurityUtils.compareStrings(remoteDir, "\\") || SecurityUtils.compareStrings(remoteDir, "//"))
191-
{
192149
try
193150
{
194-
this.channel.UploadFile(stream, GetFileNamne(localPath), true, null);
151+
control = this.channel.WorkingDirectory.Contains("/");
152+
195153
}
196-
catch (Exception s)
154+
catch (Exception e)
197155
{
198-
this.error.setError("SF012", s.Message);
156+
this.error.setError("SF018", e.Message);
199157
return false;
200158
}
201-
}
202-
else
203-
{
204-
this.error.setError("SF013", e.Message);
205-
return false;
206-
}
159+
if (control)
160+
{
161+
remoteDir = $"/{remoteDir.Replace(@"\", "/")}";
162+
rDir = SecurityUtils.compareStrings(remoteDir, "/") ? remoteDir : remoteDir + "/";
163+
}
164+
else
165+
{
166+
rDir = SecurityUtils.compareStrings(remoteDir, "\\") ? remoteDir : remoteDir + "\\";
167+
}
168+
rDir += GetFileNamne(localPath);
169+
if (rDir.Length > 1)
170+
{
171+
if (rDir.StartsWith("\\") || rDir.StartsWith("/"))
172+
{
173+
rDir = rDir.Substring(1, rDir.Length - 1);
174+
}
175+
}
176+
177+
try
178+
{
179+
this.channel.UploadFile(stream, rDir, true, null);
180+
}
181+
catch (Exception e)
182+
{
183+
if (SecurityUtils.compareStrings(remoteDir, "/") || SecurityUtils.compareStrings(remoteDir, "\\") || SecurityUtils.compareStrings(remoteDir, "//"))
184+
{
185+
try
186+
{
187+
this.channel.UploadFile(stream, GetFileNamne(localPath), true, null);
188+
}
189+
catch (Exception s)
190+
{
191+
this.error.setError("SF012", s.Message);
192+
return false;
193+
}
194+
}
195+
else
196+
{
197+
this.error.setError("SF013", e.Message);
198+
return false;
199+
}
207200

208201

202+
}
203+
204+
return true;
205+
}
209206
}
210207

211-
return true;
208+
catch (Exception e)
209+
{
210+
this.error.setError("SF011", e.Message);
211+
return false;
212+
}
212213

213214
}
214215

@@ -253,12 +254,13 @@ public override bool Get(String remoteFilePath, String localDir)
253254
}
254255
try
255256
{
257+
using (Stream file = new FileStream(localDir + GetFileNamne(remoteFilePath), FileMode.Create))
258+
{
256259

260+
//Stream file = new FileStream(localDir + GetFileNamne(remoteFilePath), FileMode.Create);
261+
this.channel.DownloadFile(rDir, file);
257262

258-
Stream file = new FileStream(localDir + GetFileNamne(remoteFilePath), FileMode.Create);
259-
this.channel.DownloadFile(rDir, file);
260-
261-
263+
}
262264
}
263265
catch (Exception e)
264266
{

0 commit comments

Comments
 (0)