forked from ThibArg/NuxeoHotFolderUploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNuxeoHotFolderUploader.sh
124 lines (102 loc) · 3.93 KB
/
NuxeoHotFolderUploader.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
#!/bin/bash
# ============================================================
# Change the following variables to match your environment
# ============================================================
SERVER_BASE_URL=http://localhost:8080/nuxeo
USER_LOGIN=Administrator
USER_PWD=Administrator
# This folder, or workspace, or container *must*:
# - Exists
# - Have ACL which allows USER_LOGIN to create document
NUXEO_DESTINATION_URL=$SERVER_BASE_URL/api/v1/path/default-domain/workspaces/hot-folder-import
# This is the lazzy way of logging... It will work
# on Linux, Mac, with no access right problem.
# But maybe not the best way not the best place
# to store a log ;->
LOG_PATH=$HOME/nuxeo-hot-folder-uploader.log
# ============================================================
# Other variables
# ============================================================
# COPY_DEST_FOLDER will ve possibly filled if the optionnal "<optionnal copy folder>" argument is passed
# (see print_usage)
COPY_DEST_FOLDER=""
# ============================================================
# Main functions
# ============================================================
function watch_folder {
has_files=0
# Don't spend time on empty folders
if [ "$(ls -A $1)" ]; then
cd "$1"
for file in "$1"/*; do
# Ignore invisible files, or system which adds /* for empty folders
if [[ $file != .* && $file != "*" && $file != "$1/*" ]]; then
if [ "$has_files" = "0" ]; then
{ echo "========== <new_import>"; date; } | tee -a $LOG_PATH
has_files=1
fi
nuxeo_doc_type="File"
mime_type=`file --mime-type -b "$file"`
case "$mime_type" in
image/*)
nuxeo_doc_type="Picture";;
video/*)
nuxeo_doc_type="Video";;
*)
case $file in
*.mov)
nuxeo_doc_type="Video";;
*.ORF)
nuxeo_doc_type="Picture";;
*.xmp)
nuxeo_doc_type="Picture";;
*)
nuxeo_doc_type="File";;
esac
esac
send_to_nuxeo "$file" "$nuxeo_doc_type"
fi
done
if [ "$has_files" = "1" ]; then
{ echo ""; date; echo "========== </new_import>"; } | tee -a $LOG_PATH
fi
fi
}
function send_to_nuxeo {
file_full_path=$1
doc_type=$2
filename="${file_full_path##*/}"
filename_clean=${filename// /_}
echo "Sending file $filename, type $doc_type" >> $LOG_PATH
curl -H "X-Batch-Id: $filename_clean" -H "X-File-Idx:0" -H "X-File-Name:$filename" -F file=@"$file_full_path" -u "${USER_LOGIN}":"${USER_PWD}" "${SERVER_BASE_URL}/site/automation/batch/upload" | tee -a $LOG_PATH
# Used during devug, mainly
#echo "" >> $LOG_PATH
#echo "CHECKING THE BATCH" >> $LOG_PATH
#curl -u "${USER_LOGIN}":"${USER_PWD}" "${SERVER_BASE_URL}/site/automation/batch/files/$filename_clean" >> $LOG_PATH | tee -a $LOG_PATH
{ echo ""; echo "Creating a $doc_type document"; } >> $LOG_PATH
curl -X POST -H "Content-Type: application/json" -u "${USER_LOGIN}":"${USER_PWD}" -d "{ \"entity-type\": \"document\", \"name\":\"${filename_clean}\", \"type\": \"${doc_type}\", \"properties\" : { \"dc:title\":\"${filename}\",\"file:content\": {\"upload-batch\":\"${filename_clean}\",\"upload-fileId\":\"0\"}}}" "${NUXEO_DESTINATION_URL}" | tee -a $LOG_PATH
{ echo ""; echo "";} >> $LOG_PATH
if [ -n "$COPY_DEST_FOLDER" ]; then
mv "$filename" "$COPY_DEST_FOLDER/$filename"
else
rm "$filename"
fi
}
function print_usage {
echo "Usage: upload_files.sh <path-to-hotfolder> <dest-container-in-nuxeo> <optionnal copy folder>"
echo " If <optionnal copy folder> is used, files are moved to"
echo " this folder instead of being deleted afetr being sent."
echo " (Mainly used during development/debug.)"
}
if [ -d "$1" ]
then
if [ -n "$2" ]; then
COPY_DEST_FOLDER="$2"
fi
watch_folder "$1"
else
echo "Error: hot folder doesn't exist"
print_usage
exit 1
fi
#--EOF--