Need help with workflow_script

Hej NC-members,

unfortunatly, the documentation for the app workflow_script is not complete - careful said.
On the other hand it seems to be the best way to solve my idea:
After creation (upload) of a jpg-file in a dedicated directory /files/jpg-upload the image shall be converted (with image-magick) into a lower resolution.

So I installed workflow_script and tried build the trigger-rule:
If “File created”
and “Filename” “is” “???”
How is the path to be formed? Absolute? Relative to what?
Is it possible to use a wildcard?
Is there a logfile?

And when this part is done, the next question would be how to put in the script that shall be executed. Where in the filesystem must that script be placed? Grants?

Is there anybody out there having this managed to work??

Thanks for help!

Boris

As usual this depends on the point of view :wink:

The best way to start is always to use the search function of this forum because most likely similar questions have already been asked and answered in the past:

https://help.nextcloud.com/search?q=workflow_scripts%20%40j-ed

If “File created”
and “Filename” “is” “???”

I think it’s a bad idea to rely on file names because they can be spoofed. Better is to use the mime type, e.g. “image/jpeg” as a selection criteria.

How is the path to be formed? Absolute? Relative to what?

To make a script call as flexible as possible for as many users as possible, I prefer to filter files within the called batch script.

Is it possible to use a wildcard?

see above, use the mime type as a criteria.

Is there a logfile?
There is no dedicated log file for this app, except the Nextcloud log file. If you call a batch script you can add the following two lines of code to it to trace the command execution within the script:

#!/bin/sh

exec 2>/tmp/workflow_script-trace-$$.txt
set -x
...

Finally keep in mind that “%n” only contains the relative file path from the Nextcloud data directory, e.g. “/user/files/YourDirectory/20191111_184052.jpg”. You need to prefix it with the data directory path to get a full qualified path :wink:

Hej j-ed,

thank you very much for your hints.
Yes, in fact I tried to search the forum and found that one which describes quite the same like I am looking for:
https://help.nextcloud.com/t/imagemagick-or-similar-convert-command-on-file-upload-flow/

But the OP already solved the fist step I am still hanging with.

So I followed your description and placed a script with the mentioned content in /var/www/nextcloud/onjpgexec.sh, made it be owned by www-data and chmodded it executable.
Executing it from bash it creates a tracefile in /tmp.
So far s good.

Also, I formed the condition for the execution as
If “file created”
and “file mime type” is “pictures”

In the run script field I wrote the whole path
/var/www/nextcloud/onjpgexec.sh %n

Uploading a JPG to my files doesn’t make the tracefile being created.

What’s wrong?

  1. I would recommend to quote the option %n -> "%n" to make sure that possible spaces in file and path names are not split into several parts.

  2. I don’t know any mime type which is called “picture” or do you meant "image/jpeg”?

  3. I would check the access rights and ownership of the script file. I need to be accessible and executable by the web server user. Very often users are testing their scripts under a root environment where access to nearly everything is possible. The Nextcloud log file should contain further information about the reason why e.g. a script cannot be executed.

  4. I personally prefer to save script files in a directory outside of the Nextcloud document root directory, e.g. in /var/nextcloud/.scripts etc., because the Nextcloud system check most likely will throw out an error if the unknown file is found. But this can be changed once the script execution problems have been solved.

Hej j-ed,

thanks again for caring!

  1. Done. I put quotation marks in front of behind %n. On the other hand my test-JPGs are very easy-named: Just as they come from my cam, for example: 20201003_144613.jpg

  2. Well, of course… I’m using the German translation and in fact it’s called “Bilder”

I also tried the “Benutzerdefinierter MIME-Typ” and put in “image/jpeg” - with no success.

  1. I followed this hint, too. Have a look at the screenshot provided.

  2. And yes: The directory .scripts and all its content is owned by www-data:www-data and the permissions on the script are set to 755.

Actually, I’m afraid to make a general mistake:
Me, being user and administrator on my NC have two menu-topics “Ablauf” in the tree on the left: One under ‘personal’ and the other under ‘Verwaltung’ - please see second screenshot. I am using the one under ‘Verwaltung’ because the other doesn’t show the option ‘run script’.

Thanks,

Boris

That looks fine to me.

Have you added at least one more command to your batch file, beside the “exec” and “set” commands, e.g. echo "%1" to produce traceable output?

Are there any related messages in the Nextcloud log file?

Hej j-ed,

Thanks so much. You helped me keeping on topic and believing it will work…

Finally I found out the trigger condition is checked every five minutes by cronjob. So my trying was too impatient! I thought the trigger is fired right after uploading the JPG.

OK, it’s done - so far…

Last question - and I admit not having searched the forum:
Now when the conversion is done, the script created a new file in the filesystem that has to be ‘registered’ within NC. How can that be done?

Thanks again,

Boris

What about these lines of code:

if [ -x ${docroot}/occ ]
then
    # force rescan of new mp4-file
    ${docroot}/occ files:scan --no-ansi --quiet --shallow --unscanned --path="${full-path-to-new-file}" 2>/dev/null
else
    # occ command not found or not executable
    echo "Warning: '${docroot}/occ' cannot be executed!"
fi

BTW, it might also be a good idea to automatically remove the old file from your system :wink:

OK j-ed,

Thank you very much! I am sure I will be able to bring this together and make my idea work just fine!
Let’s close this topic and hope others will find it when working on a similar thing.

Thanks and regards,

Boris

Although I already marked the thread as closed (I just removed this flag), two questions came up while I was realizing the solution. So far I have achieved that images stored in NC are converted in a subdirectory.

Both questions have to do with saving computing resources:

  1. i would like to limit the trigger for the script to a certain directory, otherwise the script runs every time an image is uploaded. The script could test itself in which directory it works, but then it has already been started unnecessarily.
    How do I have to formulate a second rule that describes the directory?

  2. j-ed had provided a few lines of code as a template for registering the processed images in NC.
    I have not yet realized this part. But does it make sense in principle to realize this process for every file? I know the directory and the name of the file and it seems not necessary to scan it. Is there any way I can register the file directly?

Thanks and greetings

Boris

I haven’t had the requirement to restrict the access to a specific directory yet, but due to the fact that no directory/path selector exists I would recommend to give the “file-name eqals regular-expression” a try to catch a specific directory.

You register the file directly by executing the “occ files:scan …” command right after you’ve modified it. By providing the full path to the file, including the file name, only this item will be scanned and not the whole directory.

OK, it’s done. For some reason I had to change --unscanned to --all .
Thank you very much again.