1010import org .springframework .web .reactive .function .server .ServerResponse ;
1111import reactor .core .publisher .Mono ;
1212import run .ikaros .api .custom .GroupVersionKind ;
13- import run .ikaros .api .custom .ReactiveCustomClient ;
1413import run .ikaros .api .endpoint .CustomEndpoint ;
1514import run .ikaros .api .infra .utils .StringUtils ;
1615
1716import java .net .URLDecoder ;
18- import java .nio .charset .Charset ;
1917import java .nio .charset .StandardCharsets ;
2018import java .util .Arrays ;
2119import java .util .Base64 ;
2220import java .util .Objects ;
2321
2422import static org .springdoc .core .fn .builders .content .Builder .contentBuilder ;
25- import static org .springdoc .core .fn .builders .encoding .Builder .encodingBuilder ;
26- import static org .springdoc .core .fn .builders .schema .Builder .schemaBuilder ;
2723
2824@ Slf4j
2925@ Component
3026public class AListEndpoint implements CustomEndpoint {
3127
3228 private GroupVersionKind groupVersionKind =
33- new GroupVersionKind ("plugin.ikaros.run" , "v1alpha1" , AListPlugin .NAME );
29+ new GroupVersionKind ("plugin.ikaros.run" , "v1alpha1" , AListPlugin .NAME );
3430
3531 private final AListClient aListClient ;
3632
@@ -41,8 +37,8 @@ public AListEndpoint(AListClient aListClient) {
4137 @ Override
4238 public RouterFunction <ServerResponse > endpoint () {
4339 var tag = groupVersionKind .group ()
44- + "/" + groupVersionKind .version ()
45- + "/" + groupVersionKind .kind ();
40+ + "/" + groupVersionKind .version ()
41+ + "/" + groupVersionKind .kind ();
4642 return SpringdocRouteBuilder .route ()
4743 .POST ("/alist/import" , this ::doImportFilesFromAList ,
4844 builder -> builder .operationId ("ImportAlistFiles" )
@@ -53,7 +49,7 @@ public RouterFunction<ServerResponse> endpoint() {
5349 .content (contentBuilder ()
5450 .mediaType (MediaType .APPLICATION_JSON_VALUE ))
5551 .implementation (AListImportPostBody .class )))
56- .build ();
52+ .build ();
5753 }
5854
5955 Mono <ServerResponse > doImportFilesFromAList (ServerRequest request ) {
@@ -62,8 +58,10 @@ Mono<ServerResponse> doImportFilesFromAList(ServerRequest request) {
6258 .map (AListImportPostBody ::getPath )
6359 .map (path -> new String (Base64 .getDecoder ().decode (path ), StandardCharsets .UTF_8 ))
6460 .map (path -> URLDecoder .decode (path , StandardCharsets .UTF_8 ))
65- .doOnSuccess (s -> log .debug ("paths : {}" , s ))
61+ .doOnSuccess (s -> log .debug ("path : {}" , s ))
6662 .filter (StringUtils ::isNotBlank )
63+ .flatMap (this ::removeHttpPrefixIfExists )
64+ .doOnSuccess (s -> log .debug ("relative path: {}" , s ))
6765 .map (path -> Arrays .stream (path .split ("/" )).filter (StringUtils ::isNotBlank ).toList ())
6866 .doOnSuccess (strings -> log .debug ("strings size: {}" , strings .size ()))
6967 .filter (Objects ::nonNull )
@@ -73,6 +71,22 @@ Mono<ServerResponse> doImportFilesFromAList(ServerRequest request) {
7371 .switchIfEmpty (ServerResponse .notFound ().build ());
7472 }
7573
74+ /**
75+ * 支持直接复制AList浏览器URL,而无需去区分是不是相对路径。
76+ *
77+ * @param path 浏览器URL
78+ * @return 相对路径
79+ */
80+ private Mono <String > removeHttpPrefixIfExists (String path ) {
81+ if (path .startsWith ("http://" ) || path .startsWith ("https://" )) {
82+ return aListClient .getToken ()
83+ .map (AListToken ::getUrl )
84+ .map (url -> path .replace (url , "" ))
85+ .map (p -> p .startsWith ("/" ) ? p : "/" + p );
86+ }
87+ return Mono .just (path );
88+ }
89+
7690 @ Override
7791 public GroupVersionKind groupVersionKind () {
7892 return groupVersionKind ;
0 commit comments