Browse Source

Started working on upload

pull/407/head
Alon Albert 7 years ago
parent
commit
7c64ded9c2
  1. 2
      app/src/main/java/org/transdroid/core/gui/search/FilePickerHelper.java
  2. 82
      app/src/main/java/org/transdroid/daemon/Deluge/DelugeDirectAdapter.java

2
app/src/main/java/org/transdroid/core/gui/search/FilePickerHelper.java

@ -42,7 +42,7 @@ public class FilePickerHelper {
public static void startFilePicker(final Activity activity) { public static void startFilePicker(final Activity activity) {
try { try {
// Start a file manager that can handle the file/* file/* intents // Start a file manager that can handle the file/* file/* intents
activity.startActivityForResult(new Intent(Intent.ACTION_GET_CONTENT).setType("file/*"), activity.startActivityForResult(new Intent(Intent.ACTION_GET_CONTENT).setType("application/x-bittorrent"),
ACTIVITY_FILEPICKER); ACTIVITY_FILEPICKER);
} catch (Exception e1) { } catch (Exception e1) {
try { try {

82
app/src/main/java/org/transdroid/daemon/Deluge/DelugeDirectAdapter.java

@ -19,11 +19,18 @@ package org.transdroid.daemon.Deluge;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.text.TextUtils; import android.text.TextUtils;
import deluge.impl.net.AcceptAllTrustManager; import deluge.impl.net.AcceptAllTrustManager;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.Socket; import java.net.Socket;
import java.net.URI;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -39,6 +46,8 @@ import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream; import java.util.zip.InflaterInputStream;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManager;
import org.base64.android.Base64;
import org.transdroid.core.gui.log.Log; import org.transdroid.core.gui.log.Log;
import org.transdroid.daemon.Daemon; import org.transdroid.daemon.Daemon;
import org.transdroid.daemon.DaemonException; import org.transdroid.daemon.DaemonException;
@ -51,6 +60,7 @@ import org.transdroid.daemon.Torrent;
import org.transdroid.daemon.TorrentDetails; import org.transdroid.daemon.TorrentDetails;
import org.transdroid.daemon.TorrentFile; import org.transdroid.daemon.TorrentFile;
import org.transdroid.daemon.TorrentStatus; import org.transdroid.daemon.TorrentStatus;
import org.transdroid.daemon.task.AddByFileTask;
import org.transdroid.daemon.task.DaemonTask; import org.transdroid.daemon.task.DaemonTask;
import org.transdroid.daemon.task.DaemonTaskFailureResult; import org.transdroid.daemon.task.DaemonTaskFailureResult;
import org.transdroid.daemon.task.DaemonTaskResult; import org.transdroid.daemon.task.DaemonTaskResult;
@ -59,8 +69,7 @@ import org.transdroid.daemon.task.GetFileListTask;
import org.transdroid.daemon.task.GetFileListTaskSuccessResult; import org.transdroid.daemon.task.GetFileListTaskSuccessResult;
import org.transdroid.daemon.task.GetTorrentDetailsTask; import org.transdroid.daemon.task.GetTorrentDetailsTask;
import org.transdroid.daemon.task.GetTorrentDetailsTaskSuccessResult; import org.transdroid.daemon.task.GetTorrentDetailsTaskSuccessResult;
import org.transdroid.daemon.task.PauseTask; import org.transdroid.daemon.task.RemoveTask;
import org.transdroid.daemon.task.ResumeTask;
import org.transdroid.daemon.task.RetrieveTask; import org.transdroid.daemon.task.RetrieveTask;
import org.transdroid.daemon.task.RetrieveTaskSuccessResult; import org.transdroid.daemon.task.RetrieveTaskSuccessResult;
import se.dimovski.rencode.Rencode; import se.dimovski.rencode.Rencode;
@ -180,25 +189,19 @@ public class DelugeDirectAdapter implements IDaemonAdapter {
case AddByMagnetUrl: case AddByMagnetUrl:
return notSupported(task); return notSupported(task);
case AddByFile: case AddByFile:
return notSupported(task); return doAddByFile((AddByFileTask) task);
case Remove: case Remove:
return notSupported(task); return doRemove((RemoveTask)task);
case Pause: case Pause:
return doPause((PauseTask) task); return doControl(task, METHOD_PAUSE);
case PauseAll: case PauseAll:
return notSupported(task); sendRequest(METHOD_PAUSE_ALL);
return new DaemonTaskSuccessResult(task);
case Resume: case Resume:
return doResume((ResumeTask) task); return doControl(task, METHOD_RESUME);
case ResumeAll: case ResumeAll:
return notSupported(task); sendRequest(METHOD_RESUME_ALL);
case Stop: return new DaemonTaskSuccessResult(task);
return notSupported(task);
case StopAll:
return notSupported(task);
case Start:
return notSupported(task);
case StartAll:
return notSupported(task);
case GetFileList: case GetFileList:
return doGetFileList((GetFileListTask) task); return doGetFileList((GetFileListTask) task);
case SetFilePriorities: case SetFilePriorities:
@ -227,6 +230,45 @@ public class DelugeDirectAdapter implements IDaemonAdapter {
} }
} }
private DaemonTaskResult doAddByFile(AddByFileTask task) throws DaemonException {
final String file = task.getFile();
final byte[] bytes = loadFile(file);
final String fileContent = Base64.encodeBytes(bytes);
sendRequest(METHOD_ADD_FILE, new Object[]{ file, fileContent, new HashMap<>() });
return new DaemonTaskSuccessResult(task);
}
private byte[] loadFile(String url) throws DaemonException {
final File file = new File(URI.create(url));
final BufferedInputStream in;
try {
in = new BufferedInputStream(new FileInputStream(file));
} catch (FileNotFoundException e) {
throw new DaemonException(ExceptionType.FileAccessError, "File not found: " + file.getAbsolutePath());
}
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
final byte[] buffer = new byte[1024];
while (true) {
final int n = in.read(buffer);
if (n < 0) {
break;
}
out.write(buffer, 0, n);
}
return out.toByteArray();
} catch (IOException e) {
throw new DaemonException(ExceptionType.FileAccessError, "Error reading file: " + file.getAbsolutePath());
} finally {
try {
in.close();
} catch (IOException e) {
// ignore
}
}
}
@Override @Override
public Daemon getType() { public Daemon getType() {
return settings.getType(); return settings.getType();
@ -301,13 +343,13 @@ public class DelugeDirectAdapter implements IDaemonAdapter {
return new GetFileListTaskSuccessResult(task, files); return new GetFileListTaskSuccessResult(task, files);
} }
private DaemonTaskResult doResume(ResumeTask task) throws DaemonException { private DaemonTaskResult doControl(DaemonTask task, String method) throws DaemonException {
sendRequest(METHOD_RESUME, new Object[]{ new String[] { task.getTargetTorrent().getUniqueID()}}); sendRequest(method, new Object[]{ new String[] { task.getTargetTorrent().getUniqueID()}});
return new DaemonTaskSuccessResult(task); return new DaemonTaskSuccessResult(task);
} }
private DaemonTaskResult doPause(PauseTask task) throws DaemonException { private DaemonTaskResult doRemove(RemoveTask task) throws DaemonException {
sendRequest(METHOD_PAUSE, new Object[]{ new String[] { task.getTargetTorrent().getUniqueID()}}); sendRequest(METHOD_REMOVE, new Object[]{ task.getTargetTorrent().getUniqueID(), task.includingData()});
return new DaemonTaskSuccessResult(task); return new DaemonTaskSuccessResult(task);
} }

Loading…
Cancel
Save