コード例 #1
0
ファイル: setup.py プロジェクト: LittleBuster/DokuMail
class SendFilesThread(QtCore.QThread):
    connectionStart = QtCore.pyqtSignal()
    err = QtCore.pyqtSignal(str)
    compressStart = QtCore.pyqtSignal(str)
    cryptStart = QtCore.pyqtSignal(str)
    sendStart = QtCore.pyqtSignal(str)
    sendComplete = QtCore.pyqtSignal()
    sendFileComplete = QtCore.pyqtSignal()

    def __init__(self):
        super(SendFilesThread, self).__init__()
        self.cfg = Configs()
        self.app_path = AppPath().main()
        self.a_key = AES256_cert_read("".join((self.app_path, "transf.crt")))

    def send(self, wnd, TCPServer, TCPPort, flist, toUsr):
        """
        Set connection configs
		"""
        self._wnd = wnd
        self._server = TCPServer
        self._port = TCPPort
        self.fileList = flist
        self._toUsr = toUsr

    def run(self):
        toUser = str("")
        self.client = TcpClient()

        mdb = MariaDB()
        if not mdb.connect(self._wnd.MDBServer, self._wnd.MDBUser, self._wnd.MDBPasswd, self._wnd.MDBBase):
            self.err.emit('Ошибка соединения с Базой Данных!')
            return
        toUser = mdb.get_user_by_alias(self._toUsr)
        mdb.close()

        if not os.path.exists("".join((self._wnd.app_path, "sendfiles"))):
            os.makedirs("".join((self._wnd.app_path,"sendfiles")))

        self.connectionStart.emit()
        if not self.client.connect(self._server, self._port, self._wnd.user, self._wnd.passwd):
            print("fail connection")
            self.err.emit("Ошибка соединения с сервером!")
            return

        exts = []
        try:
            exts = self.cfg.unzip_formats()
        except:
            Log().local("Error reading unzip formats")

        c_exts = []
        try:
            c_exts = self.cfg.uncrypt_formats()
        except:
            Log().local("Error reading uncrypted formats")

        print("start send")
        self.client.begin_send_files(toUser)

        for sfile in self.fileList:
            lsf = sfile.split("/")
            l = len(lsf)
            fname = lsf[l - 1]

            """
            Checking extension
			"""
            isCompress = True
            isCrypt = True

            tmp_fname = fname.split(".")
            ext = tmp_fname[len(tmp_fname) - 1].lower()

            for ex in exts:
                if ex == ext:
                    isCompress = False
                    break

            for ex in c_exts:
                if ex == ext:
                    isCrypt = False
                    break

            """
            Rename file
            """
            while True:
                try:
                    parts = fname.split("'")
                    fname = ""
                    l = len(parts)
                    i = 0
                    for part in parts:
                        i += 1
                        if i < l:
                            fname = fname + part + "_"
                        else:
                            fname = fname + part
                    break
                except:
                    break

            self.compressStart.emit(fname)
            if isCompress:
                if not zlib_compress_file(sfile, "".join((self._wnd.app_path, "sendfiles/", fname, ".z"))):
                    Log().local("Error compressing send file: " + fname)
                    print("error compressing")
                    self.client.close()
                    self.err.emit("Ошибка при сжатии файла")
                    return
                else:
                    print("".join((fname, " compressed")))
            else:
                print(fname + " not compressed")
                shutil.copy2(sfile, "sendfiles/" + fname + ".z")

            if isCrypt:
                self.cryptStart.emit(fname)
                if not AES256_encode_file("".join((self._wnd.app_path, "sendfiles/", fname, ".z")),
                                          "".join((self._wnd.app_path, "sendfiles/", fname, ".bin")),
                                          self.a_key):
                    Log().local("Error encrypting send file: " + fname)
                    print("error crypting")
                    self.client.close()
                    self.err.emit("Ошибка при шифровании сообщения")
                else:
                    print("".join((fname, " crypted")))
            else:
                print(fname + " not crypt")
                shutil.copy2("".join((self._wnd.app_path,"sendfiles/", fname, ".z")),
                             "".join((self._wnd.app_path,"sendfiles/", fname, ".bin")))

            self.sendStart.emit(fname)
            self.client.send_file("".join((self._wnd.app_path, "sendfiles/", fname)))
            self.sendFileComplete.emit()

            try:
                os.remove("".join((self._wnd.app_path, "sendfiles/", fname, ".z")))
                os.remove("".join((self._wnd.app_path, "sendfiles/", fname, ".bin")))
            except:
                Log().local("Error filename")
                self.err.emit("Ошибка в имени файла!")

        self.client.end_send_files()

        self.sendComplete.emit()
        print("send complete")
        self.client.close()