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 )
using HelpersLib ;
using System ;
using System.ComponentModel ;
using System.Drawing.Design ;
using System.IO ;
using UploadersLib.HelperClasses ;
namespace UploadersLib
{
public class FTPAccount : ICloneable
{
2014-04-20 13:16:51 +12:00
[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 ; }
2014-04-20 13:16:51 +12:00
[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 ; }
[Category("FTP"), Description("Choose an appropriate protocol to be accessed by the server. This affects the server address."), DefaultValue(ServerProtocol.Ftp)]
public ServerProtocol ServerProtocol { get ; set ; }
2014-05-20 14:14:03 +12:00
[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 ; }
[Category("FTP"), Description("Choose an appropriate protocol to be accessed by the browser"), DefaultValue(BrowserProtocol.Http)]
public BrowserProtocol BrowserProtocol { get ; set ; }
2014-05-20 14:14:03 +12:00
[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 ; }
2014-04-20 13:16:51 +12:00
[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" ;
2014-04-20 13:16:51 +12:00
[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 ) ;
}
}
2014-04-20 13:16:51 +12:00
[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
2014-04-20 13:16:51 +12:00
[Category("SFTP"), Description("OpenSSH key passphrase"), PasswordPropertyText(true)]
2013-11-03 23:53:49 +13:00
public string Passphrase { get ; set ; }
2014-04-20 13:16:51 +12:00
[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 ;
2014-05-20 14:14:03 +12:00
Name = "New account" ;
2013-11-03 23:53:49 +13:00
Host = "host" ;
Port = 21 ;
ServerProtocol = ServerProtocol . Ftp ;
SubFolderPath = string . Empty ;
BrowserProtocol = BrowserProtocol . Http ;
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
}
2014-05-20 14:14:03 +12:00
public string GetSubFolderPath ( string filename = null )
2013-11-03 23:53:49 +13:00
{
NameParser parser = new NameParser ( NameParserType . URL ) ;
2014-05-20 14:14:03 +12:00
string path = parser . Parse ( SubFolderPath . Replace ( "%host" , Host ) ) ;
return Helpers . CombineURL ( path , filename ) ;
2013-11-03 23:53:49 +13:00
}
2014-04-20 13:16:51 +12:00
public string GetHttpHomePath ( )
{
2014-05-20 14:14:03 +12:00
// @ deprecated
if ( HttpHomePath . StartsWith ( "@" ) )
{
HttpHomePath = HttpHomePath . Substring ( 1 ) ;
HttpHomePathAutoAddSubFolderPath = false ;
}
2014-05-30 21:09:13 +12:00
HttpHomePath = URLHelpers . RemovePrefixes ( HttpHomePath ) ;
2014-05-20 14:14:03 +12:00
2014-04-20 13:16:51 +12:00
NameParser nameParser = new NameParser ( NameParserType . URL ) ;
return nameParser . Parse ( HttpHomePath . Replace ( "%host" , Host ) ) ;
}
2013-11-03 23:53:49 +13:00
public string GetUriPath ( string filename )
{
if ( string . IsNullOrEmpty ( Host ) )
{
return string . Empty ;
}
if ( HttpHomePathNoExtension )
{
filename = Path . GetFileNameWithoutExtension ( filename ) ;
}
2014-04-20 13:16:51 +12:00
filename = Helpers . URLEncode ( filename ) ;
2013-11-03 23:53:49 +13:00
string subFolderPath = GetSubFolderPath ( ) ;
2014-04-20 13:16:51 +12:00
subFolderPath = Helpers . URLPathEncode ( subFolderPath ) ;
string httpHomePath = GetHttpHomePath ( ) ;
httpHomePath = Helpers . URLPathEncode ( httpHomePath ) ;
string path ;
2013-11-03 23:53:49 +13:00
if ( string . IsNullOrEmpty ( httpHomePath ) )
{
string host = Host ;
if ( host . StartsWith ( "ftp." ) )
{
host = host . Substring ( 4 ) ;
}
path = Helpers . CombineURL ( host , subFolderPath , filename ) ;
}
else
{
2014-04-20 13:16:51 +12:00
if ( HttpHomePathAutoAddSubFolderPath )
2013-11-03 23:53:49 +13:00
{
path = Helpers . CombineURL ( httpHomePath , subFolderPath ) ;
}
else
{
2014-04-20 13:16:51 +12:00
path = httpHomePath ;
2013-11-03 23:53:49 +13:00
}
if ( path . EndsWith ( "=" ) )
{
path + = filename ;
}
else
{
path = Helpers . CombineURL ( path , filename ) ;
}
}
2014-04-20 13:16:51 +12:00
string browserProtocol = BrowserProtocol . GetDescription ( ) ;
2013-11-03 23:53:49 +13:00
if ( ! path . StartsWith ( browserProtocol ) )
{
path = browserProtocol + path ;
}
return path ;
}
public string GetFtpPath ( string filemame )
{
if ( string . IsNullOrEmpty ( FTPAddress ) )
{
return string . Empty ;
}
return Helpers . CombineURL ( FTPAddress , GetSubFolderPath ( filemame ) ) ;
}
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
}
}
}