fileupload plugin: show errors and progress

This commit is contained in:
Sergey Ponomarev
2026-06-07 18:18:58 +03:00
parent 284e29a9f5
commit e944c226d8
3 changed files with 45 additions and 9 deletions

View File

@ -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}.

View File

@ -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}.

View File

@ -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);
}
}