Skip to content

Commit 240e2f7

Browse files
committed
fix: Potential unhandled exception
1 parent 1323012 commit 240e2f7

File tree

4 files changed

+42
-39
lines changed

4 files changed

+42
-39
lines changed

Ui/Model/Protocol/FileTransmit/Transmitters/TransmissionController/TransmitTask.cs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ private async Task<bool> CheckExistedFileHostToServer(TransmitItem item, IEnumer
510510
}
511511

512512
/// <summary>
513-
/// check if any same name file exited, return if can continue transmit.
513+
/// check if any same name file exited, return if it can continue to transmit.
514514
/// </summary>
515515
/// <returns></returns>
516516
private async Task<bool> CheckExistedFiles(IEnumerable<RemoteItem> remoteItems)
@@ -538,7 +538,7 @@ private async Task<bool> CheckExistedFiles(IEnumerable<RemoteItem> remoteItems)
538538

539539
var vm = IoC.Get<SessionControlService>().GetTabByConnectionId(ConnectionId)?.GetViewModel();
540540
if (existedFiles > 0
541-
&& false == MessageBoxHelper.Confirm(_languageService.Translate("file_transmit_host_warning_same_names", existedFiles.ToString()),
541+
&& false == MessageBoxHelper.Confirm(_languageService.Translate("file_transmit_host_warning_same_names", existedFiles.ToString()),
542542
_languageService.Translate("file_transmit_host_warning_same_names_title"), ownerViewModel: vm == null ? this : vm))
543543
{
544544
return false;
@@ -577,26 +577,34 @@ private void DataTransmitting(ref TransmitItem item, ulong readLength)
577577

578578
private async Task RunTransmitServerToHost(TransmitItem item)
579579
{
580-
if (item.IsDirectory)
581-
{
582-
if (!Directory.Exists(item.DstPath))
583-
Directory.CreateDirectory(item.DstPath);
584-
}
585-
else
580+
try
586581
{
587-
var fi = new FileInfo(item.DstPath);
588-
if (fi?.Directory?.Exists == false)
589-
fi.Directory.Create();
590-
if (fi!.Exists)
591-
fi.Delete();
592-
593-
if (_trans != null)
582+
if (item.IsDirectory)
594583
{
595-
item.TransmittedSize = 0;
596-
await _trans.DownloadFile(item.SrcPath, item.DstPath,
597-
readLength => { DataTransmitting(ref item, readLength); },
598-
_cancellationSource.Token);
584+
if (!Directory.Exists(item.DstPath))
585+
Directory.CreateDirectory(item.DstPath);
599586
}
587+
else
588+
{
589+
var fi = new FileInfo(item.DstPath);
590+
if (fi?.Directory?.Exists == false)
591+
fi.Directory.Create();
592+
if (fi!.Exists)
593+
fi.Delete();
594+
595+
if (_trans != null)
596+
{
597+
item.TransmittedSize = 0;
598+
await _trans.DownloadFile(item.SrcPath, item.DstPath,
599+
readLength => { DataTransmitting(ref item, readLength); },
600+
_cancellationSource.Token);
601+
}
602+
}
603+
}
604+
catch (Exception e)
605+
{
606+
TransmitTaskStatus = ETransmitTaskStatus.Cancel;
607+
SimpleLogHelper.Error(e);
600608
}
601609
}
602610

Ui/Model/Protocol/FileTransmit/Transmitters/TransmitterFtp.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ public async Task<List<RemoteItem>> ListDirectoryItems(string path)
6161
if (_ftp != null)
6262
{
6363
IEnumerable<FtpListItem> items = await _ftp.GetListing(path);
64-
if (items == null ||
65-
!items.Any())
64+
if (!items.Any())
6665
return ret;
6766

6867
items = items.OrderBy(x => x.Name);
@@ -102,19 +101,17 @@ private RemoteItem FtpListItem2RemoteItem(FtpListItem item)
102101
};
103102
if (item.Type == FtpObjectType.Directory)
104103
{
105-
if (newItem.IsSymlink)
106-
{
107-
newItem.Icon = TransmitItemIconCache.GetDictIcon(Environment.GetFolderPath(Environment.SpecialFolder.Favorites));
108-
}
109-
else
110-
{
111-
newItem.Icon = TransmitItemIconCache.GetDictIcon();
112-
}
104+
newItem.Icon = TransmitItemIconCache.GetDictIcon();
113105
newItem.ByteSize = 0;
114106
newItem.FileType = "folder";
107+
if (newItem.IsSymlink)
108+
newItem.Icon = TransmitItemIconCache.GetDictIcon(Environment.GetFolderPath(Environment.SpecialFolder.Favorites));
115109
}
116110
else
117111
{
112+
if (newItem.IsSymlink)
113+
newItem.FileType = ".lnk";
114+
118115
if (item.Name.IndexOf(".", StringComparison.Ordinal) > 0)
119116
{
120117
var ext = item.Name.Substring(item.Name.LastIndexOf(".", StringComparison.Ordinal)).ToLower();

Ui/Model/Protocol/FileTransmit/Transmitters/TransmitterSFtp.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ public async Task<List<RemoteItem>> ListDirectoryItems(string path)
7676
var ret = new List<RemoteItem>();
7777
if (_sftp != null)
7878
{
79-
IEnumerable<ISftpFile> items = new List<SftpFile>();
80-
items = _sftp.ListDirectory(path);
81-
if (items == null || !items.Any())
79+
var items = _sftp!.ListDirectory(path);
80+
var sftpFiles = items as ISftpFile[] ?? items.ToArray();
81+
if (!sftpFiles.Any())
8282
return ret;
8383

84-
items = items.OrderBy(x => x.Name);
84+
items = sftpFiles.OrderBy(x => x.Name);
8585
foreach (var item in items)
8686
{
8787
if (item.Name == "." || item.Name == "..")
@@ -108,7 +108,7 @@ private RemoteItem SftpFile2RemoteItem(ISftpFile item)
108108
Name = item.Name,
109109
FullName = fn,
110110
LastUpdate = item.LastWriteTime,
111-
ByteSize = (ulong)item.Length,
111+
ByteSize = (ulong)Math.Max(item.Length, 0),
112112
};
113113
if (item.IsDirectory)
114114
{
@@ -123,18 +123,16 @@ private RemoteItem SftpFile2RemoteItem(ISftpFile item)
123123
if (item.IsSymbolicLink)
124124
newItem.FileType = ".lnk";
125125

126-
BitmapSource icon;
127126
if (item.Name.IndexOf(".", StringComparison.Ordinal) > 0)
128127
{
129128
var ext = item.Name.Substring(item.Name.LastIndexOf(".", StringComparison.Ordinal)).ToLower();
130129
newItem.FileType = ext;
131-
icon = TransmitItemIconCache.GetFileIcon(ext);
130+
newItem.Icon = TransmitItemIconCache.GetFileIcon(ext);
132131
}
133132
else
134133
{
135-
icon = TransmitItemIconCache.GetFileIcon();
134+
newItem.Icon = TransmitItemIconCache.GetFileIcon();
136135
}
137-
newItem.Icon = icon;
138136
}
139137
return newItem;
140138
}

Ui/View/Host/ProtocolHosts/VmFileTransmitHost.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private void ShowFolder(string path, int mode = 0, bool showIoMessage = true)
222222
{
223223
//SimpleLogHelper.Debug($"ShowFolder before ListDirectoryItems");
224224
var items = await Trans.ListDirectoryItems(path);
225-
if (Enumerable.Any<RemoteItem>(items))
225+
if (items.Any())
226226
{
227227
remoteItemInfos = new ObservableCollection<RemoteItem>(items);
228228
}

0 commit comments

Comments
 (0)