Initial work on transering files between servers

This commit is contained in:
Robin Appelman 2010-07-06 12:50:37 +02:00
parent 761b54fc17
commit 9fe7f992d9
4 changed files with 110 additions and 0 deletions

View File

@ -72,6 +72,8 @@ if($arguments['action']){
echo 'false'; echo 'false';
} }
break; break;
case 'pull':
return OC_FILES::pull($arguments['source'],$arguments['token'],$arguments['dir'],$arguments['file']);
} }
} }

11
files/pull.php Normal file
View File

@ -0,0 +1,11 @@
<?php
$token=$_GET['token'];
$file=sys_get_temp_dir().'/'.'remoteCloudFile'.$token;
if(file_exists($file) and is_readable($file) and is_writable($file)){
readfile($file);
unlink($file);
}else{
header("HTTP/1.0 404 Not Found");
}
?>

View File

@ -78,6 +78,7 @@ class OC_REMOTE_CLOUD{
$result=trim(curl_exec($ch)); $result=trim(curl_exec($ch));
$info=curl_getinfo($ch); $info=curl_getinfo($ch);
$httpCode=$info['http_code']; $httpCode=$info['http_code'];
curl_close($ch);
if($httpCode==200 or $httpCode==0){ if($httpCode==200 or $httpCode==0){
return json_decode($result,$assoc); return json_decode($result,$assoc);
}else{ }else{
@ -130,6 +131,48 @@ class OC_REMOTE_CLOUD{
} }
return $this->apiCall('getfiles',array('dir'=>$dir),true); return $this->apiCall('getfiles',array('dir'=>$dir),true);
} }
/**
* get a remove file and save it in a temporary file and return the path of the temporary file
* @param string $dir
* @param string $file
* @return string
*/
public function getFile($dir, $file){
if(!$this->connected){
return false;
}
$ch=curl_init();
if(!$this->cookiefile){
$this->cookiefile=sys_get_temp_dir().'/remoteCloudCookie'.uniqid();
}
$tmpfile=tempnam(sys_get_temp_dir(),'remoteCloudFile');
$fp=fopen($tmpfile,'w+');
$url=$this->path.="/files/api.php?action=get&dir=$dir&file=$file";
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_COOKIEFILE,$this->cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR,$this->cookiefile);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
fclose($fp);
curl_close($ch);
return $tmpfile;
}
public function sendFile($sourceDir,$sourceFile,$targetDir,$targetFile){
global $WEBROOT;
$source=$sourceDir.'/'.$sourceFile;
$tmp=OC_FILESYSTEM::toTmpFile($source);
$token=sha1(uniqid().$source);
$file=sys_get_temp_dir().'/'.'remoteCloudFile'.$token;
rename($tmp,$file);
if((isset($CONFIG_HTTPFORCESSL) and $CONFIG_HTTPFORCESSL) or isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] == 'on') {
$url = "https://". $_SERVER['SERVER_NAME'] . $WEBROOT;
}else{
$url = "http://". $_SERVER['SERVER_NAME'] . $WEBROOT;
}
return $this->apiCall('pull',array('dir'=>$targetDir,'file'=>$targetFile,'token'=>$token,'source'=>$url),true);
}
} }
function OC_CONNECT_TEST($path,$user,$password){ function OC_CONNECT_TEST($path,$user,$password){
@ -146,6 +189,30 @@ function OC_CONNECT_TEST($path,$user,$password){
foreach($files as $file){ foreach($files as $file){
echo "{$file['type']} {$file['name']}: {$file['size']} bytes<br/>"; echo "{$file['type']} {$file['name']}: {$file['size']} bytes<br/>";
} }
echo 'getting file "'.$file['name'].'"...';
$size=$file['size'];
$file=$remote->getFile('',$file['name']);
if(file_exists($file)){
$newSize=filesize($file);
if($size!=$newSize){
echo "fail<br/>Error: $newSize bytes received, $size expected.";
echo '<br/><br/>Recieved file:<br/>';
readfile($file);
unlink($file);
return;
}
OC_FILESYSTEM::fromTmpFile($file,'/remoteFile');
echo 'done<br/>';
echo 'sending file "burning_avatar.png"...';
$res=$remote->sendFile('','burning_avatar.png','','burning_avatar.png');
if($res){
echo 'done<br/>';
}else{
echo 'fail<br/>';
}
}else{
echo 'fail<br/>';
}
}else{ }else{
echo 'fail<br/>'; echo 'fail<br/>';
} }

View File

@ -242,6 +242,36 @@ class OC_FILES {
static function getMimeType($path){ static function getMimeType($path){
return OC_FILESYSTEM::getMimeType($path); return OC_FILESYSTEM::getMimeType($path);
} }
/**
* pull a file from a remote server
* @param string source
* @param string token
* @param string dir
* @param string file
* @return string guessed mime type
*/
static function pull($source,$token,$dir,$file){
$tmpfile=tempnam(sys_get_temp_dir(),'remoteCloudFile');
$fp=fopen($tmpfile,'w+');
$url=$source.="/files/pull.php?token=$token";
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($parameters));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
fclose($fp);
$info=curl_getinfo($ch);
$httpCode=$info['http_code'];
curl_close($ch);
if($httpCode==200 or $httpCode==0){
OC_FILESYSTEM::fromTmpFile($tmpfile,$dir.'/'.$file);
return true;
}else{
return false;
}
}
} }
function zipAddDir($dir,$zip,$internalDir=''){ function zipAddDir($dir,$zip,$internalDir=''){