#!/bin/bash
# Nautilus script to move selected media files from phone, camera, etc to correct location on shared location of server
# Destination to move media to
DEST="$HOME/Share"
# Check if 'Share' folder is mounted
if [[ -z $(ls $DEST) ]]
then
zenity --error --text="Share folder does not appear to be mounted!\n\nGet junior to fix it."
exit 1
fi
# Function to handle jpegs
function handle_jpeg {
local DATE_TIME=`identify -ping -format "%[EXIF:DateTimeOriginal]" $1 2>/dev/null`
# If returned value
if [[ -n $DATE_TIME ]]
then
local DATE=`echo $DATE_TIME | cut -f1 -d\ `
echo "Photos $(echo $DATE | tr ':' ' ')"
fi
}
# Function to handle mp4s
function handle_mp4 {
local CREATION_TIME=`ffprobe -v quiet $1 -print_format csv -show_entries format_tags=creation_time | cut -f2 -d,`
if [[ -n $CREATION_TIME ]]
then
local DATE=`echo $CREATION_TIME | cut -f1 -dT`
echo "HomeVideos $(echo $DATE | tr '-' ' ')"
fi
}
# mkdestdir makes a directory for a media item according to its type, year, month and day
function mkdestdir {
local DIR="$DEST/$1/$2/$3/$4"
mkdir -p $DIR
echo $DIR
}
echo 'Will now process each selected file'
FILES=(${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS//;/ })
FILES_LEN=${#FILES[@]}
MOVED=0
{
for (( i=0;i<FILES_LEN;i++ ))
do
file=${FILES[${i}]}
mime=`file -b --mime-type $file`
echo $[ $i*100/$FILES_LEN ]
echo "#Moving $file ($[ $i+1 ]/$FILES_LEN)"
# Check for supported mimes
# Each supported one should set a function that 'returns' the correct typ, year, month and day
case "$mime" in
inode/directory)
echo "#$file is a directory"
continue;;
image/jpeg) func="handle_jpeg";;
video/mp4) func="handle_mp4";;
esac
# Call the function
read typ year month day < <( $func $file )
if [[ -n $typ && -n $year && -n $month && -n $day ]]
then
# Move picture using update mode
mv -u $file $(mkdestdir $typ $year $month $day)
((MOVED++))
fi
done
MES="Done sorting and moving media"
if [[ $MOVED -lt $FILES_LEN ]]
then
MES="$MES.\n$[ $FILES_LEN-$MOVED ] items where not recognized."
fi
zenity --notification --window-icon="info" --text="$MES"
} |
zenity --progress --title="Sorting and moving media" --percentage=0 --auto-close