-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathflashair-command
executable file
·392 lines (310 loc) · 13 KB
/
flashair-command
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env bash
# -------------------------------------------------------------------
# Tool to synchronise Toshiba Flashair SD Card
# It synchronise either :
# - directory content
# - media files
#
# Revision history :
# 30/03/2017, V1.0 - Creation by N. Bernaerts
# 15/04/2017, V1.1 - Complete rewrite with parameters
# 28/04/2017, V1.2 - Add single instance lock
# 12/08/2017, V1.3 - Add --delete-move parameter and notifications
# 02/09/2017, V1.4 - Add service notifications on current desktop
# -------------------------------------------------------------------
# check tools
command -v curl >/dev/null 2>&1 || { logger "FlashAir - curl is not installed"; exit 1; }
command -v composite >/dev/null 2>&1 || { logger "FlashAir - composite is not installed [imagemagick package]"; exit 1; }
command -v convert >/dev/null 2>&1 || { logger "FlashAir - convert is not installed [imagemagick package]"; exit 1; }
command -v inotifywait >/dev/null 2>&1 || { logger "FlashAir - inotifywait is not installed [inotify-tools package]"; exit 1; }
command -v zenity >/dev/null 2>&1 || { logger "FlashAir - zenity is not installed"; exit 1; }
command -v yad >/dev/null 2>&1 || { logger "FlashAir - yad is not installed"; exit 1; }
command -v desktop-display >/dev/null 2>&1 || { logger "FlashAir - desktop-display is not installed (https://github.com/NicolasBernaerts/ubuntu-scripts/tree/master/desktop)"; exit 1; }
# load flashair environment variables
. /usr/local/sbin/flashair-common
# empty arrays
ARR_ITEM=()
# if no argument, display help
if [ $# -eq 0 ]
then
echo "Tool to synchronise Toshiba Flashair SD Card content"
echo "Only usable if laptop is connected to a Toshiba FlashAir wifi network"
echo "Parameters are :"
echo " --connect Actions to setup new connexion"
echo " --disconnect Actions to deal with a disconnexion"
echo " --update Update directory content from Flashair card"
echo " --download Download files from Flashair card"
echo " --upload Write files to Flashair card"
echo " --delete Delete file from Flashair card"
echo " <item1> <item2> ... Files/directories to handle"
exit 1
fi
# loop to retrieve arguments
while test $# -gt 0
do
case "$1" in
"--connect") MODE="connect"; shift; ;;
"--disconnect") MODE="disconnect"; shift; ;;
"--update") MODE="update"; shift; ;;
"--download") MODE="download"; shift; ;;
"--upload") MODE="upload"; shift; ;;
"--delete") MODE="delete"; shift; ;;
*) ARR_ITEM=( "${ARR_ITEM[@]}" "$1" ); shift; ;;
esac
done
echo "flashair command - ${MODE} : ${ARR_ITEM[@]}" >> /tmp/flash.log
# get default parameters from flashair.conf
FLASHAIR_CONFIRM=$(grep "^confirm-${MODE}=" "${FLASHAIR_CONF}" | cut -d'=' -f2-)
FLASHAIR_FILELIST=$(grep "^flashair-filelist=" "${FLASHAIR_CONF}" | cut -d'=' -f2-)
# create temporary files
TMP_HEADER=$(mktemp --tmpdir header-XXXXXXXXX.html)
TMP_FILE=$(mktemp --tmpdir file-XXXXXXXXX.jpg)
# --------------------------------------
# Step 1 : Controls & Main parameters
# --------------------------------------
# confirmation by default
CONFIRMED=0
# get number of files to handle
NBR_ITEM=${#ARR_ITEM[@]}
# if given, get first parameter
[ ${NBR_ITEM} -gt 0 ] && CARD_FIRST_PATH=$(echo "${ARR_ITEM[0]}" | sed "s|^${FLASHAIR_ROOT}||")
# action according to command
case "${MODE}" in
# -----------------------------
# flashair network connexion
# -----------------------------
"connect")
# get flashair network address
FLASHAIR_INTERFACE="${ARR_ITEM[0]}"
FLASHAIR_NETWORK=$(ifconfig | sed -n -e '/'${FLASHAIR_INTERFACE}'/,$p' | grep "inet addr" | sed "s/^.*inet addr:\([0-9.]*\).*$/\1/" | cut -d'.' -f1-3)
# look for entry in /etc/hosts
FLASHAIR_ENTRY=$(grep " flashair$" "/etc/hosts")
if [ "${FLASHAIR_ENTRY}" = "" ]
then
# create flashair entry in /etc/hosts
echo "# flashair card" >> "/etc/hosts"
echo "${FLASHAIR_NETWORK}.1 flashair" >> "/etc/hosts"
else
# update flashair address in /etc/hosts
sed -i "s/^.* flashair$/${FLASHAIR_NETWORK}.1 flashair/g" "/etc/hosts"
fi
# if neeeded, create media directory
[ ! -d "${FLASHAIR_ROOT}" ] && { mkdir "${FLASHAIR_ROOT}"; chmod 777 "${FLASHAIR_ROOT}"; }
# if first time, create flashair bookmark to user nautilus configuration
FLASHAIR_BOOKMARK=$(grep "${FLASHAIR_ROOT}" "/home/${DESKTOP_USER}/.config/gtk-3.0/bookmarks")
[ "${FLASHAIR_BOOKMARK}" = "" ] && echo "file://${FLASHAIR_ROOT} Flashair (disconnected)" >> "/home/${DESKTOP_USER}/.config/gtk-3.0/bookmarks"
# enable right click download menu
sed -i "s/^Enabled=.*$/Enabled=true/g" "/usr/local/share/file-manager/actions/flashair-action-download.desktop"
# get the upload status of the card
UPLOAD_ENABLED=$(curl "http://flashair/command.cgi?op=118")
# if upload is allowed, enable right click upload and delete menus
if [ "${UPLOAD_ENABLED}" = "1" ]
then
# enable upload and delete actions
sed -i "s/^Enabled=.*$/Enabled=true/g" "/usr/local/share/file-manager/actions/flashair-action-upload.desktop"
sed -i "s/^Enabled=.*$/Enabled=true/g" "/usr/local/share/file-manager/actions/flashair-action-delete.desktop"
# update nautilus bookmark as (read-write)
sed -i "s/Flashair.*$/Flashair (read-write)/g" "/home/${DESKTOP_USER}/.config/gtk-3.0/bookmarks"
else
# update nautilus bookmark as (read-only)
sed -i "s/Flashair.*$/Flashair (read-only)/g" "/home/${DESKTOP_USER}/.config/gtk-3.0/bookmarks"
fi
# no file to handle in step 2
ARR_ITEM=( )
# desktop notification
desktop-display notify-send --category=network.connected --icon=flashair-icon "FlashAir" "Card connected thru wifi & accessible under ${FLASHAIR_ROOT}"
;;
# -----------------------------
# flashair network disconnexion
# -----------------------------
"disconnect")
# disable right click menus
sed -i "s/^Enabled=.*$/Enabled=false/g" "/usr/local/share/file-manager/actions/flashair-action-download.desktop"
sed -i "s/^Enabled=.*$/Enabled=false/g" "/usr/local/share/file-manager/actions/flashair-action-upload.desktop"
sed -i "s/^Enabled=.*$/Enabled=false/g" "/usr/local/share/file-manager/actions/flashair-action-delete.desktop"
# update nautilus bookmark with (disconnected)
sed -i "s/Flashair.*$/Flashair (disconnected)/g" "/home/${DESKTOP_USER}/.config/gtk-3.0/bookmarks"
# no file to handle in step 2
ARR_ITEM=( )
# desktop notification
desktop-display notify-send --category=network.disconnected --icon=flashair-icon "FlashAir" "Card disconnected thru wifi"
;;
# -------------------
# directory update
# -------------------
"update")
# generate temporary files
TMP_THUMB=$(mktemp --tmpdir thumb-XXXXXXXXX.png)
TMP_BULLET=$(mktemp --tmpdir bullet-XXXXXXXXX.png)
# directory to update is first element in the list
ITEM="${ARR_ITEM[0]}"
# path of the directory on flashair card
CARD_ITEM_PATH=$(echo "${ITEM}" | sed "s|^${FLASHAIR_ROOT}||")
[ "${CARD_ITEM_PATH}" = "" ] && CARD_ITEM_PATH="/"
(
# initial display
echo "1"
echo "# Flashair : Analyse content of ${CARD_ITEM_PATH}"
# path of flashair filelist for current directory
ITEM_FILELIST="${ITEM}/${FLASHAIR_FILELIST}"
# retrieve flashair filelist for directory from flashair card
curl "http://flashair/command.cgi?op=100&DIR=${CARD_ITEM_PATH}" | tail -n +2 > "${ITEM_FILELIST}"
chmod 666 "${ITEM_FILELIST}"
# loop thru flashair filelist
while read LINE;
do
# read current subitem data
CARD_SUBITEM_NAME=$(echo "${LINE}" | cut -d',' -f2)
CARD_SUBITEM_SIZE=$(echo "${LINE}" | cut -d',' -f3)
CARD_SUBITEM_TYPE=$(echo "${LINE}" | cut -d',' -f4)
CARD_SUBITEM_EXT=$(echo ${CARD_SUBITEM_NAME##*.})
# generate local subitem path
SUBITEM="${ITEM}/${CARD_SUBITEM_NAME}"
# if current subitem is a file
if [ ${CARD_SUBITEM_TYPE} -ge 32 ]
then
# if file does not exist locally, get thumbnail
if [ ! -f "${SUBITEM}" ]
then
# display progress
echo "# Flashair : Download ${CARD_ITEM_PATH}/${CARD_SUBITEM_NAME} thumbnail"
# get file size on flashair card
DISPLAY_SIZE=$(echo "${CARD_SUBITEM_SIZE}" | numfmt --to=iec)
# download embedded thumbnail
curl -o "${TMP_FILE}" --dump-header "${TMP_HEADER}" "http://flashair/thumbnail.cgi?${CARD_ITEM_PATH}/${CARD_SUBITEM_NAME}"
THUMB_OK=$(grep "image/jpeg" "${TMP_HEADER}")
# set generic thumbnail according to extension
ORG_THUMB="/usr/share/icons/flashair-type-generic.jpg"
[ "${CARD_SUBITEM_EXT}" = "jpg" -o "${CARD_SUBITEM_EXT}" = "JPG" ] && ORG_THUMB="/usr/share/icons/flashair-type-photo.jpg"
[ "${CARD_SUBITEM_EXT}" = "mp4" -o "${CARD_SUBITEM_EXT}" = "MP4" ] && ORG_THUMB="/usr/share/icons/flashair-type-video.jpg"
# if thumbnail succesfully downloaded, use it
[ "${THUMB_OK}" != "" ] && ORG_THUMB="${TMP_FILE}"
# convert icon to 192 pix
convert -resize 256x "${ORG_THUMB}" "${TMP_THUMB}"
# generate bullet
convert -fill white -pointsize 38 -draw 'text 70,48 "'${DISPLAY_SIZE}'"' "/usr/share/icons/flashair-size.png" "${TMP_BULLET}"
# create thumbnail with bullet
composite -gravity South "${TMP_BULLET}" "${TMP_THUMB}" "${SUBITEM}"
chmod 666 "${SUBITEM}"
fi
# else, if current item is a directory
elif [ ${CARD_SUBITEM_TYPE} -ge 16 ]
then
# if local directory does not exist
if [ ! -d "${SUBITEM}" ]
then
# create local subdirectory (as rwxrwxrwx)
mkdir --parent "${SUBITEM}"
chmod 777 "${SUBITEM}"
fi
fi
done < "${ITEM_FILELIST}"
# end of progress display
echo "100"
) | desktop-display yad --progress --image=flashair-icon --no-buttons --undecorated --pulsate --center --auto-close
# no file to handle in step 2
ARR_ITEM=( )
# delete temporary files
rm -f "${TMP_THUMB}" "${TMP_BULLET}"
;;
# -----------------
# files download
# -----------------
"download")
# if needed, ask confirmation
[ ${#ARR_ITEM[@]} -gt 1 ] && TEXT="Download ${NBR_ITEM} file(s) from card ?" || TEXT="Download ${CARD_FIRST_PATH} from card ?"
[ "${FLASHAIR_CONFIRM}" = "true" ] && { desktop-display yad --question --image=flashair-download --center --title "Flashair : Download" --text "${TEXT}"; CONFIRMED=$?; }
# if download canceled, empty array of items
[ ${CONFIRMED} -eq 1 ] && ARR_ITEM=( )
;;
# ---------------
# files upload
# ---------------
"upload")
# if needed, ask confirmation
[ ${#ARR_ITEM[@]} -gt 1 ] && TEXT="Upload ${NBR_ITEM} file(s) to card ?" || TEXT="Upload ${CARD_FIRST_PATH} to card ?"
[ "${FLASHAIR_CONFIRM}" = "true" ] && { desktop-display yad --question --image=flashair-delete --center --title "Flashair : Upload" --text "${TEXT}"; CONFIRMED=$?; }
# if upload canceled, empty array of items
[ ${CONFIRMED} -eq 1 ] && ARR_ITEM=( )
;;
# ---------------
# files delete
# ---------------
"delete")
# if needed, ask confirmation
[ ${#ARR_ITEM[@]} -gt 1 ] && TEXT="Delete ${NBR_ITEM} file(s) from card ?" || TEXT="Delete ${CARD_FIRST_PATH} from card ?"
[ "${FLASHAIR_CONFIRM}" = "true" ] && { desktop-display yad --question --image=flashair-delete --center --title "Flashair : Delete" --text "${TEXT}"; CONFIRMED=$?; }
# if deleted canceled, empty array of items
[ ${CONFIRMED} -eq 1 ] && ARR_ITEM=( )
;;
*)
;;
esac
# ---------------------------
# Step 2 : Action on files
# ---------------------------
# loop thru candidate files
for ITEM in "${ARR_ITEM[@]}"
do
# path of item on flashair card
CARD_ITEM_PATH=$(echo "${ITEM}" | sed "s|^${FLASHAIR_ROOT}||")
# log action
logger "flashair - ${MODE} ${CARD_ITEM_PATH}"
# action according to mode
case "${MODE}" in
# -----------------
# download
# -----------------
"download")
(
# initial display
echo "1"
echo "# Flashair : Download ${CARD_ITEM_PATH}"
# delete thumbnail
rm -f "${ITEM}"
# download file
curl -o "${ITEM}" "http://flashair/${CARD_ITEM_PATH}"
# set rw to all users
chmod 666 "${ITEM}"
echo "100"
) | desktop-display yad --progress --image=flashair-download --no-buttons --undecorated --pulsate --center --auto-close
;;
# ---------------
# upload
# ---------------
"upload")
(
# initial display
echo "1"
echo "# Flashair : Upload ${CARD_ITEM_PATH}"
# get upload directory
CARD_ITEM_DIR=$(dirname "${CARD_ITEM_PATH}")
# set upload directory
curl -o "${TMP_FILE}" --dump-header "${TMP_HEADER}" "http://flashair/upload.cgi?UPDIR=${CARD_ITEM_DIR}"
OPERATION_OK=$(grep "SUCCESS" "${TMP_FILE}")
# if upload directory set, start upload
[ "${OPERATION_OK}" != "" ] && curl --dump-header "${TMP_HEADER}" -i -X POST -H "Content-Type: multipart/form-data" -F "data=@${ITEM}" "http://flashair/upload.cgi"
echo "100"
) | desktop-display yad --progress --image=flashair-upload --no-buttons --undecorated --pulsate --center --auto-close
;;
# --------------------
# suppression
# --------------------
"delete")
(
# initial display
echo "1"
echo "# Flashair : Delete ${CARD_ITEM_PATH}"
# send deletion command
curl -o "${TMP_FILE}" --dump-header "${TMP_HEADER}" "http://flashair/upload.cgi?DEL=${CARD_ITEM_PATH}"
# delete local file
rm -f ${ITEM}
echo "100"
) | desktop-display yad --progress --image=flashair-delete --no-buttons --undecorated --pulsate --center --auto-close
;;
*)
;;
esac
done
# remove temporary files and lock
rm -f "${TMP_FILE}" "${TMP_HEADER}"