Delete post attachements of deleted mattermost posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# | |
# Execute via ctrontab: | |
# */10 * * * * mattermost /opt/mattermost/mmdel.py | |
import psycopg2 | |
import os | |
datadir = "/opt/mattermost/data" | |
# Try to connect | |
try: | |
conn=psycopg2.connect(database="mattermost", user="mattermost", password="PASSWORD", host="localhost") | |
except Exception as e: | |
print ("I am unable to connect to the database." + str(e)) | |
cur = conn.cursor() | |
try: | |
cur.execute("""SELECT path, posts.deleteat | |
FROM fileinfo | |
INNER JOIN posts ON fileinfo.postid=posts.id | |
WHERE posts.deleteat > 0;""") | |
except Exception as e: | |
print ("Select failed" + str(e)) | |
rows = cur.fetchall() | |
for row in rows: | |
fileName = os.path.join(datadir, row[0]) | |
if(os.path.isfile(fileName)): | |
print ("deleting ", fileName) | |
os.remove(fileName) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment