Generate HTTP URL Extension

This extension allows you to generate HTTP URL of the selected file.

For the extension to work correctly, you may need to configure web root folder and possible other options.

This is an official WinSCP extension and is distributed in WinSCP installer.

# @name         Generate &HTTP URL
# @command      powershell.exe -ExecutionPolicy Bypass -STA -NoProfile -File "%EXTENSION_PATH%" -webRoot "%WebRoot%" -rootPath "%RootPath%" -hostName "%HostName%" -serverName "!@" -path "!/!" %Https% %Pause% %Clipboard% %Open%
# @description  Generates HTTP URL of the selected file
# @flag         RemoteFiles
# @version      1
# @homepage     https://winscp.net/eng/docs/extension_generate_http_url
# @require      WinSCP 5.9.3
# @option       - -site group "URL"
# @option         - -site label "These options are site-specific."
# @option         WebRoot -site textbox "&Web root path"
# @option         Https -site checkbox "Use HTTP&S" "" "-https"
# @option         RootPath -site textbox "&URL root path (optional)"
# @option         HostName -site textbox "&Web server hostname override (optional)"
# @option       - group "Options"
# @option         Pause checkbox "Display URL in console" "-pause" "-pause"
# @option         Clipboard checkbox "Copy URL to clipboard" "-clipboard" "-clipboard"
# @option         Open checkbox "Open URL in web browser" "" "-open"
# @optionspage  https://winscp.net/eng/docs/extension_generate_http_url#options
 
param (
    [Parameter(Mandatory = $True)]
    $webRoot,
    $rootPath,
    $hostName,
    $serverName,
    [Parameter(Mandatory = $True)]
    $path,
    [Switch] 
    $https,
    [Switch] 
    $pause,
    [Switch] 
    $clipboard,
    [Switch] 
    $open
)
 
try
{
    if (!$webRoot -or ($webRoot.SubString($webRoot.Length - 1, 1) -ne "/"))
    {
        $webRoot += "/"
    }
 
    if (($path.Length -lt $webRoot.length) -or
        ($path.SubString(0, $webRoot.Length) -ne $webRoot))
    {
        throw "The path $path is not under web root $webRoot."
    }
    
    if ($rootPath)
    {
        if ($rootPath.SubString($rootPath.Length - 1) -ne "/")
        {
            $rootPath += "/"
        }
    }
    else
    {
        $rootPath = "/"
    }
 
    $urlPath = $path.SubString($webRoot.Length)
 
    if ($https)
    { 
        $protocol = "https://"
    }
    else
    {
        $protocol = "http://"
    }
 
    if (!$hostName)
    {
        $hostName = $serverName
    }
    
    $url = "$protocol$hostName$rootPath$urlPath"
 
    Write-Host $url
 
    if ($clipboard)
    {
        Add-Type -Assembly PresentationCore
        [Windows.Clipboard]::SetText($url) 
    }
 
    if ($open)
    {
        Start-Process $url
    }
 
    $result = 0
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    $result = 1
    $pause = $true
}
 
if ($pause)
{
    Write-Host "Press any key to exit..."
    [System.Console]::ReadKey() | Out-Null
}
 
exit $result

Options

URL

The options in the URL section are site specific. To configure them, open the respective site and only then go to the Preferences dialog.

The Web root path is the root path of the site web server. It must correspond to the URL root path below.

Check the Use HTTPS to generate https:// URL instead of http://.

Use the URL root path, if your site is accessible in a subfolder of a domain. It must correspond to the Web root path above. E.g. if your site URL root is https://www.example.com/mysite/, specify /mysite/ here. Keep empty (or use the /), if your site is the entire domain, i.e. https://www.example.com/.

Use the Web server hostname override to specify your web server hostname, if it differs from the file server domain. E.g. if you connect to the ftp.example.com to manage your files, you want to specify www.example.com here. You can also use this option to generate an URL with a non-standard port number. You can keep the option empty, if your file server domain matches the web server domain.

Options

Uncheck the Display URL in console not to keep the console window, with the generated URL, open.

Uncheck the Copy URL to clipboard not to store the generated URL to the clipboard.

 
  extension_generate_http_url.txt · Last modified: by martin
 

Search Documentation

This page

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

Associations

Site design by Black Gate