]> source.dussan.org Git - nextcloud-server.git/commitdiff
add option to add file from url
authorRobin Appelman <icewind@owncloud.com>
Wed, 7 Mar 2012 20:43:44 +0000 (21:43 +0100)
committerRobin Appelman <icewind@owncloud.com>
Wed, 7 Mar 2012 20:43:44 +0000 (21:43 +0100)
files/ajax/newfile.php
files/ajax/newfolder.php
files/js/files.js
files/templates/index.php

index afc444bc0acadbcb35247ac4691d44c255106bc9..2d1372f06ee5fb8d40c57233fd6b964d9a9d6d5e 100644 (file)
@@ -6,15 +6,35 @@ require_once('../../lib/base.php');
 OC_JSON::checkLoggedIn();
 
 // Get the params
-$dir = isset( $_GET['dir'] ) ? stripslashes($_GET['dir']) : '';
-$filename = isset( $_GET['filename'] ) ? stripslashes($_GET['filename']) : '';
-$content = isset( $_GET['content'] ) ? $_GET['content'] : '';
+$dir = isset( $_POST['dir'] ) ? stripslashes($_POST['dir']) : '';
+$filename = isset( $_POST['filename'] ) ? stripslashes($_POST['filename']) : '';
+$content = isset( $_POST['content'] ) ? $_POST['content'] : '';
+$source = isset( $_POST['source'] ) ? stripslashes($_POST['source']) : '';
 
 if($filename == '') {
        OC_JSON::error(array("data" => array( "message" => "Empty Filename" )));
        exit();
 }
 
