264 lines
8.9 KiB
Python
264 lines
8.9 KiB
Python
# coding: utf-8
|
||
import os
|
||
|
||
from PyQt6.QtCore import Qt, QTimer
|
||
from PyQt6.QtGui import QPixmap
|
||
from PyQt6.QtWidgets import QHBoxLayout, QVBoxLayout
|
||
from qfluentwidgets import (BodyLabel, CardWidget, FluentIcon, ImageLabel, InfoBar, InfoBarPosition, PrimaryToolButton,
|
||
ProgressBar, SubtitleLabel)
|
||
|
||
from app.core import (DownloadShareThread, DownloadThread, formatSize, getFileIcon, signalBus, UploadThread)
|
||
|
||
|
||
class UploadCard(CardWidget):
|
||
def __init__(self, fileType, filePath, parent=None):
|
||
super().__init__(parent=parent)
|
||
self.fileType = fileType
|
||
self.filePath = filePath
|
||
self.fileName = os.path.basename(filePath)
|
||
|
||
self.setFixedHeight(75)
|
||
|
||
self.hBoxLayout = QHBoxLayout(self)
|
||
self.infomationLayout = QVBoxLayout()
|
||
|
||
self.iconLabel = ImageLabel(self)
|
||
|
||
self.fileNameLabel = SubtitleLabel(self.fileName, self)
|
||
self.currentStatusLabel = BodyLabel("等待中...", self)
|
||
self.progressBar = ProgressBar(self)
|
||
self.progressBar.setValue(0)
|
||
|
||
self.cancelButton = PrimaryToolButton(FluentIcon.DELETE, self)
|
||
self.retryButton = PrimaryToolButton(FluentIcon.RETURN, self)
|
||
self.retryButton.clicked.connect(self.retryUpload)
|
||
self.cancelButton.clicked.connect(self.cancelUpload)
|
||
|
||
self.hBoxLayout.addWidget(
|
||
self.iconLabel, 0, Qt.AlignmentFlag.AlignCenter | Qt.AlignmentFlag.AlignLeft
|
||
)
|
||
self.hBoxLayout.addSpacing(5)
|
||
|
||
self.infomationLayout.addWidget(
|
||
self.fileNameLabel,
|
||
0,
|
||
Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft,
|
||
)
|
||
self.infomationLayout.addWidget(
|
||
self.currentStatusLabel,
|
||
0,
|
||
Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft,
|
||
)
|
||
self.infomationLayout.addWidget(self.progressBar)
|
||
self.hBoxLayout.addLayout(self.infomationLayout)
|
||
|
||
self.hBoxLayout.addSpacing(10)
|
||
|
||
self.hBoxLayout.addWidget(
|
||
self.retryButton,
|
||
0,
|
||
Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter,
|
||
)
|
||
self.hBoxLayout.addWidget(
|
||
self.cancelButton,
|
||
0,
|
||
Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter,
|
||
)
|
||
|
||
self.setIcon()
|
||
|
||
self.uploadThread = None
|
||
self.startUpload()
|
||
|
||
def startUpload(self):
|
||
self.retryButton.setEnabled(False)
|
||
self.uploadThread = UploadThread(self.filePath)
|
||
self.uploadThread.uploadApplicationApprovedSignal.connect(
|
||
self.uploadApplication
|
||
)
|
||
self.uploadThread.uploadFinished.connect(self.uploadFinished)
|
||
self.uploadThread.uploadFailed.connect(self.uploadFailed)
|
||
self.uploadThread.uploadProgress.connect(self.uploadProgress)
|
||
self.uploadThread.start()
|
||
|
||
def retryUpload(self):
|
||
self.currentStatusLabel.setText("重试...")
|
||
self.startUpload()
|
||
|
||
def cancelUpload(self):
|
||
if self.uploadThread:
|
||
self.progressBar.setValue(0)
|
||
self.selfDelete()
|
||
else:
|
||
self.selfDelete()
|
||
|
||
def selfDelete(self):
|
||
self.currentStatusLabel.setText("取消中...")
|
||
self.cancelButton.setEnabled(False)
|
||
self.retryButton.setEnabled(False)
|
||
|
||
if self.uploadThread:
|
||
|
||
self.uploadThread.cancelUpload()
|
||
self.uploadThread.terminate()
|
||
self.uploadThread = None
|
||
|
||
QTimer.singleShot(1000, self.deleteLater)
|
||
|
||
def uploadApplication(self):
|
||
self.currentStatusLabel.setText("已向服务器提交任务,读取文件中...")
|
||
|
||
def uploadFinished(self):
|
||
self.currentStatusLabel.setText("上传成功")
|
||
InfoBar.success(
|
||
"成功",
|
||
f"{self.fileName}上传成功,请注意查看~",
|
||
Qt.Orientation.Horizontal,
|
||
True,
|
||
5000,
|
||
InfoBarPosition.BOTTOM_RIGHT,
|
||
InfoBar.desktopView(),
|
||
)
|
||
signalBus.refreshFolderListSignal.emit()
|
||
self.retryButton.setEnabled(False)
|
||
self.progressBar.setValue(100)
|
||
|
||
def uploadProgress(self, progress, uploaded_size, total_size):
|
||
self.progressBar.setValue(int(progress))
|
||
self.currentStatusLabel.setText(
|
||
f"上传中...({formatSize(uploaded_size)}/{formatSize(total_size)})"
|
||
)
|
||
|
||
def uploadFailed(self, error_message):
|
||
self.currentStatusLabel.setText(f"上传失败:{error_message}")
|
||
self.progressBar.setValue(0)
|
||
self.retryButton.setEnabled(True)
|
||
self.uploadThread.terminate()
|
||
self.uploadThread = None
|
||
|
||
def setIcon(self):
|
||
icon_name = getFileIcon("file", self.fileName)
|
||
self.iconLabel.setImage(QPixmap(f":app/icons/{icon_name}"))
|
||
self.iconLabel.scaledToHeight(50)
|
||
self.iconLabel.scaledToWidth(50)
|
||
|
||
|
||
class DownloadCard(CardWidget):
|
||
def __init__(self, suffix, fileName, _id, parent=None):
|
||
super().__init__(parent=parent)
|
||
self._id = _id
|
||
self.suffix = suffix
|
||
|
||
self.setFixedHeight(75)
|
||
|
||
self.hBoxLayout = QHBoxLayout(self)
|
||
self.infomationLayout = QVBoxLayout()
|
||
|
||
self.iconLabel = ImageLabel(self)
|
||
|
||
self.fileNameLabel = SubtitleLabel(fileName, self)
|
||
self.currentStatusLabel = BodyLabel("请求中...", self)
|
||
self.progressBar = ProgressBar(self)
|
||
self.progressBar.setValue(0)
|
||
|
||
self.cancelButton = PrimaryToolButton(FluentIcon.DELETE, self)
|
||
|
||
self.cancelButton.clicked.connect(self.cancelDownload)
|
||
|
||
self.hBoxLayout.addWidget(
|
||
self.iconLabel, 0, Qt.AlignmentFlag.AlignCenter | Qt.AlignmentFlag.AlignLeft
|
||
)
|
||
self.hBoxLayout.addSpacing(5)
|
||
|
||
self.infomationLayout.addWidget(
|
||
self.fileNameLabel,
|
||
0,
|
||
Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft,
|
||
)
|
||
self.infomationLayout.addWidget(
|
||
self.currentStatusLabel,
|
||
0,
|
||
Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft,
|
||
)
|
||
self.infomationLayout.addWidget(self.progressBar)
|
||
self.hBoxLayout.addLayout(self.infomationLayout)
|
||
|
||
self.hBoxLayout.addSpacing(10)
|
||
|
||
self.hBoxLayout.addWidget(
|
||
self.cancelButton,
|
||
0,
|
||
Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter,
|
||
)
|
||
suffix = self.suffix.split(".")[1].lower()
|
||
self._type = self.suffix.split(".")[0].lower()
|
||
self.setIcon(suffix)
|
||
self.downloadThread = None
|
||
self.startUpload()
|
||
|
||
def startUpload(self):
|
||
if self._type == "own":
|
||
# 同时传递file_id和file_path,确保file_path不为空
|
||
# 对于own类型,使用self._id作为file_path,因为它已经包含了完整的cloudreve://格式路径
|
||
self.downloadThread = DownloadThread(self._id, self._id)
|
||
elif self._type == "share":
|
||
self.downloadThread = DownloadShareThread(self._id)
|
||
self.downloadThread.downloadUrlAcquired.connect(self.downloadUrlAcquired)
|
||
self.downloadThread.downloadFinished.connect(self.downloadFinished)
|
||
self.downloadThread.downloadFailed.connect(self.downloadFailed)
|
||
self.downloadThread.downloadProgress.connect(self.downloadProgress)
|
||
self.downloadThread.start()
|
||
|
||
def cancelDownload(self):
|
||
if self.downloadThread:
|
||
self.progressBar.setValue(0)
|
||
self.selfDelete()
|
||
else:
|
||
self.selfDelete()
|
||
|
||
def selfDelete(self):
|
||
self.currentStatusLabel.setText("取消中...")
|
||
self.cancelButton.setEnabled(False)
|
||
|
||
if self.downloadThread:
|
||
|
||
self.downloadThread.cancelDownload()
|
||
self.downloadThread.terminate()
|
||
self.downloadThread = None
|
||
|
||
QTimer.singleShot(1000, self.deleteLater)
|
||
|
||
def downloadUrlAcquired(self):
|
||
self.currentStatusLabel.setText("成功获取下载链接,准备下载...")
|
||
|
||
def downloadFinished(self):
|
||
self.currentStatusLabel.setText("下载成功")
|
||
InfoBar.success(
|
||
"成功",
|
||
f"{self._id}下载成功,请注意查看~",
|
||
Qt.Orientation.Horizontal,
|
||
True,
|
||
5000,
|
||
InfoBarPosition.BOTTOM_RIGHT,
|
||
InfoBar.desktopView(),
|
||
)
|
||
self.progressBar.setValue(100)
|
||
|
||
def downloadProgress(self, progress, uploaded_size, total_size):
|
||
self.progressBar.setValue(int(progress))
|
||
self.currentStatusLabel.setText(
|
||
f"下载中...({formatSize(uploaded_size)}/{formatSize(total_size)})"
|
||
)
|
||
|
||
def downloadFailed(self, error_message):
|
||
self.currentStatusLabel.setText(f"下载失败:{error_message}")
|
||
self.progressBar.setValue(0)
|
||
self.downloadThread.terminate()
|
||
self.downloadThread = None
|
||
|
||
def setIcon(self, fileName):
|
||
icon_name = getFileIcon("file", fileName)
|
||
self.iconLabel.setImage(QPixmap(f":app/icons/{icon_name}"))
|
||
self.iconLabel.scaledToHeight(50)
|
||
self.iconLabel.scaledToWidth(50)
|