Skip to content

Instantly share code, notes, and snippets.

@rhysrhaven
Created May 20, 2015 01:13
Show Gist options
  • Save rhysrhaven/8793a74e8a31ad416f9b to your computer and use it in GitHub Desktop.
Save rhysrhaven/8793a74e8a31ad416f9b to your computer and use it in GitHub Desktop.
Teachers inbox sharing mechanism
#!/bin/bash -eu
# This monitors the incoming_dir (directories named after their users)
# Any zip files uploaded to said dirs will be shared in the shared_dir
# under a random directory, and the link emailed to the users.
# Requires dma, but you can replace that with other MTAs.
incoming_dir='/home/share'
shared_dir='/var/www/drop'
share_server='share.rhavenindustrys.com/drop'
webkey_size='64'
email_domain='rhavenindustrys.com'
filetypes="
-iname *.tar -o
-iname *.tgz -o
-iname *.tar.gz -o
-iname *.gz -o
-iname *.tbz2 -o
-iname *.zip -o
-iname *.7z -o
-iname *.iso
"
webkey=''
function rand_string {
charpool=('a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z' \
'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' 'X' 'Y' 'Z' \
'0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '0' '-' )
charpool_length=${#charpool[*]}
if [ $# -lt 1 ]; then num=${webkey_size}; else num=${1}; fi
for c in $(seq $num); do
echo -n ${charpool[$((${RANDOM} % ${charpool_length}))]}
done
echo ""
}
function send_mail {
echo "\
To: "${2}@${email_domain}"
Subject: New ShareMon Link Created
Hello,
A new file has been uploaded. You can access this file for 24 hours at the following link:
https://${share_server}/${1}
Please be aware, anyone who has this link will be able to access this file. If the contents are sensitive, please consider encrypting the zip file before you upload it, and sharing the encryption key by a separate method, like phone, fax, or encrypted email.
Thanks,
The IT Script Gremlin
" | dma -t -f "sharemon@${email_domain}"
}
for user_dir in $(ls -1 ${incoming_dir}); do
# Check if there are files in the directory
if [ $(find "${incoming_dir}/${user_dir}" -type f ${filetypes}| wc -l) -gt 0 ]; then
IFS=$'\n'
for file in $(IFS=$' \t\n';find "${incoming_dir}/${user_dir}" -type f ${filetypes}); do
# Check if file has not been modified in 60 seconds
if [ $(($(stat -c %Y "${file}") + 55)) -lt $(date +%s) ]; then
webkey="$(rand_string)"
new_file="$(basename "${file}" | sed 's/\ /_/g')"
mkdir "${shared_dir}/${webkey}"
chmod a+r "${file}"
mv "${file}" "${shared_dir}/${webkey}/${new_file}"
send_mail "${webkey}/${new_file}" "${user_dir}"
fi
done
fi
done
# Delete files older than 24 Hours
if [ $(ls -1 ${shared_dir} | wc -l) -gt 0 ]; then
find ${shared_dir}/ -mindepth 1 -type d -ctime +1 -exec rm -rf {} \;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment