feat: integrate platform capabilities for enhanced download management

- Refactored `DownloadAll` component to utilize platform capabilities for download types, improving flexibility in handling various download methods.
- Introduced `buildEnabledDownloadModes` function to determine available download modes based on user settings and platform capabilities.
- Updated `FileRow` and `PanelGeneral` components to conditionally render download options based on platform capabilities.
- Added tests for new functionalities in `DownloadAll`, `FileRow`, and `PanelGeneral` to ensure correct behavior under different platform conditions.
- Enhanced error handling for unsupported download methods, providing clearer feedback to users.
This commit is contained in:
鲁树人 committed 2026-11-01 11:32:04 +09:00
1 parent b5fdc8ebbe
commit d424837e66
17 files changed
+417 -178

No files matched your search

+20 -20
View File
@@ -1,21 +1,12 @@
import { AndroidImportType, type BatchDownloadType, type NativeImportType } from '~/platform/capabilities';
import { NotImplementedError, NotSupportedMethod, PlatformNotInitializedError } from '~/platform/errors';
import { AndroidImportType, type NativeImportType } from '~/platform/capabilities';
import { NotSupportedMethod, PlatformNotInitializedError } from '~/platform/errors';
import type { PlatformProvider } from '~/platform/PlatformProvider';
import type {
AndroidAdbImportOptions,
DownloadManyInput,
DownloadOneInput,
NativeImportResult,
PlatformCapabilities,
} from '~/platform/types';
import type { AndroidAdbImportOptions, NativeImportResult, PlatformCapabilities } from '~/platform/types';
import { createBrowserDownload, type BrowserDownload } from './browserDownload';
import { loadDecryptWorkerCrypto, type BrowserCrypto } from './browserCrypto';
import { createBrowserSettingsIo, DEFAULT_SETTINGS_STORAGE_KEY, type SettingsStorage } from './browserSettingsIo';
import { detectBrowserDownloadTypes } from './detectDownloadCapabilities';
function notImplemented(capability: string): Promise<never> {
return Promise.reject(new NotImplementedError(capability));
}
function notSupported(capability: string): Promise<never> {
return Promise.reject(new NotSupportedMethod(capability));
}
@@ -32,19 +23,29 @@ export interface BrowserProviderOptions {
crypto?: BrowserCrypto;
/** Defaults to dynamic-importing decrypt-worker/client. Injected in tests. */
loadCrypto?: () => Promise<BrowserCrypto>;
/** Override download backend (tests). Defaults to FS Access / ZIP helpers. */
download?: BrowserDownload;
/** Override detected `downloadTypes` (tests). */
downloadTypes?: PlatformCapabilities['downloadTypes'];
/** Override `directFsAccess` (tests). Browser default is `false`. */
directFsAccess?: boolean;
}
/**
* In-browser `PlatformProvider`.
* Capabilities follow runtime File System Access / save-picker probing (no DirectWrite).
* Capabilities follow runtime File System Access / save-picker probing (`directFsAccess: false`).
* `settings` persists production settings via localStorage (or injected storage).
* `crypto` binds to the decrypt worker only inside `init()` (dynamic import; no construct-time Worker).
* `download` currently throws `NotImplementedError` (not yet delegated to download helpers).
* `download` delegates to anchor / directory picker / streaming ZIP / pack ZIP helpers.
* Native and ADB key import throw `NotSupportedMethod` (`nativeKeyAccess` empty, `androidKeyAccess` None).
*/
export function createBrowserProvider(options: BrowserProviderOptions = {}): PlatformProvider {
const downloadTypes = Object.freeze(
options.downloadTypes ? [...options.downloadTypes] : detectBrowserDownloadTypes(),
);
const capabilities: PlatformCapabilities = Object.freeze({
downloadTypes: Object.freeze(detectBrowserDownloadTypes()),
downloadTypes,
directFsAccess: options.directFsAccess ?? false,
androidKeyAccess: AndroidImportType.None,
nativeKeyAccess: Object.freeze([] as NativeImportType[]),
});
@@ -54,6 +55,8 @@ export function createBrowserProvider(options: BrowserProviderOptions = {}): Pla
options.settingsStorageKey ?? DEFAULT_SETTINGS_STORAGE_KEY,
);
const download = options.download ?? createBrowserDownload(downloadTypes);
const loadCrypto = options.loadCrypto ?? loadDecryptWorkerCrypto;
let backend: BrowserCrypto | null = options.crypto ?? null;
let initPromise: Promise<void> | null = options.crypto ? Promise.resolve() : null;
@@ -97,10 +100,7 @@ export function createBrowserProvider(options: BrowserProviderOptions = {}): Pla
save: (settings) => settingsIo.save(settings),
},
download: {
saveOne: (_input: DownloadOneInput) => notImplemented('download.saveOne'),
saveMany: (_type: BatchDownloadType, _input: DownloadManyInput) => notImplemented('download.saveMany'),
},
download,
keys: {
importNative: (_targets: readonly NativeImportType[]): Promise<NativeImportResult> =>