Skip to content

Commit

Permalink
Add cancel for upload
Browse files Browse the repository at this point in the history
-cancel while uploading will delete uploaded files also

Signed-off-by: anas <[email protected]>
  • Loading branch information
anasty17 committed Jul 20, 2021
1 parent e56edfe commit dee36f6
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 35 deletions.
8 changes: 3 additions & 5 deletions bot/helper/ext_utils/bot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,18 @@ def getDownloadByGid(gid):
with download_dict_lock:
for dl in download_dict.values():
status = dl.status()
if status != MirrorStatus.STATUS_UPLOADING and status != MirrorStatus.STATUS_ARCHIVING \
and status != MirrorStatus.STATUS_EXTRACTING:
if status != MirrorStatus.STATUS_ARCHIVING and status != MirrorStatus.STATUS_EXTRACTING:
if dl.gid() == gid:
return dl
return None

def getAllDownload():
with download_dict_lock:
for dlDetails in list(download_dict.values()):
if dlDetails.status() == MirrorStatus.STATUS_DOWNLOADING \
or dlDetails.status() == MirrorStatus.STATUS_WAITING:
if dlDetails.status() == MirrorStatus.STATUS_DOWNLOADING or dlDetails.status() == MirrorStatus.STATUS_WAITING:
if dlDetails:
return dlDetails
return None

def get_progress_bar_string(status):
completed = status.processed_bytes() / 8
Expand Down Expand Up @@ -137,7 +136,6 @@ def get_readable_message():
except:
pass
msg += f'\n<b>User:</b> <a href="tg://user?id={download.message.from_user.id}">{download.message.from_user.first_name}</a> (<code>{download.message.from_user.id}</code>)'
if download.status() == MirrorStatus.STATUS_DOWNLOADING or download.status() == MirrorStatus.STATUS_CLONING:
msg += f"\n<b>To Stop:</b> <code>/{BotCommands.CancelMirror} {download.gid()}</code>"
msg += "\n\n"
if STATUS_LIMIT is not None:
Expand Down
9 changes: 8 additions & 1 deletion bot/helper/mirror_utils/status_utils/upload_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@


class UploadStatus(Status):
def __init__(self, obj, size, listener):
def __init__(self, obj, size, gid, listener):
self.obj = obj
self.__size = size
self.uid = listener.uid
self.message = listener.message
self.__gid = gid

def path(self):
return f"{DOWNLOAD_DIR}{self.uid}"
Expand Down Expand Up @@ -52,3 +53,9 @@ def eta(self):
return f'{get_readable_time(seconds)}'
except ZeroDivisionError:
return '-'

def gid(self) -> str:
return self.__gid

def download(self):
return self.obj
27 changes: 24 additions & 3 deletions bot/helper/mirror_utils/upload_utils/gdriveTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(self, name=None, listener=None):
self.start_time = 0
self.total_time = 0
self.dtotal_time = 0
self.is_uploading = False
self.is_downloading = False
self.is_cloning = False
self.is_cancelled = False
Expand Down Expand Up @@ -217,9 +218,12 @@ def upload_file(self, file_path, file_name, mime_type, parent_id):
return self.upload_file(file_path, file_name, mime_type, parent_id)
else:
self.is_cancelled = True
LOGGER.info(f"Got: {reason}")
raise err
else:
raise err
if self.is_cancelled:
return
self._file_uploaded_bytes = 0
# Insert new permissions
if not IS_TEAM_DRIVE:
Expand All @@ -230,6 +234,8 @@ def upload_file(self, file_path, file_name, mime_type, parent_id):
return download_url

def upload(self, file_name: str):
self.is_downloading = False
self.is_uploading = True
if USE_SERVICE_ACCOUNTS:
self.service_account_count = len(os.listdir("accounts"))
self.__listener.onUploadStarted()
Expand All @@ -242,6 +248,8 @@ def upload(self, file_name: str):
try:
mime_type = get_mime_type(file_path)
link = self.upload_file(file_path, file_name, mime_type, parent_id)
if self.is_cancelled:
return
if link is None:
raise Exception('Upload has been manually cancelled')
LOGGER.info("Uploaded To G-Drive: " + file_path)
Expand All @@ -256,14 +264,21 @@ def upload(self, file_name: str):
return
finally:
self.updater.cancel()
if self.is_cancelled:
return
else:
try:
dir_id = self.create_directory(os.path.basename(os.path.abspath(file_name)), parent_id)
result = self.upload_dir(file_path, dir_id)
if result is None:
raise Exception('Upload has been manually cancelled!')
LOGGER.info("Uploaded To G-Drive: " + file_name)
link = f"https://drive.google.com/folderview?id={dir_id}"
if self.is_cancelled:
LOGGER.info("Deleting uploaded data from drive...")
msg = self.deletefile(link)
LOGGER.info(f"{msg}")
return
LOGGER.info("Uploaded To G-Drive: " + file_name)
except Exception as e:
if isinstance(e, RetryError):
LOGGER.info(f"Total Attempts: {e.last_attempt.attempt_number}")
Expand All @@ -275,11 +290,12 @@ def upload(self, file_name: str):
return
finally:
self.updater.cancel()
if self.is_cancelled:
return
files = self.total_files
folders = self.total_folders
typ = self.typee
self.__listener.onUploadComplete(link, size, files, folders, typ)
LOGGER.info("Deleting downloaded file/folder..")
return link

