示例#1
0
 def onDownloadComplete(self):
     with download_dict_lock:
         LOGGER.info(
             f"Download completed: {download_dict[self.uid].name()}")
         download = download_dict[self.uid]
         name = download.name()
         size = download.size_raw()
         m_path = f'{DOWNLOAD_DIR}{self.uid}/{download.name()}'
     if self.isTar:
         download.is_archiving = True
         try:
             with download_dict_lock:
                 download_dict[self.uid] = TarStatus(name, m_path, size)
             path = fs_utils.tar(m_path)
         except FileNotFoundError:
             LOGGER.info('File to archive not found!')
             self.onUploadError('Internal error occurred!!')
             return
     else:
         path = f'{DOWNLOAD_DIR}{self.uid}/{download_dict[self.uid].name()}'
     up_name = pathlib.PurePath(path).name
     LOGGER.info(f"Upload Name : {up_name}")
     drive = gdriveTools.GoogleDriveHelper(up_name, self)
     if size == 0:
         size = fs_utils.get_path_size(m_path)
     upload_status = UploadStatus(drive, size, self)
     with download_dict_lock:
         download_dict[self.uid] = upload_status
     update_all_messages()
     drive.upload(up_name)
示例#2
0
    def onDownloadComplete(self):
        with download_dict_lock:
            LOGGER.info(f"Download completed: {download_dict[self.uid].name()}")
            download = download_dict[self.uid]
            name = download.name()
            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]
            m_path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
        if self.isTar:
            download.is_archiving = True
            try:
                with download_dict_lock:
                    download_dict[self.uid] = TarStatus(name, m_path, size)
                path = fs_utils.tar(m_path)
            except FileNotFoundError:
                LOGGER.info('File to archive not found!')
                self.onUploadError('Internal error occurred!!')
                return
        elif self.extract:
            download.is_extracting = True
            try:
                path = fs_utils.get_base_name(m_path)
                LOGGER.info(
                    f"Extracting : {name} "
                )
                with download_dict_lock:
                    download_dict[self.uid] = ExtractStatus(name, m_path, size)
                pswd = self.pswd
                if pswd is not None:
                    archive_result = subprocess.run(["pextract", m_path, pswd])
                else:
                    archive_result = subprocess.run(["extract", m_path])
                if archive_result.returncode == 0:
                    threading.Thread(target=os.remove, args=(m_path,)).start()
                    LOGGER.info(f"Deleting archive : {m_path}")
                else:
                    LOGGER.warning('Unable to extract archive! Uploading anyway')
                    path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
                LOGGER.info(
                    f'got path : {path}'
                )

            except NotSupportedExtractionArchive:
                LOGGER.info("Not any valid archive, uploading file as it is.")
                path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
        else:
            path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
        up_name = pathlib.PurePath(path).name
        up_path = f'{DOWNLOAD_DIR}{self.uid}/{up_name}'
        if up_name == "None":
            up_name = "".join(os.listdir(f'{DOWNLOAD_DIR}{self.uid}/'))
        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)
        with download_dict_lock:
            download_dict[self.uid] = upload_status
        update_all_messages()
        drive.upload(up_name)
示例#3
0
 def upload(self, file_name: str):
     if USE_SERVICE_ACCOUNTS:
         self.service_account_count = len(os.listdir("accounts"))
     self.__listener.onUploadStarted()
     file_dir = f"{DOWNLOAD_DIR}{self.__listener.message.message_id}"
     file_path = f"{file_dir}/{file_name}"
     size = get_readable_file_size(get_path_size(file_path))
     LOGGER.info("Uploading File: " + file_path)
     self.start_time = time.time()
     self.updater = setInterval(self.update_interval,
                                self._on_upload_progress)
     if os.path.isfile(file_path):
         try:
             mime_type = get_mime_type(file_path)
             link = self.upload_file(file_path, file_name, mime_type,
                                     parent_id)
             if link is None:
                 raise Exception('Upload has been manually cancelled')
             LOGGER.info("Uploaded To G-Drive: " + file_path)
         except Exception as e:
             if isinstance(e, RetryError):
                 LOGGER.info(
                     f"Total Attempts: {e.last_attempt.attempt_number}")
                 err = e.last_attempt.exception()
             else:
                 err = e
             LOGGER.error(err)
             self.__listener.onUploadError(str(err))
             return
         finally:
             self.updater.cancel()
     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}"
         except Exception as e:
             if isinstance(e, RetryError):
                 LOGGER.info(
                     f"Total Attempts: {e.last_attempt.attempt_number}")
                 err = e.last_attempt.exception()
             else:
                 err = e
             LOGGER.error(err)
             self.__listener.onUploadError(str(err))
             return
         finally:
             self.updater.cancel()
     LOGGER.info(download_dict)
     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
