Page 1 of 1

Basic file system document importer

PostPosted:Sun Sep 13, 2020 8:39 pm
by rbtucker09
Hello, I'm having some trouble getting basic document importer working. My install is on Docker. This is the cron bash script I'm using:
Code: Select all
import com.openkm.core.*;
import com.openkm.api.*;
import java.io.*;
import com.openkm.module.db.stuff.DbSessionManager;
 
String token = DbSessionManager.getInstance().getSystemToken();
OKMDocument document = OKMDocument.getInstance();
OKMFolder folder = OKMFolder.getInstance();
 
public void autoImport(String okmPath, File fldpath){
  try {
    print("Scanning " + fldpath.getName() + "<br>");
    for (File file : fldpath.listFiles()) {
      print("Importing " + file.getName() + "<br>");
	try {
	  if (file.isDirectory()) {
	    try {
	      folder.createSimple(token, okmPath + file.getName());
	    } catch (ItemExistsException ie) {
	      print("folder already exists<br>");
	      // Folder already exists - just ignore exception
	    }
	    autoImport( okmPath + file.getName() + "/", file);
	  } else {
	    // Check if file is still being written to
	    long length = file.length();
	    Thread.sleep(1000);
	    if (file.length() > length) continue;  // Skip file this time
	    document.createSimple(token, okmPath + file.getName(), new FileInputStream(file));
	  }
	  print("Created " + okmPath + file.getName() + "<br>");
	} catch (Exception e) {
	  print ("Exception:" + e + "<br>");
	  // Something bad happened to prevent import. Skip to next file.
	 continue;
	}
       file.delete();
     }
  } catch (Exception e) {
    print("Exception: " + e + "<br>");
  }
}

autoImport("/okm:root/Scans/", new File(“/media/scans”));
This is the error I get from the script evaluation:
Code: Select all
bsh.EvalError: Sourced file: inline evaluation of: ``import com.openkm.core.*; import com.openkm.api.*; import java.io.*; import c . . . '' Token Parsing Error: Lexical error at line 44, column 41. Encountered: "\u201c" (8220), after : "" : at Line: 10 : in file: inline evaluation of: ``import com.openkm.core.*; import com.openkm.api.*; import java.io.*; import c . . . '' : public void autoImport ( String okmPath , File fldpath ) {
Any help is greatly appreciated!

Re: Basic file system document importer

PostPosted:Fri Sep 18, 2020 6:06 pm
by jllort
In the script is written character “ either " ( is not the same for JAVA compiler ). Try with ( take a look at the difference ):
Code: Select all
import com.openkm.core.*;
import com.openkm.api.*;
import java.io.*;
import com.openkm.module.db.stuff.DbSessionManager;
 
String token = DbSessionManager.getInstance().getSystemToken();
OKMDocument document = OKMDocument.getInstance();
OKMFolder folder = OKMFolder.getInstance();
 
public void autoImport(String okmPath, File fldpath){
  try {
    print("Scanning " + fldpath.getName() + "<br>");
    for (File file : fldpath.listFiles()) {
      print("Importing " + file.getName() + "<br>");
	try {
	  if (file.isDirectory()) {
	    try {
	      folder.createSimple(token, okmPath + file.getName());
	    } catch (ItemExistsException ie) {
	      print("folder already exists<br>");
	      // Folder already exists - just ignore exception
	    }
	    autoImport( okmPath + file.getName() + "/", file);
	  } else {
	    // Check if file is still being written to
	    long length = file.length();
	    Thread.sleep(1000);
	    if (file.length() > length) continue;  // Skip file this time
	    document.createSimple(token, okmPath + file.getName(), new FileInputStream(file));
	  }
	  print("Created " + okmPath + file.getName() + "<br>");
	} catch (Exception e) {
	  print ("Exception:" + e + "<br>");
	  // Something bad happened to prevent import. Skip to next file.
	 continue;
	}
       file.delete();
     }
  } catch (Exception e) {
    print("Exception: " + e + "<br>");
  }
}

autoImport("/okm:root/Scans/", new File("/media/scans"));
The error indicate the exact line and column what it raises the error. There're easiest way to upload files like hot folder https://www.youtube.com/watch?v=zLRjkNr ... e=emb_logo if you explain your scenario might be we can find the easiest way for you.