Page 5 of 7

Re: Automatic import

PostPosted:Wed Jul 13, 2011 1:40 pm
by ocaselvaggia
jllort wrote:ocaselvaggia can share the script on wiki ? could be useful to other users
Yes, sure!
Where and how to make it?

Re: Automatic import

PostPosted:Fri Jul 15, 2011 7:41 am
by pavila
Autoimport script added to http://wiki.openkm.com/index.php/Utilities.

Re: Automatic import

PostPosted:Fri Jul 15, 2011 3:10 pm
by ocaselvaggia
Thanks 8)

Re: Automatic import

PostPosted:Fri Aug 05, 2011 5:59 am
by joako
I still haven't forgotten, but I hate to give a script that isn't working 100%.

Right now the OCR will start to write the file, the import script won't check if the file is being written, so what happens is a 0 bytes file is imported and deleted -- before the OCR can finish writing the file. At this point the OCR fails. I will tweak the script and test. At that point I believe it will be ready for normal use.

Re: Automatic import

PostPosted:Mon Aug 08, 2011 9:23 am
by pavila
Please, post here the modified script once completed and I will update the wiki.

Re: Automatic import

PostPosted:Mon Oct 03, 2011 6:58 am
by rmm
I have tried this..I've encountered an error..

Fatal error: Class 'SoapClient' not found in /opt/OpenKM/autoimport.php on line 6

I have checked my php configuration..I have SOAP installed. Anyway, i am running on a proxy server could this be the issue? Thanks a lot.

Re: Automatic import

PostPosted:Tue Oct 04, 2011 10:29 am
by pavila
SoapClient should be in any PHP5 installation, AFAIK.

Re: Automatic import

PostPosted:Wed Oct 05, 2011 3:37 am
by joako
Sorry I never got around to posting this before, been very busy and currently we have paused in-house OpenKM development, but here's what I promised:

My cron script:
Code: Select all
#!/bin/bash
if [ -f /var/run/okmScanCron.pid ]; then
        echo "`date` Scan Cron: Already running, skipping this cycle!"
        exit 2