示例#4
0
    def onDownloadComplete(self):
        with download_dict_lock:
            LOGGER.info(
                f"File yang kamu download berhasil: {download_dict[self.uid].name()}"
            )
            download = download_dict[self.uid]
            name = download.name()
            size = download.size_raw()
            m_path = f'{DOWNLOAD_DIR}{self.uid}/{download.name()}'
        if self.isTar:
            download.is_archiving = True
            try:
                with download_dict_lock:
                    download_dict[self.uid] = TarStatus(name, m_path, size)
                path = fs_utils.tar(m_path)
            except FileNotFoundError:
                LOGGER.info('File yang pengen di arsip gk ada')
                self.onUploadError('Ada masalah internal nih')
                return
        elif self.extract:
            download.is_extracting = True
            try:
                path = fs_utils.get_base_name(m_path)
                LOGGER.info(f"Extracting : {name} ")
                with download_dict_lock:
                    download_dict[self.uid] = ExtractStatus(name, m_path, size)
                pswd = self.pswd
                if pswd is not None:
                    archive_result = subprocess.run(["pextract", m_path, pswd])
                else:
                    archive_result = subprocess.run(["extract", m_path])
                if archive_result.returncode == 0:
                    threading.Thread(target=os.remove, args=(m_path, )).start()
                    LOGGER.info(f"Arsip lagi didelete: {m_path}")
                else:
                    LOGGER.warning('Nggk bisa diarsip, aku upload aja ya')
                    path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
                LOGGER.info(f'got path : {path}')

            except NotSupportedExtractionArchive:
                LOGGER.info("Arsip yang kamu kirim gk bisa, aku kirim aja ya")
                path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
        else:
            path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
        up_name = pathlib.PurePath(path).name
        if up_name == "None":
            up_name = "".join(os.listdir(f'{DOWNLOAD_DIR}{self.uid}/'))
        up_path = f'{DOWNLOAD_DIR}{self.uid}/{up_name}'
        LOGGER.info(f"Bentar lagi upload file yang kamu minta : {up_name}")
        drive = gdriveTools.GoogleDriveHelper(up_name, self)
        size = fs_utils.get_path_size(up_path)
        upload_status = UploadStatus(drive, size, self)
        with download_dict_lock:
            download_dict[self.uid] = upload_status
        update_all_messages()
        drive.upload(up_name)
示例#5
0
    def onDownloadComplete(self):
        with download_dict_lock:
            LOGGER.info(f"Download completed: {download_dict[self.uid].name()}")
            download = download_dict[self.uid]
            name = download.name()
            size = download.size_raw()
            m_path = f'{DOWNLOAD_DIR}{self.uid}/{download.name()}'
        if self.isTar:
            download.is_archiving = True
            try:
                with download_dict_lock:
                    download_dict[self.uid] = TarStatus(name, m_path, size)
                path = fs_utils.tar(m_path)
            except FileNotFoundError:
                LOGGER.info('File to archive not found!')
                self.onUploadError('Internal error occurred!!')
                return
        elif self.extract:
            download.is_extracting = True

            path = fs_utils.get_base_name(m_path)
            if path != "unsupported":
                LOGGER.info(
                    f"Extracting : {download_dict[self.uid].name()} "
                )
                download_dict[self.uid] = ExtractStatus(name, m_path, size)
                os.system(f"extract '{m_path}'")
                if not os.path.exists(path):
                    self.onUploadError("Cannot extract file, check integrity of the file")
                    return
                LOGGER.info(
                    f'got path : {path}'
                )
                try:
                    os.remove(m_path)
                    LOGGER.info(f"Deleting archive : {m_path}")
                except Exception as e:
                    LOGGER.error(str(e))
            else:
                LOGGER.info("Not any valid archive, uploading file as it is.")
                path = f'{DOWNLOAD_DIR}{self.uid}/{download_dict[self.uid].name()}'
        else:
            path = f'{DOWNLOAD_DIR}{self.uid}/{download_dict[self.uid].name()}'
        up_name = pathlib.PurePath(path).name
        LOGGER.info(f"Upload Name : {up_name}")
        drive = gdriveTools.GoogleDriveHelper(up_name, self)
        if size == 0:
            size = fs_utils.get_path_size(m_path)
        upload_status = UploadStatus(drive, size, self)
        with download_dict_lock:
            download_dict[self.uid] = upload_status
        update_all_messages()
        drive.upload(up_name)
示例#6
0
    def onDownloadComplete(self):
        with download_dict_lock:
            LOGGER.info(f"Download completed: {download_dict[self.uid].name()}")
            download = download_dict[self.uid]
            name = download.name()
            size = download.size_raw()
            m_path = f'{DOWNLOAD_DIR}{self.uid}/{download.name()}'
        if self.isTar:
            download.is_archiving = True
            try:
                with download_dict_lock:
                    download_dict[self.uid] = TarStatus(name, m_path, size)
                path = fs_utils.tar(m_path)
            except FileNotFoundError:
                LOGGER.info('File to archive not found!')
                self.onUploadError('<b>🛣️Iɴᴛᴇʀɴᴀʟ Eʀʀᴏʀ Oᴄᴄᴜʀʀᴇᴅ!!</b>')
                return
        elif self.extract:
            download.is_extracting = True
            try:
                path = fs_utils.get_base_name(m_path)
                LOGGER.info(
                    f"Extracting : {name} "
                )
                with download_dict_lock:
                    download_dict[self.uid] = ExtractStatus(name, m_path, size)
                archive_result = subprocess.run(["extract", m_path])
                if archive_result.returncode == 0:
                    threading.Thread(target=os.remove, args=(m_path,)).start()
                    LOGGER.info(f"Deleting archive : {m_path}")
                else:
                    LOGGER.warning('Unable to extract archive! Uploading anyway')
                    path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
                LOGGER.info(
                    f'got path : {path}'
                )

            except NotSupportedExtractionArchive:
                LOGGER.info("Not any valid archive, uploading file as it is.")
                path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
        else:
            path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
        up_name = pathlib.PurePath(path).name
        up_path = f'{DOWNLOAD_DIR}{self.uid}/{up_name}'
        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)
        with download_dict_lock:
            download_dict[self.uid] = upload_status
        update_all_messages()
        drive.upload(up_name)
示例#7
0
 def upload(self, file_name: str):
     self.is_downloading = False
     self.is_uploading = True
     file_dir = f"{DOWNLOAD_DIR}{self.__listener.message.message_id}"
     file_path = f"{file_dir}/{file_name}"
     size = get_readable_file_size(get_path_size(file_path))
     LOGGER.info("Uploading File: " + file_path)
     self.updater = setInterval(self.update_interval,
                                self._on_upload_progress)
     try:
         if ospath.isfile(file_path):
             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)
         else:
             mime_type = 'Folder'
             dir_id = self.__create_directory(
                 ospath.basename(ospath.abspath(file_name)), parent_id)
             result = self.__upload_dir(file_path, dir_id)
             if result is None:
                 raise Exception('Upload has been manually cancelled!')
             link = f"https://drive.google.com/folderview?id={dir_id}"
             if self.is_cancelled:
                 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}")
             err = e.last_attempt.exception()
         else:
             err = e
         LOGGER.error(err)
         self.__listener.onUploadError(str(err))
         self.is_cancelled = True
     finally:
         self.updater.cancel()
         if self.is_cancelled:
             if mime_type == 'Folder':
                 LOGGER.info("Deleting uploaded data from Drive...")
                 link = f"https://drive.google.com/folderview?id={dir_id}"
                 self.deletefile(link)
             return
     self.__listener.onUploadComplete(link, size, self.__total_files,
                                      self.__total_folders, mime_type,
                                      self.name)
示例#8
0
文件: mirror.py 项目: MonkTeam/mega
    def onDownloadComplete(self):
        with download_dict_lock:
            LOGGER.info(
                f"⬢𝗗𝗼𝘄𝗻𝗹𝗼𝗮𝗱 𝗰𝗼𝗺𝗽𝗹𝗲𝘁𝗲𝗱: {download_dict[self.uid].name()}")
            download = download_dict[self.uid]
            name = download.name()
            size = download.size_raw()
            m_path = f'{DOWNLOAD_DIR}{self.uid}/{download.name()}'
        if self.isTar:
            download.is_archiving = True
            try:
                with download_dict_lock:
                    download_dict[self.uid] = TarStatus(name, m_path, size)
                path = fs_utils.tar(m_path)
            except FileNotFoundError:
                LOGGER.info('𝗙𝗶𝗹𝗲 𝘁𝗼 𝗮𝗿𝗰𝗵𝗶𝘃𝗲 𝗻𝗼𝘁 𝗳𝗼𝘂𝗻𝗱!')
                self.onUploadError('Internal error occurred!!')
                return
        elif self.extract:
            download.is_extracting = True
            try:
                path = fs_utils.get_base_name(m_path)
                LOGGER.info(f"⬢𝗘𝘅𝘁𝗿𝗮𝗰𝘁𝗶𝗻𝗴 : {name} ")
                with download_dict_lock:
                    download_dict[self.uid] = ExtractStatus(name, m_path, size)
                archive_result = subprocess.run(["extract", m_path])
                if archive_result.returncode == 0:
                    threading.Thread(target=os.remove, args=(m_path, )).start()
                    LOGGER.info(f"⬢𝗗𝗲𝗹𝗲𝘁𝗶𝗻𝗴 𝗮𝗿𝗰𝗵𝗶𝘃𝗲 : {m_path}")
                else:
                    LOGGER.warning(
                        '𝗨𝗻𝗮𝗯𝗹𝗲 𝘁𝗼 𝗲𝘅𝘁𝗿𝗮𝗰𝘁 𝗮𝗿𝗰𝗵𝗶𝘃𝗲! 𝗨𝗽𝗹𝗼𝗮𝗱𝗶𝗻𝗴 𝗮𝗻𝘆𝘄𝗮𝘆')
                    path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
                LOGGER.info(f'got path : {path}')

            except NotSupportedExtractionArchive:
                LOGGER.info("𝗡𝗼𝘁 𝗮𝗻𝘆 𝘃𝗮𝗹𝗶𝗱 𝗮𝗿𝗰𝗵𝗶𝘃𝗲, 𝘂𝗽𝗹𝗼𝗮𝗱𝗶𝗻𝗴 𝗳𝗶𝗹𝗲 𝗮𝘀 𝗶𝘁 𝗶𝘀.")
                path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
        else:
            path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
        up_name = pathlib.PurePath(path).name
        LOGGER.info(f"⬢𝗨𝗽𝗹𝗼𝗮𝗱 𝗡𝗮𝗺𝗲 : {up_name}")
        drive = gdriveTools.GoogleDriveHelper(up_name, self)
        if size == 0:
            size = fs_utils.get_path_size(m_path)
        upload_status = UploadStatus(drive, size, self)
        with download_dict_lock:
            download_dict[self.uid] = upload_status
        update_all_messages()
        drive.upload(up_name)
