2013-02-22 17:35:23 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2013-03-13 17:47:23 +01:00
|
|
|
|
2013-02-22 17:35:23 +01:00
|
|
|
source ./constraint.sh
|
2013-03-11 00:23:11 +01:00
|
|
|
source ./constraint_conv.sh
|
|
|
|
|
2013-03-12 19:08:46 +01:00
|
|
|
RECURSIVE_ARCHIVE_MAX=3
|
|
|
|
RECURSIVE_ARCHIVE_CURRENT=0
|
2013-03-15 09:27:26 +01:00
|
|
|
ARCHIVE_BOMB=0
|
2013-08-04 18:30:59 +02:00
|
|
|
LOGFILE="${LOGS}/processing.txt"
|
2013-06-09 19:37:41 +02:00
|
|
|
|
|
|
|
# Something went wrong.
|
|
|
|
error_handler(){
|
|
|
|
echo "FAILED." >> ${LOGFILE}
|
2013-10-29 15:48:21 +01:00
|
|
|
echo -e "\tSomething went wrong during the duplication of the last file." >> ${LOGFILE}
|
|
|
|
echo -e "\tPlease open a bug on https://www.github.com/Rafiot/KittenGroomer" >> ${LOGFILE}
|
|
|
|
continue
|
2013-06-09 19:37:41 +02:00
|
|
|
}
|
|
|
|
|
2013-10-28 22:48:50 +01:00
|
|
|
trap error_handler ERR TERM INT
|
2013-02-22 17:35:23 +01:00
|
|
|
|
2013-12-09 14:47:03 +01:00
|
|
|
office_n_txt(){
|
|
|
|
src_file=${1}
|
|
|
|
dst_file=${2}${1##$CURRENT_SRC}.html
|
|
|
|
temp=${2}/temp
|
|
|
|
${LO} --headless --convert-to pdf --outdir "${temp}" "${src_file}"
|
|
|
|
${PDF} --dest-dir=/ ${temp}/*.pdf ${dst_file}
|
|
|
|
rm -rf "${temp}"
|
|
|
|
}
|
|
|
|
|
2013-02-25 01:27:15 +01:00
|
|
|
copy(){
|
|
|
|
src_file=${1}
|
|
|
|
dst_file=${2}
|
2013-05-16 11:19:16 +02:00
|
|
|
mkdir -p `dirname "${dst_file}"`
|
|
|
|
cp "${src_file}" "${dst_file}"
|
2013-02-25 01:27:15 +01:00
|
|
|
}
|
|
|
|
|
2013-02-22 17:35:23 +01:00
|
|
|
# Plain text
|
|
|
|
text(){
|
|
|
|
echo Text file ${1}
|
2013-12-09 14:47:03 +01:00
|
|
|
office_n_txt ${1} ${2}
|
2013-02-22 17:35:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Multimedia
|
2013-02-25 01:27:15 +01:00
|
|
|
## WARNING: They are assumed safe.
|
2013-02-22 17:35:23 +01:00
|
|
|
audio(){
|
|
|
|
echo Audio file ${1}
|
2013-03-13 17:47:23 +01:00
|
|
|
copy ${1} ${2}${1##$CURRENT_SRC}
|
2013-02-22 17:35:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
image(){
|
|
|
|
echo Image file ${1}
|
2013-03-13 17:47:23 +01:00
|
|
|
copy ${1} ${2}${1##$CURRENT_SRC}
|
2013-02-22 17:35:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
video(){
|
|
|
|
echo Video file ${1}
|
2013-03-13 17:47:23 +01:00
|
|
|
copy ${1} ${2}${1##$CURRENT_SRC}
|
2013-02-22 17:35:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Random - Used
|
|
|
|
|
2013-03-12 19:08:46 +01:00
|
|
|
archive(){
|
2013-03-13 17:47:23 +01:00
|
|
|
echo Archive file ${1}
|
2013-03-15 09:27:26 +01:00
|
|
|
if [ ${ARCHIVE_BOMB} -eq 0 ]; then
|
|
|
|
temp_extract_dir=${2}_temp
|
2013-05-16 11:19:16 +02:00
|
|
|
mkdir -p "${temp_extract_dir}"
|
2015-03-09 18:37:16 +01:00
|
|
|
${UNPACKER} -p1 x "${1}" -o"${temp_extract_dir}" -bd
|
2013-03-15 09:27:26 +01:00
|
|
|
main ${2} ${RECURSIVE_ARCHIVE_CURRENT} ${temp_extract_dir} || true
|
2013-05-16 11:19:16 +02:00
|
|
|
rm -rf "${temp_extract_dir}"
|
2013-03-15 09:27:26 +01:00
|
|
|
fi
|
|
|
|
if [ ${ARCHIVE_BOMB} -eq 1 ]; then
|
2013-05-16 11:19:16 +02:00
|
|
|
rm -rf "${2}"
|
|
|
|
rm -rf "${2}_temp"
|
2013-03-15 09:27:26 +01:00
|
|
|
fi
|
2014-07-30 21:49:00 +02:00
|
|
|
CURRENT_SRC="/media/${SRC}"
|
2013-03-12 19:08:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-22 17:35:23 +01:00
|
|
|
application(){
|
|
|
|
echo App file ${1}
|
2013-02-25 01:27:15 +01:00
|
|
|
src_file=${1}
|
2013-03-13 17:47:23 +01:00
|
|
|
dst_file=${2}${1##$CURRENT_SRC}
|
2013-02-25 01:27:15 +01:00
|
|
|
mime_details=${3}
|
|
|
|
case ${mime_details} in
|
2013-03-11 00:23:11 +01:00
|
|
|
pdf)
|
2013-02-25 01:27:15 +01:00
|
|
|
echo "Got a pdf"
|
2013-05-16 11:19:16 +02:00
|
|
|
${PDF} --dest-dir "${2}" "${src_file}"
|
2013-03-11 00:23:11 +01:00
|
|
|
;;
|
2013-03-11 18:56:04 +01:00
|
|
|
msword|vnd.openxmlformats-officedocument.*|vnd.ms-*|vnd.oasis.opendocument*)
|
|
|
|
# https://blogs.msdn.com/b/vsofficedeveloper/archive/2008/05/08/office-2007-open-xml-mime-types.aspx
|
|
|
|
# http://plan-b-for-openoffice.org/glossary/term/mime-type
|
2013-03-11 00:23:11 +01:00
|
|
|
echo "MS Office or ODF document"
|
2013-12-09 14:47:03 +01:00
|
|
|
office_n_txt ${src_file} ${2}
|
2013-02-25 01:27:15 +01:00
|
|
|
;;
|
|
|
|
*xml*)
|
|
|
|
echo "Got an XML"
|
2013-12-09 14:47:03 +01:00
|
|
|
office_n_txt ${src_file} ${2}
|
2013-02-25 01:27:15 +01:00
|
|
|
;;
|
2013-03-11 00:23:11 +01:00
|
|
|
x-dosexec)
|
|
|
|
echo "Win executable"
|
2013-06-05 14:09:39 +02:00
|
|
|
copy ${src_file} ${2}/DANGEROUS_${1##$CURRENT_SRC/}_DANGEROUS
|
2013-03-11 00:23:11 +01:00
|
|
|
;;
|
2013-06-04 18:16:40 +02:00
|
|
|
zip|x-rar|x-bzip2|x-lzip|x-lzma|x-lzop|x-xz|x-compress|x-gzip|x-tar|*compressed)
|
2013-03-13 17:47:23 +01:00
|
|
|
echo "Compressed file"
|
|
|
|
archive ${src_file} ${dst_file}
|
|
|
|
;;
|
2013-03-11 00:23:11 +01:00
|
|
|
octet-stream)
|
2013-02-25 01:27:15 +01:00
|
|
|
echo "Unknown type."
|
2013-03-11 00:23:11 +01:00
|
|
|
copy ${src_file} ${dst_file}.bin
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Unhandled type"
|
|
|
|
copy ${src_file} ${dst_file}
|
2013-02-25 01:27:15 +01:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2013-02-22 17:35:23 +01:00
|
|
|
}
|
|
|
|
|
2013-02-25 01:27:15 +01:00
|
|
|
# Random - Unused?
|
|
|
|
## WARNING: They are assumed safe.
|
2013-02-22 17:35:23 +01:00
|
|
|
|
|
|
|
example(){
|
|
|
|
echo Example file ${1}
|
2013-03-13 17:47:23 +01:00
|
|
|
copy ${1} ${2}${1##$CURRENT_SRC}
|
2013-02-22 17:35:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
message(){
|
|
|
|
echo Message file ${1}
|
2013-03-13 17:47:23 +01:00
|
|
|
copy ${1} ${2}${1##$CURRENT_SRC}
|
2013-02-22 17:35:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
model(){
|
|
|
|
echo Model file ${1}
|
2013-03-13 17:47:23 +01:00
|
|
|
copy ${1} ${2}${1##$CURRENT_SRC}
|
2013-02-22 17:35:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
multipart(){
|
|
|
|
echo Multipart file ${1}
|
2013-03-13 17:47:23 +01:00
|
|
|
copy ${1} ${2}${1##$CURRENT_SRC}
|
2013-02-22 17:35:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
main(){
|
|
|
|
if [ -z ${1} ]; then
|
|
|
|
echo "Please specify the destination directory."
|
|
|
|
exit
|
|
|
|
fi
|
2013-10-29 15:48:21 +01:00
|
|
|
set -e
|
|
|
|
set -x
|
2013-02-22 17:35:23 +01:00
|
|
|
|
2013-03-13 17:47:23 +01:00
|
|
|
if [ -z ${2} ]; then
|
2014-07-30 21:49:00 +02:00
|
|
|
CURRENT_SRC="/media/${SRC}"
|
2013-03-13 17:47:23 +01:00
|
|
|
RECURSIVE_ARCHIVE_CURRENT=0
|
2013-03-15 09:27:26 +01:00
|
|
|
ARCHIVE_BOMB=0
|
2013-03-13 17:47:23 +01:00
|
|
|
else
|
|
|
|
RECURSIVE_ARCHIVE_CURRENT=${2}
|
|
|
|
CURRENT_SRC=${3}
|
2013-03-12 19:08:46 +01:00
|
|
|
if [ ${RECURSIVE_ARCHIVE_CURRENT} -gt ${RECURSIVE_ARCHIVE_MAX} ]; then
|
|
|
|
echo Archive bomb.
|
2013-03-15 09:27:26 +01:00
|
|
|
ARCHIVE_BOMB=1
|
2013-06-09 19:37:41 +02:00
|
|
|
echo "ARCHIVE BOMB." >> ${LOGFILE}
|
|
|
|
echo "The content of the archive contains recursively other archives." >> ${LOGFILE}
|
|
|
|
echo "This is a bad sign so the archive is not extracted to the destination key." >> ${LOGFILE}
|
2013-03-13 17:47:23 +01:00
|
|
|
return
|
|
|
|
else
|
|
|
|
RECURSIVE_ARCHIVE_CURRENT=`expr ${RECURSIVE_ARCHIVE_CURRENT} + 1`
|
2013-03-12 19:08:46 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2013-03-13 17:47:23 +01:00
|
|
|
FILE_LIST=`find ${CURRENT_SRC} -type f`
|
2013-03-29 02:42:01 +01:00
|
|
|
SAVEIFS=$IFS
|
|
|
|
IFS=$(echo -en "\n\b")
|
2013-02-22 17:35:23 +01:00
|
|
|
for file in ${FILE_LIST}; do
|
2013-03-15 09:27:26 +01:00
|
|
|
# first param is the destination dir
|
|
|
|
dest=${1}
|
|
|
|
|
2013-05-16 11:19:16 +02:00
|
|
|
mime=`file -b --mime-type "${file}"`
|
2013-02-25 01:27:15 +01:00
|
|
|
echo ${mime}
|
2013-03-15 09:27:26 +01:00
|
|
|
main_mime=`echo ${mime} | cut -f1 -d/`
|
2013-02-22 17:35:23 +01:00
|
|
|
details=`echo ${mime} | cut -f2 -d/`
|
2013-06-09 19:37:41 +02:00
|
|
|
echo -n "Processing ${file} (${mime})... " >> ${LOGFILE}
|
2013-03-15 09:27:26 +01:00
|
|
|
case "${main_mime}" in
|
2013-02-22 17:35:23 +01:00
|
|
|
"text")
|
2013-10-29 15:48:21 +01:00
|
|
|
text ${file} ${dest} || error_handler
|
2013-02-25 01:27:15 +01:00
|
|
|
;;
|
|
|
|
"audio")
|
2013-10-29 15:48:21 +01:00
|
|
|
audio ${file} ${dest} || error_handler
|
2013-02-25 01:27:15 +01:00
|
|
|
;;
|
|
|
|
"image")
|
2013-10-29 15:48:21 +01:00
|
|
|
image ${file} ${dest} || error_handler
|
2013-02-25 01:27:15 +01:00
|
|
|
;;
|
|
|
|
"video")
|
2013-10-29 15:48:21 +01:00
|
|
|
video ${file} ${dest} || error_handler
|
2013-02-25 01:27:15 +01:00
|
|
|
;;
|
|
|
|
"application")
|
2013-10-29 15:48:21 +01:00
|
|
|
application ${file} ${dest} ${details} || error_handler
|
2013-02-25 01:27:15 +01:00
|
|
|
;;
|
|
|
|
"example")
|
2013-10-29 15:48:21 +01:00
|
|
|
example ${file} ${dest} || error_handler
|
2013-02-25 01:27:15 +01:00
|
|
|
;;
|
|
|
|
"message")
|
2013-10-29 15:48:21 +01:00
|
|
|
message ${file} ${dest} || error_handler
|
2013-02-25 01:27:15 +01:00
|
|
|
;;
|
|
|
|
"model")
|
2013-10-29 15:48:21 +01:00
|
|
|
model ${file} ${dest} || error_handler
|
2013-02-25 01:27:15 +01:00
|
|
|
;;
|
|
|
|
"multipart")
|
2013-10-29 15:48:21 +01:00
|
|
|
multipart ${file} ${dest} || error_handler
|
2013-02-22 17:35:23 +01:00
|
|
|
;;
|
|
|
|
*)
|
2013-02-25 01:27:15 +01:00
|
|
|
echo "This should never happen... :]"
|
2013-03-15 09:27:26 +01:00
|
|
|
echo $mime $main_mime $details
|
2013-02-22 17:35:23 +01:00
|
|
|
;;
|
|
|
|
esac
|
2013-06-09 19:37:41 +02:00
|
|
|
echo "done." >> ${LOGFILE}
|
2013-02-22 17:35:23 +01:00
|
|
|
done
|
2013-03-29 02:42:01 +01:00
|
|
|
IFS=$SAVEIFS
|
2013-10-29 15:48:21 +01:00
|
|
|
return 0
|
2013-02-22 17:35:23 +01:00
|
|
|
}
|
|
|
|
|
2013-10-28 22:48:50 +01:00
|
|
|
|