| 1 |
{ |
| 2 |
Copyright 2013-2020 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Download an URL, write result on standard output (stdout). |
| 17 |
This is a simple test of TCastleDownload that allows you to grab any URL |
| 18 |
to a Stream. |
| 19 |
|
| 20 |
Writes diagnostic output to the log (see |
| 21 |
https://castle-engine.io/manual_log.php for information where is the log; |
| 22 |
note that we set LogEnableStandardOutput = false, |
| 23 |
which means that on Linux it is usually inside ~/.config/castle_download/ ). |
| 24 |
|
| 25 |
Try from command-line like |
| 26 |
castle_download https://castle-engine.io/ > output.html |
| 27 |
castle_download https://castle-engine.io/modern_pascal_introduction.html > output.html |
| 28 |
castle_download https://castle-engine.io/latest.zip > output.zip |
| 29 |
} |
| 30 |
|
| 31 |
uses |
| 32 |
{$ifdef UNIX} CThreads, {$endif} // necessary to have asynchronous downloading on Unix |
| 33 |
SysUtils, Classes, StrUtils, Math, |
| 34 |
{$ifdef FPC} OpenSSLSockets, {$endif} // support HTTPS |
| 35 |
CastleDownload, CastleParameters, CastleClassUtils, CastleLog, CastleUtils, |
| 36 |
CastleApplicationProperties, CastleStringUtils; |
| 37 |
|
| 38 |
const |
| 39 |
MaxDots = 80; |
| 40 |
var |
| 41 |
WrittenDots: Integer; |
| 42 |
WrittenTotalBytes: Boolean; |
| 43 |
|
| 44 |
{ Makes a trivial "progress bar" using dots written on ErrOutput. } |
| 45 |
procedure ReportProgress(const DownloadedBytes, TotalBytes: Int64); |
| 46 |
const |
| 47 |
// In case size is unknown, how many bytes we need to write next dot. |
| 48 |
DotPerBytes = 1024 * 1024; |
| 49 |
var |
| 50 |
NewWrittenDots: Integer; |
| 51 |
begin |
| 52 |
if TotalBytes > 0 then |
| 53 |
begin |
| 54 |
if not WrittenTotalBytes then |
| 55 |
begin |
| 56 |
Writeln(ErrOutput, 'Need to download: ' + SizeToStr(TotalBytes)); |
| 57 |
WrittenTotalBytes := true; |
| 58 |
end; |
| 59 |
|
| 60 |
NewWrittenDots := Max(WrittenDots, Round(MaxDots * DownloadedBytes / TotalBytes)); |
| 61 |
end else |
| 62 |
begin |
| 63 |
// some requests never report TotalBytes, write some progress anyway |
| 64 |
NewWrittenDots := Max(WrittenDots, DownloadedBytes div DotPerBytes); |
| 65 |
|
| 66 |
if (WrittenDots = 0) and (NewWrittenDots > 0) then |
| 67 |
begin |
| 68 |
Writeln(ErrOutput, Format('Total download size unknown (not reported by HTTP server), we will report after each %d bytes (%s)', [ |
| 69 |
DotPerBytes, |
| 70 |
SizeToStr(DotPerBytes) |
| 71 |
])); |
| 72 |
end; |
| 73 |
end; |
| 74 |
|
| 75 |
Write(ErrOutput, DupeString('.', NewWrittenDots - WrittenDots)); |
| 76 |
WrittenDots := NewWrittenDots; |
| 77 |
end; |
| 78 |
|
| 79 |
{ Main program code. } |
| 80 |
var |
| 81 |
Download: TCastleDownload; |
| 82 |
begin |
| 83 |
LogEnableStandardOutput := false; // do not put log in stdout, it would be mixed with downloaded output |
| 84 |
InitializeLog; |
| 85 |
Parameters.CheckHigh(1); |
| 86 |
|
| 87 |
Download := TCastleDownload.Create(nil); |
| 88 |
try |
| 89 |
Download.Url := Parameters[1]; |
| 90 |
Download.HttpHeader('User-Agent', 'castle_download/1.0'); |
| 91 |
Download.Start; |
| 92 |
|
| 93 |
while Download.Status = dsDownloading do |
| 94 |
begin |
| 95 |
ReportProgress(Download.DownloadedBytes, Download.TotalBytes); |
| 96 |
|
| 97 |
{ In a normal GUI application, e.g. using TCastleWindow or TCastleControl, |
| 98 |
the update is done automatically. |
| 99 |
There's no need to call "ApplicationProperties._Update" explicitly, |
| 100 |
and no point in calling "Sleep" if you want to display some smooth animation |
| 101 |
while the download is ongoing. |
| 102 |
But it makes sense in this demo, where we only wait for the download to finish. } |
| 103 |
ApplicationProperties._Update; |
| 104 |
Sleep(100); |
| 105 |
end; |
| 106 |
|
| 107 |
case Download.Status of |
| 108 |
dsSuccess: |
| 109 |
ReadGrowingStream(Download.Contents, StdOutStream, false); |
| 110 |
dsError: |
| 111 |
Writeln(ErrOutput, Download.ErrorMessage); |
| 112 |
else |
| 113 |
raise EInternalError.Create('No other status is possible when download finished'); |
| 114 |
end; |
| 115 |
finally FreeAndNil(Download) end; |
| 116 |
|
| 117 |
if WrittenDots <> 0 then |
| 118 |
Writeln(ErrOutput); // otherwise ErrOutput ends without newline and looks weird |
| 119 |
|
| 120 |
(* Note: Blocking version of this would be even simpler: |
| 121 |
|
| 122 |
EnableBlockingDownloads := true; |
| 123 |
Stream := Download(Parameters[1]); |
| 124 |
try |
| 125 |
ReadGrowingStream(Stream, StdOutStream, false); |
| 126 |
finally FreeAndNil(Stream) end; |
| 127 |
*) |
| 128 |
end. |