示例#9
0
 def onDownloadComplete(self):
     with download_dict_lock:
         LOGGER.info(
             f"📥Download completed: {download_dict[self.uid].name()}")
         download = download_dict[self.uid]
         name = download.name()
         size = download.size_raw()
         m_path = f'{DOWNLOAD_DIR}{self.uid}/{download.name()}'
     if self.isTar:
         download.is_archiving = True
         try:
             with download_dict_lock:
                 download_dict[self.uid] = TarStatus(name, m_path, size)
             path = fs_utils.tar(m_path)
         except FileNotFoundError:
             LOGGER.info('File to 🗂archive not found!')
             self.onUploadError('Internal ⚠️error occurred!!')
             return
     elif self.extract:
         if tarfile.is_tarfile(m_path) or zipfile.is_zipfile(m_path):
             LOGGER.info(f"Extracting : {download_dict[self.uid].name()} ")
             path = fs_utils.unzip(m_path)
             LOGGER.info(f'got path : {path}')
             try:
                 os.remove(m_path)
             except Exception as e:
                 LOGGER.error(str(e))
             pass
             LOGGER.info(f"Deleting archive : {m_path}")
         else:
             LOGGER.info("Not any valid archive, uploading file as it is.")
             path = f'{DOWNLOAD_DIR}{self.uid}/{download_dict[self.uid].name()}'
     else:
         path = f'{DOWNLOAD_DIR}{self.uid}/{download_dict[self.uid].name()}'
     up_name = pathlib.PurePath(path).name
     LOGGER.info(f"Upload Name : {up_name}")
     drive = gdriveTools.GoogleDriveHelper(up_name, self)
     if size == 0:
         size = fs_utils.get_path_size(m_path)
     upload_status = UploadStatus(drive, size, self)
     with download_dict_lock:
         download_dict[self.uid] = upload_status
     update_all_messages()
     drive.upload(up_name)
示例#10
0
 def processed_bytes(self):
     if self.obj.downloaded_bytes != 0:
         return self.obj.downloaded_bytes
     else:
         return get_path_size(f"{DOWNLOAD_DIR}{self.uid}")
