Skip to content

Commit

Permalink
feat: add alist endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
li-guohao committed May 25, 2024
1 parent c1da016 commit 9fd69cb
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 38 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ext {
springContext = '6.0.3'
pf4j = '3.8.0'
lombok = '1.18.24'
libFile = 'lib/api-0.12.0.jar'
libFile = 'lib/api-0.13.0.jar'
}


Expand Down
12 changes: 6 additions & 6 deletions console/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { definePlugin } from "@runikaros/shared"
import HelloIkaros from '@/views/HelloIkaros.vue';
import AListControl from '@/views/AListControl.vue';
import { Files as FilesIcon } from '@element-plus/icons-vue';
import { markRaw } from "vue"

Expand All @@ -10,13 +10,13 @@ export default definePlugin({
{
parentName: "Root",
route: {
path: '/PluginStarter',
component: HelloIkaros,
name: "HelloIkaros",
path: '/PluginAListControl',
component: AListControl,
name: "AListControl",
meta: {
title: '示例页面',
title: 'AList',
menu: {
name: '示例页面',
name: 'AList',
group: 'tool',
icon: markRaw(FilesIcon),
priority: 2,
Expand Down
23 changes: 23 additions & 0 deletions console/src/views/AListControl.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script setup lang="ts">
</script>

<template>
<div class="ik-plugin-alist-container">
Hello Ikaros Plugin AList Home View.
<hr />
<form name="form" method="post" action="#">
<input name="path" placeholder="请输入需要导入的基础路径" required>
<input type="submit" name="submit" value="提交导入">
</form>
</div>
</template>

<style scoped>
.ik-plugin-alist-container {
display: inline-block;
width: 100%;
height: 100%;
background-color: antiquewhite;
border: 1px solid blue;
}
</style>
17 changes: 0 additions & 17 deletions console/src/views/HelloIkaros.vue

This file was deleted.

Binary file not shown.
Binary file renamed lib/api-0.12.0.jar → lib/api-0.13.0.jar
Binary file not shown.
25 changes: 25 additions & 0 deletions src/main/java/run/ikaros/plugin/alist/AListAttachment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package run.ikaros.plugin.alist;

import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;

@Data
public class AListAttachment {
private List<String> paths;
private String name;
private Long size;
private Boolean is_dir;
private LocalDateTime modified;
private LocalDateTime created;
private String sign;
private String thumb;
private int type;
private String hashinfo;
private String hash_info;
private String raw_url;
private String readme;
private String header;
private String provider;
}
114 changes: 102 additions & 12 deletions src/main/java/run/ikaros/plugin/alist/AListClient.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package run.ikaros.plugin.alist;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Publisher;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.EventListener;
Expand All @@ -26,10 +28,7 @@
import run.ikaros.plugin.alist.AListConst.ConfigMapKey;

import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;

@Slf4j
@Component
Expand All @@ -47,6 +46,15 @@ public AListClient(ReactiveCustomClient customClient) {
this.customClient = customClient;
}

public Mono<Void> doImportFilesFromAListPath(String path) {
if (StringUtils.isEmpty(path)) {
return Mono.error(IllegalArgumentException::new);
}
// 查询路径,递归导入

return Mono.empty();
}


/**
* @see <https://alist.nn.ci/zh/guide/>
Expand All @@ -70,20 +78,14 @@ public Mono<AListToken> refreshToken() {
Map<String, String> bodyMap = new HashMap<>();
bodyMap.put("username", token.getUsername());
bodyMap.put("password", token.getPassword());
String body = null;
try {
body = new ObjectMapper().writeValueAsString(bodyMap);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}

String body = JsonUtils.obj2Json(bodyMap);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> httpEntity = new HttpEntity<>(body, httpHeaders);

ResponseEntity<ApiResult> responseEntity =
restTemplate.postForEntity(token.getUrl() + API.AUTH_TOKEN, httpEntity, ApiResult.class);
ApiResult apiResult = responseEntity.getBody();
log.debug("post token to alist for token: {}", JsonUtils.obj2Json(token));
// log.debug("post token to alist for token: {}", JsonUtils.obj2Json(token));
if (apiResult != null && apiResult.getCode() == 200) {
Object token1 = apiResult.getData().get("token");
httpHeaders.set("Authorization", String.valueOf(token1));
Expand All @@ -109,6 +111,94 @@ public Mono<AListToken> refreshToken() {
}


public Flux<AListAttachment> getAListAttachments(String basePath) {
if (StringUtils.isEmpty(basePath)) {
return Flux.empty();
}

if (StringUtils.isBlank(token.getUrl())
|| StringUtils.isBlank(token.getUsername())
|| StringUtils.isBlank(token.getPassword())) {
log.warn("token url or username or password is null or empty for token: {}",
JsonUtils.obj2Json(token));
return Flux.empty();
}

List<String> paths = Arrays.stream(basePath.split("/")).filter(StringUtils::isNotBlank).toList();

Map<String, String> bodyMap = new HashMap<>();
bodyMap.put("path", basePath);
String body = JsonUtils.obj2Json(bodyMap);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> httpEntity = new HttpEntity<>(body, httpHeaders);

ResponseEntity<ApiResult> responseEntity =
restTemplate.postForEntity(token.getUrl() + API.FS_LIST, httpEntity, ApiResult.class);
ApiResult apiResult = responseEntity.getBody();
if (apiResult != null && apiResult.getCode() == 200) {
Object content = apiResult.getData().get("content");
AListAttachment[] aListAttachments = JsonUtils.obj2Arr(content, new TypeReference<AListAttachment[]>() {
});
return Flux.fromArray(aListAttachments)
.map(aListAttachment -> {
List<String> newPaths = new ArrayList<>(paths);
newPaths.add(aListAttachment.getName());
aListAttachment.setPaths(newPaths);
return aListAttachment;
})
.flatMap(aListAttachment -> {
if (aListAttachment.getIs_dir()) {
return Mono.just(aListAttachment);
} else {
List<String> paths1 = aListAttachment.getPaths();
StringBuilder path1 = new StringBuilder("/");
for (String p: paths1) {
path1.append(p).append("/");
}
return fetchAttachmentDetailByPath(path1.toString(), aListAttachment);
}

});
} else {
log.error("post get alist attachments for apiResult: {}", apiResult);
}

return Flux.empty();
}

private Mono<AListAttachment> fetchAttachmentDetailByPath(String path, AListAttachment attachment) {
if (StringUtils.isEmpty(path)) {
return Mono.just(attachment);
}

if (StringUtils.isBlank(token.getUrl())
|| StringUtils.isBlank(token.getUsername())
|| StringUtils.isBlank(token.getPassword())) {
log.warn("token url or username or password is null or empty for token: {}",
JsonUtils.obj2Json(token));
return Mono.empty();
}

Map<String, String> bodyMap = new HashMap<>();
bodyMap.put("path", path);
String body = JsonUtils.obj2Json(bodyMap);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> httpEntity = new HttpEntity<>(body, httpHeaders);

ResponseEntity<ApiResult> responseEntity =
restTemplate.postForEntity(token.getUrl() + API.FS_GET, httpEntity, ApiResult.class);
ApiResult apiResult = responseEntity.getBody();
if (apiResult != null && apiResult.getCode() == 200) {
AListAttachment aListAttachment = JsonUtils.json2obj(JsonUtils.obj2Json(apiResult.getData()), AListAttachment.class);
if (Objects.isNull(aListAttachment)) return Mono.just(attachment);
aListAttachment.setPaths(attachment.getPaths());
return Mono.just(aListAttachment);
} else {
log.error("post alist attachment details for apiResult: {}", apiResult);
}
return Mono.just(attachment);
}


public Mono<ConfigMap> getConfigMap() {
return customClient.findOne(ConfigMap.class, AListPlugin.NAME);
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/run/ikaros/plugin/alist/AListEndpoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package run.ikaros.plugin.alist;

import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.fn.builders.requestbody.Builder;
import org.springdoc.webflux.core.fn.SpringdocRouteBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import run.ikaros.api.custom.GroupVersionKind;
import run.ikaros.api.custom.ReactiveCustomClient;
import run.ikaros.api.endpoint.CustomEndpoint;

@Slf4j
@Component
public class AListEndpoint implements CustomEndpoint {

private GroupVersionKind groupVersionKind =
new GroupVersionKind("plugin.ikaros.run", "v1alpha1", AListPlugin.NAME);

private final AListClient aListClient;

public AListEndpoint(AListClient aListClient) {
this.aListClient = aListClient;
}

@Override
public RouterFunction<ServerResponse> endpoint() {
var tag = groupVersionKind.group()
+ "/" + groupVersionKind.version()
+ "/" + groupVersionKind.kind();
return SpringdocRouteBuilder.route()
.POST("/alist/import", this::doImportFilesFromAList,
builder -> builder.operationId("ImportAlistFiles")
.tag(tag)
.description("Import Alist Files")
.requestBody(Builder.requestBodyBuilder()
.implementation(AListImportPostBody.class)))
.build();
}

Mono<ServerResponse> doImportFilesFromAList(ServerRequest request) {
return request.bodyToMono(AListImportPostBody.class)
.map(AListImportPostBody::getPath)
.flatMap(path -> aListClient.doImportFilesFromAListPath(path))
.then(ServerResponse.ok().build());
}

@Override
public GroupVersionKind groupVersionKind() {
return groupVersionKind;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package run.ikaros.plugin.alist;

import lombok.Data;

@Data
public class AListImportPostBody {
private String path;
}
4 changes: 2 additions & 2 deletions src/main/resources/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ name: PluginAList
# plugin entry class that extends BasePlugin
clazz: run.ikaros.plugin.alist.AListPlugin
# plugin 'version' is a valid semantic version string (see semver.org).
version: 0.12.0
requires: ">=0.12.0"
version: 0.13.0
requires: ">=0.13.0"
author:
name: Ikaros OSS Team
website: https://github.com/ikaros-dev
Expand Down

0 comments on commit 9fd69cb

Please sign in to comment.