-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-synoindex.sh
368 lines (291 loc) · 9.77 KB
/
update-synoindex.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/bin/sh
# Synology synoindexd service only works with FTP, SMB, AFP
# this program index all files that synoindexd left
# you can select extensions, modified time, user and paths
# for the searching and treatment
#
# Usage: update-synoindex.sh --> first for create config file
# change values of config/update-synoindex-conf.txt
# update-synoindex.sh
#
# ---------------------------------------------------------------------
# Copyright (C) 2015-01-16 CPVprogrammer
# https://github.com/CPVprogrammer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
#
# v1.00 first release
# v1.01 corrected an issue with path when parents path not found in DB
# v1.02 corrected a bug with path and filenames and added a log file
# v1.03 corrected a bug with postgresql roles in query
# v1.04 corrected a bug with postgresql path for DSM 6.0
# v1.05 corrected a bug with paths with spaces in config file
# v1.06 corrected a bug with find parameters
# v1.07 corrected a bug with log paths with spaces in config file
#---------------------------------------------
#function to set the environment
#---------------------------------------------
set_environment(){
ALL_EXT="ASF AVI DIVX FLV IMG ISO M1V M2P M2T M2TS M2V M4V MKV MOV MP4 MPEG4 MPE MPG MPG4 MTS QT RM TP TRP TS VOB WMV XVID"
CONFIG_DIR=$(dirname $0)"/config"
if [[ ! -d "$CONFIG_DIR" ]]; then
mkdir "$CONFIG_DIR"
fi
SCRIPT_NAME=${0##*/}
SCRIPT_NAME=${SCRIPT_NAME%.*}
CONFIG_FILE=$SCRIPT_NAME"-conf.txt"
PSQL_PATH=`which psql`
FICH_CONF="$CONFIG_DIR/$CONFIG_FILE"
if [[ ! -f "$FICH_CONF" ]]; then
#insert into file default values
echo "#extensions
$ALL_EXT
#Modified time --> none or \"command find time\" --> 24 hours example = \"-mtime 0\" ----> 1 hour = \"-mmin -60\"
none
#user: none, root, transmission, ftp, etc.
none
#directory for log/filename --> none, or path, or path/filename. for paths only must end with /
none
#directories to treat --> 0 recursive, 1 no recursive
1 /volume1
1 /volume1
1 /volume1" > "$FICH_CONF"
exit
fi
#flag for read extensions, time for find and user for find from file FICH_CONF
READ_EXT=0
READ_TIME=0
READ_USER=0
READ_LOG=0
}
#---------------------------------------------
#function to log the execution of the script
#---------------------------------------------
log_this(){
if [ "$LOG_FILE" == "none" ]; then
echo $*
else
echo $* | tee -a "$LOG_FILE"
fi
}
#---------------------------------------------
#function to extract the extension of a path
#---------------------------------------------
extension(){
FICH_EXT=${FICH_MEDIA##*.}
#convert to uppercase the extension
FICH_EXT=$(echo $FICH_EXT | tr 'a-z' 'A-Z')
}
#---------------------------------------------
#function to check it is a treatable extension
#---------------------------------------------
check_extension(){
if echo "$ALL_EXT" | grep -q "$FICH_EXT"; then
TREATABLE=1
else
TREATABLE=0
fi
return "$TREATABLE"
}
#---------------------------------------------
#function to check if directory is in the DB
#---------------------------------------------
search_directory_DB(){
PATH_MEDIA=${FICH_MEDIA%/*}
PATH_CREATE="$PATH_MEDIA"
PATH_MEDIA_DB=$(echo $PATH_MEDIA | tr 'A-Z' 'a-z')
#replace "'" with "\'"
PATH_MEDIA_SQL=${PATH_MEDIA_DB//"'"/"\'"}
TOTAL=0
FIRST=1
CREATE_DIR=0
while : ; do
TOTAL=`$PSQL_PATH mediaserver postgres -tA -c "select count(1) from directory where lower(path) like '%$PATH_MEDIA_SQL%'"`
if [ "$TOTAL" = 0 ]; then
if [ "$FIRST" = 1 ]; then
FIRST=0
else
PATH_CREATE=${PATH_CREATE%/*}
fi
CREATE_DIR=1
fi
PATH_MEDIA_SQL=${PATH_MEDIA_SQL%/*}
if [ -z "$PATH_MEDIA_SQL" ]; then
break
fi
done
return "$CREATE_DIR"
}
#---------------------------------------------
#function to check if file is in the DB
#---------------------------------------------
search_file_DB(){
FICH_MEDIA_DB=$(echo $FICH_MEDIA | tr 'A-Z' 'a-z')
#replace "'" with "\'"
FICH_MEDIA_SQL=${FICH_MEDIA_DB//"'"/"\'"}
TOTAL=`$PSQL_PATH mediaserver postgres -tA -c "select count(1) from video where lower(path) like '%$FICH_MEDIA_SQL%'"`
return "$TOTAL"
}
#---------------------------------------------
#function to add directory to DB
#---------------------------------------------
add_directory_DB(){
if [ "$LAST_CREATED" != "$PATH_CREATE" ]; then
LAST_CREATED="$PATH_CREATE"
synoindex -A "$PATH_CREATE"
log_this "added directory: $PATH_CREATE to DB"
fi
}
#---------------------------------------------
#function to add file to DB
#---------------------------------------------
add_file_DB(){
synoindex -a "$FICH_MEDIA"
log_this "added file: $FICH_MEDIA to DB"
}
#---------------------------------------------
#function to treat directories
#---------------------------------------------
treat_directories(){
CREATE_FILE=1
search_directory_DB
SEARCH_RETVAL=$?
if [ "$SEARCH_RETVAL" == 1 ]; then
add_directory_DB
CREATE_FILE=0
fi
return $CREATE_FILE
}
#---------------------------------------------
#function to treat files
#---------------------------------------------
treat_files(){
extension
check_extension
EXT_RETVAL=$?
if [ "$EXT_RETVAL" == 1 ]; then
search_file_DB
SEARCH_RETVAL=$?
if [ "$SEARCH_RETVAL" == 0 ]; then
treat_directories
EXT_RETVAL=$?
if [ "$EXT_RETVAL" == 1 ]; then
add_file_DB
fi
fi
fi
}
#---------------------------------------------
#function for the main program
#---------------------------------------------
treatment(){
#read file FICH_CONF
while read LINE || [ -n "$LINE" ]; do
#skip comment and blank lines
case "$LINE" in \#*) continue ;; esac
[ -z "$LINE" ] && continue
#read the extensions from file
if [[ "$READ_EXT" -eq 0 ]]; then
ALL_EXT=$LINE
#convert to uppercase
ALL_EXT=$(echo $ALL_EXT | tr 'a-z' 'A-Z')
READ_EXT=1
continue
fi
#read the update time from file
if [[ "$READ_TIME" -eq 0 ]]; then
LINE=$(echo $LINE | tr 'A-Z' 'a-z')
if [ "$LINE" == "none" ]; then
TIME_UPD=""
else
TIME_UPD="$LINE"
fi
READ_TIME=1
continue
fi
#read the user from file
if [[ "$READ_USER" -eq 0 ]]; then
LINE=$(echo $LINE | tr 'A-Z' 'a-z')
if [ "$LINE" == "none" ]; then
USER_OWN=""
else
USER_OWN="-user $LINE"
fi
READ_USER=1
continue
fi
#read the log path file
if [[ "$READ_LOG" -eq 0 ]]; then
LINE=$(echo $LINE | tr 'A-Z' 'a-z')
if [ "$LINE" == "none" ]; then
LOG_FILE="none"
else
#get directory name
DIRECTORY="${LINE%/*}"
if [ -d "$DIRECTORY" ]; then
if [ "${LINE%/}" == "$DIRECTORY" ]; then
LOG_FILE="${LINE%/}/$SCRIPT_NAME.log"
if [ ! -f "$LOG_FILE" ]; then
> $LOG_FILE
echo "This is the logfile for the script: $SCRIPT_NAME" > $LOG_FILE
fi
else
LOG_FILE=$LINE
if [ ! -f "$LINE" ]; then
> $LOG_FILE
echo "This is the logfile for the script: $SCRIPT_NAME" > $LOG_FILE
fi
fi
else
echo "The log directory: \"$DIRECTORY\" doesn't exist"
LOG_FILE="none"
fi
fi
NOW=$(date +"%Y-%m-%d %H:%M:%S")
log_this "program executed at: " $NOW
READ_LOG=1
continue
fi
#read the paths from file
RECURSIVE=$(echo $LINE | awk -F" " '{print $1}')
PATH_FILE=$(echo $LINE | awk '{print substr($0,2)}')
#remove leading spaces
shopt -s extglob
PATH_FILE="${PATH_FILE##*( )}"
shopt -u extglob
#delete last / if exist
PATH_FILE="${PATH_FILE%/}"
if [[ "$RECURSIVE" -eq 0 ]]; then
#recursive find
RECURSIVE=""
else
#no recursive find
RECURSIVE="-maxdepth $RECURSIVE"
fi
TYPEFIND="-type f"
FIND_PARAMETERS=($RECURSIVE $TIME_UPD $TYPEFIND $USER_OWN)
PARAMETERS=$(find "$PATH_FILE" "${FIND_PARAMETERS[@]}")
IFS=$'\n'
for FICH_MEDIA in $PARAMETERS
do
treat_files
done
unset IFS
done < $FICH_CONF
}
#---------------------------------------------
#main
#---------------------------------------------
set_environment
treatment