Resize image using Worfklow External Script - newFile is not shown

Hello,
I’m trying to implement a workflow with an external script which should resize an uploaded image by a given factor. For this I use the “worfklow external script”-extension.

Short information about my system:
I am using the latest nextcloud-aio images using a docker-compose setup.

I’ve set up the flow using the following:

  • When: “File created”
  • and “FILE MIME Type” matches “Images”
  • Path and argument for external script: /var/www/html/CUSTOM_DATA/scale_image.sh %f
    • %f based on the documentation, it is the locally available file.

The script is correctly called (I think per default there is a cron job every 5min which searches for new files) and it also creates the needed picture, but for any reason it is not showing in my file using the Nextcloud UI.
SSHing into my nextcloud host, the file is created in the passed locally available directory, but Nextcloud does not consider those files.
Is there anyting I miss in regard the file-directories?

Thanks :slight_smile:

Last but non least, here is my script:

#!/bin/bash

SCALE_PERCENT=80

# Log-Datei definieren
LOG_FILE="nextcloud_image_resize.log"

# passed argument 
FILE="$1"

# Log-function
log() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
}

log "----- FILE WAS UPLOADED: $FILE -----"

# check for passed file
if [[ ! -f "$FILE" ]]; then
    log "Error: File not found - $FILE"
    exit 1
fi

# get needed information
DIR=$(dirname "$FILE")
BASENAME=$(basename "$FILE")
EXTENSION="${BASENAME##*.}"
FILENAME="${BASENAME%.*}"

log "Filename: $BASENAME | Directory: $DIR | File-Extension: $EXTENSION"

# just in case; probably not neeed as we are checking for MIME type using the extenal script definition
case "$EXTENSION" in
    png|PNG|jpg|JPG|jpeg|JPEG)
        log "File is supported."
        ;;
    *)
        log "Error: Unsupported file - $EXTENSION"
        exit 2
        ;;
esac

# create new file suffixed with "resized"
NEW_FILE="${DIR}/${FILENAME}_resized.${EXTENSION}"


log "New file: $NEW_FILE"

# Resize original FILE and save it as NEW_FILE
convert "$FILE" -resize "${SCALE_PERCENT}%" "$NEW_FILE"

# Check for processing
if [[ -f "$NEW_FILE" ]]; then
    log "Success: File exists: $NEW_FILE"
    exit 0
else
    log "Error: File does not exis"
    exit 3
fi

Yes. You have to add the newly created image file to the filecache table of the database. Otherwise the file will remain invisible to netxcloud.

You can add it to the database using → the occ tool ←

occ files:scan --help
Description:
  rescan filesystem

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
      --generate-metadata[=GENERATE-METADATA]  Generate metadata for all scanned files; if specified only generate for named value [default: ""]
      --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 help for the given command. When no command is given display help for the list command
  -q, --quiet                                  Do not output any message
  -V, --version                                Display this application version
      --ansi|--no-ansi                         Force (or disable --no-ansi) 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

Much and good luck,
ernolf

2 Likes

Thanks for the additional information.
With the following adjustments to my script, everything is working as expected :slight_smile:

# Check for processing
if [[ -f "$NEW_FILE" ]]; then
    log "Success: File exists: $NEW_FILE"
    log "Rescan files for: $USER"
    # ATTENTION: We need to call the occ tool directly
    # if you would like to call the tool from the system, the following needs to be used; otherwise you will get an error like 
    # --- DIRECT CALL ---
    #  sudo -u www-data php /var/www/html/occ files:scan "$USER" --all >> "$LOG_FILE"
    # --- ERROR ---
    # Console has to be executed with the user that owns the file
    # config/config.php
    # Current user id: 0
    # Owner id of config.php: 33
    # Try adding 'sudo -u #33' to the beginning of the command (without the single quotes)
    # If running with 'docker exec' try adding the option '-u 33' to the docker command (without the single quotes)

    php /var/www/html/occ files:scan "$USER" --all >> "$LOG_FILE"
    exit 0
else
    log "Error: File does not exis"
    exit 3
fi
1 Like

as your script already know the file path likely it would run faster if you scan only the picture folder rather the whole user directory.

1 Like

Good point. I’ve added the --path Param and now only the passed directory is re-scanned :slight_smile:

For everyone who is interested for the full script, find it below. Some short notices

  • Script rescales the picture to 60% resolution
  • Script minimized the quality to 60%
  • Script overwrites original file ($NEW_FILE is not used anymore)
scale images script
#!/bin/bash

SCALE_PERCENT=60
IMAGE_QUALITY=60

# Define log file
LOG_FILE="nextcloud_image_resize.log"

# passed argument 
FILE="$1"
USER="$2"
FILE_REL="$3"


# Log-function
log() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
}

log "----- FILE WAS UPLOADED: $FILE -----"

# check for passed file
if [[ ! -f "$FILE" ]]; then
    log "Error: File not found - $FILE"
    exit 1
fi

# get needed information
DIR=$(dirname "$FILE")
DIR_REL=$(dirname "$FILE_REL")
BASENAME=$(basename "$FILE")
EXTENSION="${BASENAME##*.}"
FILENAME="${BASENAME%.*}"

log "Filename: $BASENAME | Directory: $DIR | File-Extension: $EXTENSION | User: $USER"

# just in case; probably not neeed as we are checking for MIME type using the extenal script definition
case "$EXTENSION" in
    png|PNG|jpg|JPG|jpeg|JPEG)
        log "File is supported."
        ;;
    *)
        log "Error: Unsupported file - $EXTENSION"
        exit 2
        ;;
esac

# create new file suffixed with "resized"
NEW_FILE="${DIR}/${FILENAME}_resized.${EXTENSION}"


log "New file: $NEW_FILE to path $DIR_REL with quality $IMAGE_QUALITY %"

# Resize original FILE and save it as NEW_FILE with Quality: $IMAGE_QUALITY
convert "$FILE" -resize "${SCALE_PERCENT}%" -quality "$IMAGE_QUALITY" -strip -interlace JPEG "$FILE"


# Check for processing
if [[ -f "$FILE" ]]; then
    log "Success: File exists: $NEW_FILE; delete old file"
    log "START - Rescan files for user: $USER"
    php /var/www/html/occ files:scan "$USER" --path="$DIR_REL" >> "$LOG_FILE"
    log "END - Rescan files for user: $USER"
    # ATTENTION: We need to call the occ tool directly
    # if you would like to call the tool from the system, the following needs to be used; otherwise you will get an error like 
    # --- DIRECT CALL ---
    #  sudo -u www-data php /var/www/html/occ files:scan "$USER" --all >> "$LOG_FILE"
    # --- ERROR ---
    # Console has to be executed with the user that owns the file
    # config/config.php
    # Current user id: 0
    # Owner id of config.php: 33
    # Try adding 'sudo -u #33' to the beginning of the command (without the single quotes)
    # If running with 'docker exec' try adding the option '-u 33' to the docker command (without the single quotes)
    exit 0
else
    log "Error: File does not exis"
    exit 3
fi

It’s called using the the external workflow script using the following command line: /var/www/html/CUSTOM_DATA/scale_image.sh %f %o %n

  • %f = locally available file (absolute path)
  • %o = owner’s user id
  • %n = nextcloud-relative path
1 Like

This topic was automatically closed 8 days after the last reply. New replies are no longer allowed.