diff --git a/web-service/src/main/java/cn/qaiu/lz/common/util/URLParamUtil.java b/web-service/src/main/java/cn/qaiu/lz/common/util/URLParamUtil.java index d0efac2..105f3f2 100644 --- a/web-service/src/main/java/cn/qaiu/lz/common/util/URLParamUtil.java +++ b/web-service/src/main/java/cn/qaiu/lz/common/util/URLParamUtil.java @@ -71,7 +71,8 @@ public class URLParamUtil { // 忽略单独处理的参数,不应拼接到分享URL中 if (!paramName.equals("url") && !paramName.equals("pwd") && !paramName.equals("dirId") && !paramName.equals("uuid") && !paramName.equals("auth") - && !paramName.equals("stoken") && !paramName.equals("zml")) { + && !paramName.equals("stoken") && !paramName.equals("zml") + && !paramName.equals("page") && !paramName.equals("pageCursor")) { if (firstParam) { urlBuilder.append("?"); firstParam = false; diff --git a/web-service/src/main/java/cn/qaiu/lz/web/controller/ParserApi.java b/web-service/src/main/java/cn/qaiu/lz/web/controller/ParserApi.java index badfee0..aa07971 100644 --- a/web-service/src/main/java/cn/qaiu/lz/web/controller/ParserApi.java +++ b/web-service/src/main/java/cn/qaiu/lz/web/controller/ParserApi.java @@ -10,9 +10,11 @@ import cn.qaiu.lz.common.util.URLParamUtil; import cn.qaiu.lz.web.model.AuthParam; import cn.qaiu.lz.web.model.CacheLinkInfo; import cn.qaiu.lz.web.model.ClientLinkResp; +import cn.qaiu.lz.web.model.FileListPageResp; import cn.qaiu.lz.web.model.LinkInfoResp; import cn.qaiu.lz.web.model.StatisticsInfo; import cn.qaiu.lz.web.service.DbService; +import cn.qaiu.parser.FolderPaging; import cn.qaiu.parser.PanDomainTemplate; import cn.qaiu.parser.IPanTool; import cn.qaiu.parser.ParserCreate; @@ -162,9 +164,50 @@ public class ParserApi { }}).collect(Collectors.toList()); } + /** + * 目录列表。不带 page 时行为与旧版一致;带 page 时返回该页(结构不变)。 + * 需要 hasMore / nextCursor 时用 {@link #getFileListPage}。 + */ @RouteMapping("/getFileList") public Future> getFileList(HttpServerRequest request, String pwd, String dirId, String uuid, - String stoken, String zml, String auth) { + String stoken, String zml, String auth, + String page, String pageCursor) { + Integer pageNo; + try { + pageNo = FolderPaging.parsePage(page); + } catch (IllegalArgumentException e) { + return Future.failedFuture(e.getMessage()); + } + return listFolder(request, pwd, dirId, uuid, stoken, zml, auth, pageNo, pageCursor) + .map(FolderListResult::list); + } + + /** + * 目录分页:参数同 /getFileList,另有 page(默认 1)与 pageCursor(上一页的 nextCursor)。 + */ + @RouteMapping("/getFileListPage") + public Future getFileListPage(HttpServerRequest request, String pwd, String dirId, String uuid, + String stoken, String zml, String auth, + String page, String pageCursor) { + int pageNo; + try { + Integer parsed = FolderPaging.parsePage(page); + pageNo = parsed == null ? 1 : parsed; + } catch (IllegalArgumentException e) { + return Future.failedFuture(e.getMessage()); + } + return listFolder(request, pwd, dirId, uuid, stoken, zml, auth, pageNo, pageCursor) + .map(r -> r.unpagedBeyondFirst() + ? FileListPageResp.beyondUnpaged(pageNo) + : FileListPageResp.of(r.list(), pageNo, FolderPaging.pageInfo(r.shareLinkInfo()))); + } + + private record FolderListResult(List list, ShareLinkInfo shareLinkInfo, boolean unpagedBeyondFirst) { + } + + private Future listFolder(HttpServerRequest request, String pwd, String dirId, String uuid, + String stoken, String zml, String auth, + Integer page, String pageCursor) { String url = URLParamUtil.parserParams(request); ParserCreate parserCreate; try { @@ -172,6 +215,16 @@ public class ParserApi { } catch (Exception e) { return Future.failedFuture(e); } + ShareLinkInfo info = parserCreate.getShareLinkInfo(); + if (page != null && page > 1 && !FileListPageResp.isPagedType(info.getType())) { + return Future.succeededFuture(new FolderListResult(new ArrayList<>(), info, true)); + } + if (page != null) { + info.getOtherParam().put(FolderPaging.PAGE, page); + if (StringUtils.isNotBlank(pageCursor)) { + info.getOtherParam().put(FolderPaging.CURSOR, pageCursor); + } + } String linkPrefix = getLinkPrefix(request); JsonObject otherParam = ParserAuthUtil.buildOtherParam(request, auth, linkPrefix); parserCreate.getShareLinkInfo().getOtherParam().put("domainName", linkPrefix); @@ -198,7 +251,8 @@ public class ParserApi { ParserAuthUtil.recordAutoDonatedFailureIfNeeded(dbService, parserCreate.getShareLinkInfo(), t); }); - }); + }) + .map(list -> new FolderListResult(list, info, false)); } // 目录解析下载文件 diff --git a/web-service/src/main/java/cn/qaiu/lz/web/model/FileListPageResp.java b/web-service/src/main/java/cn/qaiu/lz/web/model/FileListPageResp.java new file mode 100644 index 0000000..8d12d86 --- /dev/null +++ b/web-service/src/main/java/cn/qaiu/lz/web/model/FileListPageResp.java @@ -0,0 +1,53 @@ +package cn.qaiu.lz.web.model; + +import cn.qaiu.entity.FileInfo; +import cn.qaiu.parser.FolderPaging; +import io.vertx.core.json.JsonObject; +import lombok.Builder; +import lombok.Data; + +import java.util.List; + +/** + * /v2/getFileListPage 的返回体。list 与 /v2/getFileList 的 data 结构一致。 + * paged=false 表示该网盘不支持分页,list 已是完整目录,不要再请求下一页。 + */ +@Data +@Builder +public class FileListPageResp { + private List list; + private boolean paged; + private int page; + private Integer pageSize; + private boolean hasMore; + private Integer nextPage; + private String nextCursor; + private Integer total; + private Integer totalPage; + + public static FileListPageResp of(List list, int page, JsonObject pageInfo) { + if (pageInfo == null) { + return FileListPageResp.builder().list(list).paged(false).page(page).hasMore(false).build(); + } + return FileListPageResp.builder() + .list(list) + .paged(true) + .page(pageInfo.getInteger("page", page)) + .pageSize(pageInfo.getInteger("pageSize")) + .hasMore(Boolean.TRUE.equals(pageInfo.getBoolean("hasMore"))) + .nextPage(pageInfo.getInteger("nextPage")) + .nextCursor(pageInfo.getString("nextCursor")) + .total(pageInfo.getInteger("total")) + .totalPage(pageInfo.getInteger("totalPage")) + .build(); + } + + /** 不分页网盘的第 2 页及以后:返回空页,避免客户端重复追加同一份列表。 */ + public static FileListPageResp beyondUnpaged(int page) { + return FileListPageResp.builder().list(List.of()).paged(false).page(page).hasMore(false).build(); + } + + public static boolean isPagedType(String type) { + return FolderPaging.supports(type); + } +} diff --git a/web-service/src/test/java/cn/qaiu/lz/common/util/URLParamUtilPagingTest.java b/web-service/src/test/java/cn/qaiu/lz/common/util/URLParamUtilPagingTest.java new file mode 100644 index 0000000..72b492f --- /dev/null +++ b/web-service/src/test/java/cn/qaiu/lz/common/util/URLParamUtilPagingTest.java @@ -0,0 +1,41 @@ +package cn.qaiu.lz.common.util; + +import io.vertx.core.MultiMap; +import io.vertx.core.http.HttpServerRequest; +import org.junit.Test; + +import java.lang.reflect.Proxy; + +import static org.junit.Assert.assertEquals; + +/** 分页参数不能被当作截断的分享链接参数拼回 url,否则会改变分享链接与解析结果。 */ +public class URLParamUtilPagingTest { + + @Test + public void pagingParamsAreNotAppendedToShareUrl() { + MultiMap params = MultiMap.caseInsensitiveMultiMap() + .add("url", "https://pan.lanzoui.com/b66477") + .add("pwd", "1234") + .add("page", "2") + .add("pageCursor", "eyJ2IjoxfQ"); + assertEquals("https://pan.lanzoui.com/b66477", URLParamUtil.parserParams(request(params))); + } + + @Test + public void truncatedShareParamsStillAppended() { + MultiMap params = MultiMap.caseInsensitiveMultiMap() + .add("url", "https://moteam.lanzoum.com/b00yaejvte?a=1") + .add("webpage", "x") + .add("page", "3"); + assertEquals("https://moteam.lanzoum.com/b00yaejvte?a=1&webpage=x", URLParamUtil.parserParams(request(params))); + } + + private static HttpServerRequest request(MultiMap params) { + return (HttpServerRequest) Proxy.newProxyInstance(HttpServerRequest.class.getClassLoader(), + new Class[]{HttpServerRequest.class}, (proxy, method, args) -> switch (method.getName()) { + case "params" -> params; + case "absoluteURI" -> "http://localhost/v2/getFileListPage"; + default -> throw new UnsupportedOperationException(method.getName()); + }); + } +} diff --git a/web-service/src/test/java/cn/qaiu/lz/web/model/FileListPageRespTest.java b/web-service/src/test/java/cn/qaiu/lz/web/model/FileListPageRespTest.java new file mode 100644 index 0000000..f414608 --- /dev/null +++ b/web-service/src/test/java/cn/qaiu/lz/web/model/FileListPageRespTest.java @@ -0,0 +1,47 @@ +package cn.qaiu.lz.web.model; + +import cn.qaiu.entity.FileInfo; +import io.vertx.core.json.JsonObject; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +public class FileListPageRespTest { + + @Test + public void unpagedProviderReturnsWholeListAsLastPage() { + List list = List.of(new FileInfo().setFileName("a")); + FileListPageResp resp = FileListPageResp.of(list, 1, null); + assertFalse(resp.isPaged()); + assertFalse(resp.isHasMore()); + assertEquals(1, resp.getList().size()); + assertNull(resp.getNextCursor()); + } + + @Test + public void unpagedProviderBeyondFirstPageIsEmpty() { + FileListPageResp resp = FileListPageResp.beyondUnpaged(2); + assertTrue(resp.getList().isEmpty()); + assertFalse(resp.isHasMore()); + assertEquals(2, resp.getPage()); + } + + @Test + public void pagedInfoIsCopied() { + JsonObject info = new JsonObject().put("paged", true).put("page", 2).put("pageSize", 50) + .put("hasMore", true).put("nextPage", 3).put("nextCursor", "c").putNull("total"); + FileListPageResp resp = FileListPageResp.of(List.of(), 2, info); + assertTrue(resp.isPaged()); + assertTrue(resp.isHasMore()); + assertEquals(Integer.valueOf(3), resp.getNextPage()); + assertEquals("c", resp.getNextCursor()); + assertNull(resp.getTotal()); + assertTrue(FileListPageResp.isPagedType("lz")); + assertFalse(FileListPageResp.isPagedType("qk")); + } +}