Core Function HTTPDownload

From Sputnik Wiki
Revision as of 19:43, 18 January 2013 by UberFoX (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
HTTPDownload( <url>, <dest>, <progress>, <completed> )

Contents

Description

Download a file from a given URL.

Parameters

url

URL to the file

dest

Location and filename to save it as

progress

Code to execute at each progress update

completed

Code to execute when download finishes

Return Value

Success: Returns true.

Failure: Returns false.

Remarks

None

Example

Heres what the uBlox launcher does notice it will download files and report progress of the download as it happens

$Server = 'http://uberfox.no-ip.org/uBlox/update/';
$str = HTTPGetString($Server.'index.php');
$str = replace($str, '<br>', "\n");
$str =~ m/^(.*);(.*);(.*)$/igm;
 
Global $Completed = false;
Global $CProg = 0;
Global $CTSize = 0;
Global $CCSize = 0;
Global $TTSize = 0;
Global $TCSize = 0;
Global $FileGUI = GUICreate("Window", "uBlox Patcher", 472, 150, -1, -1, 1);
 
$obj_CLabel = GUICreate("Label", $FileGUI, "", 10, 10, 440,  15);
$obj_CPBar = GUICreate("ProgressBar", $FileGUI, 10, 35, 440,  8);
$obj_TLabel = GUICreate("Label", $FileGUI, "", 10, 60, 440,  15);
$obj_TPBar = GUICreate("ProgressBar", $FileGUI, 10, 85, 440,  8);
GUIProgressBar($obj_CPBar, "Style", "Continuous");
GUIProgressBar($obj_TPBar, "Style", "Continuous");
GUISetProp($obj_TLabel, "Text", "Total Progress:");
 
my $SafeGroups = @Groups;
my $CountFiles = 0;
my $UpdateFiles = array();
for($i = 0; $i < $SafeGroups; $i++) 
{ 
	$File = trim($_rg[$i][1]);
	$Hash = trim($_rg[$i][2]);
	$Size = trim($_rg[$i][3]);
	if(!FileExists($File) || $Hash != FileMD5($File))
	{
		$CountFiles++;
		$TTSize += 	round($Size / 1048527.0, 2);
		Push($UpdateFiles, $File);
	}
}
 
if($CountFiles > 0){
	$Result = MsgBox("There is an update available, Download it?\n(This will close all instances of uBlox.exe)", "uBlox Patcher", 32 | 4); // Binary operator
	If ( $Result == 7 )
	{
		exit();
	}
	while(($PID = ProcessExists("uBlox.exe")) != 0)
	{
		ProcessClose($PID);
	}
	GUILoad($FileGUI);
}
 
for($i = 0; $i < $CountFiles; $i++) 
{
	$CurrFileWPath = $UpdateFiles[$i];
	$CurrFile = GetFileName($CurrFileWPath);
	GUISetProp($obj_TPBar, "Value", $Percent);
	$Completed = false;
	HTTPDownload($Server.$CurrFileWPath, ">$CurrFileWPath", "progress();", "completed();");
	while(!$Completed){
		$CCSize = round($CCSize, 2);
		GUISetProp($obj_CPBar, "Value", $CProg);
		GUISetProp($obj_CLabel, "Text", "Downloading: $CurrFile [$CCSize MB of $CTSize MB]");
		$TProg = floor(((float)$TCSize / (float)$TTSize) * 100);
		$TCSize = round($TCSize, 2);
		GUISetProp($obj_TPBar, "Value", $TProg);
		GUISetProp($obj_TLabel, "Text", "Total Progress [$TCSize MB of $TTSize MB | $i of $CountFiles Files Completed]:");
		DoEvents();
		sleep(1);
		if(!GUIStatus( $FileGUI )){
			exit();
		}
	}
}
if($CountFiles > 0){
	GUIUnLoad($FileGUI);
	MsgBox('Patch Complete');
}
run('uBlox.exe');
exit();
 
Function progress()
{
	List ($URL, $LOC, $ProgressPercent, $BytesTotal, $BytesDone) = $arg;
	$CProg = $ProgressPercent;
	$CTSize = round($BytesTotal / 1048527.0, 2);
	$TCSize += ($BytesDone / 1048527.0) - $CCSize;
	$CCSize = $BytesDone / 1048527.0;
}
 
Function completed()
{
	List ($URL, $LOC) = $arg;
	$Completed = true;
	$CCSize = 0;
	$CTSize = 0;
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox