[Solved] Update directories that are updated by another application on the server

I upload files to a certain directory with: PUT remote.php/dav/files/user/path/to/file

That works flawless and for the uploaded files I have created a little batch-script. That script takes each uploaded file and uses imagemagick to reduce the resolution to be able to send it with emails. It runs in a cron-job once a minute.

Here is the script:

#!/bin/bash
ncpath=/data/nextcloud
ncuser=someone
mydir="$ncpath/data/$ncuser/files/FileDrop/Upload"
# Change to new wd
cd $mydir
# Load list of files
allfiles=`ls`
# Loop through list
for thisfile in ${allfiles} 
do
# 	# Make sure the file isn't being currently uploaded
	if [ $thisfile != *.part ]
	then
# 		# Reduce the size and put the result new dir
		convert "$thisfile" -resize 1024\> "../Reduced/$thisfile"
# 		# Change owner from root to www-data
		chown www-data:www-data "../Reduced/$thisfile"
# 		# Delete the original
		rm "$thisfile"
	fi
done

This also works without any problems, but now the client still “sees” the deleted files and does not “see” the newly reduced and copied files in “$ncpath/data/$ncuser/files/FileDrop/Reduced”

How can I get NC to update these 2 directories, so that the client(s) will sync the changes?

You can use the occ command lien tool to rescan the directory:

> ./occ files:scan --help
Usage:
  files:scan [options] [--] [<user_id>]...

Arguments:
  user_id                will rescan all files of the given user(s)

Options:
      --output[=OUTPUT]  Output format (plain, json or json_pretty, default is plain) [default: "plain"]
  -p, --path=PATH        limit rescan to this path, eg. --path="/alice/files/Music", the user_id is determined by the path and the user_id parameter and --all are ignored
      --all              will rescan all files of all known users
      --unscanned        only scan files which are marked as not fully scanned
      --shallow          do not scan folders recursively
      --home-only        only scan the home storage, ignoring any mounted external storage or share
  -h, --help             Display this help message
  -q, --quiet            Do not output any message
  -V, --version          Display this application version
      --ansi             Force ANSI output
      --no-ansi          Disable ANSI output
  -n, --no-interaction   Do not ask any interactive question
      --no-warnings      Skip global warnings, show command output only
  -v|vv|vvv, --verbose   Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Help:
  rescan filesystem
1 Like

Thank you j-ed, that works really good!

The final bash-script now looks like this:

#!/bin/bash
ncpath=/data/nextcloud
ncuser=someone
mydir="$ncpath/data/$ncuser/files/FileDrop/Upload"
# Change to new wd
cd $mydir
# Load list of files
allfiles=`ls`
# Loop through list
for thisfile in ${allfiles} 
do
#       # Make sure the file isn't being currently uploaded
        if [ $thisfile != *.part ]
        then
#               # Reduce the size and put the result new dir
                convert "$thisfile" -resize 1024\> "../Reduced/$thisfile"
#               # Change owner from root to www-data
                chown www-data:www-data "../Reduced/$thisfile"
#               # Delete the original
                rm "$thisfile"
                rescan=1
        fi
done
if [ $rescan=1 ]
then
        sudo -u www-data php "$ncpath/occ" files:scan -q -n -p "/$ncuser/files/FileDrop"
fi
1 Like