Reputation: 27
I upload a file into OpenKM and get the URI to the file as response. I want to save the link as processVariable.
const { Client, logger } = require("camunda-external-task-client-js");
const { Variables } = require("camunda-external-task-client-js");
const { exec } = require('child_process');
const http = require('http');
const fs = require('fs');
const config = { baseUrl: "http://localhost:8080/engine-rest" };
const client = new Client(config);
var moment = require("moment");
client.subscribe("Rechnung", async function({ task, taskService }) {
console.log("*** Processing task " + task.id);
var uploadedFile = task.variables.get("rechnung");
const file = fs.createWriteStream(uploadedFile.filename);
const request = http.get("http://localhost:8080/engine-rest" + uploadedFile.remotePath, function(response) {
response.pipe(file);
file.on("finish", () => {
file.close();
});
});
var cmd = "curl -u okmAdmin:admin -H \"Accept: application/json\" -X POST -F docPath=\"/okm:root/manuel/" + task.id + "_" + uploadedFile.filename + "\" -F content=\"@" + uploadedFile.filename + "\" http://172.20.16.90:8080/OpenKM/services/rest/document/createSimple";
var newlink = "";
exec(cmd, (err, stdout, stderr) => {
newlink = "http://172.20.16.90:8080/OpenKM/frontend/index.jsp?uuid=" + JSON.parse(stdout).uuid;
if(err)
console.log("error " + JSON.stringify(stderr));
});
var processVariables = new Variables();
processVariables.set("link", newlink );
await taskService.complete(task, processVariables);
});
The variable is created but the link is empty. I tried encodeURIComponent but without any success. Can you tell me what should i do?
Thank you!
Upvotes: 0
Views: 182
Reputation: 27
The problem was that the async function does not wait for exec to finish. I solved it this way
const { Client, logger } = require("camunda-external-task-client-js");
const { Variables } = require("camunda-external-task-client-js");
const { exec } = require('child_process');
const http = require('http');
const fs = require('fs');
const config = { baseUrl: "http://localhost:8080/engine-rest" };
const client = new Client(config);
// rechnungseingang
client.subscribe("Rechnung", async function({ task, taskService }) {
console.log("*** Processing task " + task.id + " - " + task.processInstanceId);
console.log(JSON.stringify(task));
// get the uploaded file
var uploadedFile = task.variables.get("rechnung");
// save it to disk
const file = fs.createWriteStream(uploadedFile.filename);
const request = http.get("http://localhost:8080/engine-rest" + uploadedFile.remotePath, function(response) { response.pipe(file); file.on("finish", () => { file.close(); }); });
var createFolder = "curl -u okmAdmin:admin -H \"Accept: application/json\" -H \"Content-Type: application/json\" -X POST -d \"/okm:root/ibass/" + task.processInstanceId + "\" http://192.168.0.31:8080/OpenKM/services/rest/folder/createSimple";
await sh(createFolder);
// upload to openkm
var cmd = "curl -u okmAdmin:admin -H \"Accept: application/json\" -X POST -F docPath=\"/okm:root/ibass/" + task.processInstanceId + "/" + uploadedFile.filename + "\" -F content=\"@" + uploadedFile.filename + "\" http://192.168.0.31:8080/OpenKM/services/rest/document/createSimple";
let newlink = await sh(cmd);
var processVariables = new Variables();
processVariables.set("link", "http://192.168.0.31:8080/OpenKM/frontend/Download?uuid=" + newlink);
await taskService.complete(task, processVariables);
});
function sh(cmd) {
return new Promise(resolve => {
exec(cmd, (err, stdout, stderr) => {
let link;
link = JSON.parse(stdout).uuid;
if(err)
console.log("error " + JSON.stringify(stderr));
resolve(link);
});
})
}
Upvotes: 1