public static async Task <FtpListItemViewModel> FromFtpListItemAsync(FluentFTP.FtpListItem item)
        {
            var result = new FtpListItemViewModel
            {
                Source   = item,
                FileName = item.Name,
                FileSize = GetSizeString(item.Size),
            };

            switch (item.Type)
            {
            case FluentFTP.FtpFileSystemObjectType.Directory:
                result.Icon = await GetIconOfFolderAsync();

                break;

            case FluentFTP.FtpFileSystemObjectType.File:
                result.Icon = await GetIconOfFileAsync(Path.GetExtension(item.FullName));

                break;

            case FluentFTP.FtpFileSystemObjectType.Link:
                result.Icon = null;
                break;
            }
            return(result);
        }
Пример #2
0
        /// <summary>
        /// 导航到指定的地址。需要登录时自动弹出登录界面。导航失败时,自动导航到失败页面。
        /// 不抛出异常。
        /// </summary>
        /// <param name="address">要导航到的地址</param>
        /// <returns>导航是否成功</returns>
        private async Task <bool> NavigateAsync(Uri address)
        {
            await ftpSemaphore.WaitAsync();

            try
            {
                errorMessage.Visibility     = Visibility.Collapsed;
                progressBar.Visibility      = Visibility.Visible;
                progressBar.IsIndeterminate = true;

                currentAddress = address;
                // 显示新的地址
                UriBuilder uriBuilder = new UriBuilder(currentAddress);
                uriBuilder.UserName = string.Empty;
                uriBuilder.Password = string.Empty;
                addressBox.Text     = uriBuilder.Uri.ToString();


                // 不是ftp协议则抛出异常
                if (address.Scheme != "ftp" && address.Scheme != "ftps")
                {
                    throw new InvalidOperationException("只支持ftp协议");
                }

                // Host改变时重新连接
                if (client == null)
                {
                    client = CreateFtpClient(address, GetCredential(address.UserInfo), address.Scheme == "ftps");
                    await FtpConnectAsync(client);
                }
                else if (client.Host != address.Host ||
                         (client.EncryptionMode != FluentFTP.FtpEncryptionMode.None && address.Scheme == "ftp") ||
                         (client.EncryptionMode != FluentFTP.FtpEncryptionMode.Explicit && address.Scheme == "ftps") ||
                         (client.Port != 21 && address.Port < 0) ||
                         (client.Port != address.Port && address.Port >= 0) ||
                         !string.IsNullOrEmpty(address.UserInfo))
                {
                    client.Disconnect();
                    client.Dispose();
                    client = CreateFtpClient(address, GetCredential(address.UserInfo), address.Scheme == "ftps");
                    await FtpConnectAsync(client);
                }

                // FTP路径可能包含#号,#号后面的内容会被归入Fragment。
                string remotePath = address.LocalPath + address.Fragment;

                listItemsVM.Clear();
                foreach (var item in (await client.GetListingAsync(remotePath)).OrderBy(x => x.Name))
                {
                    listItemsVM.Add(await FtpListItemViewModel.FromFtpListItemAsync(item));
                }

                using (Data.HistoryContext db = new Data.HistoryContext())
                {
                    Data.HistoryEntry h = new Data.HistoryEntry
                    {
                        Time = DateTimeOffset.Now,
                        Url  = uriBuilder.Uri.ToString()
                    };
                    await db.AddAsync(h);

                    await db.SaveChangesAsync();
                }

                return(true);
            }
            catch (FluentFTP.FtpCommandException exception)
            {
                if (exception.CompletionCode == "530")
                {
                    errorMessage.Text = "认证失败,请尝试登录。";
                    loginButton.Flyout.ShowAt(loginButton);
                    loginErrorMessage.Visibility = Visibility.Visible;
                }
                else
                {
                    errorMessage.Text = string.Format(
                        "发生错误,FTP返回代码:{0}。详细信息:\n{1}", exception.CompletionCode, exception.Message);
                }

                errorMessage.Visibility = Visibility.Visible;
                listItemsVM.Clear();

                return(false);
            }
            catch (Exception exception)
            {
                errorMessage.Text       = string.Format("发生错误。详细信息:\n{0}", exception.Message);
                errorMessage.Visibility = Visibility.Visible;
                listItemsVM.Clear();

                return(false);
            }
            finally
            {
                progressBar.Visibility = Visibility.Collapsed;
                ftpSemaphore.Release();
            }
        }