fi
touch /var/run/okmScanCron.pid
for file in /opt/openkm/IMPORT/NEW-Scan/*.pdf; do
if [ -d "$file" ]; then
        echo "`date` Scan Cron: Dazed and consufed,  \"$file\" is a directory!"
fi

if [ -z "$file" ]; then
        rm -rf /var/run/okmScanCron.pid
        exit 0
fi

if [ ! -f "$file" ]; then
        rm -rf /var/run/okmScanCron.pid
        exit 0
fi
        if [  -s "$file" ];then
                                if [ $(/usr/sbin/lsof -t $file | wc -l) -ge 1 ];then
                        echo "File $file still being written, skipping"
                else
                        hash1=$(md5sum $file)
                        sleep 5
                        hash2=$(md5sum $file)
                        if [ ! "$hash1" = "$hash2" ];then
                                echo "File $file still being written, skipping"
                        else
                                                 /opt/openkm/import.sh "$file" NEW-Scan
                        fi
                fi
                else
                        echo "`date` Scan Cron:  $file is empty!"
                fi
done;
rm -rf /var/run/okmScanCron.pid
import.sh script:
Code: Select all
#!/bin/bash
# JOAKO Import 1.0.2 - Copyright 2011 Andrew Joakimsen
# Distributed under the terms of the GNU General Public
# License Version 2 and no future versions.
#
# Script to import files into OpenKM and report error/success.
# Requires the PHP import script written by snowman, with a
# few small tweaks.

###################################
###                             ###
###  C O N F I G U R A T I O N  ###
###                             ###
###################################


# OpenKM directory where files are imported to
OKM_IMPORT_DIR=NEW

# Filesystem directory (no trailing slash) where we put imported files (it will be created)
STORE_DIR=/opt/openkm/IMPORT/Archive

# Filesystem directory (no trailing slash) where we put files that encountered import errors
ERROR_STORE_DIR=/opt/openkm/IMPORT/Error

####################################

# First let's see if the runtime condition is sane
if [ -z "$1" ]; then
        echo "JOAKO Import: Missing file name"
        echo "Usage: import.sh [filename] <[target-directory]>"
        exit 1
fi


# If ARG1 is a real file
if [ -d "$1" ]; then
        echo "ERROR: \"$1\" is a directory!"
        exit 1
fi

if [ ! -f "$1" ]; then
        echo "ERROR: \"$1\" Invalid file name "
        exit 1
else
        INFILE=$1
fi

# Check if OKM_IMPORT_DIR was passed as argument
if [ -z "$2" ]; then
        OKM_DIR=$OKM_IMPORT_DIR
else
        OKM_DIR=$2
fi

# Check if the imported files directory exsists
if [ ! -d $STORE_DIR/$OKM_DIR ]; then
        if [ ! -d $STORE_DIR ]; then
                mkdir $STORE_DIR
        fi
        mkdir $STORE_DIR/$OKM_DIR
fi
import.php script:
Code: Select all
<?php

$okmurl="http://doc-server/OpenKM/";
$okmid="okmAdmin";
$okmpw="admin";
$impfile = $argv[1];
$filename =  $argv[2];
$folder =  $argv[3];

$OKMAuth = new SoapClient($okmurl."OKMAuth?wsdl");
$OKMDocument = new SoapClient($okmurl."OKMDocument?wsdl");

// open file and encode if necessary
$handle = fopen($impfile,'rb');
$file_content = fread($handle,filesize($impfile));
fclose($handle);
$encoded = base64_encode($file_content);

// Login
$token = $OKMAuth->login($okmid, $okmpw);
$OKMDocument->createSimple($token,"/okm:root/$folder/$filename",$file_content);

// Logout
$OKMAuth->logout($token);
?>
Working well for me currently. I still feel there's the possibility for issues but I haven't seen much of those lately.

Re: Automatic import

PostPosted:Fri Jul 20, 2012 5:31 am
by shaardu
Can anyone please let me know how to auto import ? I tried with the php script, it didnt work. Also I was wondering how ll php script work with java cron? Anyway can anyone please shed some light on this. Its very urgent/

Re: Automatic import

PostPosted:Fri Jul 20, 2012 4:03 pm
by jllort
I suggest you test step my step the joako example starting with php, you can execute from terminal if you have installed php5 ( you're on windows or on linux ? )

Re: Automatic import

PostPosted:Sat Jul 21, 2012 4:59 pm
by shaardu
I am on windows. You mean the above version works perfectly? Only I have to install PHP5?

Re: Automatic import

PostPosted:Mon Jul 23, 2012 7:33 am
by dsc68
Here is a BeanShell script that imports files from a directory on the server. Save it as a file with the extention '.bsh' and install it as an OpenKM cron job to run every couple of minutes.
Code: Select all
import javax.jcr.*;
import com.openkm.core.*;
import com.openkm.api.OKMDocument;
import java.io.*;

String token = JcrSessionManager.getInstance().getSystemToken();
Session session = JcrSessionManager.getInstance().get(token);
OKMDocument document = OKMDocument.getInstance();


public class OnlyExt implements FilenameFilter {
  String ext;

  public OnlyExt(String ext) {
    this.ext = "." + ext;
  }

  public boolean accept(File dir, String name) {
    return name.endsWith(ext);
  }
}


File scans = new File("/home/scanner");
Thread.sleep(10000);  // Sleep 10 seconds in case files are still being written
try {
  for (File scan : scans.listFiles(new OnlyExt("pdf"))) {
    
    try {
      document.createSimple(token, "/okm:root/Scans/" + scan.getName(), new FileInputStream(scan));
      scan.delete();
    }catch (Exception e) {
      print("Exception: " + e);
    }
  }
  
} catch (Exception e) {
  print("Exception: " + e);
}
I use this with my network connected scanner. The scanner saves scans as pdfs to a shared folder ('/home/scanner') on the server then this script picks them up, loads them into an OpenKM folder ('/okm:root/Scans') and deletes the originals. I am also going to set up a CUPS print to PDF file queue that will print documents into OpenKM using the same method.

Note: This script only loads PDF files. If you want other files loaded then change the OnlyExt class to accept other file types.

Re: Automatic import

PostPosted:Mon Jul 23, 2012 10:58 am
by jllort
I have shared your example in our wiki doumentation http://wiki.openkm.com/index.php/Crontab

Re: Automatic import

PostPosted:Mon Jul 23, 2012 6:32 pm
by shaardu
dude dsc68, fantastic piece of work there....Hats off to yo man, keep it up....As always, thanks jllort and pavila for this wonderful product, you guys are awesome!!!

Re: Automatic import

PostPosted:Mon Aug 27, 2012 5:31 am
by pavila
Glad to read it 8)