Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions src/Files.App.Storage/Ftp/FtpHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@

using Files.Shared.Extensions;
using FluentFTP;
using System.Net;

namespace Files.App.Storage
{
internal static class FtpHelpers
{
public static string GetFtpPath(string path)
{
path = path.Replace('\\', '/');
path = path.Replace('\', '/');

Check failure on line 14 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

Syntax error, ',' expected

Check failure on line 14 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

Too many characters in character literal

Check failure on line 14 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

Newline in constant

Check failure on line 14 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

Too many characters in character literal

Check failure on line 14 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

Syntax error, ',' expected

Check failure on line 14 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

Too many characters in character literal

Check failure on line 14 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

Newline in constant

Check failure on line 14 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

Too many characters in character literal

Check failure on line 14 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

Syntax error, ',' expected

Check failure on line 14 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

Too many characters in character literal

Check failure on line 14 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

Newline in constant

Check failure on line 14 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

Too many characters in character literal

Check failure on line 14 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

Syntax error, ',' expected

Check failure on line 14 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

Too many characters in character literal

Check failure on line 14 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

Newline in constant

Check failure on line 14 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

Too many characters in character literal

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of this change? I think it won't compile.


var schemaIndex = path.IndexOf("://", StringComparison.Ordinal) + 3;

Check failure on line 16 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

) expected

Check failure on line 16 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

Syntax error, ',' expected

Check failure on line 16 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

) expected

Check failure on line 16 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

Syntax error, ',' expected

Check failure on line 16 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

) expected

Check failure on line 16 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

Syntax error, ',' expected

Check failure on line 16 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

) expected

Check failure on line 16 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

Syntax error, ',' expected
var hostIndex = path.IndexOf('/', schemaIndex);

return hostIndex == -1 ? "/" : path.Substring(hostIndex);
Expand All @@ -26,25 +27,34 @@
public static string GetFtpHost(string path)
{
var authority = GetFtpAuthority(path);
var index = authority.IndexOf(':', StringComparison.Ordinal);
var atIndex = authority.IndexOf('@', StringComparison.Ordinal);
if (atIndex != -1)
{
var hostPart = authority.Substring(atIndex + 1);
var colonIndex = hostPart.IndexOf(':', StringComparison.Ordinal);
return colonIndex == -1 ? hostPart : hostPart.Substring(0, colonIndex);
}

var index = authority.IndexOf(':', StringComparison.Ordinal);
return index == -1 ? authority : authority.Substring(0, index);
}

public static ushort GetFtpPort(string path)
{
var authority = GetFtpAuthority(path);
var index = authority.IndexOf(':', StringComparison.Ordinal);
var atIndex = authority.IndexOf('@', StringComparison.Ordinal);
var hostPart = atIndex != -1 ? authority.Substring(atIndex + 1) : authority;
var index = hostPart.IndexOf(':', StringComparison.Ordinal);

if (index != -1)
return ushort.Parse(authority.Substring(index + 1));
return ushort.Parse(hostPart.Substring(index + 1));

return path.StartsWith("ftps://", StringComparison.OrdinalIgnoreCase) ? (ushort)990 : (ushort)21;
}

public static string GetFtpAuthority(string path)
{
path = path.Replace('\\', '/');
path = path.Replace('\', '/');

Check failure on line 57 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

Syntax error, ',' expected

Check failure on line 57 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

Too many characters in character literal

Check failure on line 57 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

Newline in constant

Check failure on line 57 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

Too many characters in character literal

Check failure on line 57 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

Syntax error, ',' expected

Check failure on line 57 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

Too many characters in character literal

Check failure on line 57 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

Newline in constant

Check failure on line 57 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

Too many characters in character literal

Check failure on line 57 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

Syntax error, ',' expected

Check failure on line 57 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

Too many characters in character literal

Check failure on line 57 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

Newline in constant

Check failure on line 57 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

Too many characters in character literal

Check failure on line 57 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

Syntax error, ',' expected

Check failure on line 57 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

Too many characters in character literal

Check failure on line 57 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

Newline in constant

Check failure on line 57 in src/Files.App.Storage/Ftp/FtpHelpers.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

Too many characters in character literal
var schemaIndex = path.IndexOf("://", StringComparison.Ordinal) + 3;
var hostIndex = path.IndexOf('/', schemaIndex);

Expand All @@ -53,12 +63,34 @@

return path.Substring(schemaIndex, hostIndex - schemaIndex);
}
public static NetworkCredential? GetFtpCredentials(string path)
{
var authority = GetFtpAuthority(path);
var atIndex = authority.IndexOf('@', StringComparison.Ordinal);

if (atIndex == -1)
return null; // No credentials in URL

var credentialsPart = authority.Substring(0, atIndex);
var colonIndex = credentialsPart.IndexOf(':', StringComparison.Ordinal);

if (colonIndex == -1)
{
// Only username, no password
return new NetworkCredential(Uri.UnescapeDataString(credentialsPart), "");
}

var username = Uri.UnescapeDataString(credentialsPart.Substring(0, colonIndex));
var password = Uri.UnescapeDataString(credentialsPart.Substring(colonIndex + 1));

return new NetworkCredential(username, password);
}

public static AsyncFtpClient GetFtpClient(string ftpPath)
{
var host = GetFtpHost(ftpPath);
var port = GetFtpPort(ftpPath);
var credentials = FtpManager.Credentials.Get(host, FtpManager.Anonymous);
var credentials = GetFtpCredentials(ftpPath) ?? FtpManager.Credentials.Get(host, FtpManager.Anonymous);

return new(host, credentials, port);
}
Expand Down
Loading