mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-11-25 06:47:00 +00:00
parser: page share/list folders for 小飞机 and 蓝奏优享
offset is a 1-based page number; record total/totalPage as pageInfo. Default requests still fetch page 1 with limit 60. FjTool now fails instead of hanging on non-JSON or malformed list items. Co-authored-by: qaiu <[email protected]>
This commit is contained in:
6 files changed
+482
-65
No files matched your search
@@ -0,0 +1,145 @@
|
||||
package cn.qaiu.parser;
|
||||
|
||||
import cn.qaiu.entity.ShareLinkInfo;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 目录列表分页约定(解析器与 API 共用)。
|
||||
* <p>
|
||||
* 入参写在 {@link ShareLinkInfo#getOtherParam()}:
|
||||
* <ul>
|
||||
* <li>{@link #PAGE}:从 1 开始的页码;不传时解析器按原逻辑只取第一页</li>
|
||||
* <li>{@link #CURSOR}:上一页返回的 {@code nextCursor},可选,只是加速,不可信</li>
|
||||
* </ul>
|
||||
* 解析器取完一页后写回 {@link #PAGE_INFO},见 {@link #pageInfo}。
|
||||
* 没写 {@link #PAGE_INFO} 的解析器视为不分页,整份列表就是第 1 页。
|
||||
*/
|
||||
public final class FolderPaging {
|
||||
|
||||
public static final String PAGE = "page";
|
||||
public static final String CURSOR = "pageCursor";
|
||||
public static final String PAGE_INFO = "pageInfo";
|
||||
|
||||
/** 页码上限。蓝奏无游标时要从第 1 页顺序翻到目标页,必须有界。 */
|
||||
public static final int MAX_PAGE = 500;
|
||||
|
||||
private static final Set<String> PAGED_TYPES = Set.of("lz", "fj", "iz");
|
||||
|
||||
private FolderPaging() {
|
||||
}
|
||||
|
||||
/** 该网盘的目录解析是否支持按页取。 */
|
||||
public static boolean supports(String type) {
|
||||
return type != null && PAGED_TYPES.contains(type.toLowerCase(Locale.ROOT));
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析页码参数。空值返回 null(保持旧行为);非法值抛 IllegalArgumentException。
|
||||
*/
|
||||
public static Integer parsePage(String raw) {
|
||||
if (raw == null || raw.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
int page;
|
||||
try {
|
||||
page = Integer.parseInt(raw.trim());
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException("page 必须是正整数: " + raw);
|
||||
}
|
||||
if (page < 1 || page > MAX_PAGE) {
|
||||
throw new IllegalArgumentException("page 超出范围 1-" + MAX_PAGE + ": " + raw);
|
||||
}
|
||||
return page;
|
||||
}
|
||||
|
||||
/** 本次请求的页码;没传返回 null。 */
|
||||
public static Integer requestedPage(ShareLinkInfo info) {
|
||||
Object v = info.getOtherParam().get(PAGE);
|
||||
if (v instanceof Number n) {
|
||||
return n.intValue();
|
||||
}
|
||||
return v == null ? null : parsePage(v.toString());
|
||||
}
|
||||
|
||||
/** 本次请求的页码;没传按第 1 页。 */
|
||||
public static int pageOrFirst(ShareLinkInfo info) {
|
||||
Integer page = requestedPage(info);
|
||||
return page == null ? 1 : page;
|
||||
}
|
||||
|
||||
public static String cursor(ShareLinkInfo info) {
|
||||
Object v = info.getOtherParam().get(CURSOR);
|
||||
return v == null || v.toString().isBlank() ? null : v.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 写回分页结果。
|
||||
*
|
||||
* @param pageSize 上游每页条数(原始条数口径,含被过滤的占位项)
|
||||
* @param hasMore 上游明确还有下一页
|
||||
* @param total 上游给出的总条数,未知传 null
|
||||
* @param totalPage 上游给出的总页数,未知传 null
|
||||
* @param nextCursor 取下一页可直接复用的游标,没有传 null
|
||||
*/
|
||||
public static JsonObject pageInfo(ShareLinkInfo info, int page, int pageSize, boolean hasMore,
|
||||
Integer total, Integer totalPage, String nextCursor) {
|
||||
JsonObject json = new JsonObject()
|
||||
.put("paged", true)
|
||||
.put("page", page)
|
||||
.put("pageSize", pageSize)
|
||||
.put("hasMore", hasMore)
|
||||
.put("nextPage", hasMore ? page + 1 : null)
|
||||
.put("total", total)
|
||||
.put("totalPage", totalPage)
|
||||
.put("nextCursor", hasMore ? nextCursor : null);
|
||||
info.getOtherParam().put(PAGE_INFO, json);
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上游给了总页数就以它为准;没给时整页满才认为可能还有。空页一律结束,避免无限翻页。
|
||||
*/
|
||||
public static boolean hasMore(int page, int rawCount, int pageSize, Integer totalPage) {
|
||||
if (rawCount <= 0) {
|
||||
return false;
|
||||
}
|
||||
if (totalPage != null && totalPage > 0) {
|
||||
return page < totalPage;
|
||||
}
|
||||
return rawCount >= pageSize;
|
||||
}
|
||||
|
||||
/** 小飞机 / 蓝奏优享 share/list 响应:offset 为页码,带 total / totalPage。 */
|
||||
public static JsonObject offsetListPage(ShareLinkInfo info, JsonObject resp, int rawCount, int pageSize) {
|
||||
int page = pageOrFirst(info);
|
||||
Integer total = intOrNull(resp, "total");
|
||||
Integer totalPage = intOrNull(resp, "totalPage");
|
||||
return pageInfo(info, page, pageSize, hasMore(page, rawCount, pageSize, totalPage), total, totalPage, null);
|
||||
}
|
||||
|
||||
private static Integer intOrNull(JsonObject json, String key) {
|
||||
if (json == null) {
|
||||
return null;
|
||||
}
|
||||
Object v = json.getValue(key);
|
||||
if (v instanceof Number n) {
|
||||
return n.intValue();
|
||||
}
|
||||
if (v instanceof CharSequence s) {
|
||||
try {
|
||||
return Integer.parseInt(s.toString().trim());
|
||||
} catch (NumberFormatException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static JsonObject pageInfo(ShareLinkInfo info) {
|
||||
Object v = info.getOtherParam().get(PAGE_INFO);
|
||||
return v instanceof JsonObject json ? json : null;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package cn.qaiu.parser.impl;
|
||||
|
||||
import cn.qaiu.entity.FileInfo;
|
||||
import cn.qaiu.entity.ShareLinkInfo;
|
||||
import cn.qaiu.parser.FolderPaging;
|
||||
import cn.qaiu.parser.PanBase;
|
||||
import cn.qaiu.util.*;
|
||||
import io.vertx.core.Future;
|
||||
@@ -60,7 +61,10 @@ public class FjTool extends PanBase {
|
||||
|
||||
private static final String FILE_LIST_URL = API_URL_PREFIX + "/share/list?devType=6&devModel=Chrome&uuid" +
|
||||
"={uuid}&extra=2×tamp={ts}&shareId={shareId}&folderId" +
|
||||
"={folderId}&offset=1&limit=60";
|
||||
"={folderId}&offset={offset}&limit={limit}";
|
||||
|
||||
/** share/list 每页条数;offset 是从 1 开始的页码。 */
|
||||
static final int LIST_LIMIT = 60;
|
||||
|
||||
private static final MultiMap header;
|
||||
private static final MultiMap header0;
|
||||
@@ -492,7 +496,11 @@ public class FjTool extends PanBase {
|
||||
.setParserUrl(id)
|
||||
.setPanType(shareLinkInfo.getType());
|
||||
List<FileInfo> result = new ArrayList<>();
|
||||
result.add(fileInfo);
|
||||
int page = FolderPaging.pageOrFirst(shareLinkInfo);
|
||||
if (page == 1) {
|
||||
result.add(fileInfo);
|
||||
}
|
||||
FolderPaging.pageInfo(shareLinkInfo, page, LIST_LIMIT, false, 1, 1, null);
|
||||
promise.complete(result);
|
||||
return;
|
||||
}
|
||||
@@ -506,86 +514,101 @@ public class FjTool extends PanBase {
|
||||
.setTemplateParam("uuid", uuid)
|
||||
.setTemplateParam("ts", tsEncode)
|
||||
.setTemplateParam("folderId", id)
|
||||
.setTemplateParam("offset", String.valueOf(FolderPaging.pageOrFirst(shareLinkInfo)))
|
||||
.setTemplateParam("limit", String.valueOf(LIST_LIMIT))
|
||||
.send().onSuccess(res -> {
|
||||
JsonArray list;
|
||||
JsonObject jsonObject;
|
||||
try {
|
||||
JsonObject jsonObject = asJson(res);
|
||||
jsonObject = asJson(res);
|
||||
log.debug("目录列表: {}", jsonObject.encodePrettily());
|
||||
list = jsonObject.getJsonArray("list");
|
||||
} catch (Exception e) {
|
||||
log.error("解析目录失败: {}", res.bodyAsString());
|
||||
promise.tryFail("目录解析失败: 响应不是 JSON");
|
||||
return;
|
||||
}
|
||||
if (list == null) {
|
||||
promise.tryFail("目录解析失败: 响应缺少 list 字段: " + jsonObject);
|
||||
return;
|
||||
}
|
||||
ArrayList<FileInfo> result = new ArrayList<>();
|
||||
list.forEach(item->{
|
||||
JsonObject fileJson = (JsonObject) item;
|
||||
FileInfo fileInfo = new FileInfo();
|
||||
try {
|
||||
list.forEach(item->{
|
||||
JsonObject fileJson = (JsonObject) item;
|
||||
FileInfo fileInfo = new FileInfo();
|
||||
|
||||
// 映射已知字段fileInfo
|
||||
String fileId = fileJson.getString("fileId");
|
||||
String userId = fileJson.getString("userId");
|
||||
// 映射已知字段fileInfo
|
||||
String fileId = fileJson.getString("fileId");
|
||||
String userId = fileJson.getString("userId");
|
||||
|
||||
// 其他参数
|
||||
long nowTs2 = System.currentTimeMillis();
|
||||
String tsEncode2 = AESUtils.encrypt2Hex(Long.toString(nowTs2));
|
||||
String fidEncode = AESUtils.encrypt2Hex(fileId + "|" + userId);
|
||||
String auth = AESUtils.encrypt2Hex(fileId + "|" + nowTs2);
|
||||
// 其他参数
|
||||
long nowTs2 = System.currentTimeMillis();
|
||||
String tsEncode2 = AESUtils.encrypt2Hex(Long.toString(nowTs2));
|
||||
String fidEncode = AESUtils.encrypt2Hex(fileId + "|" + userId);
|
||||
String auth = AESUtils.encrypt2Hex(fileId + "|" + nowTs2);
|
||||
|
||||
// 回传用到的参数
|
||||
//"fidEncode", paramJson.getString("fidEncode"))
|
||||
//"uuid", paramJson.getString("uuid"))
|
||||
//"ts", paramJson.getString("ts"))
|
||||
//"auth", paramJson.getString("auth"))
|
||||
//"shareId", paramJson.getString("shareId"))
|
||||
JsonObject entries = JsonObject.of(
|
||||
"fileId", fileId,
|
||||
"userId", userId,
|
||||
"fidEncode", fidEncode,
|
||||
"uuid", uuid,
|
||||
"ts", tsEncode2,
|
||||
"auth", auth,
|
||||
"shareId", shareId);
|
||||
String param = CommonUtils.urlBase64Encode(entries.encode());
|
||||
// 回传用到的参数
|
||||
//"fidEncode", paramJson.getString("fidEncode"))
|
||||
//"uuid", paramJson.getString("uuid"))
|
||||
//"ts", paramJson.getString("ts"))
|
||||
//"auth", paramJson.getString("auth"))
|
||||
//"shareId", paramJson.getString("shareId"))
|
||||
JsonObject entries = JsonObject.of(
|
||||
"fileId", fileId,
|
||||
"userId", userId,
|
||||
"fidEncode", fidEncode,
|
||||
"uuid", uuid,
|
||||
"ts", tsEncode2,
|
||||
"auth", auth,
|
||||
"shareId", shareId);
|
||||
String param = CommonUtils.urlBase64Encode(entries.encode());
|
||||
|
||||
if (fileJson.getInteger("fileType") == 2) {
|
||||
// 如果是目录
|
||||
fileInfo.setFileName(fileJson.getString("name"))
|
||||
.setFileId(fileJson.getString("folderId"))
|
||||
.setCreateTime(fileJson.getString("updTime"))
|
||||
.setFileType("folder")
|
||||
.setSize(0L)
|
||||
.setSizeStr("0B")
|
||||
if (fileJson.getInteger("fileType") == 2) {
|
||||
// 如果是目录
|
||||
fileInfo.setFileName(fileJson.getString("name"))
|
||||
.setFileId(fileJson.getString("folderId"))
|
||||
.setCreateTime(fileJson.getString("updTime"))
|
||||
.setFileType("folder")
|
||||
.setSize(0L)
|
||||
.setSizeStr("0B")
|
||||
.setCreateBy(fileJson.getLong("userId").toString())
|
||||
.setDownloadCount(fileJson.getInteger("fileDownloads"))
|
||||
.setCreateTime(fileJson.getString("updTime"))
|
||||
.setFileIcon(fileJson.getString("fileIcon"))
|
||||
.setPanType(shareLinkInfo.getType())
|
||||
// 设置目录解析的URL
|
||||
.setParserUrl(String.format("%s/v2/getFileList?url=%s&dirId=%s&uuid=%s", getDomainName(),
|
||||
shareLinkInfo.getShareUrl(), fileJson.getString("folderId"), uuid));
|
||||
result.add(fileInfo);
|
||||
return;
|
||||
}
|
||||
long fileSize = fileJson.getLong("fileSize") * 1024;
|
||||
fileInfo.setFileName(fileJson.getString("fileName"))
|
||||
.setFileId(fileJson.getString("fileId"))
|
||||
.setCreateTime(fileJson.getString("createTime"))
|
||||
.setFileType("file")
|
||||
.setSize(fileSize)
|
||||
.setSizeStr(FileSizeConverter.convertToReadableSize(fileSize))
|
||||
.setCreateBy(fileJson.getLong("userId").toString())
|
||||
.setDownloadCount(fileJson.getInteger("fileDownloads"))
|
||||
.setCreateTime(fileJson.getString("updTime"))
|
||||
.setFileIcon(fileJson.getString("fileIcon"))
|
||||
.setPanType(shareLinkInfo.getType())
|
||||
// 设置目录解析的URL
|
||||
.setParserUrl(String.format("%s/v2/getFileList?url=%s&dirId=%s&uuid=%s", getDomainName(),
|
||||
shareLinkInfo.getShareUrl(), fileJson.getString("folderId"), uuid));
|
||||
.setParserUrl(String.format("%s/v2/redirectUrl/%s/%s", getDomainName(),
|
||||
shareLinkInfo.getType(), param))
|
||||
.setPreviewUrl(String.format("%s/v2/viewUrl/%s/%s", getDomainName(),
|
||||
shareLinkInfo.getType(), param));
|
||||
result.add(fileInfo);
|
||||
return;
|
||||
}
|
||||
long fileSize = fileJson.getLong("fileSize") * 1024;
|
||||
fileInfo.setFileName(fileJson.getString("fileName"))
|
||||
.setFileId(fileJson.getString("fileId"))
|
||||
.setCreateTime(fileJson.getString("createTime"))
|
||||
.setFileType("file")
|
||||
.setSize(fileSize)
|
||||
.setSizeStr(FileSizeConverter.convertToReadableSize(fileSize))
|
||||
.setCreateBy(fileJson.getLong("userId").toString())
|
||||
.setDownloadCount(fileJson.getInteger("fileDownloads"))
|
||||
.setCreateTime(fileJson.getString("updTime"))
|
||||
.setFileIcon(fileJson.getString("fileIcon"))
|
||||
.setPanType(shareLinkInfo.getType())
|
||||
.setParserUrl(String.format("%s/v2/redirectUrl/%s/%s", getDomainName(),
|
||||
shareLinkInfo.getType(), param))
|
||||
.setPreviewUrl(String.format("%s/v2/viewUrl/%s/%s", getDomainName(),
|
||||
shareLinkInfo.getType(), param));
|
||||
result.add(fileInfo);
|
||||
});
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.error("目录列表条目解析失败: {}", jsonObject, e);
|
||||
promise.tryFail("目录解析失败: 列表条目格式异常 " + e);
|
||||
return;
|
||||
}
|
||||
FolderPaging.offsetListPage(shareLinkInfo, jsonObject, list.size(), LIST_LIMIT);
|
||||
promise.complete(result);
|
||||
});
|
||||
}).onFailure(err -> promise.tryFail("目录解析失败: " + err.getMessage()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,6 +2,7 @@ package cn.qaiu.parser.impl;
|
||||
|
||||
import cn.qaiu.entity.FileInfo;
|
||||
import cn.qaiu.entity.ShareLinkInfo;
|
||||
import cn.qaiu.parser.FolderPaging;
|
||||
import cn.qaiu.parser.PanBase;
|
||||
import cn.qaiu.parser.TokenCache;
|
||||
import cn.qaiu.util.AESUtils;
|
||||
@@ -56,7 +57,10 @@ public class IzTool extends PanBase {
|
||||
|
||||
private static final String FILE_LIST_URL = API_URL_PREFIX + "/share/list?devType=6&devModel=Chrome&uuid" +
|
||||
"={uuid}&extra=2×tamp={ts}&shareId={shareId}&folderId" +
|
||||
"={folderId}&offset=1&limit=60";
|
||||
"={folderId}&offset={offset}&limit={limit}";
|
||||
|
||||
/** share/list 每页条数;offset 是从 1 开始的页码。 */
|
||||
static final int LIST_LIMIT = 60;
|
||||
|
||||
|
||||
WebClientSession webClientSession = WebClientSession.create(clientNoRedirects);
|
||||
@@ -569,7 +573,11 @@ public class IzTool extends PanBase {
|
||||
.setParserUrl(id)
|
||||
.setPanType(shareLinkInfo.getType());
|
||||
List<FileInfo> result = new ArrayList<>();
|
||||
result.add(fileInfo);
|
||||
int page = FolderPaging.pageOrFirst(shareLinkInfo);
|
||||
if (page == 1) {
|
||||
result.add(fileInfo);
|
||||
}
|
||||
FolderPaging.pageInfo(shareLinkInfo, page, LIST_LIMIT, false, 1, 1, null);
|
||||
promise.complete(result);
|
||||
return;
|
||||
}
|
||||
@@ -586,6 +594,8 @@ public class IzTool extends PanBase {
|
||||
.setTemplateParam("uuid", uuid)
|
||||
.setTemplateParam("ts", tsEncode)
|
||||
.setTemplateParam("folderId", id)
|
||||
.setTemplateParam("offset", String.valueOf(FolderPaging.pageOrFirst(shareLinkInfo)))
|
||||
.setTemplateParam("limit", String.valueOf(LIST_LIMIT))
|
||||
.send().onSuccess(res -> {
|
||||
String resBody = asText(res);
|
||||
// 检查是否包含 cookie 验证
|
||||
@@ -600,6 +610,8 @@ public class IzTool extends PanBase {
|
||||
.setTemplateParam("uuid", uuid)
|
||||
.setTemplateParam("ts", tsEncode)
|
||||
.setTemplateParam("folderId", id)
|
||||
.setTemplateParam("offset", String.valueOf(FolderPaging.pageOrFirst(shareLinkInfo)))
|
||||
.setTemplateParam("limit", String.valueOf(LIST_LIMIT))
|
||||
.send().onSuccess(res2 -> {
|
||||
processDirResponse(res2, shareId, promise);
|
||||
}).onFailure(err -> {
|
||||
@@ -691,6 +703,7 @@ public class IzTool extends PanBase {
|
||||
shareLinkInfo.getType(), param));
|
||||
result.add(fileInfo);
|
||||
});
|
||||
FolderPaging.offsetListPage(shareLinkInfo, jsonObject, list.size(), LIST_LIMIT);
|
||||
promise.complete(result);
|
||||
} catch (Exception e) {
|
||||
log.error("处理目录响应异常: {}", e.getMessage(), e);
|
||||
|
||||
@@ -2,6 +2,7 @@ package cn.qaiu.parser.impl;
|
||||
|
||||
import cn.qaiu.entity.FileInfo;
|
||||
import cn.qaiu.entity.ShareLinkInfo;
|
||||
import cn.qaiu.parser.FolderPaging;
|
||||
import cn.qaiu.parser.PanBase;
|
||||
import cn.qaiu.util.AESUtils;
|
||||
import cn.qaiu.util.AcwScV2Generator;
|
||||
@@ -54,7 +55,10 @@ public class IzToolWithAuth extends PanBase {
|
||||
|
||||
private static final String FILE_LIST_URL = API_URL_PREFIX + "/share/list?devType=6&devModel=Chrome&uuid" +
|
||||
"={uuid}&extra=2×tamp={ts}&shareId={shareId}&folderId" +
|
||||
"={folderId}&offset=1&limit=60";
|
||||
"={folderId}&offset={offset}&limit={limit}";
|
||||
|
||||
/** share/list 每页条数;offset 是从 1 开始的页码。 */
|
||||
static final int LIST_LIMIT = 60;
|
||||
|
||||
|
||||
WebClientSession webClientSession = WebClientSession.create(clientNoRedirects);
|
||||
@@ -512,7 +516,11 @@ public class IzToolWithAuth extends PanBase {
|
||||
.setParserUrl(id)
|
||||
.setPanType(shareLinkInfo.getType());
|
||||
List<FileInfo> result = new ArrayList<>();
|
||||
result.add(fileInfo);
|
||||
int page = FolderPaging.pageOrFirst(shareLinkInfo);
|
||||
if (page == 1) {
|
||||
result.add(fileInfo);
|
||||
}
|
||||
FolderPaging.pageInfo(shareLinkInfo, page, LIST_LIMIT, false, 1, 1, null);
|
||||
promise.complete(result);
|
||||
return;
|
||||
}
|
||||
@@ -565,6 +573,8 @@ public class IzToolWithAuth extends PanBase {
|
||||
.setTemplateParam("uuid", uuid)
|
||||
.setTemplateParam("ts", tsEncode)
|
||||
.setTemplateParam("folderId", id)
|
||||
.setTemplateParam("offset", String.valueOf(FolderPaging.pageOrFirst(shareLinkInfo)))
|
||||
.setTemplateParam("limit", String.valueOf(LIST_LIMIT))
|
||||
.send().onSuccess(res -> {
|
||||
String resBody = asText(res);
|
||||
// 检查是否包含 cookie 验证
|
||||
@@ -579,6 +589,8 @@ public class IzToolWithAuth extends PanBase {
|
||||
.setTemplateParam("uuid", uuid)
|
||||
.setTemplateParam("ts", tsEncode)
|
||||
.setTemplateParam("folderId", id)
|
||||
.setTemplateParam("offset", String.valueOf(FolderPaging.pageOrFirst(shareLinkInfo)))
|
||||
.setTemplateParam("limit", String.valueOf(LIST_LIMIT))
|
||||
.send().onSuccess(res2 -> {
|
||||
processDirResponse(res2, shareId, promise);
|
||||
}).onFailure(err -> {
|
||||
@@ -671,6 +683,7 @@ public class IzToolWithAuth extends PanBase {
|
||||
shareLinkInfo.getType(), param));
|
||||
result.add(fileInfo);
|
||||
});
|
||||
FolderPaging.offsetListPage(shareLinkInfo, jsonObject, list.size(), LIST_LIMIT);
|
||||
promise.complete(result);
|
||||
} catch (Exception e) {
|
||||
log.error("处理目录响应异常: {}", e.getMessage(), e);
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package cn.qaiu.parser;
|
||||
|
||||
import cn.qaiu.entity.ShareLinkInfo;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class FolderPagingTest {
|
||||
|
||||
@Test
|
||||
public void blankPageKeepsLegacyBehaviour() {
|
||||
assertNull(FolderPaging.parsePage(null));
|
||||
assertNull(FolderPaging.parsePage(" "));
|
||||
assertEquals(1, FolderPaging.pageOrFirst(info()));
|
||||
assertNull(FolderPaging.requestedPage(info()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invalidPagesAreRejected() {
|
||||
assertEquals(Integer.valueOf(3), FolderPaging.parsePage(" 3 "));
|
||||
assertThrows(IllegalArgumentException.class, () -> FolderPaging.parsePage("0"));
|
||||
assertThrows(IllegalArgumentException.class, () -> FolderPaging.parsePage("-1"));
|
||||
assertThrows(IllegalArgumentException.class, () -> FolderPaging.parsePage("abc"));
|
||||
assertThrows(IllegalArgumentException.class, () -> FolderPaging.parsePage(String.valueOf(FolderPaging.MAX_PAGE + 1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onlyLzFjIzArePaged() {
|
||||
assertTrue(FolderPaging.supports("lz"));
|
||||
assertTrue(FolderPaging.supports("FJ"));
|
||||
assertTrue(FolderPaging.supports("iz"));
|
||||
assertFalse(FolderPaging.supports("qk"));
|
||||
assertFalse(FolderPaging.supports(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyPageAlwaysEnds() {
|
||||
assertFalse(FolderPaging.hasMore(2, 0, 60, 9));
|
||||
assertFalse(FolderPaging.hasMore(1, 0, 50, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void totalPageWinsOverFullPageHeuristic() {
|
||||
assertFalse(FolderPaging.hasMore(2, 60, 60, 2));
|
||||
assertTrue(FolderPaging.hasMore(1, 5, 60, 2));
|
||||
assertTrue(FolderPaging.hasMore(1, 50, 50, null));
|
||||
assertFalse(FolderPaging.hasMore(1, 49, 50, null));
|
||||
assertTrue(FolderPaging.hasMore(1, 50, 50, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void offsetListPageReadsTotals() {
|
||||
ShareLinkInfo info = info();
|
||||
info.getOtherParam().put(FolderPaging.PAGE, 2);
|
||||
JsonObject resp = new JsonObject().put("total", "121").put("totalPage", 3).put("offset", 2).put("limit", 60);
|
||||
JsonObject page = FolderPaging.offsetListPage(info, resp, 60, 60);
|
||||
assertEquals(Integer.valueOf(121), page.getInteger("total"));
|
||||
assertEquals(Integer.valueOf(3), page.getInteger("totalPage"));
|
||||
assertTrue(page.getBoolean("hasMore"));
|
||||
assertEquals(Integer.valueOf(3), page.getInteger("nextPage"));
|
||||
assertEquals(page, FolderPaging.pageInfo(info));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noMoreClearsNextPageAndCursor() {
|
||||
JsonObject page = FolderPaging.pageInfo(info(), 4, 50, false, null, null, "cursor");
|
||||
assertNull(page.getValue("nextPage"));
|
||||
assertNull(page.getValue("nextCursor"));
|
||||
}
|
||||
|
||||
private static ShareLinkInfo info() {
|
||||
return ShareLinkInfo.newBuilder().type("lz").shareKey("b1").build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package cn.qaiu.parser.impl;
|
||||
|
||||
import cn.qaiu.WebClientVertxInit;
|
||||
import cn.qaiu.entity.FileInfo;
|
||||
import cn.qaiu.entity.ShareLinkInfo;
|
||||
import cn.qaiu.parser.FolderPaging;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.http.HttpServer;
|
||||
import io.vertx.core.json.JsonArray;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.ext.web.client.WebClient;
|
||||
import io.vertx.uritemplate.UriTemplate;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/** 小飞机 share/list:offset 是从 1 开始的页码,limit 为每页条数。 */
|
||||
public class FjToolFolderPagingTest {
|
||||
|
||||
private Vertx vertx;
|
||||
private WebClient local;
|
||||
private HttpServer server;
|
||||
private final List<String> queries = new CopyOnWriteArrayList<>();
|
||||
private volatile String override;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
vertx = Vertx.vertx();
|
||||
WebClientVertxInit.init(vertx);
|
||||
local = WebClient.create(vertx);
|
||||
server = vertx.createHttpServer().requestHandler(req -> {
|
||||
if (req.path().endsWith("/share/list")) {
|
||||
queries.add(req.query());
|
||||
if (override != null) {
|
||||
req.response().end(override);
|
||||
return;
|
||||
}
|
||||
int offset = Integer.parseInt(req.getParam("offset"));
|
||||
int limit = Integer.parseInt(req.getParam("limit"));
|
||||
int total = 125;
|
||||
int totalPage = (total + limit - 1) / limit;
|
||||
JsonArray list = new JsonArray();
|
||||
for (int i = (offset - 1) * limit; i < Math.min(total, offset * limit); i++) {
|
||||
list.add(new JsonObject().put("fileType", 1).put("fileId", String.valueOf(1000 + i)).put("fileName", "f" + i + ".txt")
|
||||
.put("fileSize", 1).put("fileIcon", "txt").put("userId", 9).put("fileDownloads", 0)
|
||||
.put("updTime", "2024-01-01 00:00:00"));
|
||||
}
|
||||
req.response().end(new JsonObject().put("code", 200).put("list", list).put("total", total)
|
||||
.put("offset", offset).put("limit", limit).put("totalPage", totalPage).encode());
|
||||
} else {
|
||||
req.response().setStatusCode(404).end();
|
||||
}
|
||||
}).listen(0, "127.0.0.1").toCompletionStage().toCompletableFuture().get(10, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() throws Exception {
|
||||
local.close();
|
||||
server.close().toCompletionStage().toCompletableFuture().get(10, TimeUnit.SECONDS);
|
||||
vertx.close().toCompletionStage().toCompletableFuture().get(10, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultRequestIsFirstPageWithLegacyLimit() throws Exception {
|
||||
ShareLinkInfo info = info(null);
|
||||
List<FileInfo> list = list(info);
|
||||
assertEquals(60, list.size());
|
||||
assertTrue(queries.get(0).contains("offset=1"));
|
||||
assertTrue(queries.get(0).contains("limit=60"));
|
||||
assertTrue(FolderPaging.pageInfo(info).getBoolean("hasMore"));
|
||||
assertEquals(Integer.valueOf(125), FolderPaging.pageInfo(info).getInteger("total"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pagesAreDistinctAndLastPageEnds() throws Exception {
|
||||
java.util.Set<String> ids = new java.util.HashSet<>();
|
||||
for (int page = 1; page <= 3; page++) {
|
||||
ShareLinkInfo info = info(page);
|
||||
for (FileInfo f : list(info)) {
|
||||
assertTrue("重复 " + f.getFileId(), ids.add(f.getFileId()));
|
||||
}
|
||||
assertEquals(page < 3, FolderPaging.pageInfo(info).getBoolean("hasMore"));
|
||||
assertTrue(queries.get(queries.size() - 1).contains("offset=" + page));
|
||||
}
|
||||
assertEquals(125, ids.size());
|
||||
ShareLinkInfo beyond = info(4);
|
||||
assertTrue(list(beyond).isEmpty());
|
||||
assertFalse(FolderPaging.pageInfo(beyond).getBoolean("hasMore"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonJsonListFailsInsteadOfHanging() throws Exception {
|
||||
override = "<html>busy</html>";
|
||||
try {
|
||||
list(info(null));
|
||||
fail();
|
||||
} catch (ExecutionException e) {
|
||||
assertTrue(e.getCause().getMessage().contains("目录解析失败"));
|
||||
}
|
||||
}
|
||||
|
||||
private List<FileInfo> list(ShareLinkInfo info) throws Exception {
|
||||
return tool(info).parseFileList().toCompletionStage().toCompletableFuture().get(10, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
private ShareLinkInfo info(Integer page) {
|
||||
ShareLinkInfo info = ShareLinkInfo.newBuilder().type("fj").shareKey("share")
|
||||
.shareUrl("https://share.feijipan.com/s/share").build();
|
||||
info.getOtherParam().put("dirId", "777");
|
||||
info.getOtherParam().put("uuid", "test-uuid");
|
||||
if (page != null) {
|
||||
info.getOtherParam().put(FolderPaging.PAGE, page);
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
private FjTool tool(ShareLinkInfo info) {
|
||||
WebClient routed = (WebClient) Proxy.newProxyInstance(WebClient.class.getClassLoader(),
|
||||
new Class<?>[]{WebClient.class}, (proxy, method, args) -> {
|
||||
if (args != null && args.length > 0 && args[0] instanceof UriTemplate template) {
|
||||
args[0] = Proxy.newProxyInstance(UriTemplate.class.getClassLoader(),
|
||||
new Class<?>[]{UriTemplate.class}, (p, m, a) -> {
|
||||
Object expanded = m.invoke(template, a);
|
||||
return expanded instanceof String url
|
||||
? url.replace("https://api.feijipan.com", "http://127.0.0.1:" + server.actualPort())
|
||||
: expanded;
|
||||
});
|
||||
}
|
||||
return method.invoke(local, args);
|
||||
});
|
||||
return new FjTool(info) {{ client = routed; clientNoRedirects = routed; }};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user