summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/ftp/UploadTask.java
diff options
context:
space:
mode:
Diffstat (limited to 'dozentenmodul/src/main/java/ftp/UploadTask.java')
-rw-r--r--dozentenmodul/src/main/java/ftp/UploadTask.java143
1 files changed, 72 insertions, 71 deletions
diff --git a/dozentenmodul/src/main/java/ftp/UploadTask.java b/dozentenmodul/src/main/java/ftp/UploadTask.java
index 61f81c3b..7069db29 100644
--- a/dozentenmodul/src/main/java/ftp/UploadTask.java
+++ b/dozentenmodul/src/main/java/ftp/UploadTask.java
@@ -1,15 +1,17 @@
package ftp;
import java.io.File;
-import java.io.FileInputStream;
+import java.io.IOException;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
-import org.apache.log4j.Logger;
-
import models.Image;
+import org.apache.log4j.Logger;
+import org.openslx.filetransfer.UploadStatusCallback;
+import org.openslx.filetransfer.Uploader;
+
/**
* Executes the file upload in a background thread and updates progress to
* listeners that implement the java.beans.PropertyChangeListener interface.
@@ -24,26 +26,21 @@ public class UploadTask extends SwingWorker<Void, Void> {
*/
private final static Logger LOGGER = Logger.getLogger(UploadTask.class);
- private static final int BUFFER_SIZE = 1024 * 1024;
private static final double UPDATE_INTERVAL_SECONDS = 0.6;
private static final double UPDATE_INTERVAL_MS = UPDATE_INTERVAL_SECONDS * 1000;
private static final double BYTES_PER_MIB = 1024 * 1024;
- private String host;
- private int port;
- private String username;
- private String password;
- private String destDir;
- private File uploadFile;
- private int percentCompleted;
+ private final String host;
+ private final int port;
+ private final String uploadToken;
+ private final File uploadFile;
+ private boolean success = false;
- public UploadTask(String host, int port, String username, String password,
- String destDir, File uploadFile) {
+ public UploadTask(String host, int port, String uploadToken, File uploadFile) {
+ // TODO: SSL
this.host = host;
this.port = port;
- this.username = username;
- this.password = password;
- this.destDir = destDir;
+ this.uploadToken = uploadToken;
this.uploadFile = uploadFile;
}
@@ -52,60 +49,63 @@ public class UploadTask extends SwingWorker<Void, Void> {
* Executed in background thread
*/
@Override
- protected Void doInBackground() throws Exception {
- FTPUtility util = new FTPUtility(host, port, username, password);
+ protected Void doInBackground() {
+ Uploader upload = null;
try {
- util.connect();
- util.uploadFile(uploadFile, destDir);
-
- // show filesize in the GUI
- long fileSize = uploadFile.length();
- Image.Filesize = fileSize;
- firePropertyChange("filesize", 0, fileSize);
-
- // prepare input stream
- FileInputStream inputStream = new FileInputStream(uploadFile);
-
- // initialize the counters needed for speed calculations
- percentCompleted = 0;
- byte[] buffer = new byte[BUFFER_SIZE];
- int bytesRead = -1;
- long totalBytesRead = 0;
- long lastUpdate = 0;
- long lastBytes = 0;
- long currentBytes = 0;
- while ((bytesRead = inputStream.read(buffer)) != -1 && !isCancelled()) {
- util.writeFileBytes(buffer, 0, bytesRead);
- currentBytes += bytesRead;
- totalBytesRead += bytesRead;
- long now = System.currentTimeMillis();
- if (lastUpdate + UPDATE_INTERVAL_MS < now) {
- percentCompleted = (int) ((totalBytesRead * 100) / fileSize);
- setProgress(percentCompleted);
- lastBytes = (lastBytes * 2 + currentBytes) / 3;
- final double speed = lastBytes / UPDATE_INTERVAL_SECONDS;
- firePropertyChange("speed", 0, speed / BYTES_PER_MIB);
- firePropertyChange("bytesread", 0, totalBytesRead);
- lastUpdate = now;
- currentBytes = 0;
+ upload = new Uploader(host, port, null, uploadToken); // TODO: SSL
+
+ final Uploader ul = upload;
+ final long fileSize = Image.Filesize = uploadFile.length();
+ firePropertyChange("filesize", 0, fileSize); // Updates GUI
+
+ final boolean ret = upload.upload(uploadFile.getAbsolutePath(), new UploadStatusCallback() {
+ // progress counter
+ private long totalBytesRead = 0;
+ // initialize the counters needed for speed calculations
+ private long lastUpdate = 0;
+ private long lastBytes = 0;
+ private long currentBytes = 0;
+
+ @Override
+ public void uploadProgress(long bytesSent) {
+ totalBytesRead += bytesSent;
+ currentBytes += bytesSent;
+ final long now = System.currentTimeMillis();
+ if (lastUpdate + UPDATE_INTERVAL_MS < now) {
+ final int percentCompleted = (int) ((totalBytesRead * 100) / fileSize);
+ setProgress(percentCompleted);
+ lastBytes = (lastBytes * 2 + currentBytes) / 3;
+ final double speed = lastBytes / UPDATE_INTERVAL_SECONDS;
+ firePropertyChange("speed", 0, speed / BYTES_PER_MIB);
+ firePropertyChange("bytesread", 0, totalBytesRead);
+ lastUpdate = now;
+ currentBytes = 0;
+ }
}
+
+ @Override
+ public void uploadError(String message) {
+ JOptionPane.showMessageDialog(null, "Error uploading file: " + message, "Error",
+ JOptionPane.ERROR_MESSAGE);
+ ul.close(null);
+ }
+ });
+
+ // if the upload succeeded, set the progress to 100% manually again here to make
+ // sure the GUI knows about it.
+ if (ret) {
+ setProgress(100);
+ firePropertyChange("bytesread", 0, uploadFile.length());
+ firePropertyChange("success", false, true);
+ success = true;
}
- // finalize the upload by updating the progress bar one last time
- // (in case we didn't get to do it because of the time interval)
- percentCompleted = (int) ((totalBytesRead * 100) / fileSize);
- setProgress(percentCompleted);
- firePropertyChange("bytesread", 0, totalBytesRead);
- inputStream.close();
- util.finish();
- } catch (FTPException ex) {
- JOptionPane.showMessageDialog(null,
- "Error uploading file: " + ex.getMessage(), "Error",
+ } catch (IOException e) {
+ JOptionPane.showMessageDialog(null, "Error uploading file: " + e.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
- ex.printStackTrace();
- setProgress(0);
- cancel(true);
+ LOGGER.error("Upload of " + uploadFile.getAbsolutePath() + " failed!", e);
} finally {
- util.disconnect();
+ if (upload != null)
+ upload.close(null);
}
return null;
@@ -116,14 +116,15 @@ public class UploadTask extends SwingWorker<Void, Void> {
*/
@Override
protected void done() {
- if (!isCancelled() && percentCompleted == 100) {
+ if (isCancelled())
+ return;
+ if (success) {
LOGGER.info("Datei erfolgreich hochgeladen.");
- JOptionPane.showMessageDialog(null, "Datei erfolgreich hochgeladen.",
- "Message", JOptionPane.INFORMATION_MESSAGE);
- } else if (!isCancelled() && percentCompleted != 100) {
+ JOptionPane.showMessageDialog(null, "Datei erfolgreich hochgeladen.", "Message",
+ JOptionPane.INFORMATION_MESSAGE);
+ } else {
LOGGER.error("Datei wurde unvollständig hochgeladen.");
- JOptionPane.showMessageDialog(null,
- "Datei wurde unvollständig hochgeladen. Bitte wiederholen.",
+ JOptionPane.showMessageDialog(null, "Datei wurde unvollständig hochgeladen. Bitte wiederholen.",
"Message", JOptionPane.INFORMATION_MESSAGE);
}
}