From fc9a0fdeaef6920c3f7f0a6c1dd5cca40811f8d1 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 31 Aug 2020 11:55:49 +0200 Subject: "hard" option for media_find_missing_files command --- ishtar_common/utils.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'ishtar_common/utils.py') diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 6183cdc49..2eacce013 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -1717,12 +1717,13 @@ def remove_empty_dirs(path=None): return False -def try_fix_file(filename, make_copy=True): +def try_fix_file(filename, make_copy=True, hard=False): """ Try to find a file with a similar name on the same dir. :param filename: filename (full path) :param make_copy: make the copy of the similar file found + :param hard: search on the whole media dir :return: name of the similar file found or None """ path, current_name, simplified_ref_name = simplify_name( @@ -1733,8 +1734,8 @@ def try_fix_file(filename, make_copy=True): except UnicodeDecodeError: dirs = list(sorted(os.listdir(path.encode('utf-8')))) # check existing files in the path - for file in dirs: - full_file = os.sep.join([path, file]) + for f in dirs: + full_file = os.sep.join([path, f]) if not os.path.isfile(full_file): # must be a file continue _, _, name = simplify_name(full_file, check_existing=False) @@ -1742,4 +1743,17 @@ def try_fix_file(filename, make_copy=True): # a candidate is found if make_copy: shutil.copy2(full_file, filename) - return file + return f + if not hard: + return + for root, __, files in os.walk(settings.MEDIA_ROOT): + for f in files: + full_file = os.sep.join([root, f]) + if not os.path.isfile(full_file): # must be a file + continue + _, _, name = simplify_name(full_file, check_existing=False) + if simplified_ref_name.lower() == name.lower(): + # a candidate is found + if make_copy: + shutil.copy2(full_file, filename) + return f -- cgit v1.2.3