copy sources from branch develop/0.1.0

This commit is contained in:
Thomas Peetz
2025-04-24 16:05:05 +02:00
committed by Thomas Peetz
parent cb0fa3f728
commit c222d4cd7a
18 changed files with 1512 additions and 231 deletions
+38 -38
View File
@@ -36,7 +36,7 @@ class FileStatus:
self.id = response['id']
def get_status_of_file(found_file: Path, cursor, logger) -> FileStatus:
def get_status_of_file(found_file: Path, cursor, log) -> FileStatus:
status = FileStatus()
try:
cursor.execute(f'SELECT id, cloud_link FROM media_file WHERE file_name="{found_file.name}"')
@@ -45,7 +45,7 @@ def get_status_of_file(found_file: Path, cursor, logger) -> FileStatus:
status.status_type = StatusType.FILE_NAME
status.id = rows[0][0]
except mariadb.Error as error:
logger.debug(f'select failed with {error}')
log.debug(f'select failed with {error}')
try:
cursor.execute(f'SELECT id FROM media_file WHERE id="{found_file.stem}"')
rows = cursor.fetchall()
@@ -55,9 +55,9 @@ def get_status_of_file(found_file: Path, cursor, logger) -> FileStatus:
if len(rows) > 1:
status.status_type = StatusType.DUPLICATE
for row in rows:
logger.info(f"found {row[0]} with {found_file}")
log.info(f"found {row[0]} with {found_file}")
except mariadb.Error as error:
logger.debug(f'select failed with {error}')
log.debug(f'select failed with {error}')
try:
cursor.execute(f'SELECT id FROM media_file WHERE cloud_link LIKE "%{found_file.stem}%"')
rows = cursor.fetchall()
@@ -68,75 +68,75 @@ def get_status_of_file(found_file: Path, cursor, logger) -> FileStatus:
else:
status.status_type = StatusType.CLOUD_LINK
except mariadb.Error as error:
logger.debug(f'select failed with {error}')
log.debug(f'select failed with {error}')
response = requests.get(f"http://127.0.0.1:8800/media/files/{found_file.stem}")
logger.debug(f"Status: {response.status_code}")
log.debug(f"Status: {response.status_code}")
if response.status_code == 200:
status.status_type = StatusType.FILE_ID
status.id = response.json()['id']
return status
def rename_files_to_id(media_dir, dry_run, conn, logger):
def rename_files_to_id(media_dir, dry_run, conn, log):
media_path = Path(media_dir)
cursor = conn.cursor()
for file in media_path.iterdir():
logger.debug('found file: {}'.format(file.name))
status: FileStatus = get_status_of_file(file, cursor, logger)
log.debug('found file: {}'.format(file.name))
status: FileStatus = get_status_of_file(file, cursor, log)
file_id = status.id
if not file_id:
logger.info(f"ID of file {file.name} is unknown")
log.info(f"ID of file {file.name} is unknown")
continue
new_file_path = file.with_name(f"{file_id}{file.suffix}")
match status.status_type:
case StatusType.FILE_NAME:
logger.info(f'status of {file.name} is file_name')
rename_file(file, new_file_path, dry_run, logger)
update_cloud_link(file_id, new_file_path, conn, dry_run, logger)
log.info(f'status of {file.name} is file_name')
rename_file(file, new_file_path, dry_run, log)
update_cloud_link(file_id, new_file_path, conn, dry_run, log)
case StatusType.FILE_ID:
logger.info(f'status of {file.name} is file_id')
update_cloud_link(file_id, new_file_path, conn, dry_run, logger)
log.info(f'status of {file.name} is file_id')
update_cloud_link(file_id, new_file_path, conn, dry_run, log)
case StatusType.CLOUD_LINK:
logger.info(f'status of {file.name} is cloud_link')
rename_file(file, new_file_path, dry_run, logger)
update_cloud_link(file_id, new_file_path, conn, dry_run, logger)
log.info(f'status of {file.name} is cloud_link')
rename_file(file, new_file_path, dry_run, log)
update_cloud_link(file_id, new_file_path, conn, dry_run, log)
case StatusType.CLOUD_LINK_ID:
logger.debug(f'status of {file.name} is cloud_link_id')
update_cloud_link(file_id, new_file_path, conn, dry_run, logger)
log.debug(f'status of {file.name} is cloud_link_id')
update_cloud_link(file_id, new_file_path, conn, dry_run, log)
case StatusType.DUPLICATE:
logger.info(f'status of {file.name} is duplicate')
log.info(f'status of {file.name} is duplicate')
case StatusType.UNKNOWN:
logger.info(f'status of {file.name} is unknown')
log.info(f'status of {file.name} is unknown')
def rename_file(current_file, new_file_path, dry_run, logger):
def rename_file(current_file, new_file_path, dry_run, log):
if dry_run:
logger.info('rename file {} to {}'.format(current_file.name, new_file_path.name))
log.info('rename file {} to {}'.format(current_file.name, new_file_path.name))
else:
current_file.rename(Path(new_file_path))
def update_cloud_link(file_id, file_path, conn, dry_run, logger):
def update_cloud_link(file_id, file_path, conn, dry_run, log):
cursor = conn.cursor()
logger.debug(f'update entry {file_id} with {file_path.absolute()}')
log.debug(f'update entry {file_id} with {file_path.absolute()}')
if dry_run:
logger.debug(f'UPDATE media_file: cloud_link={file_path.absolute()}')
log.debug(f'UPDATE media_file: cloud_link={file_path.absolute()}')
else:
cursor.execute('UPDATE media_file SET cloud_link="{}" WHERE id="{}"'.format(file_path.absolute(), file_id))
conn.commit()
def reset_cloud_link(conn, dry_run, logger):
def reset_cloud_link(conn, dry_run, log):
cursor = conn.cursor()
if dry_run:
logger.info('UPDATE media_file SET cloud_link=""')
log.info('UPDATE media_file SET cloud_link=""')
else:
cursor.execute('UPDATE media_file SET cloud_link="" WHERE id is NOT NULL')
conn.commit()
def check_file_with_db(json_file: Path, conn, logger):
logger.info(f"read json file: {json_file}")
cursor = conn.cursor()
with open(json_file, 'r') as json_file:
def check_file_with_db(data_file: Path, m_conn, log):
log.info(f"read json file: {data_file}")
cursor = m_conn.cursor()
with open(data_file, 'r') as json_file:
json_load = json.load(json_file)
for table in json_load:
logger.info(f"{table}: {len(json_load[table])}")
log.info(f"{table}: {len(json_load[table])}")
items = json_load[table]
for item in items:
item_id = item['id']
@@ -144,11 +144,11 @@ def check_file_with_db(json_file: Path, conn, logger):
cursor.execute(select_statement)
rows = cursor.fetchall()
count = len(rows)
logger.info(f"{count} entries found for {item_id}")
log.info(f"{count} entries found for {item_id}")
if count == 0:
logger.info(f"entry for {item_id} not found")
log.info(f"entry for {item_id} not found")
if count == 1:
logger.info(f"check entry {item_id}")
log.info(f"check entry {item_id}")
#log.info(f"entry {rows[0]}")
columns = []
values = []
@@ -156,7 +156,7 @@ def check_file_with_db(json_file: Path, conn, logger):
columns.append(key)
values.append(value)
for index, _ in enumerate(columns):
logger.info(f"compare {values[index]} with {rows[0][index]}")
log.info(f"compare {values[index]} with {rows[0][index]}")