fix(core): finalize c2c file upload so the recipient can download (#157)
Private (c2c) file sends uploaded the bytes (apply 0xE37_1700 + Highway) and PbSendMsg returned ok, but the receiver saw "文件传输失败": the message carried only FileExtra.file (metadata) and not the server-issued download routing. Group files use a different element, hence c2c-only. Port NapCat's upload→finalize→send sequence: - add 0xE37_800 (FinalizeOfflineFile) OIDB service, run after the Highway PUT to fetch the download metadata (field30) - extend FileExtra with field6 (PrivateFileExtra.field2) and populate it from that metadata in sendC2cFile; covers both the url-upload and the file_id-resend send paths (both funnel through sendC2cFile) Tests: byte-oracle locks the 0xE37_800 request + FileExtra.field6 tags; core routing test now asserts finalize runs and field6 is populated. Needs real-machine verification (send a private file, confirm download).
This commit is contained in:
7 files changed
+325
-3
No files matched your search
@@ -344,6 +344,10 @@
|
||||
"types": "./src/oidb-services/group-file/upload-private-file-request.ts",
|
||||
"default": "./src/oidb-services/group-file/upload-private-file-request.ts"
|
||||
},
|
||||
"./oidb-services/group-file/finalize-offline-file": {
|
||||
"types": "./src/oidb-services/group-file/finalize-offline-file.ts",
|
||||
"default": "./src/oidb-services/group-file/finalize-offline-file.ts"
|
||||
},
|
||||
"./oidb-services/group-file/list-group-files-page": {
|
||||
"types": "./src/oidb-services/group-file/list-group-files-page.ts",
|
||||
"default": "./src/oidb-services/group-file/list-group-files-page.ts"
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
// 0xE37_800 — c2c (private) offline-file finalize.
|
||||
//
|
||||
// Runs AFTER the Highway PUT for a private file. The apply (0xE37_1700)
|
||||
// + Highway upload get the bytes onto the server, but the recipient
|
||||
// can't download the file until this call returns the server-issued
|
||||
// download routing (metadata). Those fields ride along in the outgoing
|
||||
// PbSendMsg as `FileExtra.field6` — without them the receiver's client
|
||||
// shows "文件传输失败" even though PbSendMsg returned ok.
|
||||
//
|
||||
// NapCat runs this unconditionally (even when the apply reports the file
|
||||
// already exists). Ported from NapCat `highway/DownloadOfflineFile`.
|
||||
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type {
|
||||
OidbOfflineFileFinalizeReq,
|
||||
OidbOfflineFileFinalizeResp,
|
||||
OidbOfflineFileMetadata,
|
||||
} from '@snowluma/proto-defs/oidb-actions/media';
|
||||
import { invokeOidb, type OidbSender } from '../../oidb-service';
|
||||
|
||||
export namespace FinalizeOfflineFile {
|
||||
export const command = 0xE37;
|
||||
export const subCommand = 800;
|
||||
|
||||
export interface Params {
|
||||
senderUid: string;
|
||||
receiverUid: string;
|
||||
fileUuid: string;
|
||||
fileHash: string;
|
||||
}
|
||||
|
||||
export type Deps = OidbSender;
|
||||
|
||||
export const serialize = (_ctx: Deps, p: Params): OidbOfflineFileFinalizeReq => ({
|
||||
subCommand: 800,
|
||||
field2: 0,
|
||||
body: {
|
||||
senderUid: p.senderUid,
|
||||
receiverUid: p.receiverUid,
|
||||
fileUuid: p.fileUuid,
|
||||
fileHash: p.fileHash,
|
||||
},
|
||||
field101: 3,
|
||||
field102: 1,
|
||||
field200: 1,
|
||||
});
|
||||
|
||||
export const deserialize = (_ctx: Deps, body: OidbOfflineFileFinalizeResp): OidbOfflineFileMetadata =>
|
||||
body.body?.metadata ?? {};
|
||||
|
||||
export const encode = (env: OidbBase<OidbOfflineFileFinalizeReq>): Uint8Array =>
|
||||
protobuf_encode<OidbBase<OidbOfflineFileFinalizeReq>>(env);
|
||||
|
||||
export const decode = (bytes: Uint8Array): OidbBase<OidbOfflineFileFinalizeResp> =>
|
||||
protobuf_decode<OidbBase<OidbOfflineFileFinalizeResp>>(bytes);
|
||||
|
||||
export const invoke = (deps: Deps, params: Params): Promise<OidbOfflineFileMetadata> =>
|
||||
invokeOidb(deps, FinalizeOfflineFile, params);
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type { OidbOfflineFileFinalizeResp } from '@snowluma/proto-defs/oidb-actions/media';
|
||||
import type { FileExtra } from '@snowluma/proto-defs/message';
|
||||
import type { SendPacketResult } from '@snowluma/common/packet-sender';
|
||||
|
||||
import { FinalizeOfflineFile } from '../../../src/oidb-services/group-file/finalize-offline-file';
|
||||
import { env, m, s, v } from '../_pb-oracle';
|
||||
|
||||
// `bytes` fields are wire type 2 — identical framing to a sub-message, so the
|
||||
// oracle's `m()` (tag + length + payload) doubles as the bytes emitter.
|
||||
const b = m;
|
||||
|
||||
function makeDeps(resp?: OidbOfflineFileFinalizeResp) {
|
||||
const responseData = resp !== undefined
|
||||
? Buffer.from(protobuf_encode<OidbBase<OidbOfflineFileFinalizeResp>>({ body: resp }))
|
||||
: Buffer.alloc(0);
|
||||
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData };
|
||||
return { sendRawPacket: vi.fn(async () => r) };
|
||||
}
|
||||
|
||||
describe('FinalizeOfflineFile (0xE37_800)', () => {
|
||||
it('declares 0xE37_800', () => {
|
||||
expect(FinalizeOfflineFile.command).toBe(0xE37);
|
||||
expect(FinalizeOfflineFile.subCommand).toBe(800);
|
||||
});
|
||||
|
||||
it('serializes the request body byte-for-byte (oracle locks every tag)', async () => {
|
||||
const deps = makeDeps({});
|
||||
await FinalizeOfflineFile.invoke(deps, {
|
||||
senderUid: 'su', receiverUid: 'ru', fileUuid: 'uuid', fileHash: 'hh',
|
||||
});
|
||||
const [wire, bytes] = deps.sendRawPacket.mock.calls[0]!;
|
||||
expect(wire).toBe('OidbSvcTrpcTcp.0xe37_800');
|
||||
|
||||
// body(10) { senderUid(10) receiverUid(20) fileUuid(30) fileHash(40) }
|
||||
const innerBody = [...s(10, 'su'), ...s(20, 'ru'), ...s(30, 'uuid'), ...s(40, 'hh')];
|
||||
// req: subCommand(1)=800, field2(2)=0 omitted, body(10), field101(101)=3,
|
||||
// field102(102)=1, field200(200)=1
|
||||
const req = [...v(1, 800), ...m(10, innerBody), ...v(101, 3), ...v(102, 1), ...v(200, 1)];
|
||||
const expected = env(0xE37, 800, req, false);
|
||||
expect(Buffer.from(bytes).toString('hex')).toBe(expected);
|
||||
});
|
||||
|
||||
it('extracts the download-routing metadata from field30 of the response', async () => {
|
||||
const deps = makeDeps({
|
||||
body: { metadata: { field3: 5, field100: new Uint8Array([9, 9]), field101: new Uint8Array([8]), field110: 7, timestamp1: 42 } },
|
||||
});
|
||||
const meta = await FinalizeOfflineFile.invoke(deps, {
|
||||
senderUid: 'su', receiverUid: 'ru', fileUuid: 'uuid', fileHash: 'hh',
|
||||
});
|
||||
expect(meta.field3).toBe(5);
|
||||
expect(meta.field110).toBe(7);
|
||||
expect(meta.timestamp1).toBe(42);
|
||||
expect(Array.from(meta.field100 ?? [])).toEqual([9, 9]);
|
||||
expect(Array.from(meta.field101 ?? [])).toEqual([8]);
|
||||
});
|
||||
|
||||
it('returns {} when the response carries no metadata', async () => {
|
||||
const deps = makeDeps({});
|
||||
const meta = await FinalizeOfflineFile.invoke(deps, {
|
||||
senderUid: 'su', receiverUid: 'ru', fileUuid: 'uuid', fileHash: 'hh',
|
||||
});
|
||||
expect(meta).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe('FileExtra.field6 (c2c download routing — issue #157)', () => {
|
||||
it('encodes field6.field2 with the exact NapCat tag layout', () => {
|
||||
const extra: FileExtra = {
|
||||
field6: {
|
||||
field2: {
|
||||
field1: 7,
|
||||
fileUuid: 'uu',
|
||||
fileName: 'n.bin',
|
||||
field6: 3,
|
||||
field7: new Uint8Array([1, 2]),
|
||||
field8: new Uint8Array([3]),
|
||||
timestamp1: 99,
|
||||
fileHash: 'hh',
|
||||
selfUid: 'su',
|
||||
destUid: 'du',
|
||||
},
|
||||
},
|
||||
};
|
||||
const hex = Buffer.from(protobuf_encode<FileExtra>(extra)).toString('hex');
|
||||
|
||||
// PrivateFileExtraField2 in ascending tag order:
|
||||
// field1(1) fileUuid(4) fileName(5) field6(6) field7(7,bytes)
|
||||
// field8(8,bytes) timestamp1(9) fileHash(14) selfUid(15) destUid(16)
|
||||
const f2 = [
|
||||
...v(1, 7),
|
||||
...s(4, 'uu'),
|
||||
...s(5, 'n.bin'),
|
||||
...v(6, 3),
|
||||
...b(7, [1, 2]),
|
||||
...b(8, [3]),
|
||||
...v(9, 99),
|
||||
...s(14, 'hh'),
|
||||
...s(15, 'su'),
|
||||
...s(16, 'du'),
|
||||
];
|
||||
// PrivateFileExtra { field2(2) } → FileExtra { field6(6) }
|
||||
const expected = Buffer.from(m(6, m(2, f2))).toString('hex');
|
||||
expect(hex).toBe(expected);
|
||||
});
|
||||
|
||||
it('round-trips field6 through encode/decode', () => {
|
||||
const extra: FileExtra = {
|
||||
file: { fileUuid: 'u', fileName: 'f', subcmd: 1 },
|
||||
field6: { field2: { field1: 1, fileUuid: 'u', fileName: 'f', timestamp1: 5, selfUid: 's', destUid: 'd' } },
|
||||
};
|
||||
const back = protobuf_decode<FileExtra>(protobuf_encode<FileExtra>(extra));
|
||||
expect(back.field6?.field2).toMatchObject({
|
||||
field1: 1, fileUuid: 'u', fileName: 'f', timestamp1: 5, selfUid: 's', destUid: 'd',
|
||||
});
|
||||
expect(back.file?.subcmd).toBe(1);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user