示例#11
0
 def onDownloadComplete(self):
     with download_dict_lock:
         LOGGER.info(f"Download completed: {download_dict[self.uid].name()}")
         download = download_dict[self.uid]
         name = str(download.name()).replace('/', '')
         gid = download.gid()
         size = download.size_raw()
         if name == "None" or self.isQbit:
             name = listdir(f'{DOWNLOAD_DIR}{self.uid}')[-1]
         m_path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
     if self.isZip:
         try:
             with download_dict_lock:
                 download_dict[self.uid] = ZipStatus(name, m_path, size)
             pswd = self.pswd
             path = m_path + ".zip"
             LOGGER.info(f'Zip: orig_path: {m_path}, zip_path: {path}')
             if pswd is not None:
                 if self.isLeech and int(size) > TG_SPLIT_SIZE:
                     path = m_path + ".zip"
                     srun(["7z", f"-v{TG_SPLIT_SIZE}b", "a", "-mx=0", f"-p{pswd}", path, m_path])
                 else:
                     srun(["7z", "a", "-mx=0", f"-p{pswd}", path, m_path])
             elif self.isLeech and int(size) > TG_SPLIT_SIZE:
                 path = m_path + ".zip"
                 srun(["7z", f"-v{TG_SPLIT_SIZE}b", "a", "-mx=0", path, m_path])
             else:
                 srun(["7z", "a", "-mx=0", path, m_path])
         except FileNotFoundError:
             LOGGER.info('File to archive not found!')
             self.onUploadError('Internal error occurred!!')
             return
         try:
             rmtree(m_path)
         except:
             osremove(m_path)
     elif self.extract:
         try:
             if ospath.isfile(m_path):
                 path = get_base_name(m_path)
             LOGGER.info(f"Extracting: {name}")
             with download_dict_lock:
                 download_dict[self.uid] = ExtractStatus(name, m_path, size)
             pswd = self.pswd
             if ospath.isdir(m_path):
                 for dirpath, subdir, files in walk(m_path, topdown=False):
                     for file_ in files:
                         if search(r'\.part0*1.rar$', file_) or search(r'\.7z.0*1$', file_) \
                            or (file_.endswith(".rar") and not search(r'\.part\d+.rar$', file_)) \
                            or file_.endswith(".zip") or search(r'\.zip.0*1$', file_):
                             m_path = ospath.join(dirpath, file_)
                             if pswd is not None:
                                 result = srun(["7z", "x", f"-p{pswd}", m_path, f"-o{dirpath}", "-aot"])
                             else:
                                 result = srun(["7z", "x", m_path, f"-o{dirpath}", "-aot"])
                             if result.returncode != 0:
                                 LOGGER.error('Unable to extract archive!')
                     for file_ in files:
                         if file_.endswith(".rar") or search(r'\.r\d+$', file_) \
                            or search(r'\.7z.\d+$', file_) or search(r'\.z\d+$', file_) \
                            or search(r'\.zip.\d+$', file_) or file_.endswith(".zip"):
                             del_path = ospath.join(dirpath, file_)
                             osremove(del_path)
                 path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
             else:
                 if pswd is not None:
                     result = srun(["bash", "pextract", m_path, pswd])
                 else:
                     result = srun(["bash", "extract", m_path])
                 if result.returncode == 0:
                     LOGGER.info(f"Extract Path: {path}")
                     osremove(m_path)
                     LOGGER.info(f"Deleting archive: {m_path}")
                 else:
                     LOGGER.error('Unable to extract archive! Uploading anyway')
                     path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
         except NotSupportedExtractionArchive:
             LOGGER.info("Not any valid archive, uploading file as it is.")
             path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
     else:
         path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
     up_name = PurePath(path).name
     up_path = f'{DOWNLOAD_DIR}{self.uid}/{up_name}'
     size = get_path_size(f'{DOWNLOAD_DIR}{self.uid}')
     if self.isLeech and not self.isZip:
         checked = False
         for dirpath, subdir, files in walk(f'{DOWNLOAD_DIR}{self.uid}', topdown=False):
             for file_ in files:
                 f_path = ospath.join(dirpath, file_)
                 f_size = ospath.getsize(f_path)
                 if int(f_size) > TG_SPLIT_SIZE:
                     if not checked:
                         checked = True
                         with download_dict_lock:
                             download_dict[self.uid] = SplitStatus(up_name, up_path, size)
                         LOGGER.info(f"Splitting: {up_name}")
                     fssplit(f_path, f_size, file_, dirpath, TG_SPLIT_SIZE)
                     osremove(f_path)
     if self.isLeech:
         LOGGER.info(f"Leech Name: {up_name}")
         tg = TgUploader(up_name, self)
         tg_upload_status = TgUploadStatus(tg, size, gid, self)
         with download_dict_lock:
             download_dict[self.uid] = tg_upload_status
         update_all_messages()
         tg.upload()
     else:
         LOGGER.info(f"Upload Name: {up_name}")
         drive = GoogleDriveHelper(up_name, self)
         upload_status = UploadStatus(drive, size, gid, self)
         with download_dict_lock:
             download_dict[self.uid] = upload_status
         update_all_messages()
         drive.upload(up_name)
