Che tipo di formato usa per i file di testo nextcloud?

uso nexcloud su un raspberry pi4 e sto cercando di far funzionare lo script sotto in modo che legga per ogni riga i links in un qualsiasi .txt e li passi al programma yt-dlp per scaricarli
la cartella sul server nextcloud è monitorata da inotifywait
il programma detox serve per normalizzare unix like i file creati
poi resetto i permessi e alla fine riscanziono i file nella cartella dell’utente

se creo il file sul computer e lo carico tramite l’interfaccia web di nextcloud funziona mente se creo direttamente il file da nextcloud non c’è modo di farlo funzionare
quello che volevo chiedere è se nextcloud usa un formato particolare per i file che crea dall’interfaccia web e se qualcuno ha qualche idea per farlo funzionare.

Sono nuovo qui se ho sbagliato sezione vi prego di spostarmi su quella giusta.

#!/bin/bash

WATCH_DIR="/PERCORSO/files/Musica/Youtube"
IGNORED_PREFIX="_"
DETOX_CMD="sudo detox --remove-trailing $WATCH_DIR/"
PERMISSION_CMD="sudo chown -R www-data:www-data $WATCH_DIR"
NEXTCLOUD_CMD="sudo -u www-data php /var/www/html/occ files:scan --path=NOMEUTENTE/files/Musica/Youtube"

# Monitoraggio di file rinominati nella directory
inotifywait -m -e moved_to --format "%w%f" "$WATCH_DIR" | while read -r FILEPATH; do
    FILENAME=$(basename "$FILEPATH")
    echo "Trovato file: $FILENAME"

    # Ignora file che iniziano con "_"
    if [[ "$FILENAME" == ${IGNORED_PREFIX}* ]]; then
        echo "File ignorato: $FILENAME (inizia con il prefisso '${IGNORED_PREFIX}')"
        continue
    fi

    # Controlla che sia un file .txt
    if [[ "$FILENAME" == *.txt ]]; then
        echo "Elaborando file: $FILENAME"

        # Attendi che il file sia completamente scritto
        echo "Verifica della disponibilità del file..."
        while lsof "$FILEPATH" >/dev/null 2>&1; do
            sleep 1
        done

        # Normalizza i terminatori di riga
        echo "Normalizzando i terminatori di riga per: $FILEPATH"
        sed -i 's/\r$//' "$FILEPATH"

        # Leggi ogni link dal file e scarica il contenuto
        while IFS= read -r LINK; do
            echo "Scaricando: $LINK"
            yt-dlp -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 --embed-thumbnail --add-metadata --output "$WATCH_DIR/%(title)s.%(ext)s" "$LINK"
        done < "$FILEPATH"

        # Esegui i comandi post-download
        echo "Eseguendo comandi post-download..."
        rm "${WATCH_DIR}/${FILENAME}"
        $DETOX_CMD
        $PERMISSION_CMD
        $NEXTCLOUD_CMD
    else
        echo "Il file non è un file di testo: $FILENAME (ignorato)"
    fi
done

Se descrivi il tuo problema in inglese, avrai maggiori possibilità di ricevere aiuto.

I continue in english, since I need to much of technical assistance with Italian :wink:

This is what I could understand after translating your problem:

  • Your script works as expected when you create a *.txt file locally and then upload it via the Nextcloud web interface.

  • when you create the file directly through the Nextcloud web interface, the script does not function as intended.

I could only imagine this as an explanation: Nextcloud does not trigger a “moved_to” event when creating files through the web interface. Instead, Nextcloud writes the file in multiple steps, typically using events like CREATE, MODIFY, and CLOSE_WRITE. (See man inotifywait, EVENTS section)
To handle files created directly in Nextcloud properly, you should change the inotifywait event from just moved_to to include close_write (or use it exclusively). For example, replace:

inotifywait -m -e moved_to --format "%w%f" "$WATCH_DIR"

with:

inotifywait -m -e close_write --format "%w%f" "$WATCH_DIR"

or combine both events:

inotifywait -m -e close_write -e moved_to --format "%w%f" "$WATCH_DIR"

Using the close_write event ensures that the file processing starts only after the file has been completely written, which addresses the issue you’re encountering with files created directly from the Nextcloud interface.

Additionally, note that Nextcloud does not use any special file format for *.txt files created via the web interface—they are standard text files. The key difference is solely in the upload and file creation method.

h.t.h.

I would be grateful for feedback if this solves your problem


Much and good luck,
ermolf

Thank you ernolf for your replay,
i try your:
inotifywait -m -e close_write -e moved_to --format “%w%f” “$WATCH_DIR” | while read -r FILEPATH; do

but the file .txt has been delete before read it
if i remove the comand “rm” the script goes in loop

the same with:
inotifywait -m -e close_write --format “%w%f” “$WATCH_DIR” | while read -r FILEPATH; do

and the output is:
Il file non è un file di testo: sedxRJvgu (ignorato) relative to:
echo “Il file non è un file di testo: $FILENAME (ignorato)” of the script