@retry(wait=wait_exponential(multiplier=2, min=3, max=6), stop=stop_after_attempt(5),
Expand All @@ -305,6 +321,7 @@ def copyFile(self, file_id, dest_id):
return self.copyFile(file_id,dest_id)
else:
self.is_cancelled = True
LOGGER.info(f"Got: {reason}")
raise err
else:
raise err
Expand Down Expand Up @@ -360,7 +377,7 @@ def clone(self, link):
LOGGER.info("Deleting cloned data from drive...")
msg = self.deletefile(durl)
LOGGER.info(f"{msg}")
return "Your clone has been stopped and cloned data has been deleted!", "cancelled"
return "your clone has been stopped and cloned data has been deleted!", "cancelled"
msg += f'<b>Filename: </b><code>{meta.get("name")}</code>\n<b>Size: </b><code>{get_readable_file_size(self.transferred_size)}</code>'
msg += f'\n<b>Type: </b><code>Folder</code>'
msg += f'\n<b>SubFolders: </b><code>{self.total_folders}</code>'
Expand Down Expand Up @@ -826,6 +843,7 @@ def download_file(self, file_id, path, filename, mime_type):
return self.download_file(file_id, path, filename, mime_type)
else:
self.is_cancelled = True
LOGGER.info(f"Got: {reason}")
raise err
else:
raise err
Expand All @@ -845,3 +863,6 @@ def cancel_download(self):
self.__listener.onDownloadError('Download stopped by user!')
elif self.is_cloning:
LOGGER.info(f"Cancelling Clone: {self.name}")
elif self.is_uploading:
LOGGER.info(f"Cancelling upload: {self.name}")
self.__listener.onUploadError('your upload has been stopped and uploaded data has been deleted!')
12 changes: 3 additions & 9 deletions bot/modules/cancel_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,14 @@ def cancel_mirror(update, context):
elif not mirror_message:
sendMessage(msg, context.bot, update)
return
if dl.status() == "Uploading...📤":
sendMessage("Upload in Progress, You Can't Cancel It.", context.bot, update)
return
elif dl.status() == "Archiving...🔐":
if dl.status() == "Archiving...🔐":
sendMessage("Archival in Progress, You Can't Cancel It.", context.bot, update)
return
elif dl.status() == "Extracting...📂":
sendMessage("Extract in Progress, You Can't Cancel It.", context.bot, update)
return
else:
dl.download().cancel_download()
sleep(3) # incase of any error with ondownloaderror listener, clean_download will delete the folder but the download will stuck in status msg.
clean_download(f'{DOWNLOAD_DIR}{mirror_message.message_id}/')
sleep(3) # incase of any error with ondownloaderror listener
clean_download(f'{DOWNLOAD_DIR}{mirror_message.message_id}/')


def cancel_all(update, context):
Expand All @@ -68,7 +63,6 @@ def cancel_all(update, context):
else:
gid = dl.gid()
dl.download().cancel_download()
sleep(0.5)
count += 1
else:
break
Expand Down
28 changes: 13 additions & 15 deletions bot/modules/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,29 @@ def cloneNode(update, context):
Interval.append(setInterval(DOWNLOAD_STATUS_UPDATE_INTERVAL, update_all_messages))
sendStatusMessage(update, context.bot)
result, button = drive.clone(link)
with download_dict_lock:
del download_dict[update.message.message_id]
count = len(download_dict)
try:
if count == 0:
Interval[0].cancel()
del Interval[0]
delete_all_messages()
else:
update_all_messages()
except IndexError:
pass
if update.message.from_user.username:
uname = f'@{update.message.from_user.username}'
else:
uname = f'<a href="tg://user?id={update.message.from_user.id}">{update.message.from_user.first_name}</a>'
if uname is not None:
cc = f'\n\ncc: {uname}'
men = f'{uname} '
if button == "cancelled":
sendMessage(men + result, context.bot, update)
elif button == "":
if button == "cancelled" or button == "":
sendMessage(men + result, context.bot, update)
else:
sendMarkup(result + cc, context.bot, update, button)
try:
with download_dict_lock:
del download_dict[update.message.message_id]
count = len(download_dict)
if count == 0:
Interval[0].cancel()
del Interval[0]
delete_all_messages()
else:
update_all_messages()
except (IndexError, KeyError):
pass
else:
sendMessage('Provide G-Drive Shareable Link to Clone.', context.bot, update)

Expand Down
11 changes: 9 additions & 2 deletions bot/modules/mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def onDownloadComplete(self):
LOGGER.info(f"Download completed: {download_dict[self.uid].name()}")
download = download_dict[self.uid]
name = download.name()
gid = download.gid()
size = download.size_raw()
if name is None: # when pyrogram's media.file_name is of NoneType
name = os.listdir(f'{DOWNLOAD_DIR}{self.uid}')[0]
Expand Down Expand Up @@ -112,7 +113,7 @@ def onDownloadComplete(self):
LOGGER.info(f"Upload Name : {up_name}")
drive = gdriveTools.GoogleDriveHelper(up_name, self)
size = fs_utils.get_path_size(up_path)
upload_status = UploadStatus(drive, size, self)
upload_status = UploadStatus(drive, size, gid, self)
with download_dict_lock:
download_dict[self.uid] = upload_status
update_all_messages()
Expand Down Expand Up @@ -218,7 +219,13 @@ def onUploadError(self, error):
pass
del download_dict[self.message.message_id]
count = len(download_dict)
sendMessage(e_str, self.bot, self.update)
if self.message.from_user.username:
uname = f"@{self.message.from_user.username}"
else:
uname = f'<a href="tg://user?id={self.message.from_user.id}">{self.message.from_user.first_name}</a>'
if uname is not None:
men = f'{uname} '
sendMessage(men + e_str, self.bot, self.update)
if count == 0:
self.clean()
else:
Expand Down

0 comments on commit dee36f6

Please sign in to comment.