Trigger a script when a new share is created

Hi there,
I would like to trigger an action each time an user A shares a folder to user B. I thought I could use Flow for this purpose but it does not provide a “new share” trigger.
Could it be a new feature ?

The extension of this question is: does anyone have an idea on how to achieve this ? More specifically I would like to create a symlink of the shared folder from user A to a specific location of my server.

Thanks :slight_smile:

My understanding is symlinks are not supported in nextcloud.

That would be verry simple.
You only need to monitor the database tabel oc_share, where sharetype is 0 and uid_initiator is user A and share with is user B and stime is larger then the last time you runed the (cron)job.
Then you have to create the symlink.

This is a shoot from the hip how such a solution could look like if run by cron every 10 minutes:

#!/bin/bash

last_run_time=$(date -u -d '10 minutes ago' +'%s')

query="SELECT oc_filecache.path
FROM oc_filecache, oc_share
WHERE oc_filecache.fileid = oc_share.file_source
  AND oc_share.share_type = 0
  AND oc_share.share_with = '$userB'
  AND oc_share.uid_initiator = '$userA'
  AND oc_share.stime > $last_run_time"

# Execute the SQL query:

query_result=$(mysql -u $DB_USER -p$DB_PASS  --disable-auto-rehash --default-character-set=utf8mb4 $DB_NAME "$query")

# Iterate over each file path in the query result
while IFS= read -r file_path; do
  # Create the symlink
  ln -s "$file_path" /path/to/symlink_directory/$(basename "$file_path")
done <<< "$query_result"

or if you prefere JOIN in place of a subquery

SELECT oc_filecache.path
FROM oc_filecache
JOIN oc_share ON oc_share.file_source = oc_filecache.fileid
WHERE oc_share.share_type = 0
  AND oc_share.share_with = '$userB'
  AND oc_share.uid_initiator = '$userA'
  AND oc_share.stime > $last_run_time

This is just a shot from the hip as a hint on how to solve it.
You now have to implement everything step by step by yourself (understand it first, of course!), set the characters and quotation marks correctly and debug it so that it works.

Thank your very much for your quick and accurate response, @ernolf ! I had to tweak it a little bit because I use MariaDB but your script is exactly what I needed :raised_hands:

Here is the version I’m finally using with MariaDB, 12 hours delay and check for empty results:

#!/bin/bash

# base params
export userA=Alice
export userB=Bob
export nc_files_dir=/var/nextcloud/data
export symlinks_dir=/Where/to/symlink/stuff

last_run_time=$(date -u -d '12 hours ago' +'%s')

query="SELECT oc_filecache.path
FROM oc_filecache, oc_share
WHERE oc_filecache.fileid = oc_share.file_source
  AND oc_share.share_type = 0
  AND oc_share.share_with = '$userB'
  AND oc_share.uid_initiator = '$userA'
  AND oc_share.stime > $last_run_time;"


query_result=$(mariadb -u nextcloud -p$NC_DB_PASSWORD  -N --no-auto-rehash --default-character-set=utf8mb4 -e "$query" nextclouddb)

while IFS= read -r file_path; do
  # Create the symlink
    if [ ! -z "$file_path"] 
    then
        ln -s "$nc_files_dir/$userA/$file_path" "$symlinks_dir/$(basename \"$file_path\")"
    fi
done <<< "$query_result"
echo "done :)"
1 Like