Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

refactor(mobile): reuse attachment metadata in direct group uploads

liuliuyaoba8991527e85
diff --git a/apps/mobile/src/__tests__/chatServerUpload.test.ts b/apps/mobile/src/__tests__/chatServerUpload.test.ts
index f25663d802..ef5d4a85c4 100644
--- a/apps/mobile/src/__tests__/chatServerUpload.test.ts
+++ b/apps/mobile/src/__tests__/chatServerUpload.test.ts
@@ -21,7 +21,9 @@ describe('mobile direct private media upload', () => {
[`/conversations/${room}/media/${media}/complete`, 'POST', { operationId: `seal:${media}` }],
]);
expect(f.put).toHaveBeenCalledWith('https://upload.example.invalid/signed', 'file:///picked', mimeType, undefined);
- expect(result).toMatchObject({ id: media, name: file.name, path: '', category: mimeType.startsWith('image/') ? 'image' : 'file' });
+ // Unit: the upload entry returns the same dotted extension metadata as the existing attachment pipeline.
+ // report.pdf exposed the direct adapter's divergent 'pdf' value; existing success checks missed this field.
+ expect(result).toMatchObject({ id: media, name: file.name, path: '', ext: '.pdf', category: mimeType.startsWith('image/') ? 'image' : 'file' });
expect(JSON.stringify(result)).not.toContain('https:');
});
it('does not seal or send after a failed PUT', async () => {
diff --git a/apps/mobile/src/chat/chatServerUpload.ts b/apps/mobile/src/chat/chatServerUpload.ts
index 419f786248..3fa96a4294 100644
--- a/apps/mobile/src/chat/chatServerUpload.ts
+++ b/apps/mobile/src/chat/chatServerUpload.ts
@@ -1,4 +1,6 @@
import type { RemoteSerializedAttachment } from '@/session/types';
+import { effectiveUploadMimeType, extractRemoteFileExt } from '@/session/attachments';
+import type { MobileAttachmentUploadCandidate } from '@/session/mobileAttachmentUpload';
import type { ChatRequest } from './chatServerClient';
import { chatHttpsUrl, chatId } from './chatServerClient';
@@ -9,7 +11,7 @@ export function createChatServerUpload(deps: {
operationId(): string;
put(url: string, uri: string, mimeType: string, signal?: AbortSignal): Promise<void>;
}) {
- return async (roomId: string, candidate: { name: string; size: number; mimeType?: string }, uri: string,
+ return async (roomId: string, candidate: MobileAttachmentUploadCandidate, uri: string,
signal?: AbortSignal): Promise<RemoteSerializedAttachment> => {
const check = () => {
if (!deps.current()) throw new Error('OWNER_CHANGED');
@@ -19,7 +21,7 @@ export function createChatServerUpload(deps: {
if (!Number.isSafeInteger(candidate.size) || candidate.size <= 0) throw new Error('ATTACHMENT_UNAVAILABLE');
if (candidate.size > 100 * 1024 * 1024) throw new Error('ATTACHMENT_TOO_LARGE');
const room = chatId(roomId);
- const type = candidate.mimeType?.trim() || 'application/octet-stream';
+ const type = effectiveUploadMimeType(candidate.mimeType);
const upload = await deps.request<{ id: string; uploadUrl: string }>(`/conversations/${room}/media`, 'POST', {
operationId: `media:${deps.operationId()}`, name: candidate.name, type, size: candidate.size,
});
@@ -31,6 +33,6 @@ export function createChatServerUpload(deps: {
check();
// No computer/relay reference: abandoned media follows the server's media lifecycle.
return { id, name: candidate.name, category: type.startsWith('image/') ? 'image' : 'file',
- mimeType: type, size: candidate.size, path: '', ext: candidate.name.includes('.') ? candidate.name.split('.').pop()! : '' };
+ mimeType: type, size: candidate.size, path: '', ext: extractRemoteFileExt(candidate.name) };
};
}
diff --git a/apps/mobile/src/session/attachments.ts b/apps/mobile/src/session/attachments.ts
index e42b2b6828..cf443d3564 100644
--- a/apps/mobile/src/session/attachments.ts
+++ b/apps/mobile/src/session/attachments.ts
@@ -8,6 +8,20 @@ export type MobileAttachmentCategory = RemoteSerializedAttachment['category'];
export const MOBILE_MAX_ATTACHMENTS = 20;
+/**
+ * mimeType 缺失时的统一兜底。预签名的 contentType 与 PUT 实发的 Content-Type
+ * 必须**逐字节一致**且**都不能缺**:缺失时预签名把 Content-Type 位签成空串,而
+ * expo-file-system 原生直传层会自动补 application/octet-stream,两边不一致直接
+ * OSS SignatureDoesNotMatch 403(2026-07 粘贴图片无 mimeType 实撞)。presign 与
+ * buildMobileAttachmentUploadHeaders 都必须经 effectiveUploadMimeType 取值。
+ */
+const FALLBACK_UPLOAD_MIME = 'application/octet-stream';
+
+export function effectiveUploadMimeType(mimeType: string | undefined): string {
+ const trimmed = mimeType?.trim();
+ return trimmed ? trimmed : FALLBACK_UPLOAD_MIME;
+}
+
/**
* 与桌面一致,附件不设产品层体积上限。直连只受电脑磁盘空间约束;只有 OSS 保底中转有
* 服务端单对象上限,超过它的附件没有保底、只能直连发送。
diff --git a/apps/mobile/src/session/mobileAttachmentUpload.ts b/apps/mobile/src/session/mobileAttachmentUpload.ts
index dbb1da1178..5e6712da34 100644
--- a/apps/mobile/src/session/mobileAttachmentUpload.ts
+++ b/apps/mobile/src/session/mobileAttachmentUpload.ts
@@ -7,6 +7,7 @@ import { withTransientRemoteRetry } from '@/device-link/remoteRetry';
import { isAttachmentOssRef, parseAttachmentOssRef } from '@/session/attachmentOssRef';
import {
buildMobileUploadedAttachment,
+ effectiveUploadMimeType,
assertMobileDocumentSize,
assertMobileOssAttachmentSize,
isWithinMobileOssAttachmentLimit,
@@ -54,19 +55,8 @@ interface UploadDeps {
const PRESIGN_TIMEOUT_MS = 12_000;
const PRESIGN_MAX_ATTEMPTS = 2;
-/**
- * mimeType 缺失时的统一兜底。预签名的 contentType 与 PUT 实发的 Content-Type
- * 必须**逐字节一致**且**都不能缺**:缺失时预签名把 Content-Type 位签成空串,而
- * expo-file-system 原生直传层会自动补 application/octet-stream,两边不一致直接
- * OSS SignatureDoesNotMatch 403(2026-07 粘贴图片无 mimeType 实撞)。presign 与
- * buildMobileAttachmentUploadHeaders 都必须经 effectiveUploadMimeType 取值。
- */
-const FALLBACK_UPLOAD_MIME = 'application/octet-stream';
-
-export function effectiveUploadMimeType(mimeType: string | undefined): string {
- const trimmed = mimeType?.trim();
- return trimmed ? trimmed : FALLBACK_UPLOAD_MIME;
-}
+// Preserve the existing helper import for upload callers.
+export { effectiveUploadMimeType } from '@/session/attachments';
export async function presignMobileAttachmentUpload(
candidate: MobileAttachmentUploadCandidate,