Skip to content

Commit b26ccff

Browse files
committed
cli download tool completed
1 parent ef544b2 commit b26ccff

File tree

4 files changed

+146
-45
lines changed

4 files changed

+146
-45
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
composer.lock
1111
*.swp
1212
*.swo
13+
*.zip
1314
.DS_Store

examples/HomeController.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use inhere\console\Controller;
4+
use inhere\console\utils\Download;
45
use inhere\console\utils\Interact;
56

67
/**
@@ -139,4 +140,42 @@ public function envCommand()
139140

140141
Interact::panel($info);
141142
}
143+
144+
/**
145+
* download a file to local
146+
*
147+
* @usage {command} url=url saveTo=[saveAs] type=[bar|text]
148+
* @example {command} url=https://github.com/inhere/php-librarys/archive/v2.0.1.zip type=bar
149+
*/
150+
public function downCommand()
151+
{
152+
$url = $this->input->getArg('url');
153+
154+
if (!$url) {
155+
\inhere\console\utils\Show::error('Please input you want to downloaded file url, use: url=[url]', 1);
156+
}
157+
158+
$saveAs = $this->input->getArg('saveAs');
159+
$type = $this->input->getArg('type', 'text');
160+
161+
if (!$saveAs) {
162+
$saveAs = __DIR__ . '/' . basename($url);
163+
}
164+
165+
$goon = Interact::confirm("Now, will download $url to $saveAs, go on");
166+
167+
if (!$goon) {
168+
\inhere\console\utils\Show::notice('Quit download, Bye!');
169+
170+
return 0;
171+
}
172+
173+
Download::down(
174+
$url,
175+
$saveAs,
176+
$type === 'bar' ? Download::PROGRESS_BAR : Download::PROGRESS_TEXT
177+
);
178+
179+
return 0;
180+
}
142181
}

src/utils/Download.php

