Clean up and document a little

This commit is contained in:
Sebastian Spaeth 2022-11-08 18:27:40 +01:00
parent 2497e71c3b
commit c8588634b3

View File

@ -3,24 +3,23 @@
#TODO: Sanity checks: Are files on the file system that the db does not know about?
#TODO: Sanity checks: Are there thumbnails in the db that do not have corresponding media file entries?
# mediaapi_media_repository: media_id | media_origin | content_type | file_size_bytes | creation_ts | upload_name | base64hash | user_id
# mediaapi_thumbnail: media_id | media_origin | content_type | file_size_bytes | creation_ts | width | height | resize_method
from datetime import datetime, timedelta
from pathlib import Path
import argparse
import logging
import typing
import argparse, logging, typing
try:
import psycopg2
except ImportError as e:
print("Please install psycopg2")
exit(1)
try:
import yaml
except ImportError as e:
print("Please install pyyaml / python3-yaml")
print("Please install psycopg2 and pyyaml")
exit(1)
def read_config(conf_file):
"""Read in the dendrite config file and return db creds and media path"""
try:
with open(conf_file) as f:
config = yaml.safe_load(f)
@ -51,9 +50,11 @@ def read_config(conf_file):
return (BASE_PATH, CONN_STR)
#------------------------------------------------------------------------------------
class File:
def __init__(self, media_repo, media_id, creation_ts, base64hash: str):
"""Represents a file in our database together with (hopefully) a physical file and thumbnails"""
def __init__(self, media_repo: 'MediaRepository', media_id: str, creation_ts: int, base64hash: str):
# The MediaRepository in which this file is recorded
self.repo = media_repo
self.media_id = media_id
@ -73,9 +74,10 @@ class File:
if self.fullpath() is None:
logging.info(f"No known path for file id '{self.media_id}', cannot delete file.")
elif not self.fullpath().is_dir():
logging.info(f"No known path for file id '{self.media_id}', cannot delete.")
logging.info(f"Path for file id '{self.media_id}' exists but is not directory, ignoring instead of deleting.")
else:
for file in self.fullpath().glob('*'):
# note: this does not handle directories in fullpath()
file.unlink()
self.fullpath().rmdir()
logging.debug(f"Deleted directory {self.fullpath()}")
@ -96,11 +98,11 @@ class File:
def has_thumbnail(self):
with self.repo.conn.cursor() as cur:
# media_id | media_origin | content_type | file_size_bytes | creation_ts | upload_name | base64hash | user_id
cur.execute(f"select COUNT(media_id) from mediaapi_thumbnail WHERE media_id='{self.media_id}';")
row = cur.fetchone()
return(row[0])
#----------------------------------------------------------------------
class MediaRepository:
def __init__(self, media_path: Path, connection_string: str):
@ -117,8 +119,7 @@ class MediaRepository:
self.connect_db();
def connect_db(self):
#postgresql://user:pass@hostname/database?params
# postgres://dendrite:dendrite@localhost/dendrite?
#postgresql://user:pass@localhost/database?params
if self.db_conn_string is None or not self.db_conn_string.startswith("postgres://"):
errstr = "DB connection not a postgres one"
logging.error(errstr)
@ -126,20 +127,15 @@ class MediaRepository:
self.conn = psycopg2.connect(self.db_conn_string)
def get_remote_media(self):
cur = self.conn.cursor()
# media_id | media_origin | content_type | file_size_bytes | creation_ts | upload_name | base64hash | user_id
res = cur.execute("select media_id, creation_ts, base64hash from mediaapi_media_repository WHERE user_id = '';")
#select * from mediaapi_media_repository WHERE user_id = '';
files = []
for row in cur.fetchall():
# creation_ts is ms since the epoch, so we need to make sec out of it
f = File(self, row[0], row[1]//1000, row[2])
files.append(f)
cur.close()
with self.conn.cursor() as cur:
# media_id | media_origin | content_type | file_size_bytes | creation_ts | upload_name | base64hash | user_id
res = cur.execute("select media_id, creation_ts, base64hash from mediaapi_media_repository WHERE user_id = '';")
files = []
for row in cur.fetchall():
# creation_ts is ms since the epoch, so convert to seconds
f = File(self, row[0], row[1]//1000, row[2])
files.append(f)
return files
# mediaapi_thumbnail:
# media_id | media_origin | content_type | file_size_bytes | creation_ts | width | height | resize_method
def parse_options():
loglevel=logging.INFO # default