Esempio n. 1
0
 def save(self):
     self.mod = False
     if self.script == 'JSP':
         r = jspCore.uploadFile(self.url, self.password, self.text(),
                                self.filenameArg)
     else:
         r = phpCore.uploadFile(self.url, self.password, self.text(),
                                self.filenameArg, self.useRSA)
     if r != '1':
         raise Exception('可能没有权限')
    def generateFileListMenu(self, data, pos):
        url = data[0]
        password = data[1]
        treeWidget = data[2]
        fileTableWidget = data[3]
        rdata = data[4]
        useRSA = data[6]
        script = data[7]

        if treeWidget.currentItem() is None:
            item = data[5]
        else:
            item = treeWidget.currentItem()
        # 当前选择文件的目录
        dir = self.parsePath(item, rdata)

        menu = QtWidgets.QMenu()
        # 计算当前行数
        self.mainWindow.row_num = -1
        for i in fileTableWidget.selectionModel().selection().indexes():
            self.mainWindow.row_num = i.row()

        if self.mainWindow.row_num != -1:
            if fileTableWidget.item(self.mainWindow.row_num,
                                    0).text().endswith('/'):
                item0 = menu.addAction('上传文件')
                item0.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/upload_easyicon.svg'))
                item1 = menu.addAction('重命名')
                item1.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/filename_easyicon.svg'))
                item2 = menu.addAction('删除文件')
                item2.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/delete_easyicon.svg'))
                item3 = menu.addAction('更改权限')
                item3.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/management_easyicon.svg'))
                item4 = menu.addAction('刷新目录')
                item4.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/refresh_easyicon.svg'))
                action = menu.exec_(fileTableWidget.mapToGlobal(pos))

                if action == item0:
                    try:
                        filePath = QtWidgets.QFileDialog.getOpenFileName(
                            self.mainWindow, '选择文件')
                        if filePath[0] != '':
                            with open(filePath[0], encoding='utf-8') as f:
                                buffer = f.read()
                            if script == 'JSP':
                                r = jspCore.uploadFile(
                                    url, password, buffer,
                                    dir + os.path.basename(filePath[0]))
                            else:
                                r = phpCore.uploadFile(
                                    url, password, buffer,
                                    dir + os.path.basename(filePath[0]),
                                    useRSA)

                            if r == '1':
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "上传成功!", '文件已上传')
                                # 更新文件目录
                                if script == 'JSP':
                                    files = jspCore.scanDir(
                                        url, password, dir).split('\n')
                                else:
                                    files = phpCore.scanDir(
                                        url, password, dir, useRSA).split('\n')
                                files = list(filter(None, files))
                                self.updataTable(files, fileTableWidget)
                            else:
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "上传失败!", '可能没有权限')

                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "上传失败!",
                                                    str(Exception(e)))
                elif action == item1:
                    try:
                        rfilename = fileTableWidget.item(
                            self.mainWindow.row_num, 0).text()
                        dfilename, ok = QtWidgets.QInputDialog.getText(
                            self.mainWindow, '重命名', '更改后的文件名:', text=rfilename)
                        if ok:
                            if script == 'JSP':
                                reResult = jspCore.renameFile(
                                    url, password, dir + rfilename,
                                    dir + dfilename)
                            else:
                                reResult = phpCore.renameFile(
                                    url, password, dir + rfilename,
                                    dir + dfilename, useRSA)

                            if reResult == '1':
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "重命名成功!", '文件已重命名')
                            else:
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "重命名失败!", '可能没有权限')
                            # 更新文件目录
                            if script == 'JSP':
                                files = jspCore.scanDir(url, password,
                                                        dir).split('\n')
                            else:
                                files = phpCore.scanDir(
                                    url, password, dir, useRSA).split('\n')
                            files = list(filter(None, files))
                            self.updataTable(files, fileTableWidget)
                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "重命名失败!",
                                                    str(Exception(e)))
                elif action == item2:
                    try:
                        file = fileTableWidget.item(self.mainWindow.row_num,
                                                    0).text()
                        reply = QtWidgets.QMessageBox.question(
                            self.mainWindow, '删除文件', "确定要删除该文件吗?",
                            QtWidgets.QMessageBox.Yes
                            | QtWidgets.QMessageBox.No,
                            QtWidgets.QMessageBox.Yes)
                        if reply == QtWidgets.QMessageBox.Yes:
                            if script == 'JSP':
                                deResult = jspCore.deleteFile(
                                    url, password, dir + file)
                            else:
                                deResult = phpCore.deleteFile(
                                    url, password, dir + file, useRSA)
                            if deResult == '1':
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "删除成功!", '文件已删除')
                            else:
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "删除失败!", '可能没有权限')
                            # 更新文件目录
                            if script == 'JSP':
                                files = jspCore.scanDir(url, password,
                                                        dir).split('\n')
                            else:
                                files = phpCore.scanDir(
                                    url, password, dir, useRSA).split('\n')

                            files = list(filter(None, files))
                            self.updataTable(files, fileTableWidget)
                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "删除文件失败!",
                                                    str(Exception(e)))
                elif action == item3:
                    try:
                        file = fileTableWidget.item(self.mainWindow.row_num,
                                                    0).text()
                        rmode = fileTableWidget.item(self.mainWindow.row_num,
                                                     3).text()
                        nmode, ok = QtWidgets.QInputDialog.getText(
                            self.mainWindow, '更改权限', '更改为:', text=rmode)
                        if ok:
                            searchObj = re.search('^0[0-7][0-7][0-7]$', nmode)
                            if searchObj is None:
                                raise Exception('输入不合法')
                            if script == 'JSP':
                                chmodResult = jspCore.chmodFile(
                                    url, password, dir + file, nmode)
                            else:
                                chmodResult = phpCore.chmodFile(
                                    url, password, dir + file, nmode, useRSA)

                            if chmodResult == '1':
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "更改成功!", '权限已经更改')
                            else:
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "更改失败!", '更改权限失败!')
                            # 更新文件目录
                            if script == 'JSP':
                                files = jspCore.scanDir(url, password,
                                                        dir).split('\n')
                            else:
                                files = phpCore.scanDir(
                                    url, password, dir, useRSA).split('\n')
                            self.mainWindow.updataTable(files, fileTableWidget)
                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "更改权限失败!",
                                                    str(Exception(e)))
                elif action == item4:
                    # 更新文件目录
                    if script == 'JSP':
                        files = jspCore.scanDir(url, password, dir).split('\n')
                    else:
                        files = phpCore.scanDir(url, password, dir,
                                                useRSA).split('\n')
                    files = list(filter(None, files))
                    self.updataTable(files, fileTableWidget)
            else:
                # 文件
                item0 = menu.addAction('上传文件')
                item0.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/upload_easyicon.svg'))
                item1 = menu.addAction('下载文件')
                item1.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/download_easyicon.svg'))
                item2 = menu.addAction('重命名')
                item2.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/filename_easyicon.svg'))
                item3 = menu.addAction('删除文件')
                item3.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/delete_easyicon.svg'))
                item4 = menu.addAction('更改权限')
                item4.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/management_easyicon.svg'))
                item5 = menu.addAction('刷新目录')
                item5.setIcon(
                    QIcon(
                        os.path.dirname(os.path.realpath(sys.argv[0])) +
                        '/Icons/refresh_easyicon.svg'))
                action = menu.exec_(fileTableWidget.mapToGlobal(pos))

                if action == item0:
                    try:
                        filePath = QtWidgets.QFileDialog.getOpenFileName(
                            self.mainWindow, '选择文件')
                        if filePath[0] != '':
                            with open(filePath[0], encoding='utf-8') as f:
                                buffer = f.read()
                            if script == 'JSP':
                                upResult = jspCore.uploadFile(
                                    url, password, buffer,
                                    dir + os.path.basename(filePath[0]))
                            else:
                                upResult = phpCore.uploadFile(
                                    url, password, buffer,
                                    dir + os.path.basename(filePath[0]),
                                    useRSA)
                            if upResult == '1':
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "上传成功!", '文件已上传')
                                # 更新文件目录
                                if script == 'JSP':
                                    files = jspCore.scanDir(
                                        url, password, dir).split('\n')
                                else:
                                    files = phpCore.scanDir(
                                        url, password, dir, useRSA).split('\n')
                                files = list(filter(None, files))
                                self.updataTable(files, fileTableWidget)
                            else:
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "上传失败!", '可能没有权限')

                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "上传失败!",
                                                    str(Exception(e)))
                elif action == item1:
                    try:
                        # TODO 后台下载
                        filename = fileTableWidget.item(
                            self.mainWindow.row_num, 0).text()
                        file = QtWidgets.QFileDialog.getSaveFileName(
                            self.mainWindow, '保存路径', filename)
                        if file[0] != '':
                            if script == 'JSP':
                                buffer = jspCore.downloadFile(
                                    url, password, dir + filename)
                            else:
                                buffer = phpCore.downloadFile(
                                    url, password, dir + filename, useRSA)
                            with open(file[0], 'w', encoding='utf-8') as f:
                                f.write(buffer)
                            QtWidgets.QMessageBox.about(
                                self.mainWindow, "下载成功!", '文件已保存')
                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "下载失败!",
                                                    str(Exception(e)))
                elif action == item2:
                    try:
                        rfilename = fileTableWidget.item(
                            self.mainWindow.row_num, 0).text()
                        dfilename, ok = QtWidgets.QInputDialog.getText(
                            self.mainWindow, '重命名', '更改后的文件名:', text=rfilename)
                        if ok:
                            if script == 'JSP':
                                reResult = jspCore.renameFile(
                                    url, password, dir + rfilename,
                                    dir + dfilename)
                            else:
                                reResult = phpCore.renameFile(
                                    url, password, dir + rfilename,
                                    dir + dfilename, useRSA)
                            if reResult == '1':
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "重命名成功!", '文件已重命名')
                            else:
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "重命名失败!", '可能没有权限')
                            # 更新文件目录
                            if script == 'JSP':
                                files = jspCore.scanDir(url, password,
                                                        dir).split('\n')
                            else:
                                files = phpCore.scanDir(
                                    url, password, dir, useRSA).split('\n')
                            files = list(filter(None, files))
                            self.updataTable(files, fileTableWidget)
                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "重命名失败!",
                                                    str(Exception(e)))
                elif action == item3:
                    try:
                        file = fileTableWidget.item(self.mainWindow.row_num,
                                                    0).text()
                        reply = QtWidgets.QMessageBox.question(
                            self.mainWindow, '删除文件', "确定要删除该文件吗?",
                            QtWidgets.QMessageBox.Yes
                            | QtWidgets.QMessageBox.No,
                            QtWidgets.QMessageBox.Yes)
                        if reply == QtWidgets.QMessageBox.Yes:
                            if script == 'JSP':
                                deResult = jspCore.deleteFile(
                                    url, password, dir + file)
                            else:
                                deResult = phpCore.deleteFile(
                                    url, password, dir + file, useRSA)
                            if deResult == '1':
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "删除成功!", '文件已删除')
                            else:
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "删除失败!", '可能没有权限')
                            # 更新文件目录
                            if script == 'JSP':
                                files = jspCore.scanDir(url, password,
                                                        dir).split('\n')
                            else:
                                files = phpCore.scanDir(
                                    url, password, dir, useRSA).split('\n')
                            files = list(filter(None, files))
                            self.updataTable(files, fileTableWidget)
                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "删除文件失败!",
                                                    str(Exception(e)))
                elif action == item4:
                    try:
                        file = fileTableWidget.item(self.mainWindow.row_num,
                                                    0).text()
                        rmode = fileTableWidget.item(self.mainWindow.row_num,
                                                     3).text()
                        nmode, ok = QtWidgets.QInputDialog.getText(
                            self.mainWindow, '更改权限', '更改为:', text=rmode)
                        if ok:
                            searchObj = re.search('^0[0-7][0-7][0-7]$', nmode)
                            if searchObj is None:
                                raise Exception('输入不合法')
                            if script == 'JSP':
                                chmodResult = jspCore.chmodFile(
                                    url, password, dir + file, nmode)
                            else:
                                chmodResult = phpCore.chmodFile(
                                    url, password, dir + file, nmode, useRSA)
                            if chmodResult == '1':
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "更改成功!", '权限已经更改')
                            else:
                                QtWidgets.QMessageBox.about(
                                    self.mainWindow, "更改失败!", '更改权限失败!')
                            # 更新文件目录
                            if script == 'JSP':
                                files = jspCore.scanDir(url, password,
                                                        dir).split('\n')
                            else:
                                files = phpCore.scanDir(
                                    url, password, dir, useRSA).split('\n')
                            files = list(filter(None, files))
                            self.updataTable(files, fileTableWidget)
                    except Exception as e:
                        QtWidgets.QMessageBox.about(self.mainWindow, "更改权限失败!",
                                                    str(Exception(e)))
                elif action == item5:
                    # 更新文件目录
                    if script == 'JSP':
                        files = jspCore.scanDir(url, password, dir).split('\n')
                    else:
                        files = phpCore.scanDir(url, password, dir,
                                                useRSA).split('\n')
                    files = list(filter(None, files))
                    self.updataTable(files, fileTableWidget)
        else:
            item1 = menu.addAction('上传文件')
            item1.setIcon(
                QIcon(
                    os.path.dirname(os.path.realpath(sys.argv[0])) +
                    '/Icons/upload_easyicon.svg'))
            item2 = menu.addAction('刷新目录')
            item2.setIcon(
                QIcon(
                    os.path.dirname(os.path.realpath(sys.argv[0])) +
                    '/Icons/refresh_easyicon.svg'))
            action = menu.exec_(fileTableWidget.mapToGlobal(pos))
            if action == item1:
                try:
                    filePath = QtWidgets.QFileDialog.getOpenFileName(
                        self.mainWindow, '选择文件')
                    if filePath[0] != '':
                        with open(filePath[0], encoding='utf-8') as f:
                            buffer = f.read()
                        if script == 'JSP':
                            r = jspCore.uploadFile(
                                url, password, buffer,
                                dir + os.path.basename(filePath[0]))
                        else:
                            r = phpCore.uploadFile(
                                url, password, buffer,
                                dir + os.path.basename(filePath[0]), useRSA)
                        if r == '1':
                            QtWidgets.QMessageBox.about(
                                self.mainWindow, "上传成功!", '文件已上传')
                            # 更新文件目录
                            if script == 'JSP':
                                files = jspCore.scanDir(url, password,
                                                        dir).split('\n')
                            else:
                                files = phpCore.scanDir(
                                    url, password, dir, useRSA).split('\n')
                            files = list(filter(None, files))
                            self.updataTable(files, fileTableWidget)
                        else:
                            QtWidgets.QMessageBox.about(
                                self.mainWindow, "上传失败!", '可能没有权限')

                except Exception as e:
                    QtWidgets.QMessageBox.about(self.mainWindow, "上传失败!",
                                                str(Exception(e)))
            elif action == item2:
                # 更新文件目录
                if script == 'JSP':
                    files = jspCore.scanDir(url, password, dir).split('\n')
                else:
                    files = phpCore.scanDir(url, password, dir,
                                            useRSA).split('\n')
                files = list(filter(None, files))
                self.updataTable(files, fileTableWidget)