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。

fix(files): 本地缓存完成后回收临时云端文件

Dash18b4bc1833af
diff --git a/apps/mobile/src/__tests__/fileBrowserExportStreaming.test.ts b/apps/mobile/src/__tests__/fileBrowserExportStreaming.test.ts
index eefd75c283..179b9c9258 100644
--- a/apps/mobile/src/__tests__/fileBrowserExportStreaming.test.ts
+++ b/apps/mobile/src/__tests__/fileBrowserExportStreaming.test.ts
@@ -6,6 +6,34 @@ import { clearPeerMedia, installPeerFileDownload, recordPeerMedia } from '@/devi
vi.mock('@react-native-async-storage/async-storage', () => ({ default: {} }));
describe('file browser playback and download', () => {
+ it.each([true, false])('reclaims the cloud object only after a local copy is available: %s', async (downloaded) => {
+ const local = { ossKey: '', size: 3, mimeType: 'application/pdf' };
+ const uri = 'file:///cached-export.pdf';
+ const dispose = vi.fn();
+ recordPeerMedia(local, uri, dispose);
+ const deleteOssObject = vi.fn();
+ const cacheDownload = vi.fn(async () => {
+ expect(deleteOssObject).not.toHaveBeenCalled();
+ return downloaded ? local : null;
+ });
+ const maker = createMobileMakerTransport({ deviceId: 'cleanup-export', invoke: vi.fn() });
+ vi.spyOn(maker.fileBrowser, 'readBytes').mockResolvedValue({
+ ...local, ossKey: 'export/cloud-key', cacheDownload,
+ });
+ try {
+ const url = await exportRemoteFileToUrl({
+ maker, deviceId: 'cleanup-export', stream: false,
+ openLink: vi.fn(async () => {}),
+ presignGet: vi.fn(async () => ({ getUrl: 'https://example.test/export', expiresAt: new Date(Date.now() + 3_600_000).toISOString() })),
+ deleteOssObject,
+ }, '/p', 'file.pdf', 1);
+ expect(url).toBe(downloaded ? uri : 'https://example.test/export');
+ expect(cacheDownload).toHaveBeenCalledWith('https://example.test/export');
+ expect(deleteOssObject.mock.calls).toEqual(downloaded ? [['export/cloud-key']] : []);
+ expect(dispose).not.toHaveBeenCalled();
+ } finally { clearPeerMedia(); }
+ });
+
it.each(['audio/mpeg', 'video/mp4', 'image/png', 'application/pdf', 'application/octet-stream'])('keeps %s previews off cached peer downloads in either order', async (mimeType) => {
for (const first of [false, true]) {
const direct = { ossKey: '', size: 70_000, mimeType };
diff --git a/apps/mobile/src/__tests__/remoteAbsFileFetch.test.ts b/apps/mobile/src/__tests__/remoteAbsFileFetch.test.ts
index 9439fd727c..7c4225c686 100644
--- a/apps/mobile/src/__tests__/remoteAbsFileFetch.test.ts
+++ b/apps/mobile/src/__tests__/remoteAbsFileFetch.test.ts
@@ -7,6 +7,7 @@ import {
type RemoteAbsFileFetchDeps,
} from '@/session/remoteAbsFileFetch';
import type { RemoteTextFilePreviewResult } from '@/device-link/mobileMakerTransport';
+import { clearPeerMedia, recordPeerMedia } from '@/device-link/peerFileRegistry';
// 文案已 i18n 化;固定 zh-CN 让字面量断言与语言环境解耦(全局 mock 默认 en-US)。
beforeAll(async () => {
@@ -38,6 +39,29 @@ function makeDeps(overrides: Partial<{
}
describe('fetchRemoteAbsFileToUrl', () => {
+ it.each([true, false])('reclaims the cloud object only after a local copy is available: %s', async (downloaded) => {
+ const { deps } = makeDeps();
+ const local = { ossKey: '', size: 3, mimeType: 'image/png' };
+ const uri = 'file:///cached-absolute.png';
+ const dispose = vi.fn();
+ recordPeerMedia(local, uri, dispose);
+ const deleteOssObject = vi.fn();
+ const cacheDownload = vi.fn(async () => {
+ expect(deleteOssObject).not.toHaveBeenCalled();
+ return downloaded ? local : null;
+ });
+ deps.maker.fetchRemoteMedia = vi.fn(async () => ({
+ ...local, ossKey: 'absolute/cloud-key', cacheDownload,
+ }));
+ try {
+ const url = await fetchRemoteAbsFileToUrl({ ...deps, stream: false, deleteOssObject }, '/tmp/shot.png');
+ expect(url).toBe(downloaded ? uri : 'https://oss.example/get/abc');
+ expect(cacheDownload).toHaveBeenCalledWith('https://oss.example/get/abc');
+ expect(deleteOssObject.mock.calls).toEqual(downloaded ? [['absolute/cloud-key']] : []);
+ expect(dispose).not.toHaveBeenCalled();
+ } finally { clearPeerMedia(); }
+ });
+
it('revalidates complete files while retaining streaming URLs', async () => {
for (const first of [true, false]) {
const { deps, fetchRemoteMedia } = makeDeps();
diff --git a/apps/mobile/src/session/fileBrowserExport.ts b/apps/mobile/src/session/fileBrowserExport.ts
index 7b1e5f23aa..757b7a224d 100644
--- a/apps/mobile/src/session/fileBrowserExport.ts
+++ b/apps/mobile/src/session/fileBrowserExport.ts
@@ -81,7 +81,10 @@ export async function exportRemoteFileToUrl(
});
const downloaded = await result.cacheDownload?.(signed.getUrl);
const downloadedUri = downloaded && peerMediaUri(downloaded);
- if (downloadedUri) return downloadedUri;
+ if (downloadedUri) {
+ deps.deleteOssObject?.(result.ossKey);
+ return downloadedUri;
+ }
mobileDebugLog("debug", "files", "file export presigned", {
ms: Date.now() - presignStartedAt,
size: result.size,
diff --git a/apps/mobile/src/session/remoteAbsFileFetch.ts b/apps/mobile/src/session/remoteAbsFileFetch.ts
index b1c504e3b0..6fe8541c74 100644
--- a/apps/mobile/src/session/remoteAbsFileFetch.ts
+++ b/apps/mobile/src/session/remoteAbsFileFetch.ts
@@ -131,6 +131,10 @@ export async function fetchRemoteAbsFileToUrl(
Object.keys(fetchOpts).length ? fetchOpts : undefined,
);
});
+ // Only the local copy can outlive its temporary cloud object.
+ if (resolved.ossKey && resolved.url.startsWith('file://')) {
+ deps.deleteOssObject?.(resolved.ossKey);
+ }
if (deps.stream !== false) rememberFetch(key, resolved, Date.now());
assertActive();
return resolved.url;