Select a file from the beginning of a flow
Select a file from the beginning of a flow
Hello,
I have this flow: In the Flow I get a PDF-File from Email convert it to text search with regex in the text.
When I work with the File i always use File.read(job.getPath(), 'UTF-8'); wich gives me the incoming file wich is the txt-File.
Now I also need the PDF from the beginning, how can I select this?
I want to upload it to an API with theHTTP.setAttachedFile(job.getPath() , filename); ...
Thanks in Advance for your Help!
I have this flow: In the Flow I get a PDF-File from Email convert it to text search with regex in the text.
When I work with the File i always use File.read(job.getPath(), 'UTF-8'); wich gives me the incoming file wich is the txt-File.
Now I also need the PDF from the beginning, how can I select this?
I want to upload it to an API with theHTTP.setAttachedFile(job.getPath() , filename); ...
Thanks in Advance for your Help!
Benjamin
Re: Select a file from the beginning of a flow
You have to branch out before the text conversion so you get a copy of the PDF. Then you can upload that one.
The PDF you convert to text is sent to null after the conversion.
The PDF you convert to text is sent to null after the conversion.
Re: Select a file from the beginning of a flow
okay, I have this PDF ARCHIV 48h trashcan in there, where all the incoming PDF are archived for 48 hours.
Is there a way to select the belonging PDF-file in the script, some job.get... function or similar?
Is there a way to select the belonging PDF-file in the script, some job.get... function or similar?
Benjamin
Re: Select a file from the beginning of a flow
Branch of to one folder before the pdfToolbox element and then from that folder back to the folder before the script, then both the PDf and the TXT will be going in to the script. In the script you have to pick up both the PDF and the TXT file.
Re: Select a file from the beginning of a flow
Hmmm...
But I run my script for every file which means It would handle the PDF and TXT file separately. So I will end up with 2 new entries in my database.
But I run my script for every file which means It would handle the PDF and TXT file separately. So I will end up with 2 new entries in my database.
Code: Select all
function jobArrived( s : Switch, job : Job )
{
var pdf = File.read( job.getPath(), "UTF-8" );// PDF Text einlesen
function getSuppliers(){
var theHTTP = new HTTP( HTTP.SSL );
theHTTP.authScheme = HTTP.ProprietaryAuth;
theHTTP.addHeader( "Authorization", "Bearer asdasdasdasd");
theHTTP.url = "https://api.ninoxdb.de/v1/teams/asdadsasd/databases/asdasd/tables/ASDDDrecords?perPage=9999";
theHTTP.get();
while (!theHTTP.waitForFinished(3)) {
job.log(1, 'API CALL IN PROGRESS!');
}
if (theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200) {
job.log(1, 'getSuppliers - API CALL successfull RESPONSE: ' +theHTTP.statusCode ); // theHTTP.getServerResponse().toString('UTF-8')
job.log(1,"FULL RESPONSE GET SUPPLIERS: "+theHTTP.getServerResponse().toString('UTF-8'))
supplierJson = JSON.parse(theHTTP.getServerResponse().toString('UTF-8'));
// job.log( 1, JSON.stringify( jsonReturn[0]['fields']['Bestellungen'] ));
// job.log(1,jsonReturn[0]['fields']['Name oder Firma']);
} else {
job.log(
3,
'API CALL Failed: ' +
theHTTP.getServerResponse().toString('UTF-8') +
' Status Code: ' +
theHTTP.statusCode.toString('UTF-8')
);
}
return supplierJson
}
function getCompany( supplierJson, fileAsText ){
var newSupplierJson = [];
for ( var j=0; j< supplierJson.length; j++ ){
if ( "UST ID" in supplierJson[j]['fields'] ){
newSupplierJson.push(supplierJson[j]);
}
}
//File.write("C:\\Users\\Administrator.BECHTELDRUCK\\Nextcloud\\Documents\\coding\\SwitchScripts\\RECHNUNGSEINGANG\\RECHNUNGEN AUSLESEN TEST\\TXT LOWER ARCHIV\\"+job.getName(), pdfLower, 'UTF-8'); // AUCH ALS LOWER TEXT ABSPEICHERN!
// job.log(1,"länge json: "+supplierJson.length);
supplierId = 0;
for (var i=0; i< newSupplierJson.length; i++){
var supplierUSTID = newSupplierJson[i]['fields']['UST ID'];
var regex = "("+supplierUSTID+")";
var pattern = new RegExp( regex );
pattern.global = true;
pattern.ignoreCase = true;
//job.log(1,i);
var matched = pdf.match(pattern);
if (matched){
job.log(1,"Pattern found: "+supplierUSTID+" Name: "+ newSupplierJson[i]['fields']['Name oder Firma'] );
job.setPrivateData("pdSupplierUSTID", supplierUSTID );
job.log(1,"matches: "+matched );
job.log(1, JSON.stringify( newSupplierJson[i] ) );
supplierId = newSupplierJson[i]['id'];
}
}
job.sendToSingle( job.getPath() );
return supplierId
};
function upload2Ninox(supplierId){
name = job.getName();
prefix = job.getUniqueNamePrefix();
filename = name.replace(prefix,"");
var pdf = File.read( job.getPath(), "UTF-8" );// PDF Text einlesen
var encodedText = HTTP.encodeURI( pdf );
var nummer = "asd";
var date="2020-07-16";
job.log(1,date);
var doktyp = "Rechnung";
var theHTTP = new HTTP (HTTP.SSL);
theHTTP.authScheme = HTTP.ProprietaryAuth;
theHTTP.authorization = "Bearer asdasd";
theHTTP.url = "https://api.ninoxdb.de/v1/teams/asdasd/databases/gasasd/tables/ASD/records";
var json = new ByteArray(
'{"fields":{"Lieferanten":"' +
supplierId+
'","Nummer":"' +
nummer +
'","Datum":"' +
date +
'","Dokumenttyp":"' +
doktyp +
'","pdf als text":"' +
encodedText +
'"}}',
'UTF-8'
);
theHTTP.setPostData(json, 'application/json');
theHTTP.post();
while( !theHTTP.waitForFinished( 3 ) )
{
job.log( 1, "API CALL IN PROGRESS!" );
}
if( theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200 )
{
job.log(1," upload2Ninox - API CALL successfull RESPONSE: "+theHTTP.getServerResponse().toString( "UTF-8" ));
// UPLOAD VIA API TO NINOX DATABASE
try{
var response = JSON.parse(theHTTP.getServerResponse().toString( "UTF-8" ));
var ninoxId =response.id;
job.setPrivateData("pdNinoxId", ninoxId );
var theHTTP = new HTTP( HTTP.SSL );
theHTTP.authScheme = HTTP.ProprietaryAuth;
theHTTP.authorization = "Bearer asdasd";
theHTTP.setAttachedFile( job.getPath() , filename );
theHTTP.url = "https://api.ninoxdb.de/v1/teams/uasdasdQ/databases/gasdadv/tables/ASD/records/"+id+"/files";
theHTTP.enableMime = true;
theHTTP.post();
while( !theHTTP.waitForFinished( 3 ) )
{
job.log( 1, "API CALL IN PROGRESS!" );
}
if( theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200 )
{
job.log(1,"API CALL successfull RESPONSE: "+theHTTP.getServerResponse().toString( "UTF-8" ));
}
else
{
job.log(3,"API CALL Failed: "+theHTTP.getServerResponse().toString( "UTF-8" )+ " Status Code: "+theHTTP.statusCode.toString( "UTF-8" ));
}
}
catch(theError) {
job.log(3,'json parse error: ' +theError);
}
}
else
{
job.log(3,"API CALL Failed: "+theHTTP.getServerResponse().toString( "UTF-8" )+ " Status Code: "+theHTTP.statusCode.toString( "UTF-8" ));
}
}
// Ausführung
getSuppliers();
getCompany(supplierJson,pdf);
upload2Ninox(supplierId);
}
Benjamin
Re: Select a file from the beginning of a flow
Well can't you let the first file create the record and the second update that record?
Re: Select a file from the beginning of a flow
How can I select the textfile and the pdf file in the same script and handle them differently?
Can I refer to the textfile when its in a different folder?
Benjamin
Re: Select a file from the beginning of a flow
One way is to embed the text file or the PDF as an Opaque dataset in the other file and in the script access that dataset. It will then be saved as a temp file that you can read from the script.
Re: Select a file from the beginning of a flow
For Example I have this flow where I want to opaque pickup a *.json file in a *.pdf file but it stucks before the pickup flow element.
Why is that?
Benjamin
Re: Select a file from the beginning of a flow
Do both files have the same name?
Re: Select a file from the beginning of a flow
Sorry I didn't see that.
But that is your problem. For the Pickup tools to work with the setting "Metadaten neben Asset" both files have to have the same name.
So you have to find a way to rename the payload file.
But that is your problem. For the Pickup tools to work with the setting "Metadaten neben Asset" both files have to have the same name.
So you have to find a way to rename the payload file.
Re: Select a file from the beginning of a flow
Is it not enough to rename one of the files?
They still have their unique filename for a second.
Benjamin
Re: Select a file from the beginning of a flow
You renamed the payload file and also changed the extension. The filename before the extension must be alike but different extensions. The filetype you want to embed as an opaque datatype i specified in the properties.
You you need two files as this: myfilename.pdf and myfilename.json
You you need two files as this: myfilename.pdf and myfilename.json