mirror of
https://github.com/Dineshkarthik/telegram_media_downloader.git
synced 2025-03-01 00:23:46 +08:00
ADD: download file management utils
This commit is contained in:
parent
b60a147ee0
commit
0d28704910
2 changed files with 39 additions and 0 deletions
0
utils/__init__.py
Normal file
0
utils/__init__.py
Normal file
39
utils/file_management.py
Normal file
39
utils/file_management.py
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
import glob
|
||||||
|
import os
|
||||||
|
import pathlib
|
||||||
|
from hashlib import md5
|
||||||
|
|
||||||
|
|
||||||
|
def get_next_name(file_path: str) -> str:
|
||||||
|
posix_path = pathlib.Path(file_path)
|
||||||
|
counter: int = 1
|
||||||
|
new_file_name: str = "{0}/{1}-copy{2}{3}"
|
||||||
|
while os.path.isfile(
|
||||||
|
new_file_name.format(
|
||||||
|
posix_path.parent,
|
||||||
|
posix_path.stem,
|
||||||
|
counter,
|
||||||
|
"".join(posix_path.suffixes),
|
||||||
|
)
|
||||||
|
):
|
||||||
|
counter += 1
|
||||||
|
return new_file_name.format(
|
||||||
|
posix_path.parent,
|
||||||
|
posix_path.stem,
|
||||||
|
counter,
|
||||||
|
"".join(posix_path.suffixes),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def manage_duplicate_file(file_path: str):
|
||||||
|
posix_path = pathlib.Path(file_path)
|
||||||
|
file_base_name: str = "".join(posix_path.stem.split("-copy")[0:-1])
|
||||||
|
name_pattern: str = f"{posix_path.parent}/{file_base_name}*"
|
||||||
|
old_files: list = glob.glob(name_pattern)
|
||||||
|
old_files.remove(file_path)
|
||||||
|
current_file_md5: str = md5(open(file_path, "rb").read()).hexdigest()
|
||||||
|
for old_file_path in old_files:
|
||||||
|
old_file_md5: str = md5(open(old_file_path, "rb").read()).hexdigest()
|
||||||
|
if current_file_md5 == old_file_md5:
|
||||||
|
os.remove(file_path)
|
||||||
|
break
|
Loading…
Reference in a new issue