示例#12
0
    def onDownloadComplete(self):
        with download_dict_lock:
            LOGGER.info(
                f"Download completed: {download_dict[self.uid].name()}")
            download = download_dict[self.uid]
            name = f"{download.name()}".replace('/', '')
            gid = download.gid()
            size = download.size_raw()
            if name == "None" or self.isQbit:  # when pyrogram's media.file_name is of NoneType
                name = os.listdir(f'{DOWNLOAD_DIR}{self.uid}')[0]
            m_path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
        if self.isTar:
            try:
                with download_dict_lock:
                    download_dict[self.uid] = TarStatus(name, m_path, size)
                if self.isZip:
                    pswd = self.pswd
                    path = m_path + ".zip"
                    LOGGER.info(f'Zip: orig_path: {m_path}, zip_path: {path}')
                    if pswd is not None:
                        subprocess.run(["7z", "a", f"-p{pswd}", path, m_path])
                    else:
                        subprocess.run(["7z", "a", path, m_path])
                else:
                    path = fs_utils.tar(m_path)
            except FileNotFoundError:
                LOGGER.info('File to archive not found!')
                self.onUploadError('Internal error occurred!!')
                return
            try:
                shutil.rmtree(m_path)
            except:
                os.remove(m_path)
        elif self.extract:
            try:
                LOGGER.info(f"Extracting: {name}")
                with download_dict_lock:
                    download_dict[self.uid] = ExtractStatus(name, m_path, size)
                pswd = self.pswd
                if os.path.isdir(m_path):
                    for dirpath, subdir, files in os.walk(m_path,
                                                          topdown=False):
                        for file in files:
                            suffixes = (".part1.rar", ".part01.rar",
                                        ".part001.rar", ".part0001.rar")
                            if (file.endswith(".rar") and "part"
                                    not in file) or file.endswith(suffixes):
                                m_path = os.path.join(dirpath, file)
                                if pswd is not None:
                                    result = subprocess.run([
                                        "7z", "x", f"-p{pswd}", m_path,
                                        f"-o{dirpath}"
                                    ])
                                else:
                                    result = subprocess.run(
                                        ["7z", "x", m_path, f"-o{dirpath}"])
                                if result.returncode != 0:
                                    LOGGER.warning(
                                        'Unable to extract archive!')
                                break
                        for file in files:
                            if file.endswith(".rar") or fnmatch(
                                    file, "*.r[0-9]") or fnmatch(
                                        file, "*.r[0-9]*"):
                                del_path = os.path.join(dirpath, file)
                                os.remove(del_path)
                    path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
                else:
                    path = fs_utils.get_base_name(m_path)
                    if pswd is not None:
                        result = subprocess.run(["pextract", m_path, pswd])
                    else:
                        result = subprocess.run(["extract", m_path])
                    if result.returncode == 0:
                        os.remove(m_path)
                        LOGGER.info(f"Deleting archive: {m_path}")
                    else:
                        LOGGER.warning(
                            'Unable to extract archive! Uploading anyway')
                        path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
                LOGGER.info(f'got path: {path}')

            except NotSupportedExtractionArchive:
                LOGGER.info("Not any valid archive, uploading file as it is.")
                path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
        else:
            path = f'{DOWNLOAD_DIR}{self.uid}/{name}'
        up_name = pathlib.PurePath(path).name
        up_path = f'{DOWNLOAD_DIR}{self.uid}/{up_name}'
        size = fs_utils.get_path_size(up_path)
        if self.isLeech:
            checked = False
            for dirpath, subdir, files in os.walk(f'{DOWNLOAD_DIR}{self.uid}',
                                                  topdown=False):
                for file in files:
                    f_path = os.path.join(dirpath, file)
                    f_size = os.path.getsize(f_path)
                    if int(f_size) > TG_SPLIT_SIZE:
                        if not checked:
                            checked = True
                            with download_dict_lock:
                                download_dict[self.uid] = SplitStatus(
                                    up_name, up_path, size)
                            LOGGER.info(f"Splitting: {up_name}")
                        fs_utils.split(f_path, f_size, file, dirpath,
                                       TG_SPLIT_SIZE)
                        os.remove(f_path)
            LOGGER.info(f"Leech Name: {up_name}")
            tg = pyrogramEngine.TgUploader(up_name, self)
            tg_upload_status = TgUploadStatus(tg, size, gid, self)
            with download_dict_lock:
                download_dict[self.uid] = tg_upload_status
            update_all_messages()
            tg.upload()
        else:
            LOGGER.info(f"Upload Name: {up_name}")
            drive = gdriveTools.GoogleDriveHelper(up_name, self)
            upload_status = UploadStatus(drive, size, gid, self)
            with download_dict_lock:
                download_dict[self.uid] = upload_status
            update_all_messages()
            drive.upload(up_name)