+if($source){
+       if(substr($source,0,8)!='https://' and substr($source,0,7)!='http://'){
+               OC_JSON::error(array("data" => array( "message" => "Not a valid source" )));
+               exit();
+       }
+       $sourceStream=fopen($source,'rb');
+       $target=$dir.'/'.$filename;
+       $result=OC_Filesystem::file_put_contents($target,$sourceStream);
+       if($result){
+               $mime=OC_Filesystem::getMimetype($target);
+               OC_JSON::success(array("data" => array('mime'=>$mime)));
+               exit();
+       }else{
+               OC_JSON::error(array("data" => array( "message" => "Error while downloading ".$source. ' to '.$target )));
+               exit();
+       }
+}
+
+
 if(OC_Files::newFile($dir, $filename, 'file')) {
        if($content){
                OC_Filesystem::file_put_contents($dir.'/'.$filename,$content);
index 6db045c4e1728fc7d555f5c8bd73fe09d798f46c..228e369fbef95c336dfd6426470a9a0951f2bbb9 100644 (file)
@@ -6,8 +6,8 @@ require_once('../../lib/base.php');
 OC_JSON::checkLoggedIn();
 
 // Get the params
-$dir = isset( $_GET['dir'] ) ? stripslashes($_GET['dir']) : '';
-$foldername = isset( $_GET['foldername'] ) ? stripslashes($_GET['foldername']) : '';
+$dir = isset( $_POST['dir'] ) ? stripslashes($_POST['dir']) : '';
+$foldername = isset( $_POST['foldername'] ) ? stripslashes($_POST['foldername']) : '';
 
 if(trim($foldername) == '') {
        OC_JSON::error(array("data" => array( "message" => "Empty Foldername" )));
index f5dc40ad45d286808c4f1f2a5e9d2814522971e4..a678e12cc2d42aaac6fbf9fd7c3a14b0c5781954 100644 (file)
@@ -127,20 +127,6 @@ $(document).ready(function() {
                procesSelection();
        });
        
-       $('#file_newfolder_form').submit(function(event) {
-               event.preventDefault();
-               $.ajax({
-                       url: 'ajax/newfolder.php',
-                       data: "dir="+$('#dir').val()+"&foldername="+$('#file_newfolder_name').val(),
-                       complete: function(data){boolOperationFinished(data, function(){
-                               var date=new Date();
-                               FileList.addDir($('#file_newfolder_name').val(),0,date);
-                               $('#file_newfolder_name').val('New Folder');
-                               $('#file_newfolder_name').blur();
-                       });}
-               });
-       });
-       
        $('#file_newfolder_name').click(function(){
                if($('#file_newfolder_name').val() == 'New Folder'){
                        $('#file_newfolder_name').val('');
@@ -312,10 +298,10 @@ $(document).ready(function() {
                        var name=$(this).val();
                        switch(type){
                                case 'file':
-                                       $.ajax({
-                                               url: OC.filePath('files','ajax','newfile.php'),
-                                               data: "dir="+encodeURIComponent($('#dir').val())+"&filename="+encodeURIComponent(name)+'&content=%20%0A',
-                                               complete: function(data){boolOperationFinished(data, function(){
+                                       $.post(
+                                               OC.filePath('files','ajax','newfile.php'),
+                                               {dir:$('#dir').val(),filename:name,content:" \n"},
+                                               function(data){
                                                        var date=new Date();
                                                        FileList.addFile(name,0,date);
                                                        var tr=$('tr').filterAttr('data-file',name);
@@ -323,18 +309,49 @@ $(document).ready(function() {
                                                        getMimeIcon('text/plain',function(path){
                                                                tr.find('td.filename').attr('style','background-image:url('+path+')');
                                                        });
-                                               });}
-                                       });
+                                               }
+                                       );
                                        break;
                                case 'folder':
-                                       $.ajax({
-                                               url: OC.filePath('files','ajax','newfolder.php'),
-                                               data: "dir="+encodeURIComponent($('#dir').val())+"&foldername="+encodeURIComponent(name),
-                                               complete: function(data){boolOperationFinished(data, function(){
+                                       $.post(
+                                               OC.filePath('files','ajax','newfolder.php'),
+                                               {dir:$('#dir').val(),foldername:name},
+                                               function(data){
                                                        var date=new Date();
                                                        FileList.addDir(name,0,date);
-                                               });}
-                                       });
+                                               }
+                                       );
+                                       break;
+                               case 'web':
+                                       if(name.substr(0,8)!='https://' && name.substr(0,7)!='http://'){
+                                               name='http://'.name;
+                                       }
+                                       var localName=name;
+                                       if(localName.substr(localName.length-1,1)=='/'){//strip /
+                                               localName=localName.substr(0,localName.length-1)
+                                       }
+                                       if(localName.indexOf('/')){//use last part of url
+                                               localName=localName.split('/').pop();
+                                       }else{//or the domain
+                                               localName=(localName.match(/:\/\/(.[^/]+)/)[1]).replace('www.','');
+                                       }
+                                       $.post(
+                                               OC.filePath('files','ajax','newfile.php'),
+                                               {dir:$('#dir').val(),source:name,filename:localName},
+                                               function(result){
+                                                       if(result.status == 'success'){
+                                                               var date=new Date();
+                                                               FileList.addFile(localName,0,date);
+                                                               var tr=$('tr').filterAttr('data-file',localName);
+                                                               tr.data('mime',result.data.mime);
+                                                               getMimeIcon(result.data.mime,function(path){
+                                                                       tr.find('td.filename').attr('style','background-image:url('+path+')');
+                                                               });
+                                                       }else{
+                                                               
+                                                       }
+                                               }
+                                       );
                                        break;
                        }
                        var li=$(this).parent();
index 7e9505dec2f936435791c8eea86457c0c91bd143..7fc51c288e1c7402c6edbb3df47281d71f9939f6 100644 (file)
@@ -7,6 +7,7 @@
                                <ul class="popup popupTop">
                                        <li style="background-image:url('<?php echo mimetype_icon('text/plain') ?>')" data-type='file'><p><?php echo $l->t('Text file');?></p></li>
                                        <li style="background-image:url('<?php echo mimetype_icon('dir') ?>')" data-type='folder'><p><?php echo $l->t('Folder');?></p></li>
+                                       <li style="background-image:url('<?php echo image_path('core','actions/public.png') ?>')" data-type='web'><p><?php echo $l->t('From url');?></p></li>
                                </ul>
                        </div>
                        <div class="file_upload_wrapper svg">