Lines changed: 104 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,71 +14,106 @@
1414
*/
1515
class Download
1616
{
17+
const PROGRESS_TEXT = 1;
18+
const PROGRESS_BAR = 2;
19+
20+
private static $fileSize = null;
21+
private static $showType = 1;
22+
1723
/*
18-
OUT:
24+
progressBar() OUT:
1925
Connected...
2026
Mime-type: text/html; charset=utf-8
2127
Being redirected to: http://no2.php.net/distributions/php-5.2.5.tar.bz2
2228
Connected...
23-
Filesize: 7773024
29+
FileSize: 7773024
2430
Mime-type: application/octet-stream
2531
[========================================> ] 40% (3076/7590 kb)
2632
*/
2733

2834
/**
29-
* @param $code
30-
* @param $severity
31-
* @param $message
32-
* @param $messageCode
33-
* @param $transferredBytes
34-
* @param $maxBytes
35+
* @param int $notifyCode stream notify code
36+
* @param int $severity severity code
37+
* @param string $message Message text
38+
* @param int $messageCode Message code
39+
* @param int $transferredBytes Have been transferred bytes
40+
* @param int $maxBytes Target max length bytes
3541
*/
36-
public static function stream_notification_callback($code, $severity, $message, $messageCode, $transferredBytes, $maxBytes)
42+
protected static function progressShow($notifyCode, $severity, $message, $messageCode, $transferredBytes, $maxBytes)
3743
{
38-
static $fileSize = null;
44+
$msg = '';
3945

40-
switch($code) {
46+
switch($notifyCode) {
4147
case STREAM_NOTIFY_RESOLVE:
4248
case STREAM_NOTIFY_AUTH_REQUIRED:
4349
case STREAM_NOTIFY_COMPLETED:
4450
case STREAM_NOTIFY_FAILURE:
4551
case STREAM_NOTIFY_AUTH_RESULT:
52+
$msg = "NOTIFY: $message(NO: $messageCode, Severity: $severity)";
4653
/* Ignore */
4754
break;
4855

4956
case STREAM_NOTIFY_REDIRECTED:
50-
echo "Being redirected to: ", $message, "\n";
57+
$msg = "Being redirected to: $message";
5158
break;
5259

5360
case STREAM_NOTIFY_CONNECT:
54-
echo "Connected...\n";
61+
$msg = 'Connected ...';
5562
break;
5663

5764
case STREAM_NOTIFY_FILE_SIZE_IS:
58-
$fileSize = $maxBytes;
59-
echo "FileSize: ", $fileSize, "\n";
65+
self::$fileSize = $maxBytes;
66+
$fileSize = sprintf('%2d',$maxBytes/1024);
67+
$msg = "Got the file size: <info>$fileSize</info> kb";
6068
break;
6169

6270
case STREAM_NOTIFY_MIME_TYPE_IS:
63-
echo "Mime-type: ", $message, "\n";
71+
$msg = "Found the mime-type: <info>$message</info>";
6472
break;
6573

6674
case STREAM_NOTIFY_PROGRESS:
6775
if ($transferredBytes > 0) {
68-
if (!isset($fileSize)) {
69-
printf("\rUnknown fileSize.. %2d kb done..", $transferredBytes/1024);
70-
} else {
71-
$length = (int)(($transferredBytes/$fileSize)*100);
72-
printf("\r[%-100s] %d%% (%2d/%2d kb)", str_repeat("=", $length). ">", $length, ($transferredBytes/1024), $fileSize/1024);
73-
}
76+
self::showProgressByType($transferredBytes);
7477
}
75-
78+
7679
break;
7780
}
81+
82+
$msg && Show::write($msg);
83+
}
84+
85+
/**
86+
* @param $transferredBytes
87+
* @return string
88+
*/
89+
protected static function showProgressByType($transferredBytes)
90+
{
91+
if ($transferredBytes <= 0) {
92+
return '';
93+
}
94+
95+
$tfKb = $transferredBytes/1024;
96+
97+
if ( self::$showType === self::PROGRESS_BAR ) {
98+
$size = self::$fileSize;
99+
100+
if ( $size === null ) {
101+
printf("\rUnknown file size... %2d kb done..", $tfKb);
102+
} else {
103+
$length = ceil(($transferredBytes/$size)*100); // ■ =
104+
printf("\r[%-100s] %d%% (%2d/%2d kb)", str_repeat('=', $length). '>', $length, $tfKb, $size/1024);
105+
}
106+
107+
} else {
108+
printf("\r\rMade some progress, downloaded %2d kb so far", $tfKb);
109+
//$msg = "Made some progress, downloaded <info>$transferredBytes</info> so far";
110+
}
111+
112+
return '';
78113
}
79114

80115
/*
81-
OUT:
116+
progressText() OUT:
82117
Connected...
83118
Found the mime-type: text/html; charset=utf-8
84119
Being redirected to: http://no.php.net/contact
@@ -95,59 +130,85 @@ public static function stream_notification_callback($code, $severity, $message,
95130
Made some progress, downloaded 1440 so far
96131
... ...
97132
*/
98-
public static function stream_notification_callback1($code, $severity, $message, $messageCode, $transferredBytes, $maxBytes)
133+
134+
/**
135+
* @param int $notifyCode stream notify code
136+
* @param int $severity severity code
137+
* @param string $message Message text
138+
* @param int $messageCode Message code
139+
* @param int $transferredBytes Have been transferred bytes
140+
* @param int $maxBytes Target max length bytes
141+
*/
142+
protected static function progressText($notifyCode, $severity, $message, $messageCode, $transferredBytes, $maxBytes)
99143
{
100-
switch($code) {
144+
$msg = '';
145+
switch($notifyCode) {
101146
case STREAM_NOTIFY_RESOLVE:
102147
case STREAM_NOTIFY_AUTH_REQUIRED:
103148
case STREAM_NOTIFY_COMPLETED:
104149
case STREAM_NOTIFY_FAILURE:
105150
case STREAM_NOTIFY_AUTH_RESULT:
106-
var_dump($code, $severity, $message, $messageCode, $transferredBytes, $maxBytes);
151+
// var_dump($notifyCode, $severity, $message, $messageCode, $transferredBytes, $maxBytes);
152+
$msg = "NOTIFY: $message(NO: $messageCode, Severity: $severity)";
107153
/* Ignore */
108154
break;
109155

110156
case STREAM_NOTIFY_REDIRECTED:
111-
echo "Being redirected to: ", $message;
157+
$msg = "Being redirected to: $message";
112158
break;
113159

114160
case STREAM_NOTIFY_CONNECT:
115-
echo "Connected...";
161+
$msg = 'Connected...';
116162
break;
117163

118164
case STREAM_NOTIFY_FILE_SIZE_IS:
119-
echo "Got the fileSize: ", $maxBytes;
165+
$fileSize = sprintf('%2d',$maxBytes/1024);
166+
$msg = "Got the file size: <info>$fileSize</info> kb";
120167
break;
121168

122169
case STREAM_NOTIFY_MIME_TYPE_IS:
123-
echo "Found the mime-type: ", $message;
170+
$msg = "Found the mime-type: <info>$message</info>";
124171
break;
125172

126173
case STREAM_NOTIFY_PROGRESS:
127-
echo "Made some progress, downloaded ", $transferredBytes, " so far";
174+
if ( $transferredBytes > 0 ) {
175+
printf("\r\rMade some progress, downloaded %2d kb so far", $transferredBytes/1024);
176+
//$msg = "Made some progress, downloaded <info>$transferredBytes</info> so far";
177+
}
128178
break;
129179
}
130-
echo "\n";
180+
181+
$msg && Show::write($msg);
131182
}
132183

133-
// php down.php <http://example.com/file> <localFile>
134-
public static function down($url, $saveTo, $type = 1)
184+
/**
185+
* eg: php down.php <http://example.com/file> <localFile>
186+
* @param string $url
187+
* @param string $saveAs
188+
* @param int $type
189+
*/
190+
public static function down($url, $saveAs, $type = self::PROGRESS_TEXT)
135191
{
192+
self::$showType = (int)$type;
136193
$ctx = stream_context_create();
137194
stream_context_set_params($ctx, [
138-
"notification" => "stream_notification_callback"
195+
// register stream notification callback
196+
'notification' => [ self::class, 'progressShow']
139197
]);
140198

141-
$fp = fopen($url, "r", false, $ctx);
199+
Show::write("Download: $url\nSave As: $saveAs \n");
200+
201+
$fp = fopen($url, 'rb', false, $ctx);
202+
203+
if (is_resource($fp) && file_put_contents($saveAs, $fp)) {
204+
self::$fileSize = null;
142205

143-
if (is_resource($fp) && file_put_contents($saveTo, $fp)) {
144-
echo "\nDone!\n";
145-
exit(0);
206+
Show::write("\nDone!", true, 0);
146207
}
147208

209+
self::$fileSize = null;
148210
$err = error_get_last();
149-
echo "\nErrrrrorr...\n", $err["message"], "\n";
150-
exit(1);
211+
Show::error("\nErr.rrr..orr...\n {$err['message']}\n", 1);
151212
}
152213

153-
}
214+
}

src/utils/Interact.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ public static function confirm($question, $default = true)
102102
return $default;
103103
}
104104

105-
if ( !strncasecmp($answer, 'y', 1) ) {
105+
if ( 0 === stripos($answer, 'y') ) {
106106
return true;
107107
}
108108

109-
if ( !strncasecmp($answer, 'n', 1) ) {
109+
if ( 0 === stripos($answer, 'n') ) {
110110
return false;
111111
}
112112
}

0 commit comments

Comments
 (0)