Skip to content
This repository has been archived by the owner on Feb 19, 2019. It is now read-only.

[Enhancement] Added FTP support for the chocolatey file downloader. #137

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 71 additions & 69 deletions src/helpers/functions/Get-ChocolateyWebFile.ps1
Original file line number Diff line number Diff line change
@@ -1,70 +1,72 @@
function Get-ChocolateyWebFile {
<#
.SYNOPSIS
Downloads a file from the internets.
.DESCRIPTION
This will download a file from a url, tracking with a progress bar.
It returns the filepath to the downloaded file when it is complete.
.PARAMETER PackageName
The name of the package we want to download - this is arbitrary, call it whatever you want.
It's recommended you call it the same as your nuget package id.
.PARAMETER FileFullPath
This is the full path of the resulting file name.
.PARAMETER Url
This is the url to download the file from.
.PARAMETER Url64bit
OPTIONAL - If there is an x64 installer to download, please include it here. If not, delete this parameter
.EXAMPLE
Get-ChocolateyWebFile '__NAME__' 'C:\somepath\somename.exe' 'URL' '64BIT_URL_DELETE_IF_NO_64BIT'
.NOTES
This helper reduces the number of lines one would have to write to download a file to 1 line.
There is no error handling built into this method.
.LINK
Install-ChocolateyPackage
#>
param(
[string] $packageName,
[string] $fileFullPath,
[string] $url,
[string] $url64bit = $url
)
Write-Debug "Running 'Get-ChocolateyWebFile' for $packageName with url:`'$url`', fileFullPath:`'$fileFullPath`',and url64bit:`'$url64bit`'";

$url32bit = $url;
$processor = Get-WmiObject Win32_Processor
$procCount=(Get-WmiObject Win32_ComputerSystem).NumberofProcessors
if ($procCount -eq '1') {
$is64bit = $processor.AddressWidth -eq 64
Write-Debug "Processor width is $($processor.AddressWidth)."
} else {
Write-Debug "First processor width is $($processor[0].AddressWidth)."
$is64bit = $processor[0].AddressWidth -eq 64
}
$systemBit = '32 bit'
if ($is64bit) {
$systemBit = '64 bit';
$url = $url64bit;
}

$downloadMessage = "Downloading $packageName ($url) to $fileFullPath"
if ($url32bit -ne $url64bit) {$downloadMessage = "Downloading $packageName $systemBit ($url) to $fileFullPath.";}
Write-Host "$downloadMessage"
#$downloader = new-object System.Net.WebClient
#$downloader.DownloadFile($url, $fileFullPath)
if ($url.StartsWith('http')) {
Get-WebFile $url $fileFullPath
} else {
Write-Debug "We are attempting to copy the local item `'$url`' to `'$fileFullPath`'"
Copy-Item $url -Destination $fileFullPath -Force
}

Start-Sleep 2 #give it a sec or two to finish up
function Get-ChocolateyWebFile {
<#
.SYNOPSIS
Downloads a file from the internets.
.DESCRIPTION
This will download a file from a url, tracking with a progress bar.
It returns the filepath to the downloaded file when it is complete.
.PARAMETER PackageName
The name of the package we want to download - this is arbitrary, call it whatever you want.
It's recommended you call it the same as your nuget package id.
.PARAMETER FileFullPath
This is the full path of the resulting file name.
.PARAMETER Url
This is the url to download the file from.
.PARAMETER Url64bit
OPTIONAL - If there is an x64 installer to download, please include it here. If not, delete this parameter
.EXAMPLE
Get-ChocolateyWebFile '__NAME__' 'C:\somepath\somename.exe' 'URL' '64BIT_URL_DELETE_IF_NO_64BIT'
.NOTES
This helper reduces the number of lines one would have to write to download a file to 1 line.
There is no error handling built into this method.
.LINK
Install-ChocolateyPackage
#>
param(
[string] $packageName,
[string] $fileFullPath,
[string] $url,
[string] $url64bit = $url
)
Write-Debug "Running 'Get-ChocolateyWebFile' for $packageName with url:`'$url`', fileFullPath:`'$fileFullPath`',and url64bit:`'$url64bit`'";

$url32bit = $url;
$processor = Get-WmiObject Win32_Processor
$procCount=(Get-WmiObject Win32_ComputerSystem).NumberofProcessors
if ($procCount -eq '1') {
$is64bit = $processor.AddressWidth -eq 64
Write-Debug "Processor width is $($processor.AddressWidth)."
} else {
Write-Debug "First processor width is $($processor[0].AddressWidth)."
$is64bit = $processor[0].AddressWidth -eq 64
}
$systemBit = '32 bit'
if ($is64bit) {
$systemBit = '64 bit';
$url = $url64bit;
}

$downloadMessage = "Downloading $packageName ($url) to $fileFullPath"
if ($url32bit -ne $url64bit) {$downloadMessage = "Downloading $packageName $systemBit ($url) to $fileFullPath.";}
Write-Host "$downloadMessage"
#$downloader = new-object System.Net.WebClient
#$downloader.DownloadFile($url, $fileFullPath)
if ($url.StartsWith('http')) {
Get-WebFile $url $fileFullPath
} elseif ($url.StartsWith('ftp')) {
Get-FtpFile $url $fileFullPath
} else {
Write-Debug "We are attempting to copy the local item `'$url`' to `'$fileFullPath`'"
Copy-Item $url -Destination $fileFullPath -Force
}

Start-Sleep 2 #give it a sec or two to finish up
}
58 changes: 58 additions & 0 deletions src/helpers/functions/Get-FtpFile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
## Get-FtpFile
##############################################################################################################
## Downloads a file from ftp
## Some code from http://stackoverflow.com/questions/265339/whats-the-best-way-to-automate-secure-ftp-in-powershell
## Additional functionality emulated from http://poshcode.org/417 (Get-WebFile)
## Written by Stephen C. Austin, Pwnt & Co. http://pwnt.co
##############################################################################################################
function Get-FtpFile {
param(
$url = '', #(Read-Host "The URL to download"),
$fileName = $null,
$username = $null,
$password = $null,
[switch]$quiet
)
# Create a FTPWebRequest object to handle the connection to the ftp server
$ftprequest = [System.Net.FtpWebRequest]::create($url)

# set the request's network credentials for an authenticated connection
$ftprequest.Credentials =
New-Object System.Net.NetworkCredential($username,$password)

$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$ftprequest.UseBinary = $true
$ftprequest.KeepAlive = $false

# send the ftp request to the server
$ftpresponse = $ftprequest.GetResponse()
[int]$goal = $ftpresponse.ContentLength

# get a download stream from the server response
$reader = $ftpresponse.GetResponseStream()

# create the target file on the local system and the download buffer
$writer = New-Object IO.FileStream ($fileName,[IO.FileMode]::Create)
[byte[]]$buffer = New-Object byte[] 1024
[int]$total = [int]$count = 0

# loop through the download stream and send the data to the target file
do{
$count = $reader.Read($buffer, 0, $buffer.Length);
$writer.Write($buffer, 0, $count);
if(!$quiet) {
$total += $count
if($goal -gt 0) {
Write-Progress "Downloading $url to $fileName" "Saving $total of $goal" -id 0 -percentComplete (($total/$goal)*100)
} else {
Write-Progress "Downloading $url to $fileName" "Saving $total bytes..." -id 0 -Completed
}
if ($total -eq $goal) {
Write-Progress "Completed download of $url." "Completed a total of $total bytes of $fileName" -id 0 -Completed
}
}
} while ($count -ne 0)

$writer.Flush()
$writer.close()
}