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
2023-01-10 09:31:02 +13:00
Copyright ( c ) 2007 - 2023 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
{
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")]
2014-03-28 22:12:07 +13:00
[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 ; }
2020-06-01 13:17:04 +12:00
[Category("Localhost"), PasswordPropertyText(true), JsonEncrypt]
2013-11-03 23:53:49 +13:00
public string Password { get ; set ; }
2014-05-20 14:14:03 +12:00
[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 ; }
2014-05-20 14:14:03 +12:00
[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 ) )
{
2016-05-25 06:15:45 +12:00
return "" ;
2013-11-03 23:53:49 +13:00
}
2022-05-03 18:34:18 +12:00
return new Uri ( FileHelpers . ExpandFolderVariables ( LocalhostRoot ) ) . AbsoluteUri ;
2013-11-03 23:53:49 +13:00
}
}
2021-12-12 10:21:19 +13:00
private string exampleFileName = "screenshot.jpg" ;
2013-11-03 23:53:49 +13:00
[Category("Localhost"), Description("Preview of the Localhost Path based on the settings above")]
public string PreviewLocalPath
{
get
{
2021-12-12 10:21:19 +13:00
return GetLocalhostUri ( exampleFileName ) ;
2013-11-03 23:53:49 +13:00
}
}
[Category("Localhost"), Description("Preview of the HTTP Path based on the settings above")]
public string PreviewRemotePath
{
get
{
2021-12-12 10:21:19 +13:00
return GetUriPath ( exampleFileName ) ;
2013-11-03 23:53:49 +13:00
}
}
public LocalhostAccount ( )
{
2014-05-20 14:14:03 +12:00
Name = "New account" ;
2016-05-25 06:15:45 +12:00
LocalhostRoot = "" ;
2014-05-20 14:14:03 +12:00
Port = 80 ;
2016-05-25 06:15:45 +12:00
SubFolderPath = "" ;
HttpHomePath = "" ;
2014-05-20 14:14:03 +12:00
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 ( )
{
2022-05-03 18:34:18 +12:00
return NameParser . Parse ( NameParserType . URL , SubFolderPath . Replace ( "%host" , FileHelpers . ExpandFolderVariables ( LocalhostRoot ) ) ) ;
2013-11-03 23:53:49 +13: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
2022-05-03 18:34:18 +12:00
return NameParser . Parse ( NameParserType . URL , HttpHomePath . Replace ( "%host" , FileHelpers . ExpandFolderVariables ( LocalhostRoot ) ) ) ;
2013-11-03 23:53:49 +13:00
}
2021-12-12 10:21:19 +13:00
public string GetUriPath ( string fileName )
2013-11-03 23:53:49 +13:00
{
if ( string . IsNullOrEmpty ( LocalhostRoot ) )
{
2016-05-25 06:15:45 +12:00
return "" ;
2013-11-03 23:53:49 +13:00
}
2014-04-21 10:17:54 +12:00
if ( HttpHomePathNoExtension )
2013-11-03 23:53:49 +13:00
{
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 ) ;
2014-04-21 10:17:54 +12:00
string subFolderPath = GetSubFolderPath ( ) ;
2018-06-22 04:25:11 +12:00
subFolderPath = URLHelpers . URLEncode ( subFolderPath , true ) ;
2014-04-21 10:17:54 +12:00
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
{
2018-06-22 04:25:11 +12:00
path = URLHelpers . URLEncode ( httpHomePath , true ) ;
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-06-10 08:00:59 +12:00
path = URLHelpers . CombineURL ( path , subFolderPath ) ;
2013-11-03 23:53:49 +13:00
}
2021-12-12 10:21:19 +13:00
path = URLHelpers . CombineURL ( path , fileName ) ;
2014-04-21 10:17:54 +12:00
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 ) )
{
2016-05-25 06:15:45 +12:00
return "" ;
2013-11-03 23:53:49 +13:00
}
2020-05-14 09:18:27 +12:00
2022-05-03 18:34:18 +12:00
return Path . Combine ( Path . Combine ( FileHelpers . ExpandFolderVariables ( LocalhostRoot ) , GetSubFolderPath ( ) ) , fileName ) ;
2013-11-03 23:53:49 +13:00
}
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
{
2016-05-25 06:15:45 +12:00
return "" ;
2013-11-03 23:53:49 +13:00
}
2014-06-10 08:00:59 +12:00
return URLHelpers . CombineURL ( localhostAddress , GetSubFolderPath ( ) , fileName ) ;
2013-11-03 23:53:49 +13:00
}
public override string ToString ( )
{
2022-05-03 18:34:18 +12:00
return string . Format ( "{0} - {1}:{2}" , Name , FileHelpers . GetVariableFolderPath ( LocalhostRoot ) , Port ) ;
2013-11-03 23:53:49 +13:00
}
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
}
}