This commit is contained in:
NikoXu 2025-02-12 08:16:35 +00:00
parent 4f4f51ee37
commit c7110a15ea
3 changed files with 88 additions and 11 deletions

View File

@ -1,6 +1,7 @@
package cn.montaro.aria2;
import cn.montaro.aria2.annotation.Aria2Method;
import cn.montaro.aria2.bean.Task;
import cn.montaro.aria2.constants.Aria2MethodName;
import cn.montaro.aria2.resp.Aria2Status;
@ -80,7 +81,7 @@ public interface Aria2Client {
String getServers(String gid);
@Aria2Method(Aria2MethodName.TELL_ACTIVE)
String tellActive(String... keys);
List<Task> tellActive(String... keys);
@Aria2Method(Aria2MethodName.TELL_WAITING)
String tellWaiting(int offset, int num, String... keys);

View File

@ -0,0 +1,78 @@
package cn.montaro.aria2.bean;
import java.io.Serializable;
import java.util.Objects;
public class Task implements Serializable {
public Long completedLength;
private Integer connections;
private Long downloadSpeed;
private String gid;
private Long totalLength;
private String status;
public int getProgress() {
if (Objects.isNull(completedLength)
|| completedLength == 0
|| Objects.isNull(totalLength)
|| totalLength == 0) {
return 0;
}
return (int) (completedLength * 100 / totalLength);
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Long getTotalLength() {
return totalLength;
}
public void setTotalLength(Long totalLength) {
this.totalLength = totalLength;
}
public String getGid() {
return gid;
}
public void setGid(String gid) {
this.gid = gid;
}
public Long getDownloadSpeed() {
return downloadSpeed;
}
public void setDownloadSpeed(Long downloadSpeed) {
this.downloadSpeed = downloadSpeed;
}
public Integer getConnections() {
return connections;
}
public void setConnections(Integer connections) {
this.connections = connections;
}
public Long getCompletedLength() {
return completedLength;
}
public void setCompletedLength(Long completedLength) {
this.completedLength = completedLength;
}
}

View File

@ -8,10 +8,7 @@ import com.google.gson.*;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ListIterator;
import java.util.UUID;
import java.util.*;
public class Aria2HttpProxy implements InvocationHandler {
@ -26,6 +23,9 @@ public class Aria2HttpProxy implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Aria2Method aria2Method = method.getDeclaredAnnotation(Aria2Method.class);
if (Objects.isNull(aria2Method)) {
return method.invoke(this, args);
}
String methodName = aria2Method.value();
Type resultType = method.getGenericReturnType();
String body = this.serialize(methodName, args);
@ -79,14 +79,12 @@ public class Aria2HttpProxy implements InvocationHandler {
}
private Object deserialize(String json, Type resultType) {
if (resultType.equals(String.class)) {
return json;
}
JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
JsonElement result = jsonObject.get("result");
if (result instanceof JsonObject && resultType.equals(String.class)) {
return result.toString();
}
if (resultType.equals(String.class)) {
return result.getAsString();
}
return gson.fromJson(result, resultType);
}
}