diff options
Diffstat (limited to 'ishtar_common/utils.py')
-rw-r--r-- | ishtar_common/utils.py | 56 |
1 files changed, 34 insertions, 22 deletions
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index a1a81cc3e..a3bc23e21 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -1499,12 +1499,13 @@ MEDIA_RE = [ ] -def simplify_name(full_path_name, check_existing=False): +def simplify_name(full_path_name, check_existing=False, min_len=15): """ Simplify a file name by removing auto save suffixes :param full_path_name: full path name :param check_existing: prevent to give name of an existing file + :param min_len: minimum lenght of the base filename :return: """ name_exp = full_path_name.split(os.sep) @@ -1517,7 +1518,7 @@ def simplify_name(full_path_name, check_existing=False): name = ".".join(names[0:-1]) ext = "." + names[-1] - while "_" in name and len(name) > 15: + while "_" in name and len(name) > min_len: oldname = name[:] for regex in MEDIA_RE: match = None @@ -1717,6 +1718,26 @@ def remove_empty_dirs(path=None): return False +def _try_copy(path, f, dest, simplified_ref_name, make_copy, min_len): + full_file = os.path.abspath(os.sep.join([path, f])) + # must be a file + if not os.path.isfile(full_file) or full_file == dest: + return + _, _, name = simplify_name(full_file, check_existing=False, min_len=min_len) + if simplified_ref_name.lower() == name.lower(): + # a candidate is found + if make_copy: + try: + os.remove(dest) + except OSError: + pass + try: + shutil.copy2(full_file, dest) + except OSError: + return + return f + + def try_fix_file(filename, make_copy=True, hard=False): """ Try to find a file with a similar name on the same dir. @@ -1726,8 +1747,9 @@ def try_fix_file(filename, make_copy=True, hard=False): :param hard: search on the whole media dir :return: name of the similar file found or None """ + filename = os.path.abspath(filename) path, current_name, simplified_ref_name = simplify_name( - filename, check_existing=False) + filename, check_existing=False, min_len=10) try: dirs = list(sorted(os.listdir(path))) @@ -1735,28 +1757,18 @@ def try_fix_file(filename, make_copy=True, hard=False): dirs = list(sorted(os.listdir(path.encode('utf-8')))) # check existing files in the path 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) - if simplified_ref_name.lower() == name.lower(): - # a candidate is found - if make_copy: - shutil.copy2(full_file, filename) - return f + result = _try_copy(path, f, filename, simplified_ref_name, make_copy, + min_len=10) + if result: + return result if not hard: return - for root, __, files in os.walk(settings.MEDIA_ROOT): + for path, __, 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 + result = _try_copy(path, f, filename, simplified_ref_name, + make_copy, min_len=15) # 15 - less agressive + if result: + return result def get_current_profile(force=False): |