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 possibly other options.

Advertisement

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
# @flag         ShowResultsInMsgBox
# @version      5
# @homepage     https://winscp.net/eng/docs/extension_generate_http_url
# @require      WinSCP 5.12
# @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" "-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,
    [Parameter(Mandatory = $True, ValueFromRemainingArguments = $True, Position = 0)]
    $paths
)
 
try
{
    if (!$webRoot -or ($webRoot.SubString($webRoot.Length - 1, 1) -ne "/"))
    {
        $webRoot += "/"
    }
 
    $result = $Null
    foreach ($filePath in $paths)
    {
        $filePath = "$path$filePath"
 
        if (($filePath.Length -lt $webRoot.length) -or
            ($filePath.SubString(0, $webRoot.Length) -ne $webRoot))
        {
            throw "**The path $filePath is not under web root $webRoot.**"
        }
        
        if ($rootPath)
        {
            if ($rootPath.SubString($rootPath.Length - 1) -ne "/")
            {
                $rootPath += "/"
            }
        }
        else
        {
            $rootPath = "/"
        }
 
        $urlPath = $filePath.SubString($webRoot.Length)
        $urlPath = ($urlPath -split "/" | %{ [System.Uri]::EscapeDataString($_) }) -join "/"
     
        if ($https)
        { 
            $protocol = "https://"
        }
        else
        {
            $protocol = "http://"
        }
 
        if (!$hostName)
        {
            $hostName = $serverName
        }
        
        $url = "$protocol$hostName$rootPath$urlPath"
        $result += $url
        if ($paths.Count -gt 1)
        {
            $result += "`r`n"
        }
 
        if ($open)
        {
            Start-Process $url
        }
    }
 
    if ($pause)
    {
        Write-Host -NoNewline $result
    }
 
    if ($clipboard)
    {
        Add-Type -Assembly PresentationCore
        [Windows.Clipboard]::SetText($result) 
    }
 
    $result = 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    $result = 1
}
 
exit $result

Advertisement

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 not to display a message box with the generated URL.

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

Check the Open URL in web browser to open the generated URL in your web browser.

In the Keyboard shortcut, you can specify a keyboard shortcut for the extension.

Last modified: by martin