Skip to content

Commit de2910b

Browse files
committed
添加一些接口
1 parent a3475cd commit de2910b

File tree

12 files changed

+381
-23
lines changed

12 files changed

+381
-23
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package run.ikaros.plugin.pan115;
2+
import run.ikaros.api.custom.Custom;
3+
import run.ikaros.api.custom.Name;
4+
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
10+
@Data
11+
@Builder
12+
@NoArgsConstructor
13+
@AllArgsConstructor
14+
@Custom(group = "run.ikaros.plugin.pan115", version = "v1",
15+
kind = "AttachmentPanCustom", singular = "attachment_pan", plural = "attachment_pans")
16+
public class AttachmentPanCustom {
17+
@Name
18+
private String title;
19+
private Long attId;
20+
private String fid;
21+
private String pid;
22+
private String fc;
23+
private String fn;
24+
private String fs;
25+
private String ico;
26+
private String pc;
27+
private String sha1;
28+
private String path;
29+
private Long playLong;
30+
private Long count;
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package run.ikaros.plugin.pan115;
2+
3+
import org.springframework.beans.factory.InitializingBean;
4+
import org.springframework.stereotype.Component;
5+
import run.ikaros.api.custom.scheme.CustomSchemeManager;
6+
7+
import lombok.extern.slf4j.Slf4j;
8+
9+
@Slf4j
10+
@Component
11+
public class CustomSchemeRegister implements InitializingBean {
12+
private final CustomSchemeManager customSchemeManager;
13+
14+
public CustomSchemeRegister(CustomSchemeManager customSchemeManager) {
15+
this.customSchemeManager = customSchemeManager;
16+
}
17+
18+
@Override
19+
public void afterPropertiesSet() {
20+
customSchemeManager.register(AttachmentPanCustom.class);
21+
}
22+
}

src/main/java/run/ikaros/plugin/pan115/Pan115AttachmentDriverFetcher.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,31 @@
22

33
import lombok.extern.slf4j.Slf4j;
44
import org.pf4j.Extension;
5+
import org.springframework.util.Assert;
56
import run.ikaros.api.core.attachment.Attachment;
67
import run.ikaros.api.core.attachment.AttachmentDriver;
78
import run.ikaros.api.core.attachment.AttachmentDriverFetcher;
9+
import run.ikaros.api.core.attachment.AttachmentOperate;
10+
import run.ikaros.api.custom.ReactiveCustomClient;
11+
import run.ikaros.api.infra.utils.ReactiveBeanUtils;
12+
import run.ikaros.api.infra.utils.StringUtils;
813
import run.ikaros.api.store.enums.AttachmentDriverType;
914

1015
import java.util.List;
1116

1217
@Slf4j
1318
@Extension
1419
public class Pan115AttachmentDriverFetcher implements AttachmentDriverFetcher {
20+
private final ReactiveCustomClient reactiveCustomClient;
21+
private final AttachmentOperate attachmentOperate;
1522
private AttachmentDriver driver;
1623

24+
public Pan115AttachmentDriverFetcher(ReactiveCustomClient reactiveCustomClient,
25+
AttachmentOperate attachmentOperate) {
26+
this.reactiveCustomClient = reactiveCustomClient;
27+
this.attachmentOperate = attachmentOperate;
28+
}
29+
1730
@Override
1831
public AttachmentDriverType getDriverType() {
1932
return AttachmentDriverType.CUSTOM;
@@ -31,6 +44,13 @@ public void setDriver(AttachmentDriver attachmentDriver) {
3144

3245
@Override
3346
public List<Attachment> getChildAttachments(Long pid, String remotePath) {
47+
Assert.notNull(driver, "Attachment driver is null");
48+
Assert.isTrue(pid >= 0, "pid is negative");
49+
// 115 网盘挂载的这个属性其实就是目录ID(cid/fid)
50+
String cid = StringUtils.isEmpty(remotePath) ? "0" : remotePath;
51+
52+
53+
3454
return List.of();
3555
}
3656

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
package run.ikaros.plugin.pan115;
22

3-
public class Pan115Const {
3+
public interface Pan115Const {
4+
String API_BASE = "https://proapi.115.com";
5+
String HOME_PAGE = "https://ikaros.run";
6+
String REPO_GITHUB_NAME = "ikaros-dev/ikaros";
7+
String REST_TEMPLATE_USER_AGENT = REPO_GITHUB_NAME + " (" + HOME_PAGE + ")";
8+
String TOKEN_PREFIX = "Bearer ";
49
}

src/main/java/run/ikaros/plugin/pan115/Pan115Plugin.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,6 @@ public Pan115Plugin(PluginWrapper wrapper, TaskOperate taskOperate) {
2222
@Override
2323
public void start() {
2424
log.info("plugin [Pan115Plugin] start success");
25-
// submitParallelTask();
26-
}
27-
28-
private void submitParallelTask() {
29-
for (int i = 0; i < 10; i++) {
30-
int finalI = i;
31-
taskOperate.submit(getWrapper().getPluginId() + "-parallel-" + i,
32-
new Runnable() {
33-
@Override
34-
public void run() {
35-
log.info("Submit task with index:{}", finalI);
36-
try {
37-
Thread.sleep(new Random().nextLong(0, 5000));
38-
} catch (InterruptedException e) {
39-
throw new RuntimeException(e);
40-
}
41-
log.info("Finish task with index:{}", finalI);
42-
43-
}
44-
})
45-
.subscribe();
46-
}
4725
}
4826

4927
@Override
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package run.ikaros.plugin.pan115;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
@Data
9+
@Builder
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
public class Path115 {
13+
private String name;
14+
private Long aid;
15+
private Long cid;
16+
private Long pid;
17+
private Long isp;
18+
private String p_cid;
19+
private String fv;
20+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package run.ikaros.plugin.pan115.repository;
2+
3+
import com.fasterxml.jackson.core.type.TypeReference;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.apache.commons.lang3.StringUtils;
6+
import org.springframework.http.*;
7+
import org.springframework.stereotype.Component;
8+
import org.springframework.web.client.HttpClientErrorException;
9+
import org.springframework.web.client.RestTemplate;
10+
import run.ikaros.api.core.attachment.Attachment;
11+
import run.ikaros.api.core.attachment.AttachmentDriver;
12+
import run.ikaros.api.core.setting.ConfigMap;
13+
import run.ikaros.plugin.pan115.Pan115Const;
14+
import run.ikaros.plugin.pan115.utils.JsonUtils;
15+
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.Objects;
19+
20+
import static run.ikaros.plugin.pan115.Pan115Const.*;
21+
22+
@Slf4j
23+
@Component
24+
public class DefaultPan115Repository implements Pan115Repository {
25+
private final RestTemplate restTemplate = new RestTemplate();
26+
private final HttpHeaders headers = new HttpHeaders();
27+
28+
@Override
29+
public boolean assertDomainReachable() {
30+
try {
31+
restTemplate
32+
.exchange(Pan115Const.API_BASE, HttpMethod.GET,
33+
new HttpEntity<>(null, headers), Map.class);
34+
return true;
35+
} catch (HttpClientErrorException exception) {
36+
return exception.getStatusCode() == HttpStatus.NOT_FOUND;
37+
}
38+
}
39+
40+
@Override
41+
public void refreshHttpHeaders(String accessToken) {
42+
log.info("refresh rest template headers...");
43+
headers.clear();
44+
headers.set(HttpHeaders.USER_AGENT, REST_TEMPLATE_USER_AGENT);
45+
headers.set(HttpHeaders.COOKIE, "chii_searchDateLine=0");
46+
headers.set(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
47+
headers.setContentType(MediaType.APPLICATION_JSON);
48+
if (StringUtils.isNotBlank(accessToken)) {
49+
log.info("update http head access token");
50+
headers.set(HttpHeaders.AUTHORIZATION, TOKEN_PREFIX + accessToken);
51+
}
52+
}
53+
54+
@Override
55+
public List<Pan115Attachment> openUFileFiles(String cid, Integer limit, Integer cur, Integer showDir) {
56+
final String url = API_BASE + " /open/ufile/files";
57+
try {
58+
String result = restTemplate.exchange(url, HttpMethod.GET,
59+
new HttpEntity<>(null, headers), String.class).getBody();
60+
log.debug("Pull [{}] result is [{}].", url, result);
61+
if (StringUtils.isBlank(result)) {
62+
return null;
63+
}
64+
Map map = JsonUtils.json2obj(result, Map.class);
65+
Object data = map.remove("data");
66+
log.debug("Pull [{}] result data is [{}].", url, data);
67+
Pan115Attachment[] pan115Attachments =
68+
JsonUtils.json2ObjArr(JsonUtils.obj2Json(data), new TypeReference<>() {
69+
});
70+
if (pan115Attachments == null) {
71+
return List.of();
72+
}
73+
74+
return List.of(pan115Attachments);
75+
} catch (HttpClientErrorException exception) {
76+
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
77+
log.warn("pan115 attachment not found for cid={}", cid);
78+
return null;
79+
}
80+
throw exception;
81+
}
82+
}
83+
84+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package run.ikaros.plugin.pan115.repository;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.util.ArrayList;
9+
10+
@Data
11+
@Builder
12+
@NoArgsConstructor
13+
@AllArgsConstructor
14+
public class Pan115Attachment {
15+
private String fid;
16+
private String aid;
17+
private String pid;
18+
private String fc;
19+
private String fn;
20+
private String fco;
21+
private String ism;
22+
private int isp;
23+
private int iss;
24+
private String pc;
25+
private int upt;
26+
private int uet;
27+
private int uppt;
28+
private int cm;
29+
private String fdesc;
30+
private int ispl;
31+
private int fvs;
32+
private int fuuid;
33+
private int opt;
34+
private ArrayList<Object> fl;
35+
private int issct;
36+
private int is_top;
37+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package run.ikaros.plugin.pan115.repository;
2+
3+
import run.ikaros.api.core.attachment.Attachment;
4+
5+
import java.util.List;
6+
7+
public interface Pan115Repository {
8+
boolean assertDomainReachable();
9+
10+
void refreshHttpHeaders(String accessToken);
11+
12+
List<Pan115Attachment> openUFileFiles(String cid, Integer limit, Integer cur,
13+
Integer showDir);
14+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package run.ikaros.plugin.pan115.utils;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
/**
6+
* @author li-guohao
7+
*/
8+
public class AssertUtils {
9+
10+
public static void notNull(Object obj, String varName) {
11+
if (obj == null) {
12+
throw new IllegalArgumentException("'" + varName + "' must not be null");
13+
}
14+
}
15+
16+
public static void notBlank(String str, String varName) {
17+
notNull(str, varName);
18+
if (StringUtils.isBlank(str)) {
19+
throw new IllegalArgumentException("'" + varName + "' must not be blank");
20+
}
21+
}
22+
23+
24+
public static void isPositive(long number, String varName) {
25+
if (number < 0) {
26+
throw new IllegalArgumentException("'" + varName + "' must be positive");
27+
}
28+
}
29+
30+
public static void isTrue(boolean condition, String varName) {
31+
if (!condition) {
32+
throw new IllegalArgumentException("'" + varName + "' must be true");
33+
}
34+
}
35+
36+
public static void isFalse(boolean condition, String varName) {
37+
if (condition) {
38+
throw new IllegalArgumentException("'" + varName + "' must be false");
39+
}
40+
}
41+
42+
}

0 commit comments

Comments
 (0)