From e944c226d85cfd39f6fccc1fde85cf291cb9ef54 Mon Sep 17 00:00:00 2001 From: Sergey Ponomarev Date: Sun, 7 Jun 2026 18:18:58 +0300 Subject: [PATCH] fileupload plugin: show errors and progress --- .../main/resources/i18n/spark_i18n.properties | 1 + .../resources/i18n/spark_i18n_ru.properties | 1 + .../plugin/fileupload/ChatRoomDecorator.java | 52 +++++++++++++++---- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/core/src/main/resources/i18n/spark_i18n.properties b/core/src/main/resources/i18n/spark_i18n.properties index 03b06ea55..08124e578 100644 --- a/core/src/main/resources/i18n/spark_i18n.properties +++ b/core/src/main/resources/i18n/spark_i18n.properties @@ -560,6 +560,7 @@ message.file.transfer.history.request.sent = Request for transfer file "{0}" ({1 message.file.transfer.history.send.complete = File "{0}" was successfully sent to {1}. message.file.transfer.history.send.error = Sending file "{0}" to {1} failed. message.file.transfer.history.send.canceled = Sending file "{0}" to {1} canceled. +message.file.transfer.history.send.quota=Reached quota. message.file.transfer.history.contact.rejected = {1} rejected transfer request for file "{0}". message.file.transfer.history.request.received = Request for transfer file "{0}" ({1}) was received from {2}. message.file.transfer.history.you.rejected = You have rejected transfer request for file "{0}" from {1}. diff --git a/core/src/main/resources/i18n/spark_i18n_ru.properties b/core/src/main/resources/i18n/spark_i18n_ru.properties index 461f5847c..91112e9bc 100644 --- a/core/src/main/resources/i18n/spark_i18n_ru.properties +++ b/core/src/main/resources/i18n/spark_i18n_ru.properties @@ -540,6 +540,7 @@ message.file.transfer.history.request.sent = Запрос на передачу message.file.transfer.history.send.complete = Файл "{0}" был успешно передан пользователю {1}. message.file.transfer.history.send.error = Не удалось отправить файл "{0}" пользователю {1}. message.file.transfer.history.send.canceled = Отправка файла "{0}" пользователю {1} отменена. +message.file.transfer.history.send.quota=Дошли до ограничения по квоте. message.file.transfer.history.contact.rejected = {1} отклонил запрос на передачу файла "{0}". message.file.transfer.history.request.received = Запрос на передачу файла "{0}" ({1}) получен от {2}. message.file.transfer.history.you.rejected = Вы отклонили запрос на передачу файла "{0}" от {1}. diff --git a/plugins/fileupload/src/main/java/org/jivesoftware/spark/plugin/fileupload/ChatRoomDecorator.java b/plugins/fileupload/src/main/java/org/jivesoftware/spark/plugin/fileupload/ChatRoomDecorator.java index 99fbe5e64..ef36e0927 100644 --- a/plugins/fileupload/src/main/java/org/jivesoftware/spark/plugin/fileupload/ChatRoomDecorator.java +++ b/plugins/fileupload/src/main/java/org/jivesoftware/spark/plugin/fileupload/ChatRoomDecorator.java @@ -22,18 +22,25 @@ import org.jivesoftware.smack.packet.MessageBuilder; import org.jivesoftware.smack.packet.StanzaBuilder; import org.jivesoftware.smack.packet.StandardExtensionElement; import org.jivesoftware.smackx.httpfileupload.HttpFileUploadManager; +import org.jivesoftware.smackx.httpfileupload.UploadService; import org.jivesoftware.spark.component.RolloverButton; import org.jivesoftware.spark.ui.ChatRoom; import org.jivesoftware.spark.util.GraphicUtils; import org.jivesoftware.spark.util.log.Log; +import org.jivesoftware.sparkimpl.plugin.filetransfer.transfer.ui.TransferUtils; -import javax.swing.JOptionPane; import javax.swing.SwingUtilities; -import javax.swing.UIManager; import java.awt.FileDialog; import java.awt.Frame; import java.io.File; import java.net.URL; +import java.time.Duration; +import java.time.Instant; + +import static org.jivesoftware.smack.XMPPException.*; +import static org.jivesoftware.smack.packet.StanzaError.Condition.resource_constraint; +import static org.jivesoftware.spark.ChatManager.ERROR_COLOR; +import static org.jivesoftware.spark.ChatManager.NOTIFICATION_COLOR; public class ChatRoomDecorator { private final HttpFileUploadManager httpFileUploadManager; @@ -73,16 +80,43 @@ public class ChatRoomDecorator { private void handleUpload(File file, ChatRoom room) { Log.debug("Uploading file: " + file.getAbsolutePath()); + long fileSize = file.length(); + if (fileSize == 0) { + return; + } + UploadService uploadService = httpFileUploadManager.getDefaultUploadService(); + Long maxSize = uploadService.getMaxFileSize(); + if (maxSize != null) { + if (fileSize > maxSize) { + String maxsizeString = TransferUtils.getAppropriateByteWithSuffix(maxSize); + String yoursizeString = TransferUtils.getAppropriateByteWithSuffix(fileSize); + String fileMaxSizeMsg = Res.getString("message.file.transfer.file.too.big.error", maxsizeString, yoursizeString); + room.getTranscriptWindow().insertNotificationMessage(fileMaxSizeMsg, ERROR_COLOR); + return; + } + } try { - URL uploadedFile = httpFileUploadManager.uploadFile(file); + Instant start = Instant.now(); + URL uploadedFile = httpFileUploadManager.uploadFile(file, (uploadedBytes, totalBytes) -> { + if (Duration.between(start, Instant.now()).toSeconds() < 10) { + return; + } + long progress = totalBytes > 0 ? uploadedBytes / totalBytes : 100; + room.getTranscriptWindow().insertNotificationMessage(progress + "%", NOTIFICATION_COLOR); + }); broadcastUploadUrl(uploadedFile.toString()); } catch (Exception e) { - Log.error("Error while attempting to uploading file", e); - UIManager.put("OptionPane.okButtonText", Res.getString("ok")); - JOptionPane.showMessageDialog(room, - "Upload failed: " + e.getMessage(), - "Http File Upload Plugin", - JOptionPane.ERROR_MESSAGE); + String errMsg = e.getMessage(); + if (e instanceof XMPPErrorException) { + if (((XMPPErrorException)e).getStanzaError().getCondition() == resource_constraint) { + errMsg += "\n" + Res.getString("message.file.transfer.history.send.quota"); + } + } else { + Log.error("Error while attempting to uploading file", e); + } + String fileSendErrorMsg = Res.getString("message.file.transfer.history.send.error", + file.getAbsolutePath(), room.getTabTitle()) + ":\n" + errMsg; + room.getTranscriptWindow().insertNotificationMessage(fileSendErrorMsg, ERROR_COLOR); } }