Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(GH-363) Show human-readable file sizes when downloading #404

Merged
merged 1 commit into from
Sep 15, 2015
Merged
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
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)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this file require attribution? If you pulled this off of the internet, it will require at the very least linking back to the source.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not, it should include this header:

# 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stole the function from SO. Instead of getting the attribution right, I rewrote it in a simpler way.

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