Skip to content

Commit

Permalink
(GH-363) Show human-readable file sizes when downloading
Browse files Browse the repository at this point in the history
When downloading files, show the download progress like 'Saving 8.5 MB of
8.82 MB (8911368/9252864)' instead of 'Saving 8911368 of 9252864'.

Adds Format-FileSize helper function to format the file sizes like
mentioned above.
  • Loading branch information
KishanBagaria committed Sep 5, 2015
1 parent 1651424 commit 8c94c71
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/chocolatey.resources/chocolatey.resources.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="helpers\chocolateyInstaller.psm1" />
<EmbeddedResource Include="helpers\functions\Format-FileSize.ps1" />
<EmbeddedResource Include="helpers\functions\Get-BinRoot.ps1" />
<EmbeddedResource Include="helpers\functions\Get-CheckSumValid.ps1" />
<EmbeddedResource Include="helpers\functions\Get-ChocolateyUnzip.ps1" />
Expand Down
25 changes: 25 additions & 0 deletions src/chocolatey.resources/helpers/functions/Format-FileSize.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright © 2011 - Present RealDimensions Software, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Function Format-FileSize() {
Param ([double]$size)
Foreach ($unit in @('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB')) {
If ($size -lt 1024) {
return [string]::Format("{0:0.##} {1}", $size, $unit)
}
$size /= 1024
}
return [string]::Format("{0:0.##} YB", $size)
}
4 changes: 3 additions & 1 deletion src/chocolatey.resources/helpers/functions/Get-FtpFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ param(
# send the ftp request to the server
$ftpresponse = $ftprequest.GetResponse()
[int]$goal = $ftpresponse.ContentLength
$goalFormatted = Format-FileSize $goal

# get a download stream from the server response
$reader = $ftpresponse.GetResponseStream()
Expand All @@ -42,8 +43,9 @@ param(
$writer.Write($buffer, 0, $count);
if(!$quiet) {
$total += $count
$totalFormatted = Format-FileSize $total
if($goal -gt 0) {
Write-Progress "Downloading $url to $fileName" "Saving $total of $goal" -id 0 -percentComplete (($total/$goal)*100)
Write-Progress "Downloading $url to $fileName" "Saving $totalFormatted of $goalFormatted ($total/$goal)" -id 0 -percentComplete (($total/$goal)*100)
} else {
Write-Progress "Downloading $url to $fileName" "Saving $total bytes..." -id 0 -Completed
}
Expand Down
4 changes: 3 additions & 1 deletion src/chocolatey.resources/helpers/functions/Get-WebFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ param(

if($res.StatusCode -eq 200) {
[long]$goal = $res.ContentLength
$goalFormatted = Format-FileSize $goal
$reader = $res.GetResponseStream()

if ($fileName) {
Expand Down Expand Up @@ -140,8 +141,9 @@ param(
$output += $encoding.GetString($buffer,0,$count)
} elseif(!$quiet) {
$total += $count
$totalFormatted = Format-FileSize $total
if($goal -gt 0 -and ++$iterLoop%10 -eq 0) {
Write-Progress "Downloading $url to $fileName" "Saving $total of $goal" -id 0 -percentComplete (($total/$goal)*100)
Write-Progress "Downloading $url to $fileName" "Saving $totalFormatted of $goalFormatted ($total/$goal)" -id 0 -percentComplete (($total/$goal)*100)
}

if ($total -eq $goal) {
Expand Down

0 comments on commit 8c94c71

Please sign in to comment.