]> source.dussan.org Git - nextcloud-server.git/commitdiff
Initial work on transering files between servers
authorRobin Appelman <icewind1991@gmail.com>
Tue, 6 Jul 2010 10:50:37 +0000 (12:50 +0200)
committerRobin Appelman <icewind1991@gmail.com>
Tue, 6 Jul 2010 10:50:37 +0000 (12:50 +0200)
files/api.php
files/pull.php [new file with mode: 0644]
inc/lib_connect.php
inc/lib_files.php

index 014bbb56bbc8983bf872e27a068829bfb958f5f4..fa94a512547ed7d879511b56accea484f7effc3a 100755 (executable)
@@ -72,6 +72,8 @@ if($arguments['action']){
                                echo 'false';
                        }
                        break;
+               case 'pull':
+                       return OC_FILES::pull($arguments['source'],$arguments['token'],$arguments['dir'],$arguments['file']);
        }
 }
 
diff --git a/files/pull.php b/files/pull.php
new file mode 100644 (file)
index 0000000..1cc8242
--- /dev/null
@@ -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");
+}
+?>
\ No newline at end of file
index ddf03eb6e7494e58ea0a363ece4744d353c84526..0c56d1a01d5e2e1ccd58874cdac050f93bef4ed5 100644 (file)
@@ -78,6 +78,7 @@ class OC_REMOTE_CLOUD{
                $result=trim(curl_exec($ch));
                $info=curl_getinfo($ch);
                $httpCode=$info['http_code'];
+               curl_close($ch);
                if($httpCode==200 or $httpCode==0){
                        return json_decode($result,$assoc);
                }else{
@@ -130,6 +131,48 @@ class OC_REMOTE_CLOUD{
                }
                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){
@@ -146,6 +189,30 @@ function OC_CONNECT_TEST($path,$user,$password){
                                foreach($files as $file){
                                        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{
                                echo 'fail<br/>';
                        }
index 1702ef20de6bef2bda088503cad800ce0174022c..20d40669d7752709bea2aea7e29ad6b13b50d5fd 100755 (executable)
@@ -242,6 +242,36 @@ class OC_FILES {
        static function 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=''){