ShareX/UploadersLib/FileUploaders/Localhost/LocalhostAccount.cs

221 lines
7.3 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)
using HelpersLib;
using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.IO;
namespace UploadersLib
{
2014-04-30 21:46:15 +12:00
public class LocalhostAccount : ICloneable
2013-11-03 23:53:49 +13:00
{
[Category("Localhost"), Description("Shown in the list as: Name - LocalhostRoot:Port")]
public string Name { get; set; }
[Category("Localhost"), Description(@"Root folder, e.g. C:\Inetpub\wwwroot")]
[Editor(typeof(DirectoryNameEditor), typeof(UITypeEditor))]
2013-11-03 23:53:49 +13:00
public string LocalhostRoot { get; set; }
[Category("Localhost"), Description("Port Number"), DefaultValue(80)]
public int Port { get; set; }
[Category("Localhost")]
public string UserName { get; set; }
[Category("Localhost"), PasswordPropertyText(true)]
public string Password { get; set; }
[Category("Localhost"), Description("Localhost Sub-folder Path, e.g. screenshots, %y = year, %mo = month. SubFolderPath will be automatically appended to HttpHomePath if HttpHomePath does not start with @")]
2013-11-03 23:53:49 +13:00
public string SubFolderPath { get; set; }
[Category("Localhost"), Description("HTTP Home Path, %host = Host e.g. google.com without http:// because you choose that in Remote Protocol.\nURL = HttpHomePath + SubFolderPath + FileName\nURL = Host + SubFolderPath + FileName (if HttpHomePath is empty)")]
2013-11-03 23:53:49 +13:00
public string HttpHomePath { get; set; }
2014-04-21 10:17:54 +12:00
[Category("Localhost"), Description("Automatically add sub folder path to end of http home path"), DefaultValue(true)]
public bool HttpHomePathAutoAddSubFolderPath { get; set; }
[Category("Localhost"), Description("Don't add file extension to URL"), DefaultValue(false)]
public bool HttpHomePathNoExtension { get; set; }
2014-05-31 03:24:43 +12:00
[Category("Localhost"), Description("Choose an appropriate protocol to be accessed by the browser. Use 'file' for Shared Folders. RemoteProtocol will always be 'file' if HTTP Home Path is empty. "), DefaultValue(BrowserProtocol.file)]
2013-11-03 23:53:49 +13:00
public BrowserProtocol RemoteProtocol { get; set; }
[Category("Localhost"), Description("file://Host:Port"), Browsable(false)]
public string LocalUri
{
get
{
if (string.IsNullOrEmpty(LocalhostRoot))
{
return string.Empty;
}
return new Uri(LocalhostRoot).AbsoluteUri;
}
}
private string exampleFilename = "screenshot.jpg";
[Category("Localhost"), Description("Preview of the Localhost Path based on the settings above")]
public string PreviewLocalPath
{
get
{
return GetLocalhostUri(exampleFilename);
}
}
[Category("Localhost"), Description("Preview of the HTTP Path based on the settings above")]
public string PreviewRemotePath
{
get
{
return GetUriPath(exampleFilename);
}
}
public LocalhostAccount()
{
Name = "New account";
LocalhostRoot = string.Empty;
Port = 80;
SubFolderPath = string.Empty;
HttpHomePath = string.Empty;
HttpHomePathAutoAddSubFolderPath = true;
HttpHomePathNoExtension = false;
2014-05-31 03:24:43 +12:00
RemoteProtocol = BrowserProtocol.file;
2013-11-03 23:53:49 +13:00
}
public string GetSubFolderPath()
{
return NameParser.Parse(NameParserType.URL, SubFolderPath.Replace("%host", LocalhostRoot));
2013-11-03 23:53:49 +13:00
}
public string GetHttpHomePath()
{
// @ deprecated
if (HttpHomePath.StartsWith("@"))
{
HttpHomePath = HttpHomePath.Substring(1);
HttpHomePathAutoAddSubFolderPath = false;
}
HttpHomePath = URLHelpers.RemovePrefixes(HttpHomePath);
return NameParser.Parse(NameParserType.URL, HttpHomePath.Replace("%host", LocalhostRoot));
2013-11-03 23:53:49 +13:00
}
2014-04-21 10:17:54 +12:00
public string GetUriPath(string filename)
2013-11-03 23:53:49 +13:00
{
if (string.IsNullOrEmpty(LocalhostRoot))
{
return string.Empty;
}
2014-04-21 10:17:54 +12:00
if (HttpHomePathNoExtension)
2013-11-03 23:53:49 +13:00
{
2014-04-21 10:17:54 +12:00
filename = Path.GetFileNameWithoutExtension(filename);
2013-11-03 23:53:49 +13:00
}
2014-04-21 10:17:54 +12:00
filename = Helpers.URLEncode(filename);
string subFolderPath = GetSubFolderPath();
subFolderPath = Helpers.URLPathEncode(subFolderPath);
string httpHomePath = GetHttpHomePath();
2013-11-03 23:53:49 +13:00
2014-04-21 10:17:54 +12:00
string path;
if (string.IsNullOrEmpty(httpHomePath))
2013-11-03 23:53:49 +13:00
{
2014-05-31 03:24:43 +12:00
RemoteProtocol = BrowserProtocol.file;
2014-04-21 10:17:54 +12:00
path = LocalUri.Replace("file://", "");
2013-11-03 23:53:49 +13:00
}
2014-04-21 10:17:54 +12:00
else
{
path = Helpers.URLPathEncode(httpHomePath);
2014-04-21 10:17:54 +12:00
}
if (Port != 80)
2013-11-03 23:53:49 +13:00
{
2014-04-21 10:17:54 +12:00
path = string.Format("{0}:{1}", path, Port);
2013-11-03 23:53:49 +13:00
}
2014-04-21 10:17:54 +12:00
if (HttpHomePathAutoAddSubFolderPath)
2013-11-03 23:53:49 +13:00
{
2014-04-21 10:17:54 +12:00
path = Helpers.CombineURL(path, subFolderPath);
2013-11-03 23:53:49 +13:00
}
2014-04-21 10:17:54 +12:00
path = Helpers.CombineURL(path, filename);
string remoteProtocol = RemoteProtocol.GetDescription();
2013-11-03 23:53:49 +13:00
2014-04-21 10:17:54 +12:00
if (!path.StartsWith(remoteProtocol))
2013-11-03 23:53:49 +13:00
{
2014-04-21 10:17:54 +12:00
path = remoteProtocol + path;
2013-11-03 23:53:49 +13:00
}
return path;
}
public string GetLocalhostPath(string fileName)
{
if (string.IsNullOrEmpty(LocalhostRoot))
{
return string.Empty;
}
return Path.Combine(Path.Combine(LocalhostRoot, GetSubFolderPath()), fileName);
}
public string GetLocalhostUri(string fileName)
{
2014-04-21 10:17:54 +12:00
string localhostAddress = LocalUri;
2013-11-03 23:53:49 +13:00
2014-04-21 10:17:54 +12:00
if (string.IsNullOrEmpty(localhostAddress))
2013-11-03 23:53:49 +13:00
{
return string.Empty;
}
2014-04-21 10:17:54 +12:00
return Helpers.CombineURL(localhostAddress, GetSubFolderPath(), fileName);
2013-11-03 23:53:49 +13:00
}
public override string ToString()
{
return string.Format("{0} - {1}:{2}", Name, LocalhostRoot, Port);
}
2014-04-30 21:46:15 +12:00
public LocalhostAccount Clone()
{
return MemberwiseClone() as LocalhostAccount;
}
object ICloneable.Clone()
{
return Clone();
}
2013-11-03 23:53:49 +13:00
}
}