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
|
2017-01-11 21:39:40 +13:00
|
|
|
|
Copyright (c) 2007-2017 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
|
|
|
|
|
{
|
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; }
|
|
|
|
|
|
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; }
|
|
|
|
|
|
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; }
|
|
|
|
|
|
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; }
|
|
|
|
|
|
2015-05-02 22:57:33 +12:00
|
|
|
|
[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
|
|
|
|
}
|
|
|
|
|
|
2015-05-02 22:57:33 +12: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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
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;
|
|
|
|
|
IsActive = 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
|
|
|
|
}
|
|
|
|
|
|
Fix issue #372
Several changes were made to fix the encoding of ftp dirpaths and allow
uploads to folders with non-ascii characters:
-Fixed encoding of valid urls in ShareX.HelpersLib.NameParser.cs
-Fixed subfolder path generation and wrong url re-encoding in
FTPAccount.cs
-Fixed preview paths in FTP Destination settings (FTPAccount.cs)
-Fixed subfolder path generation in FTP.cs
This fixes issue #372, which was reproduced with the following account
config on a local ftp server (import from clipboard on destination
settings):
{
"Protocol": "FTP",
"Name": "New account",
"Host": "localhost",
"Port": 21,
"Username": "test",
"Password": "test",
"ServerProtocol": "ftp",
"SubFolderPath": "DEV/grhh本人%",
"BrowserProtocol": "http",
"HttpHomePath": "",
"HttpHomePathAutoAddSubFolderPath": true,
"HttpHomePathNoExtension": false,
"IsActive": false,
"FTPSEncryption": "Explicit",
"FTPSCertificateLocation": "",
"Passphrase": null,
"Keypath": null
}
2015-01-02 10:44:18 +13:00
|
|
|
|
public string GetSubFolderPath(string filename = null, NameParserType nameParserType = NameParserType.URL)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
Fix issue #372
Several changes were made to fix the encoding of ftp dirpaths and allow
uploads to folders with non-ascii characters:
-Fixed encoding of valid urls in ShareX.HelpersLib.NameParser.cs
-Fixed subfolder path generation and wrong url re-encoding in
FTPAccount.cs
-Fixed preview paths in FTP Destination settings (FTPAccount.cs)
-Fixed subfolder path generation in FTP.cs
This fixes issue #372, which was reproduced with the following account
config on a local ftp server (import from clipboard on destination
settings):
{
"Protocol": "FTP",
"Name": "New account",
"Host": "localhost",
"Port": 21,
"Username": "test",
"Password": "test",
"ServerProtocol": "ftp",
"SubFolderPath": "DEV/grhh本人%",
"BrowserProtocol": "http",
"HttpHomePath": "",
"HttpHomePathAutoAddSubFolderPath": true,
"HttpHomePathNoExtension": false,
"IsActive": false,
"FTPSEncryption": "Explicit",
"FTPSCertificateLocation": "",
"Passphrase": null,
"Keypath": null
}
2015-01-02 10:44:18 +13:00
|
|
|
|
string path = NameParser.Parse(nameParserType, SubFolderPath.Replace("%host", Host));
|
2014-06-10 08:00:59 +12:00
|
|
|
|
return URLHelpers.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-06-06 07:47:36 +12:00
|
|
|
|
return NameParser.Parse(NameParserType.URL, HttpHomePath.Replace("%host", Host));
|
2014-04-20 13:16:51 +12:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-10 10:59:26 +12: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)
|
|
|
|
|
{
|
|
|
|
|
filename = Path.GetFileNameWithoutExtension(filename);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-12 20:31:33 +13:00
|
|
|
|
filename = URLHelpers.URLEncode(filename);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-08-10 10:59:26 +12:00
|
|
|
|
if (subFolderPath == null)
|
|
|
|
|
{
|
|
|
|
|
subFolderPath = GetSubFolderPath();
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-24 12:17:42 +12:00
|
|
|
|
UriBuilder httpHomeUri;
|
2015-01-12 19:48:24 +13:00
|
|
|
|
|
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))
|
|
|
|
|
{
|
2016-09-17 19:07:02 +12:00
|
|
|
|
string host = Host;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
if (host.StartsWith("ftp."))
|
|
|
|
|
{
|
|
|
|
|
host = host.Substring(4);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-24 12:17:42 +12:00
|
|
|
|
httpHomeUri = new UriBuilder(URLHelpers.CombineURL(host, subFolderPath, filename));
|
2014-12-31 21:46:28 +13:00
|
|
|
|
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
|
|
|
|
|
{
|
2014-10-09 15:53:51 +13:00
|
|
|
|
//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) : "";
|
2014-10-09 15:53:51 +13:00
|
|
|
|
|
2014-09-24 12:17:42 +12:00
|
|
|
|
if (portSpecifiedAt >= 0)
|
|
|
|
|
int.TryParse(httpHome.Substring(portSpecifiedAt + 1), out httpHomePort);
|
2014-09-26 14:43:13 +12:00
|
|
|
|
|
2014-09-24 12:17:42 +12:00
|
|
|
|
//Build URI
|
2014-10-09 15:53:51 +13:00
|
|
|
|
httpHomeUri = new UriBuilder { Host = httpHomeHost, Path = httpHomeDir, Query = httpHomeQuery };
|
2014-09-24 12:17:42 +12:00
|
|
|
|
if (portSpecifiedAt >= 0)
|
2016-09-17 19:07:02 +12:00
|
|
|
|
{
|
2014-09-24 12:17:42 +12:00
|
|
|
|
httpHomeUri.Port = httpHomePort;
|
2016-09-17 19:07:02 +12:00
|
|
|
|
}
|
2014-09-24 12:17:42 +12:00
|
|
|
|
|
|
|
|
|
if (httpHomeUri.Query.EndsWith("="))
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-10-09 15:53:51 +13:00
|
|
|
|
//Setting URIBuilder.Query automatically prepends a ? so we must trim it first.
|
|
|
|
|
if (HttpHomePathAutoAddSubFolderPath)
|
2016-09-17 19:07:02 +12:00
|
|
|
|
{
|
2014-10-09 15:53:51 +13:00
|
|
|
|
httpHomeUri.Query = URLHelpers.CombineURL(httpHomeUri.Query.Substring(1), subFolderPath, filename);
|
2016-09-17 19:07:02 +12:00
|
|
|
|
}
|
2014-10-09 15:53:51 +13:00
|
|
|
|
else
|
2016-09-17 19:07:02 +12:00
|
|
|
|
{
|
2014-10-09 15:53:51 +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
|
|
|
|
|
{
|
2014-10-09 15:53:51 +13:00
|
|
|
|
if (HttpHomePathAutoAddSubFolderPath)
|
2016-09-17 19:07:02 +12:00
|
|
|
|
{
|
2014-10-09 15:53:51 +13:00
|
|
|
|
httpHomeUri.Path = URLHelpers.CombineURL(httpHomeUri.Path, subFolderPath);
|
2016-09-17 19:07:02 +12:00
|
|
|
|
}
|
2014-10-09 15:53:51 +13:00
|
|
|
|
|
2014-09-24 12:17:42 +12:00
|
|
|
|
httpHomeUri.Path = URLHelpers.CombineURL(httpHomeUri.Path, filename);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-24 12:17:42 +12:00
|
|
|
|
httpHomeUri.Scheme = BrowserProtocol.GetDescription();
|
2015-01-13 08:14:52 +13:00
|
|
|
|
return httpHomeUri.Uri.AbsoluteUri;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetFtpPath(string filemame)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(FTPAddress))
|
|
|
|
|
{
|
2016-05-25 06:15:45 +12:00
|
|
|
|
return "";
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
Fix issue #372
Several changes were made to fix the encoding of ftp dirpaths and allow
uploads to folders with non-ascii characters:
-Fixed encoding of valid urls in ShareX.HelpersLib.NameParser.cs
-Fixed subfolder path generation and wrong url re-encoding in
FTPAccount.cs
-Fixed preview paths in FTP Destination settings (FTPAccount.cs)
-Fixed subfolder path generation in FTP.cs
This fixes issue #372, which was reproduced with the following account
config on a local ftp server (import from clipboard on destination
settings):
{
"Protocol": "FTP",
"Name": "New account",
"Host": "localhost",
"Port": 21,
"Username": "test",
"Password": "test",
"ServerProtocol": "ftp",
"SubFolderPath": "DEV/grhh本人%",
"BrowserProtocol": "http",
"HttpHomePath": "",
"HttpHomePathAutoAddSubFolderPath": true,
"HttpHomePathNoExtension": false,
"IsActive": false,
"FTPSEncryption": "Explicit",
"FTPSCertificateLocation": "",
"Passphrase": null,
"Keypath": null
}
2015-01-02 10:44:18 +13:00
|
|
|
|
return URLHelpers.CombineURL(FTPAddress, GetSubFolderPath(filemame, NameParserType.FolderPath));
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|