Skip to content

Commit

Permalink
fix: uses shutil.move instead of os.rename to move files
Browse files Browse the repository at this point in the history
useful when 2 paths aren't on the same disk, ex Docker volumes
see #286 (comment)
  • Loading branch information
EDM115 committed Apr 13, 2024
1 parent 837767c commit 265eca1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
6 changes: 2 additions & 4 deletions unzipper/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ def shutdown_bot():

if __name__ == "__main__":
try:
if not os.path.isdir(Config.DOWNLOAD_LOCATION):
os.makedirs(Config.DOWNLOAD_LOCATION)
if not os.path.isdir(Config.THUMB_LOCATION):
os.makedirs(Config.THUMB_LOCATION)
os.makedirs(Config.DOWNLOAD_LOCATION, exist_ok=True)
os.makedirs(Config.THUMB_LOCATION, exist_ok=True)
LOGGER.info(Messages.STARTING_BOT)
unzipperbot.start()
starttime = time.strftime("%Y/%m/%d - %H:%M:%S")
Expand Down
17 changes: 8 additions & 9 deletions unzipper/modules/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ async def unzipper_cb(unzip_bot: Client, query: CallbackQuery):
thumb_location = Config.THUMB_LOCATION + "/" + str(user_id) + ".jpg"
final_thumb = Config.THUMB_LOCATION + "/waiting_" + str(user_id) + ".jpg"
try:
os.rename(final_thumb, thumb_location)
shutil.move(final_thumb, thumb_location)
except:
LOGGER.warning(Messages.ERROR_THUMB_RENAME)
try:
Expand Down Expand Up @@ -281,8 +281,7 @@ async def unzipper_cb(unzip_bot: Client, query: CallbackQuery):
pass
return
length = len(messages_array)
if not os.path.isdir(download_path):
os.makedirs(download_path)
os.makedirs(download_path, exist_ok=True)
rs_time = time()
newarray = []
await merge_msg.edit(Messages.PROCESS_MSGS.format(length))
Expand Down Expand Up @@ -343,7 +342,7 @@ async def unzipper_cb(unzip_bot: Client, query: CallbackQuery):
user_id = query.from_user.id
download_path = f"{Config.DOWNLOAD_LOCATION}/{user_id}/merge"
ext_files_dir = f"{Config.DOWNLOAD_LOCATION}/{user_id}/extracted"
os.makedirs(ext_files_dir)
os.makedirs(ext_files_dir, exist_ok=True)
try:
files = await get_files(download_path)
file = files[0]
Expand Down Expand Up @@ -548,7 +547,7 @@ async def unzipper_cb(unzip_bot: Client, query: CallbackQuery):
if rfnamebro == "":
rfnamebro = unquote(url.split("/")[-1])
if unzip_resp.status == 200:
os.makedirs(download_path)
os.makedirs(download_path, exist_ok=True)
s_time = time()
if real_filename:
archive = os.path.join(download_path, real_filename)
Expand Down Expand Up @@ -737,7 +736,7 @@ async def unzipper_cb(unzip_bot: Client, query: CallbackQuery):
else:
renamed = location.replace(archive_name, rfnamebro)
try:
os.rename(location, renamed)
shutil.move(location, renamed)
except OSError as e:
await del_ongoing_task(user_id)
LOGGER.error(e)
Expand All @@ -759,7 +758,7 @@ async def unzipper_cb(unzip_bot: Client, query: CallbackQuery):
return shutil.rmtree(f"{Config.DOWNLOAD_LOCATION}/{user_id}")
await query.message.edit(Messages.SPLITTING.format(newfname))
splitteddir = f"{Config.DOWNLOAD_LOCATION}/splitted/{user_id}"
os.makedirs(splitteddir)
os.makedirs(splitteddir, exist_ok=True)
ooutput = f"{splitteddir}/{newfname}"
splittedfiles = await split_files(renamed, ooutput, Config.TG_MAX_SIZE)
if not splittedfiles:
Expand Down Expand Up @@ -1013,7 +1012,7 @@ async def unzipper_cb(unzip_bot: Client, query: CallbackQuery):
chat_id=user_id, text=Messages.SPLITTING.format(fname)
)
splitteddir = f"{Config.DOWNLOAD_LOCATION}/splitted/{user_id}"
os.makedirs(splitteddir)
os.makedirs(splitteddir, exist_ok=True)
ooutput = f"{splitteddir}/{fname}"
splittedfiles = await split_files(file, ooutput, Config.TG_MAX_SIZE)
LOGGER.info(splittedfiles)
Expand Down Expand Up @@ -1160,7 +1159,7 @@ async def unzipper_cb(unzip_bot: Client, query: CallbackQuery):
chat_id=user_id, text=Messages.SPLITTING.format(fname)
)
splitteddir = f"{Config.DOWNLOAD_LOCATION}/splitted/{user_id}"
os.makedirs(splitteddir)
os.makedirs(splitteddir, exist_ok=True)
ooutput = f"{splitteddir}/{fname}"
splittedfiles = await split_files(file, ooutput, Config.TG_MAX_SIZE)
LOGGER.info(splittedfiles)
Expand Down
3 changes: 2 additions & 1 deletion unzipper/modules/ext_script/custom_thumbnail.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) 2022 - 2024 EDM115
import os
import shutil

from PIL import Image

Expand Down Expand Up @@ -46,7 +47,7 @@ async def add_thumb(_, message):
file_name=file.split("/")[-1],
caption=Messages.EXT_CAPTION.format(file),
)
os.rename(file, pre_thumb)
shutil.move(file, pre_thumb)
size = 320, 320
try:
with Image.open(pre_thumb) as previous:
Expand Down

0 comments on commit 265eca1

Please sign in to comment.