audiobookshelf
audiobookshelf copied to clipboard
[Bug]:API to update a path on a watched library folder (#2249) not working as expected
What happened?
https://github.com/advplyr/audiobookshelf/pull/2249#issuecomment-2119068657 Also, I tried this again on a different library and, again, I see the events showing up in the log, I even see the UUID for the watched folder in the log, but it is not scanned.
What did you expect to happen?
folder scanned
Steps to reproduce the issue
- I thought I was using the api endpoint properly...
Audiobookshelf version
v.2.9.0
How are you running audiobookshelf?
Docker
What OS is your Audiobookshelf server hosted from?
Linux
If the issue is being seen in the UI, what browsers are you seeing the problem on?
None
Logs
https://github.com/advplyr/audiobookshelf/pull/2249#issuecomment-2119068657
$ ~/bin/gits/abc/abs.updatewatchedfolder --library 66e6cd96---a6c9f530a14b
{
"libraryId": "66e6cd96---a6c9f530a14b",
"path": "/library/books/-/-/-",
"type": "add"
}
abs_response=curl -X POST https://abs.website.com/api/watcher/update -H Content-Type: application/json -H Authorization: Bearer a_long_token -d { "libraryId": "66e6cd96-uuid-a6c9f530a14b", "path": "/library/books/morebooks/folder/author_name", "type": "add" })
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 138 100 2 100 136 6 452 --:--:-- --:--:-- --:--:-- 460
OK
No books found.
[2024-05-26 16:53:30.964] [Watcher] DEBUG File Added /library/books/morebooks/folder/author_name
[2024-05-26 16:53:30.965] [Watcher] DEBUG Modified file in library "folder" and folder "1d9aeae9-uuid-58c0a95484ca" with relPath "/author_name"
[2024-05-26 16:53:30.966] [Watcher] DEBUG Waiting to add file at "/library/books/morebooks/folder/author_name". mtimeMs=1716693735439.0193 lastMTimeMs=0 (loop 0)
[2024-05-26 16:53:31.261] [ApiCacheManager] DEBUG count: 4 size: 101196
[2024-05-26 16:53:31.410] [ApiCacheManager] DEBUG Cache miss: {"user":"username","url":"/libraries/7d6cc2f0-uuid-0d034cbb1459/search/?q=%2Flibrary%2Fbooks%2Fmorebooks%2Ffolder%2Fauthor_name&limit=120"}
[2024-05-26 16:53:31.967] [Watcher] DEBUG File finished adding at "/library/books/morebooks/folder/author_name"
[2024-05-26 16:53:34.970] [LibraryScanner] INFO No important changes to scan for in folder "1d9aeae9-uuid-58c0a95484ca"
But I then used the endpoint to scan the library:
59
All of those results were after using the titular api endpoint ...
### Additional Notes
_No response_
Can you give precise reproducible steps and share what you are seeing and what you are expecting to see?
Starting with audiobook Title outside of the library, I removed all permissions other than for the owner (i.e., abs has no permissions):
chown -R $USER:$USER ./Title/; chown -R go-rw ./Title/
Moved it to a folder inside of the library:
mv ./Title /library/books/audio/Last, First
LOG OUTPUT:
[2024-06-08 13:06:17.353] [Watcher] ERROR Error: EACCES: permission denied, watch '/library/books/audio/Last, First/Title'
I returned permissions to abs:
chown -R $USER:media *; chmod -R ugo+r *
abs.getlibraries
was run to check on how it output the selected library uuid and then ran just the abs.getlibraries portion of
abs.updatewatchedfolder
a couple times which accounts for the following
LOG OUTPUT:
[2024-06-08 13:15:36.174] [ApiCacheManager] DEBUG count: 0 size: 0
[2024-06-08 13:15:36.181] [ApiCacheManager] DEBUG Cache miss: {"user":"$USER","url":"/libraries"}
[2024-06-08 13:15:54.857] [ApiCacheManager] DEBUG count: 1 size: 14088
[2024-06-08 13:15:54.857] [ApiCacheManager] DEBUG Cache hit: {"user":"$USER","url":"/libraries"}
[2024-06-08 13:18:23.570] [ApiCacheManager] DEBUG count: 1 size: 14088
[2024-06-08 13:18:23.570] [ApiCacheManager] DEBUG Cache hit: {"user":"$USER","url":"/libraries"}
[2024-06-08 13:18:51.279] [ApiCacheManager] DEBUG count: 1 size: 14088
[2024-06-08 13:18:51.280] [ApiCacheManager] DEBUG Cache hit: {"user":"$USER","url":"/libraries"}
[2024-06-08 13:19:07.360] [ApiCacheManager] DEBUG count: 1 size: 14088
[2024-06-08 13:19:07.361] [ApiCacheManager] DEBUG Cache hit: {"user":"$USER","url":"/libraries"}
abs.updatewatchedfolder was run [from /library/books/audio/Last, First/Title] to completion:
edt 1.19 pm pwd: /library/books/audio/Last, First/Title
$ abs.updatewatchedfolder -L
{
"libraryId": "81ef60f9-20c0-4303-8858-fdb162bc8891",
"path": "/library/books/audio/Last, First/Title",
"type": "add"
}
abs_response=curl -X POST https://abs.website.com/api/watcher/update -H Content-Type: application/json -H Authorization: Bearer <token> -d { "libraryId": "81ef60f9-20c0-4303-8858-fdb162bc8891", "path": "/library/books/audio/Last, First/Title", "type": "add" })
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 177 100 2 100 175 11 1001 --:--:-- --:--:-- --:--:-- 1017
OK
%2Flibrary%2Fbooks%2Fprivate%2Ferotica%2FHawthorne%2C%20Annabelle%3B%20Knightley%2C%20Virgil%2FMaster%20Class%202
No books found.
LOG OUTPUT:
[2024-06-08 13:19:16.059] [Watcher] DEBUG File Added /library/books/audio/Last, First/Title
[2024-06-08 13:19:16.060] [Watcher] DEBUG Modified file in library "Audio" and folder "d93dfca7-1604-46ee-88cc-3502c940e62b" with relPath "/Last, First/Title"
[2024-06-08 13:19:16.062] [Watcher] DEBUG Waiting to add file at "/library/books/audio/Last, First/Title". mtimeMs=1717866284210.0442 lastMTimeMs=0 (loop 0)
[2024-06-08 13:19:17.062] [Watcher] DEBUG File finished adding at "/library/books/audio/Last, First/Title"
[2024-06-08 13:19:20.239] [libraryFilters] DEBUG Loaded filterdata in 0.17s
[2024-06-08 13:19:20.239] [LibraryScanner] DEBUG Scanning file update groups in folder "d93dfca7-1604-46ee-88cc-3502c940e62b" of library "Audio"
[2024-06-08 13:19:20.241] [LibraryScanner] DEBUG scanFolderUpdates fileUpdateGroup [object Object]
[2024-06-08 13:19:20.322] [LibraryScanner] WARN Files were modified in a parent directory of a library item "/library/books/audio/Last, First/A Different Title" - ignoring
[2024-06-08 13:19:20.322] [LibraryScanner] DEBUG Folder scan results [object Object]
[2024-06-08 13:19:38.146] [ApiCacheManager] DEBUG count: 1 size: 14088
[2024-06-08 13:19:38.264] [ApiCacheManager] DEBUG Cache miss: {"user":"$USER","url":"/libraries/7d6cc2f0-66c7-4d53-be5b-0d034cbb1459/search/?q=%2Faudio%2FLast%2C%20First%2FTitle%202&limit=120"}
expectations I thought the point of this endpoint was to be able to scan a folder without having to scan the entire library. I expected the book I added to the library, gave abs permission to read, and told abs to update the watched folder of would be added in due course. In my attempts so far, this endpoint clearly sees the correct library and path, the file's there, it's got good ownership and good permissions, abs won't deign to add it without unnecessarily [and undesirably] scanning the entire library.
If you look back to the OP (I haven't done it here), you'll see that scanning the library at this point will add the book—i'm certain of that.
I did not go through this script other than to make sure it didn't have e.g., my exposed api token so it's in a relatively WIP status.
#!/bin/bash
source ~/.config/abc.conf
shopt -s nullglob extglob dotglob
bold="$(tput bold)"
tput0="$(tput sgr0)"
red="$(tput setaf 1)"
redbold="${red}${bold}"
abs_user="4493e7cd-5d8a-4443-b8b2-7d1fd2e6de99"
editscript(){
local scriptpath script path swp; scriptpath=$(realpath "$0" 2>/dev/null); script="${scriptpath##*/}"; path="${scriptpath%/*}"; swp="$path/.$script.swp"; [[ ! -e "$swp" ]] && printf "\n\n%s\n\n" "$swp" && (/usr/bin/nano "$scriptpath") && exit; printf "\n%s is already being edited.\n%s exists; try fg or look in another window.\n" "$scriptpath" "$swp"; exit ;}
pause(){ read -rp "$*" ; }
percent_encode() {
jq -rn --arg str "$1" '$str | @uri'
}
display_book() {
echo "${redbold}Title:${tput0} ${title}: ${subtitle}"
echo "${redbold}Authors:${tput0}"
#for ((j = 0; j < author_count; j++)); do
# echo "- $(echo "$book" | jq -r ".authors[$j].name")"
#done
printf -- '- %s\n' "${authors[@]}"
echo "${redbold}Narrator:${tput0} ${narrator}"
echo "${redbold}Published Year:${tput0} ${published_year}"
echo "${redbold}Publisher:${tput0} ${publisher}"
echo "${redbold}Description:${tput0} ${description}"
echo "${redbold}ASIN:${tput0} ${asin}"
echo "${redbold}Series:${tput0} ${series}"
echo "${redbold}Genres:${tput0} ${genres}"
echo "${redbold}Cover Image:${tput0} ${cover_image}"
echo "${redbold}Book Directory:${tput0} ${audio_directory}"
if [[ ! -z "$user_media_progress" ]]; then
echo "User Media Progress: $user_media_progress"
echo "User duration: $user_duration"
echo "User progress: $user_progress"
echo "User current time: $user_currentTime"
echo "User is finished: $user_isFinished"
fi
echo
}
gettitles(){
abs_endpoint="/api/libraries/$libid/search/?q=$query&limit=120"
abs_url="$abs_server$abs_endpoint"
abs_response=$(curl -sS "$abs_server$abs_endpoint" -H "Authorization: Bearer $abstoken" )
if [ -z "$abs_response" ]; then
echo "Error: Failed to retrieve data from the API endpoint."
exit 1
fi
book_count=$(echo "$abs_response" | jq '.book | length')
if [ "$book_count" -eq 0 ]; then
echo "No books found."
exit 0
fi
## There's probably no reason to enable them here since they\'re not needed in the list...
for ((i=0; i<book_count; i++)); do
book=$(echo "$abs_response" | jq ".book[$i].libraryItem.media.metadata")
title=$(echo "$book" | jq -r '.title')
subtitle=$(echo "$book" | jq -r '.subtitle')
author_count=$(echo "$book" | jq '.authors | length')
published_year=$(echo "$book" | jq -r '.publishedYear')
asin=$(echo "$book" | jq -r '.asin')
libraryItemId=$(echo "$abs_response" | jq -r ".book[$i].libraryItem.media.libraryItemId")
audio_directory=$(echo "$book" | jq -r '.path'); audio_directory="${audio_directory%\/*}"
## series=$(echo "$book" | jq -r '.series[0].name')
## narrator=$(echo "$book" | jq -r '.narratorName')
## publisher=$(echo "$book" | jq -r '.publisher')
## description=$(echo "$book" | jq -r '.description')
## genres=$(echo "$book" | jq -r '.genres | join(", ")')
## cover_image=$(echo "$abs_response" | jq -r ".book[$i].libraryItem.media.coverPath")
# Add authors to the array
for ((j = 0; j < author_count; j++)); do
authors+=("$(echo "$book" | jq -r ".authors[$j].name")")
done
# Add title with subtitle to the array
#arrays:
title_subtitles+=("${title}${delimiter}: ${subtitle}")
titles+=("${title}")
subtitles+=("${subtitle}")
itemids+=( "${libraryItemId}" )
title_id["${titles[$i]}"]="${itemids[$i]}"
id_title["${itemids[i]}"]="${titles[i]}"
done
}
. ~/.config/abc.conf
libid="$default_library"
while (( $# > 0 )); do
[[ "$1" = @(edit|e|-e) ]] && editscript
[[ "$1" = -l || "$1" = --library || "$1" =~ "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[4][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$" ]] && shift && libid="$1" && shift
[[ "$1" = '-L' ]] && libid="$(abs.getlibraries)" && shift
done
libid="${libid#* }" #if the library uuid isn\'t prefixed with `uuid: ` then there shouldn\'t be a space that matters...
[[ ! "$libid" || ! "$abs_server" || ! "$abs_token" ]] && echo "Check ~/.config/abc.conf" && exit 1
abs_endpoint="/api/watcher/update"
JSON_PAYLOAD=$(cat <<EOF
{
"libraryId": "$libid",
"path": "$PWD",
"type": "add" `# or "unlink" or "rename"`
}
EOF
)
#JSON_PAYLOAD=$(cat <<EOF
#{
# "libraryId": "$libid",
# "path": "$PWD",
# "type": "add" `# or "unlink" or "rename"`
#`# "oldPath": "your-old-path" # Uncomment this line if type is "rename"`
#}
#EOF
#)
pause "$JSON_PAYLOAD"
pause "abs_response=curl -X POST "$abs_server$abs_endpoint" -H "Content-Type: application/json" -H "Authorization: Bearer $abs_token" -d "$JSON_PAYLOAD")"
abs_response="$(curl -X POST "$abs_server$abs_endpoint" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $abs_token" \
-d "$JSON_PAYLOAD")"
echo "$abs_response"
abs.searchlibrary "${PWD%%:*}"