refactor: rename RANDOM_FILE_PICKER_MEME_FOLDER to RANDOM_FILE_PICKER_MOVE_FOLDER
This commit is contained in:
+14
-14
@@ -6,7 +6,7 @@ import pathlib
|
||||
import requests
|
||||
|
||||
WEBHOOK_URL = os.environ.get("RANDOM_FILE_PICKER_WEBHOOK_URL", None)
|
||||
MEME_FOLDER = os.environ.get("RANDOM_FILE_PICKER_MEME_FOLDER", None)
|
||||
MOVE_FOLDER = os.environ.get("RANDOM_FILE_PICKER_MOVE_FOLDER", None)
|
||||
|
||||
SUPPORTED_EXTENSIONS = [
|
||||
".jpg", ".jpeg",
|
||||
@@ -83,31 +83,31 @@ def upload_file(file_path: pathlib.Path) -> None:
|
||||
response = requests.post(WEBHOOK_URL, files=files, data={}, timeout=60*5)
|
||||
response.raise_for_status() # Raise HTTPError for bad responses
|
||||
|
||||
def move_file_to_meme_folder(file_path: pathlib.Path) -> None:
|
||||
def move_file_to_move_folder(file_path: pathlib.Path) -> None:
|
||||
"""
|
||||
Move the specified file to the meme folder defined by the RANDOM_FILE_PICKER_MEME_FOLDER environment variable.
|
||||
Move the specified file to the move folder defined by the RANDOM_FILE_PICKER_MOVE_FOLDER environment variable.
|
||||
|
||||
:param file_path: The path of the file to be moved.
|
||||
"""
|
||||
if MEME_FOLDER is None:
|
||||
raise ValueError("RANDOM_FILE_PICKER_MEME_FOLDER environment variable is not set.")
|
||||
meme_dir_path = pathlib.Path(MEME_FOLDER)
|
||||
if MOVE_FOLDER is None:
|
||||
raise ValueError("RANDOM_FILE_PICKER_MOVE_FOLDER environment variable is not set.")
|
||||
move_dir_path = pathlib.Path(MOVE_FOLDER)
|
||||
|
||||
# Make sure the meme folder exists
|
||||
if not meme_dir_path.exists():
|
||||
raise RuntimeError(f"Meme folder '{meme_dir_path}' does not exist.")
|
||||
# Make sure the move folder exists
|
||||
if not move_dir_path.exists():
|
||||
raise RuntimeError(f"Move folder '{move_dir_path}' does not exist.")
|
||||
|
||||
# Make sure the file being moved is not already in the meme folder
|
||||
if file_path.parent.resolve() == meme_dir_path.resolve():
|
||||
raise RuntimeError("File is already in the meme folder.")
|
||||
# Make sure the file being moved is not already in the move folder
|
||||
if file_path.parent.resolve() == move_dir_path.resolve():
|
||||
raise RuntimeError("File is already in the move folder.")
|
||||
|
||||
# Formulate the destination path and avoid overwriting existing files
|
||||
base_name = file_path.name
|
||||
destination_path = meme_dir_path / base_name
|
||||
destination_path = move_dir_path / base_name
|
||||
if destination_path.exists():
|
||||
name, ext = destination_path.stem, destination_path.suffix
|
||||
counter = 1
|
||||
while destination_path.exists():
|
||||
destination_path = meme_dir_path / f"{name}_{counter}{ext}"
|
||||
destination_path = move_dir_path / f"{name}_{counter}{ext}"
|
||||
counter += 1
|
||||
_ = file_path.rename(destination_path)
|
||||
|
||||
Reference in New Issue
Block a user