diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2020-08-31 11:55:49 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2020-08-31 11:55:49 +0200 |
commit | 8066ede11f3843006d6029adb2436c74058be9f8 (patch) | |
tree | 1e38ca9770d04c9465a0c5cd8077ef9da6226863 /ishtar_common/utils.py | |
parent | 0e64d5cadef1d7b0ff4f0b105b379723a22f22f6 (diff) | |
download | Ishtar-8066ede11f3843006d6029adb2436c74058be9f8.tar.bz2 Ishtar-8066ede11f3843006d6029adb2436c74058be9f8.zip |
"hard" option for media_find_missing_files command
Diffstat (limited to 'ishtar_common/utils.py')
-rw-r--r-- | ishtar_common/utils.py | 22 |
1 files changed, 18 insertions, 4 deletions
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 |