ShareX/ShareX.UploadersLib/FileUploaders/FTPAccount.cs

287 lines
10 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib;
2013-11-03 23:53:49 +13:00
using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.IO;
2014-12-11 09:25:20 +13:00
namespace ShareX.UploadersLib
2013-11-03 23:53:49 +13:00
{
public class FTPAccount : ICloneable
{
[Category("FTP"), Description("Shown in the list as: Name - Server:Port")]
public string Name { get; set; }
2017-04-21 04:38:20 +12:00
[Category("Account"), Description("Connection protocol"), DefaultValue(FTPProtocol.FTP)]
public FTPProtocol Protocol { get; set; }
2013-11-03 23:53:49 +13:00
[Category("FTP"), Description("Host, e.g. google.com")]
public string Host { get; set; }
[Category("FTP"), Description("Port number"), DefaultValue(21)]
2013-11-03 23:53:49 +13:00
public int Port { get; set; }
[Category("FTP")]
public string Username { get; set; }
2020-06-01 13:17:04 +12:00
[Category("FTP"), PasswordPropertyText(true), JsonEncrypt]
2013-11-03 23:53:49 +13:00
public string Password { get; set; }
2017-04-21 04:38:20 +12:00
[Category("FTP"), Description("Set true for active or false for passive"), DefaultValue(false)]
public bool IsActive { get; set; }
[Category("FTP"), Description("FTP sub folder path, example: Screenshots.\r\nYou can use name parsing: %y = year, %mo = month.")]
2013-11-03 23:53:49 +13:00
public string SubFolderPath { get; set; }
2014-05-31 03:24:43 +12:00
[Category("FTP"), Description("Choose an appropriate protocol to be accessed by the browser"), DefaultValue(BrowserProtocol.http)]
2013-11-03 23:53:49 +13:00
public BrowserProtocol BrowserProtocol { get; set; }
[Category("FTP"), Description("URL = HttpHomePath + SubFolderPath + FileName\r\nIf HttpHomePath is empty then URL = Host + SubFolderPath + FileName\r\n%host = Host")]
2013-11-03 23:53:49 +13:00
public string HttpHomePath { get; set; }
[Category("FTP"), Description("Automatically add sub folder path to end of http home path"), DefaultValue(false)]
public bool HttpHomePathAutoAddSubFolderPath { get; set; }
2013-11-03 23:53:49 +13:00
[Category("FTP"), Description("Don't add file extension to URL"), DefaultValue(false)]
public bool HttpHomePathNoExtension { get; set; }
[Category("FTP"), Description("Protocol://Host:Port"), Browsable(false)]
2013-11-03 23:53:49 +13:00
public string FTPAddress
{
get
{
if (string.IsNullOrEmpty(Host))
{
2016-05-25 06:15:45 +12:00
return "";
2013-11-03 23:53:49 +13:00
}
string serverProtocol;
switch (Protocol)
{
default:
case FTPProtocol.FTP:
serverProtocol = "ftp://";
break;
case FTPProtocol.FTPS:
serverProtocol = "ftps://";
break;
case FTPProtocol.SFTP:
serverProtocol = "sftp://";
break;
}
return string.Format("{0}{1}:{2}", serverProtocol, Host, Port);
2013-11-03 23:53:49 +13:00
}
}
2021-12-12 10:21:19 +13:00
private string exampleFileName = "example.png";
2013-11-03 23:53:49 +13:00
[Category("FTP"), Description("Preview of the FTP path based on the settings above")]
2021-12-12 10:21:19 +13:00
public string PreviewFtpPath => GetFtpPath(exampleFileName);
2013-11-03 23:53:49 +13:00
[Category("FTP"), Description("Preview of the HTTP path based on the settings above")]
public string PreviewHttpPath
{
get
{
try
{
2021-12-12 10:21:19 +13:00
return GetUriPath(exampleFileName);
}
catch
{
return "";
}
}
}
2013-11-03 23:53:49 +13:00
2014-05-25 10:24:51 +12:00
[Category("FTPS"), Description("Type of SSL to use. Explicit is TLS, Implicit is SSL."), DefaultValue(FTPSEncryption.Explicit)]
public FTPSEncryption FTPSEncryption { get; set; }
2013-11-03 23:53:49 +13:00
2014-05-25 12:09:49 +12:00
[Category("FTPS"), Description("Certificate file location. Optional setting.")]
2014-05-25 10:24:51 +12:00
[Editor(typeof(CertFileNameEditor), typeof(UITypeEditor))]
public string FTPSCertificateLocation { get; set; }
2013-11-03 23:53:49 +13:00
[Category("SFTP"), Description("Key location")]
2013-11-03 23:53:49 +13:00
[Editor(typeof(KeyFileNameEditor), typeof(UITypeEditor))]
public string Keypath { get; set; }
2020-06-01 13:17:04 +12:00
[Category("SFTP"), Description("OpenSSH key passphrase"), PasswordPropertyText(true), JsonEncrypt]
2017-04-21 04:38:20 +12:00
public string Passphrase { get; set; }
2013-11-03 23:53:49 +13:00
public FTPAccount()
{
Name = "New account";
2017-04-21 04:38:20 +12:00
Protocol = FTPProtocol.FTP;
Host = "";
2013-11-03 23:53:49 +13:00
Port = 21;
2017-04-21 04:38:20 +12:00
IsActive = false;
2016-05-25 06:15:45 +12:00
SubFolderPath = "";
2014-05-31 03:24:43 +12:00
BrowserProtocol = BrowserProtocol.http;
2016-05-25 06:15:45 +12:00
HttpHomePath = "";
2014-05-07 09:06:26 +12:00
HttpHomePathAutoAddSubFolderPath = true;
2013-11-03 23:53:49 +13:00
HttpHomePathNoExtension = false;
2014-05-25 10:24:51 +12:00
FTPSEncryption = FTPSEncryption.Explicit;
2016-05-25 06:15:45 +12:00
FTPSCertificateLocation = "";
2013-11-03 23:53:49 +13:00
}
2021-12-12 10:21:19 +13:00
public string GetSubFolderPath(string fileName = null, NameParserType nameParserType = NameParserType.URL)
2013-11-03 23:53:49 +13:00
{
string path = NameParser.Parse(nameParserType, SubFolderPath.Replace("%host", Host));
2021-12-12 10:21:19 +13:00
return URLHelpers.CombineURL(path, fileName);
2013-11-03 23:53:49 +13:00
}
public string GetHttpHomePath()
{
string homePath = HttpHomePath.Replace("%host", Host);
2022-04-13 17:12:14 +12:00
ShareXCustomUploaderSyntaxParser parser = new ShareXCustomUploaderSyntaxParser();
parser.UseNameParser = true;
parser.NameParserType = NameParserType.URL;
return parser.Parse(homePath);
}
2021-12-12 10:21:19 +13:00
public string GetUriPath(string fileName, string subFolderPath = null)
2013-11-03 23:53:49 +13:00
{
if (string.IsNullOrEmpty(Host))
{
2016-05-25 06:15:45 +12:00
return "";
2013-11-03 23:53:49 +13:00
}
if (HttpHomePathNoExtension)
{
2021-12-12 10:21:19 +13:00
fileName = Path.GetFileNameWithoutExtension(fileName);
2013-11-03 23:53:49 +13:00
}
2021-12-12 10:21:19 +13:00
fileName = URLHelpers.URLEncode(fileName);
2013-11-03 23:53:49 +13:00
if (subFolderPath == null)
{
subFolderPath = GetSubFolderPath();
}
UriBuilder httpHomeUri;
2016-09-17 19:07:02 +12:00
string httpHomePath = GetHttpHomePath();
2014-09-26 14:43:13 +12:00
2013-11-03 23:53:49 +13:00
if (string.IsNullOrEmpty(httpHomePath))
{
string url = Host;
2013-11-03 23:53:49 +13:00
if (url.StartsWith("ftp."))
2013-11-03 23:53:49 +13:00
{
url = url.Substring(4);
2013-11-03 23:53:49 +13:00
}
if (HttpHomePathAutoAddSubFolderPath)
{
url = URLHelpers.CombineURL(url, subFolderPath);
}
2021-12-12 10:21:19 +13:00
url = URLHelpers.CombineURL(url, fileName);
httpHomeUri = new UriBuilder(url);
httpHomeUri.Port = -1; //Since httpHomePath is not set, it's safe to erase UriBuilder's assumed port number
2013-11-03 23:53:49 +13:00
}
else
{
//Parse HttpHomePath in to host, port, path and query components
2016-09-17 19:07:02 +12:00
int firstSlash = httpHomePath.IndexOf('/');
string httpHome = firstSlash >= 0 ? httpHomePath.Substring(0, firstSlash) : httpHomePath;
int portSpecifiedAt = httpHome.LastIndexOf(':');
2014-09-26 14:43:13 +12:00
2016-09-17 19:07:02 +12:00
string httpHomeHost = portSpecifiedAt >= 0 ? httpHome.Substring(0, portSpecifiedAt) : httpHome;
int httpHomePort = -1;
string httpHomePathAndQuery = firstSlash >= 0 ? httpHomePath.Substring(firstSlash + 1) : "";
int querySpecifiedAt = httpHomePathAndQuery.LastIndexOf('?');
string httpHomeDir = querySpecifiedAt >= 0 ? httpHomePathAndQuery.Substring(0, querySpecifiedAt) : httpHomePathAndQuery;
string httpHomeQuery = querySpecifiedAt >= 0 ? httpHomePathAndQuery.Substring(querySpecifiedAt + 1) : "";
if (portSpecifiedAt >= 0)
int.TryParse(httpHome.Substring(portSpecifiedAt + 1), out httpHomePort);
2014-09-26 14:43:13 +12:00
//Build URI
httpHomeUri = new UriBuilder { Host = httpHomeHost, Path = httpHomeDir, Query = httpHomeQuery };
if (portSpecifiedAt >= 0)
2016-09-17 19:07:02 +12:00
{
httpHomeUri.Port = httpHomePort;
2016-09-17 19:07:02 +12:00
}
if (httpHomeUri.Query.EndsWith("="))
2013-11-03 23:53:49 +13:00
{
//Setting URIBuilder.Query automatically prepends a ? so we must trim it first.
if (HttpHomePathAutoAddSubFolderPath)
2016-09-17 19:07:02 +12:00
{
2021-12-12 10:21:19 +13:00
httpHomeUri.Query = URLHelpers.CombineURL(httpHomeUri.Query.Substring(1), subFolderPath, fileName);
2016-09-17 19:07:02 +12:00
}
else
2016-09-17 19:07:02 +12:00
{
2021-12-12 10:21:19 +13:00
httpHomeUri.Query = httpHomeUri.Query.Substring(1) + fileName;
2016-09-17 19:07:02 +12:00
}
2013-11-03 23:53:49 +13:00
}
else
{
if (HttpHomePathAutoAddSubFolderPath)
2016-09-17 19:07:02 +12:00
{
httpHomeUri.Path = URLHelpers.CombineURL(httpHomeUri.Path, subFolderPath);
2016-09-17 19:07:02 +12:00
}
2021-12-12 10:21:19 +13:00
httpHomeUri.Path = URLHelpers.CombineURL(httpHomeUri.Path, fileName);
2013-11-03 23:53:49 +13:00
}
}
httpHomeUri.Scheme = BrowserProtocol.GetDescription();
return httpHomeUri.Uri.OriginalString;
2013-11-03 23:53:49 +13:00
}
2022-05-23 22:03:17 +12:00
public string GetFtpPath(string fileName)
2013-11-03 23:53:49 +13:00
{
if (string.IsNullOrEmpty(FTPAddress))
{
2016-05-25 06:15:45 +12:00
return "";
2013-11-03 23:53:49 +13:00
}
2022-05-23 22:03:17 +12:00
return URLHelpers.CombineURL(FTPAddress, GetSubFolderPath(fileName, NameParserType.FilePath));
2013-11-03 23:53:49 +13:00
}
public override string ToString()
{
2017-04-21 04:38:20 +12:00
return $"{Name} ({Host}:{Port})";
2013-11-03 23:53:49 +13:00
}
public FTPAccount Clone()
{
return MemberwiseClone() as FTPAccount;
}
object ICloneable.Clone()
{
2014-04-30 21:46:15 +12:00
return Clone();
2013-11-03 23:53:49 +13:00
}
}
}