ShareX/ShareX.UploadersLib/FileUploaders/FTP/FTPAccount.cs

262 lines
9.8 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
2014-05-13 21:06:40 +12:00
Copyright (C) 2007-2014 ShareX Developers
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;
2014-12-11 12:19:28 +13:00
using ShareX.UploadersLib.HelperClasses;
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("Account"), Description("Connection protocol"), DefaultValue(FTPProtocol.FTP)]
2013-11-03 23:53:49 +13:00
public FTPProtocol Protocol { get; set; }
[Category("FTP"), Description("Shown in the list as: Name - Server:Port")]
public string Name { get; set; }
[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; }
[Category("FTP"), PasswordPropertyText(true)]
public string Password { get; set; }
2014-05-31 03:24:43 +12:00
[Category("FTP"), Description("Choose an appropriate protocol to be accessed by the server. This affects the server address."), DefaultValue(ServerProtocol.ftp)]
2013-11-03 23:53:49 +13:00
public ServerProtocol ServerProtocol { 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(true)]
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("Set true for active or false for passive"), DefaultValue(false)]
public bool IsActive { get; set; }
[Category("FTP"), Description("ftp://Host:Port"), Browsable(false)]
public string FTPAddress
{
get
{
if (string.IsNullOrEmpty(Host))
{
return string.Empty;
}
return string.Format("{0}{1}:{2}", ServerProtocol.GetDescription(), Host, Port);
}
}
private string exampleFilename = "screenshot.jpg";
[Category("FTP"), Description("Preview of the FTP path based on the settings above")]
2013-11-03 23:53:49 +13:00
public string PreviewFtpPath
{
get
{
return GetFtpPath(exampleFilename);
}
}
[Category("FTP"), Description("Preview of the HTTP path based on the settings above")]
2013-11-03 23:53:49 +13:00
public string PreviewHttpPath
{
get
{
return GetUriPath(exampleFilename);
}
}
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("OpenSSH key passphrase"), PasswordPropertyText(true)]
2013-11-03 23:53:49 +13:00
public string Passphrase { get; set; }
[Category("SFTP"), Description("Key location")]
2013-11-03 23:53:49 +13:00
[Editor(typeof(KeyFileNameEditor), typeof(UITypeEditor))]
public string Keypath { get; set; }
public FTPAccount()
{
Protocol = FTPProtocol.FTP;
Name = "New account";
2013-11-03 23:53:49 +13:00
Host = "host";
Port = 21;
2014-05-31 03:24:43 +12:00
ServerProtocol = ServerProtocol.ftp;
2013-11-03 23:53:49 +13:00
SubFolderPath = string.Empty;
2014-05-31 03:24:43 +12:00
BrowserProtocol = BrowserProtocol.http;
2013-11-03 23:53:49 +13:00
HttpHomePath = string.Empty;
2014-05-07 09:06:26 +12:00
HttpHomePathAutoAddSubFolderPath = true;
2013-11-03 23:53:49 +13:00
HttpHomePathNoExtension = false;
IsActive = false;
2014-05-25 10:24:51 +12:00
FTPSEncryption = FTPSEncryption.Explicit;
FTPSCertificateLocation = string.Empty;
2013-11-03 23:53:49 +13:00
}
public string GetSubFolderPath(string filename = null)
2013-11-03 23:53:49 +13:00
{
string path = NameParser.Parse(NameParserType.URL, SubFolderPath.Replace("%host", Host));
return URLHelpers.CombineURL(path, filename);
2013-11-03 23:53:49 +13:00
}
public string GetHttpHomePath()
{
// @ deprecated
if (HttpHomePath.StartsWith("@"))
{
HttpHomePath = HttpHomePath.Substring(1);
HttpHomePathAutoAddSubFolderPath = false;
}
return NameParser.Parse(NameParserType.URL, HttpHomePath.Replace("%host", Host));
}
public string GetUriPath(string filename, string subFolderPath = null)
2013-11-03 23:53:49 +13:00
{
if (string.IsNullOrEmpty(Host))
{
return string.Empty;
}
if (HttpHomePathNoExtension)
{
filename = Path.GetFileNameWithoutExtension(filename);
}
filename = URLHelpers.URLEncode(filename);
2013-11-03 23:53:49 +13:00
if (subFolderPath == null)
{
subFolderPath = GetSubFolderPath();
}
subFolderPath = URLHelpers.URLPathEncode(subFolderPath);
UriBuilder httpHomeUri;
var httpHomePath = GetHttpHomePath();
2014-09-26 14:43:13 +12:00
2013-11-03 23:53:49 +13:00
if (string.IsNullOrEmpty(httpHomePath))
{
var host = Host;
2013-11-03 23:53:49 +13:00
if (host.StartsWith("ftp."))
{
host = host.Substring(4);
}
httpHomeUri = new UriBuilder(URLHelpers.CombineURL(host, subFolderPath, filename));
2013-11-03 23:53:49 +13:00
}
else
{
//Parse HttpHomePath in to host, port, path and query components
var firstSlash = httpHomePath.IndexOf('/');
var httpHome = firstSlash >= 0 ? httpHomePath.Substring(0, firstSlash) : httpHomePath;
var portSpecifiedAt = httpHome.LastIndexOf(':');
2014-09-26 14:43:13 +12:00
var httpHomeHost = portSpecifiedAt >= 0 ? httpHome.Substring(0, portSpecifiedAt) : httpHome;
var httpHomePort = -1;
var httpHomePathAndQuery = firstSlash >= 0 ? httpHomePath.Substring(firstSlash + 1) : "";
var querySpecifiedAt = httpHomePathAndQuery.LastIndexOf('?');
var httpHomeDir = querySpecifiedAt >= 0 ? httpHomePathAndQuery.Substring(0, querySpecifiedAt) : httpHomePathAndQuery;
var 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)
httpHomeUri.Port = httpHomePort;
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)
httpHomeUri.Query = URLHelpers.CombineURL(httpHomeUri.Query.Substring(1), subFolderPath, filename);
else
httpHomeUri.Query = httpHomeUri.Query.Substring(1) + filename;
2013-11-03 23:53:49 +13:00
}
else
{
if (HttpHomePathAutoAddSubFolderPath)
httpHomeUri.Path = URLHelpers.CombineURL(httpHomeUri.Path, subFolderPath);
httpHomeUri.Path = URLHelpers.CombineURL(httpHomeUri.Path, filename);
2013-11-03 23:53:49 +13:00
}
}
httpHomeUri.Scheme = BrowserProtocol.GetDescription();
return Uri.EscapeUriString(httpHomeUri.Uri.ToString());
2013-11-03 23:53:49 +13:00
}
public string GetFtpPath(string filemame)
{
if (string.IsNullOrEmpty(FTPAddress))
{
return string.Empty;
}
return URLHelpers.CombineURL(FTPAddress, GetSubFolderPath(filemame));
2013-11-03 23:53:49 +13:00
}
public override string ToString()
{
return string.Format("{0} - {1}:{2}", Name, Host, Port);
}
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
}
}
}