Access through SAMBA and use Project folder and uploader folder throught NETWORK drive.

edited August 1 in Tips & Tricks
Hello.

Thank you for a wonderful experience in your Repetier Server. I use a server with 2 printers that I am printing my projects from and want to share a work-around (sort of) that works for me.

I DO NOT garantee that all will work forever and please check your file sizes before you use this since this is just a quick hack to get functionality with Repetier Host and SAMBA.

Also one more thing, this script do not remove the directory in the uploader part inside the server if you remove the original Project. If there is someone who are willing to put time on it then please share that.

Step 1: Create a local user in your linux server. I use in this example the name repetieruploader


adduser repetieruploader
usermod -a -G dialout repetieruploader

Pick what ever password you want. It does not matter for this function.

Step 2: Setup SAMBA on your server. I am using Debian so this is how you do that on Debian.

apt-get install samba
nano /etc/samba/smb.conf

Add the following at the bottom of that file. If you do not want people to access it you will need to setup that yourself.
I am on a network that only me have access for.

[RepetierServerProjects]
        comment = "Project Printing Directory"
        path = /var/lib/Repetier-Server/projects
        writeable = no
        browseable = yes
        public = yes
        guest ok = yes
        force user = root

[RepetierServerProjectsUpload]
        comment = "Project Printing Directory Uploading Area"
        path = /home/repetieruploader/uploads
        writeable = yes
        browseable = yes
        public = yes
        guest ok = yes
        force user = repetieruploader

Step 3: Create the upload directory.
mkdir /home/repetieruploader/uploads

chown repetieruploader:repetieruploader repetieruploader -R


Step 4: Install PHP and the needed libraries apt-get install php php-curl php-xml
Step 5: Change to the repetieruploader and setup the PHP Script.

nano /home/repetieruploader/crontab.php

Insert the following into the script. Change the SERVERID and the KEY to your own information:

<?php

$localdir = "/home/repetieruploader/uploads";

$url = "http://0.0.0.0:3344/project/SERVERID";
$sess = "KEY";

function checkupload($dir){
global $localdir;
global $url;
global $sess;
        $files_r = scandir($localdir."/".$dir);
        foreach($files_r as $t => $file){if($file != "." && $file != ".."){
                $header = array('Content-Type: multipart/form-data');
                $xml = new SimpleXMLElement(file_get_contents("/var/lib/Repetier-Server/projects/".$dir."/info.xml"));

                $post = array('a' => 'upload','name'=>$file,'filename'=> curl_file_create($localdir."/".$dir."/".$file));

                $requesturl = $url. "/upload/".$xml->base->uuid."?a=upload&apikey=".$sess;
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL,$requesturl);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                curl_setopt($ch, CURLOPT_POST,1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                $result=json_decode(curl_exec ($ch),1);

                if($result["ok"]==1){
                        unlink($localdir."/".$dir."/".$file);
                }
                curl_close ($ch);

        }}


}


function createdir($dir){
global $localdir;
if(!is_dir($localdir."/".$dir)){
if(strpos($dir,"/")!==FALSE){
        $d_r = explode("/",$dir);
        mkdir($localdir."/".$d_r[0]);
        mkdir($localdir."/".$dir);
}else{
        mkdir($localdir."/".$dir);
}
}
}


foreach(scandir("/var/lib/Repetier-Server/projects") as $t => $file){if($file!=".." && $file != "."){
if(is_file("/var/lib/Repetier-Server/projects/".$file."/info.xml")){
        createdir($file);
        checkupload($file);
}else{

        foreach(scandir("/var/lib/Repetier-Server/projects/".$file) as $t2 => $file2){

                if(is_file("/var/lib/Repetier-Server/projects/".$file."/".$file2."/info.xml")){
                createdir($file."/".$file2);
                checkupload($file."/".$file2);
                }
        }

}
}
}


?>

Step 6: Connect to the SAMBA from your other computer with \\IP and go into the project folder and download a STL and make the gcode for it and upload to the UPLOADER share in same folder and the script will upload to that project.

Step 7: Double-check the information and try out the script.

After you have double-checked the information then try the script. If it runs correct it will create a few directories inside the upload folder that is named exactly same the project names.

Step 8: If everything works then add the script into the crontab
crontab -e

* * * * * php /home/repetieruploader/crontab.php

Comments

  • If you upload a lot of BIG files then you can edit the script and check in the samba command smbstatus -L
    for files being locked (usally when the file is being uploaded to the server)
    
    Only downside is that THIS VERSION (not the above) of the scriptyou need to run the script as root at that time.
    
    This will be impose a loop hole so do not have a weak password for the user and do not expose access ssh for the machine.
    
    Updated script down:
    
    <?php
    
    $localdir = "/home/repetieruploader/uploads";
    
    $url = "http://0.0.0.0:3344/project/890c8c8d-3c25-4286-97f3-2e6b0722cd5c";
    $sess = "a8bb6228-e60b-4164-8326-70b39ea6a0b5";
    
    function checkupload($dir){
    global $localdir;
    global $url;
    global $sess;
            $files_r = scandir($localdir."/".$dir);
            foreach($files_r as $t => $file){if($file != "." && $file != ".."){
                    $header = array('Content-Type: multipart/form-data');
                    $xml = new SimpleXMLElement(file_get_contents("/var/lib/Repetier-Server/projects/".$dir."/info.xml"));
    
                     $ch = curl_init();
    
                            $filename = urlencode($file);
                    $filetime = filemtime($localdir."/".$dir."/".$file);
    
                    exec("sudo smbstatus -L",$sambacheck);
                    echo $sambacheck = implode(" ",$sambacheck);
    
    
                    if(strpos($sambacheck,$file)===FALSE && $sambacheck != "" ){
                            $post = array('a' => 'upload','name'=>$filename,'filename'=> curl_file_create($localdir."/".$dir."/".$file));
    
                            $requesturl = $url. "/upload/".$xml->base->uuid."?a=upload&apikey=".$sess;
    
                            curl_setopt($ch, CURLOPT_URL,$requesturl);
                            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                            curl_setopt($ch, CURLOPT_POST,1);
                            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
                            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                            $result=json_decode(curl_exec ($ch),1);
    
                                    if($result["ok"]==1){
                                            unlink($localdir."/".$dir."/".$file);
                                    }
                            curl_close ($ch);
    
                    }
    
            }}
    
    
    }
    
    
    
    function createdir($dir){
    global $localdir;
    if(!is_dir($localdir."/".$dir)){
    if(strpos($dir,"/")!==FALSE){
            $d_r = explode("/",$dir);
            mkdir($localdir."/".$d_r[0]);
            mkdir($localdir."/".$dir);
    }else{
            mkdir($localdir."/".$dir);
    }
    }
    }
    
    
    foreach(scandir("/var/lib/Repetier-Server/projects") as $t => $file){if($file!=".." && $file != "."){
    if(is_file("/var/lib/Repetier-Server/projects/".$file."/info.xml")){
            createdir($file);
            checkupload($file);
    }else{
    
            foreach(scandir("/var/lib/Repetier-Server/projects/".$file) as $t2 => $file2){
    
                    if(is_file("/var/lib/Repetier-Server/projects/".$file."/".$file2."/info.xml")){
                    createdir($file."/".$file2);
                    checkupload($file."/".$file2);
                    }
            }
    
    }
    }
    }
    
    
    ?>
    
    
    
    
    
Sign In or Register to comment.