refactor(protocol): compose group-file URLs in the OIDB namespaces
Move retCode checks and URL assembly into GetGroupFileUrl, GetPrivateFileUrl, and ListGroupFilesPage so GroupFileApi only loops pages and resolves display names.
This commit is contained in:
9 files changed
+104
-82
No files matched your search
@@ -1,14 +1,30 @@
|
||||
// 0x6D6_2 — resolve a group file_id → ftn_handler download URL. The
|
||||
// server returns a `downloadUrl` blob (raw bytes that hex-encode the
|
||||
// download key) and a `downloadDns` host. The facade composes the
|
||||
// final `https://<dns>/ftn_handler/<HEX>/?fname=<fileId>` string.
|
||||
// download key) and a `downloadDns` host. This namespace owns retCode
|
||||
// and the `https://<dns>/ftn_handler/<HEX>/?fname=<fileId>` string.
|
||||
|
||||
import { toHexUpper } from '@snowluma/common/hex';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type {
|
||||
OidbGroupFileReq, OidbGroupFileResp, OidbGroupFileDownloadResp,
|
||||
} from '@snowluma/proto-defs/oidb-actions/group-file';
|
||||
import { invokeOidb, type OidbSender } from '../../oidb-service';
|
||||
import { ensureRetCodeZero } from '../shared';
|
||||
|
||||
function composeGroupFileUrl(download: OidbGroupFileDownloadResp, fileId: string): string {
|
||||
ensureRetCodeZero('group file url', download.retCode, download.retMsg, download.clientWording);
|
||||
const dns = (typeof download.downloadDns === 'string' && download.downloadDns)
|
||||
|| (typeof download.downloadIp === 'string' && download.downloadIp)
|
||||
|| '';
|
||||
const hexUrl = download.downloadUrl instanceof Uint8Array && download.downloadUrl.length > 0
|
||||
? toHexUpper(download.downloadUrl)
|
||||
: '';
|
||||
if (!dns || !hexUrl) {
|
||||
throw new Error('group file url response invalid');
|
||||
}
|
||||
return `https://${dns}/ftn_handler/${hexUrl}/?fname=${fileId}`;
|
||||
}
|
||||
|
||||
export namespace GetGroupFileUrl {
|
||||
export const command = 0x6D6;
|
||||
@@ -27,10 +43,11 @@ export namespace GetGroupFileUrl {
|
||||
},
|
||||
});
|
||||
|
||||
export const deserialize = (_ctx: Deps, body: OidbGroupFileResp): OidbGroupFileDownloadResp => {
|
||||
// Default deserialize has no fileId for fname=; invoke() rewires it.
|
||||
export const deserialize = (_ctx: Deps, body: OidbGroupFileResp): string => {
|
||||
const download = body.download;
|
||||
if (!download) throw new Error('group file url response missing');
|
||||
return download;
|
||||
return composeGroupFileUrl(download, '');
|
||||
};
|
||||
|
||||
export const encode = (env: OidbBase<OidbGroupFileReq>): Uint8Array =>
|
||||
@@ -39,6 +56,13 @@ export namespace GetGroupFileUrl {
|
||||
export const decode = (bytes: Uint8Array): OidbBase<OidbGroupFileResp> =>
|
||||
protobuf_decode<OidbBase<OidbGroupFileResp>>(bytes);
|
||||
|
||||
export const invoke = (deps: Deps, params: Params): Promise<OidbGroupFileDownloadResp> =>
|
||||
invokeOidb(deps, GetGroupFileUrl, params);
|
||||
export const invoke = (deps: Deps, params: Params): Promise<string> =>
|
||||
invokeOidb(deps, {
|
||||
...GetGroupFileUrl,
|
||||
deserialize: (_ctx, body) => {
|
||||
const download = body.download;
|
||||
if (!download) throw new Error('group file url response missing');
|
||||
return composeGroupFileUrl(download, params.fileId);
|
||||
},
|
||||
}, params);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// 0xE37_1200 — resolve a private (c2c) file_id → http download URL.
|
||||
// Carries an opaque 4-byte `field99999` magic the server's deserializer
|
||||
// requires (Lagrange has the same constant). The facade composes the
|
||||
// final `http://<server>:<port><url>&isthumb=0` string.
|
||||
// requires (Lagrange has the same constant). This namespace owns the
|
||||
// `http://<server>:<port><url>&isthumb=0` string.
|
||||
//
|
||||
// 5s timeout matches the original facade — the server batches these
|
||||
// at low priority and stalls past the default 30s.
|
||||
@@ -10,9 +10,9 @@ import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type {
|
||||
OidbPrivateFileDownloadReq, OidbPrivateFileDownloadResp,
|
||||
OidbPrivateFileDownloadRespResult,
|
||||
} from '@snowluma/proto-defs/oidb-actions/media';
|
||||
import { invokeOidb, type OidbSender } from '../../oidb-service';
|
||||
import { toInt } from '../shared';
|
||||
|
||||
const TIMEOUT_MS = 5000;
|
||||
|
||||
@@ -39,10 +39,16 @@ export namespace GetPrivateFileUrl {
|
||||
field99999: new Uint8Array([0xC0, 0x85, 0x2C, 0x01]),
|
||||
});
|
||||
|
||||
export const deserialize = (_ctx: Deps, body: OidbPrivateFileDownloadResp): OidbPrivateFileDownloadRespResult => {
|
||||
export const deserialize = (_ctx: Deps, body: OidbPrivateFileDownloadResp): string => {
|
||||
const result = body.body?.result;
|
||||
if (!result) throw new Error('private file url response invalid');
|
||||
return result;
|
||||
const server = typeof result.server === 'string' ? result.server : '';
|
||||
const port = toInt(result.port);
|
||||
const url = typeof result.url === 'string' ? result.url : '';
|
||||
if (!server || !port || !url) {
|
||||
throw new Error('private file url response invalid');
|
||||
}
|
||||
return `http://${server}:${port}${url}&isthumb=0`;
|
||||
};
|
||||
|
||||
export const encode = (env: OidbBase<OidbPrivateFileDownloadReq>): Uint8Array =>
|
||||
@@ -51,6 +57,6 @@ export namespace GetPrivateFileUrl {
|
||||
export const decode = (bytes: Uint8Array): OidbBase<OidbPrivateFileDownloadResp> =>
|
||||
protobuf_decode<OidbBase<OidbPrivateFileDownloadResp>>(bytes);
|
||||
|
||||
export const invoke = (deps: Deps, params: Params): Promise<OidbPrivateFileDownloadRespResult> =>
|
||||
export const invoke = (deps: Deps, params: Params): Promise<string> =>
|
||||
invokeOidb(deps, GetPrivateFileUrl, params, TIMEOUT_MS);
|
||||
}
|
||||
@@ -6,9 +6,17 @@
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type {
|
||||
OidbGroupFileViewReq, OidbGroupFileViewResp, OidbGroupFileListResp,
|
||||
OidbGroupFileListItemResp,
|
||||
OidbGroupFileViewReq,
|
||||
OidbGroupFileViewResp,
|
||||
} from '@snowluma/proto-defs/oidb-actions/group-file';
|
||||
import { invokeOidb, type OidbSender } from '../../oidb-service';
|
||||
import { ensureRetCodeZero } from '../shared';
|
||||
|
||||
export interface GroupFileListPage {
|
||||
items: OidbGroupFileListItemResp[];
|
||||
isEnd: boolean;
|
||||
}
|
||||
|
||||
export namespace ListGroupFilesPage {
|
||||
export const command = 0x6D8;
|
||||
@@ -37,11 +45,13 @@ export namespace ListGroupFilesPage {
|
||||
},
|
||||
});
|
||||
|
||||
/** Returns the list body directly so the facade can inspect
|
||||
* `items`, `isEnd`, and `retCode` in one place. Returns null when
|
||||
/** Returns items + isEnd after checking list.retCode. Returns null when
|
||||
* the server elides the list entirely (end-of-stream sentinel). */
|
||||
export const deserialize = (_ctx: Deps, body: OidbGroupFileViewResp): OidbGroupFileListResp | null => {
|
||||
return body.list ?? null;
|
||||
export const deserialize = (_ctx: Deps, body: OidbGroupFileViewResp): GroupFileListPage | null => {
|
||||
const list = body.list;
|
||||
if (!list) return null;
|
||||
ensureRetCodeZero('group file list', list.retCode, list.retMsg, list.clientWording);
|
||||
return { items: list.items ?? [], isEnd: !!list.isEnd };
|
||||
};
|
||||
|
||||
export const encode = (env: OidbBase<OidbGroupFileViewReq>): Uint8Array =>
|
||||
@@ -50,6 +60,6 @@ export namespace ListGroupFilesPage {
|
||||
export const decode = (bytes: Uint8Array): OidbBase<OidbGroupFileViewResp> =>
|
||||
protobuf_decode<OidbBase<OidbGroupFileViewResp>>(bytes);
|
||||
|
||||
export const invoke = (deps: Deps, params: Params): Promise<OidbGroupFileListResp | null> =>
|
||||
export const invoke = (deps: Deps, params: Params): Promise<GroupFileListPage | null> =>
|
||||
invokeOidb(deps, ListGroupFilesPage, params);
|
||||
}
|
||||
@@ -10,7 +10,7 @@
|
||||
// rKeyParam` triple → composed into `https://<domain><path><rKey>`.
|
||||
|
||||
import type { NTV2IndexNode, NTV2RichMediaResp } from '@snowluma/proto-defs/oidb-actions/media';
|
||||
import { ensureRetCodeZero } from '../shared';
|
||||
import { ensureRetCodeZero, toInt } from '../shared';
|
||||
|
||||
export function parseNtv2DownloadUrl(body: NTV2RichMediaResp): string {
|
||||
ensureRetCodeZero('ntv2 download', body.respHead?.retCode, body.respHead?.message, undefined);
|
||||
@@ -56,12 +56,6 @@ export function normalizeMediaNode(node: NtMediaIndex): NTV2IndexNode {
|
||||
};
|
||||
}
|
||||
|
||||
function toInt(value: unknown): number {
|
||||
if (typeof value === 'number' && Number.isFinite(value)) return Math.trunc(value);
|
||||
if (typeof value === 'bigint') return Number(value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
export interface NtMediaIndex {
|
||||
info?: {
|
||||
fileSize?: number;
|
||||
|
||||
Reference in new issue
Block a user