33# It is vital to have RUN env > /etc/environment in Dockerfile so that
44# cronjob can have access to env variables from docker
55. /home/hackmd/cron.env
6+ UPLOADS_DIR=" /home/hackmd/app/public/uploads"
7+ TRASH_DIR=" /home/hackmd/trash"
68isActive () {
79 if ! ldapres=$( ldapsearch -x -H $CMD_LDAP_URL -D " $CMD_LDAP_BINDDN " -w $CMD_LDAP_BINDCREDENTIALS -b $CMD_LDAP_SEARCHBASE $query 2> /dev/null) ; then
810 echo " Ldap is DOWN. Exiting..."
@@ -22,20 +24,48 @@ DELETE FROM "Users" WHERE profileid = '${profileid}';
2224SQL
2325}
2426deleteNotes (){
25- # Deleting all user's notes from postgres database
26- local id=" $1 "
27+ # Deleting all user's notes from postgres database and moving them to
28+ # trash can
29+ shortids=$( psql $CMD_DB_URL -At<< -SQL
30+ SELECT shortid FROM "Notes" where "ownerId" = '${id} ';
31+ SQL
32+ )
33+ while IFS=' ' read shortid; do
34+ title=$( psql $CMD_DB_URL -At<< -SQL
35+ SELECT content FROM "Notes" where shortid = '${shortid} ';
36+ SQL
37+ )
38+ safe_title=$( echo " $title " | tr -cd ' [:alnum:]._-' )
39+ filename=" ${USER_TRASH_DIR} /${safe_title} .md"
40+
41+ content=$( psql $CMD_DB_URL -At<< -SQL
42+ SELECT content FROM "Notes" WHERE shortid = '${shortid} ';
43+ SQL
44+ )
45+
46+ echo " $content " > " $filename "
47+ done<<< " $shortids"
48+
2749psql $CMD_DB_URL << -SQL
2850DELETE FROM "Notes" where "ownerId" = '${id} ';
2951SQL
3052}
3153deleteFiles (){
3254 # Deleting all user's uploads from location /home/hackmd/app/public/uploads
33- local id=" $1 "
3455 # The files are saved as upload_{user.id}_{random letters}.{jpg,png,...}
35- find /home/hackmd/app/public/uploads -type f -name " upload_${id} _*" -delete; 2>&1
56+ find $UPLOADS_DIR -type f -name " upload_${id} _*" -exec mv {} " $USER_TRASH_DIR " \; 2>&1
57+ }
58+ actuallyDeleteFiles (){
59+ # Deleting all user's uploads from location /home/hackmd/app/public/uploads
60+ # The files are saved as upload_{user.id}_{random letters}.{jpg,png,...}
61+
62+ echo " Removing old trash files"
63+ find $TRASH_DIR -mindepth 1 -maxdepth 1 -type d -mtime +90 -exec rm -rf {} \; 2>&1
3664}
3765# Write down the date-time
3866date
67+ # Removing old trash files
68+ actuallyDeleteFiles
3969allusers=$( psql $CMD_DB_URL -t -c ' SELECT profileid FROM "Users";' )
4070if [ -z " $allusers " ]; then
4171 echo " Ha Ha nothing to delete"
5585 echo " The account $uid is active"
5686 else
5787 echo " The account $uid must be destroyed"
58- echo " Beggining destruction..."
88+ # This will be the user specific trash can
89+ USER_TRASH_DIR=${TRASH_DIR} /${uid}
90+ echo " Moving to Trash ${USER_TRASH_DIR} ..."
91+ mkdir -p $USER_TRASH_DIR
92+ # These functions move uploads and notes to trash can
93+ # and delete them permanently after 90 days
5994 deleteUser $username
60- deleteNotes $id
61- deleteFiles $id
95+ deleteNotes
96+ deleteFiles
97+ actuallyDeleteFiles
6298
6399 fi
64100done <<< " $allusers"
0 commit comments