Open xeruf opened 1 year ago
Which type to use to upload eventFiles/attachments to an event? attachments? service? Is service=event?
I now get Nur eine Datei darf für diesen Domain Typ hochgeladen werden.
even though I am posting only one file, for any of the aforementioned types.
Responses in German are also fine, just thought I follow good internet etiquette and write understandable for anyone :)
And how do I download a PDF appropriately? When I get a song arrangement and try to download its fileUrl like this, something messes up in the PDF header, maybe an encoding issue?
churchtoolsClient.get(f.fileUrl).then(file => {
fs.createWriteStream(filename).write(file)
})
Wonder what for? This: https://code.ftt.gmbh/janek/carvent
Hallihallo, jemand da?
Hey @xeruf!
Sorry for the delay.
Which type to use to upload eventFiles/attachments to an event? attachments? service? Is service=event?
You guessed right: If you want to attach files to an event, the corresponding domainType
is service
(as in "church service" / "Gottesdienst").
I now get Nur eine Datei darf für diesen Domain Typ hochgeladen werden. even though I am posting only one file, for any of the aforementioned types.
I'm not sure if that problem still exists when you use the correct domainType. If so, please reach out again, then I'll take a closer look.
And how do I download a PDF appropriately?
Our client library is not yet capable of downloading files and it's currently not on the roadmap to introduce this feature.
But you can leverage the cookieJar and the http
/https
modules from the Node.js standard library to make that download work:
const https = require('https');
const fs = require('fs');
const cookieJar = new tough.CookieJar();
churchtoolsClient.setCookieJar(axiosCookieJarSupport.wrapper, cookieJar);
// login, get the event/agenda/song data and finally the url of the file you want to download
const url = 'https://demo.church.tools/?q=public/filedownload&id=12345&filename=abcdef...';
const file = fs.createWriteStream("file.pdf");
https.get(
url,
{
headers: {
Cookie: await cookieJar.getCookieStringSync()
}
},
(response) => {
response.pipe(file);
file.on("finish", () => {
file.close();
console.log("Download Completed");
});
}
);
I hope I could help you a bit. If anything's missing, don't hesitate to reach out again.
Gesegnete Ostern!
– Andreas Developer @ ChurchTools
Dankeschön! See also https://forum.church.tools/topic/9299/wie-kann-ich-song-attachments-hoch-und-herunterladen-via-js
Our client library is not yet capable of downloading files and it's currently not on the roadmap to introduce this feature.
What would be the suggested way? I have no problem using another language :)
I'll try the workaround regardless.
I'm not sure if that problem still exists when you use the correct domainType. If so, please reach out again, then I'll take a closer look.
As I said, I tried all domaintypes, the current code is here, not working with service
:
https://code.ftt.gmbh/janek/carvent/src/commit/f0d68dc68e3eda7a784195897bde1ad67d11e8ab/index.js#L82
I have tried it exactly as suggested, with cookieJar
as a global constant, but I get
/home/janek/data/1-projects/1-personal/carvent/index.js:70
Cookie: await cookieJar.getCookieStringSync()
^^^^^^^^^
SyntaxError: Unexpected identifier 'cookieJar'
Our client library is not yet capable of downloading files and it's currently not on the roadmap to introduce this feature.
What would be the suggested way? I have no problem using another language :)
In a nutshell, it's not intended to download any files via the API. They should only be downloaded via a browser or similar client that has been logged in and has a valid session cookie. If you can "simulate" this in any way (e.g. with my code snippet from above) you can use whatever language you like.
As I said, I tried all domaintypes, the current code is here, not working with service:
I don't think the problem is the domaintype then. I haven't worked with file uploads myself yet, but in our frontend code, it looks like this:
const formData = new FormData();
formData.append('files[]', file);
// some more form data
churchtoolsClient.post(`/files/service/${id}`, formData);
There are two things that could be the problem with your code (just guessing):
formData.append('files[]', file)
instead of formDAta.append("files", [file])
pdf-merger-js
gives you a Buffer. Maybe you need to convert it from Buffer to Blob.I have tried it exactly as suggested, with cookieJar as a global constant, but I get
The problem is here: https://code.ftt.gmbh/janek/carvent/src/branch/main/index.js#L60 If you want to use await
inside a function, that function must be marked as async
:
).then(async (result) =>
I hope this gets you further!
– Andreas Developer @ ChurchTools
@xeruf I'm closing this issue for now.
If you need further assistance, feel free to reopen it.
Managed to successfully download, now just failing on the upload, see https://code.ftt.gmbh/janek/carvent/src/branch/main/index.js#L101
[ChurchToolsClient][Error] POST https://MYCHURCH.church.tools/api/files/service/2241
/home/janek/data/1-projects/1-personal/carvent/node_modules/@churchtools/churchtools-client/node_modules/axios/lib/core/createError.js:16
var error = new Error(message);
^
Error: Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream
at createError (/home/janek/data/1-projects/1-personal/carvent/node_modules/@churchtools/churchtools-client/node_modules/axios/lib/core/createError.js:16:15)
at dispatchHttpRequest (/home/janek/data/1-projects/1-personal/carvent/node_modules/@churchtools/churchtools-client/node_modules/axios/lib/adapters/http.js:96:23)
I have tried Buffer, Blob and reading from file stream as suggested in https://stackoverflow.com/questions/25344879/uploading-file-using-post-request-in-node-js/25345124#25345124 but I am not getting anywhere...
Have you tried something like that? IMO you don't need to save and read the file again, this should be fine with saving as a blob
const PDFMerger = require('pdf-merger-js');
const merger = new PDFMerger();
return new Promise(async (resolve) => {
for(const file of result) {
await merger.add(file.value);
}
const merged = `${date}/${date}-songs-akkorde.pdf`
const mergedPdfBuffer = await merger.saveAsBlob();
const data = new FormData();
data.append("files[]", mergedPdfBuffer)
resolve(await churchtoolsClient.post(`/files/service/${event.id}`, data))
})
that is what I thought as well, but mergeAsBlob
is not part of the nodejs version of pdf-merger-js :/
Thanks for the great quickstart!
How do I send formdata to upload attachments to events with this library though? The API docs are also rather unclear on this and there seem to be typos like
attatchments
: https://demo.church.tools/api