UNPKG

@nocobase/plugin-file-manager

Version:

Provides files storage services with files collection template and attachment field.

90 lines (89 loc) 3.58 kB
/** * This file is part of the NocoBase (R) project. * Copyright (c) 2020-2024 NocoBase Co., Ltd. * Authors: NocoBase Team. * * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. * For more information, please refer to: https://www.nocobase.com/agreement. */ import type { Application } from '@nocobase/client-v2'; import { Plugin } from '@nocobase/client-v2'; import type React from 'react'; type UploadFileResult = { errorMessage?: string; data?: unknown; }; type StorageUploadOptions = { file: File; apiClient: Application['apiClient']; storageType?: string; storageId?: number; storageRules?: { size?: number; mimetype?: string | string[]; }; dataSourceKey?: string; fileCollectionName: string; query?: Record<string, string | number | boolean>; }; /** * Unified storage type definition. A single entry describes both the * settings-page form (UI: `title` + `formLoader` + `defaultValues`) and the * optional runtime upload override (`upload` / `createUploadCustomRequest`). * * Built-in storages register the UI fields only and fall back to the default * `apiClient.request` multipart upload. Commercial / third-party storage * plugins (e.g. S3 Pro) layer a presigned-PUT pipeline on top by populating * the optional runtime fields. */ export interface StorageType { /** Server-side storage type identifier — must match the persisted `storages.type` value. */ name: string; /** Display title shown in the "Add new" dropdown and drawer header. Wrapped with `t(...)` at render time. */ title: string; /** * Async loader for the form body — the list of `<Form.Item>` fields inside * the storage drawer. The loaded module's default export is used, matching * the convention of `componentLoader` and `registerModelLoaders` elsewhere * in the codebase. */ formLoader: () => Promise<{ default: React.ComponentType; }>; /** * Optional per-storage initial values merged on top of the page-level * defaults (`type` + generated `name`) when creating a new record. */ defaultValues?: Record<string, any>; /** * Optional override of the `PluginFileManagerClientV2.uploadFile` pipeline, * used when editors (Markdown, Vditor, etc.) paste / drop files outside of * an attachment field. When absent, uploads go through the default * multipart `apiClient.request` path. */ upload?: (options: StorageUploadOptions) => Promise<UploadFileResult>; /** * Optional `customRequest` factory consumed by `UploadFieldModel` for the * inline attachment Upload component. When absent, `UploadFieldModel` falls * back to `uploadFile`. */ createUploadCustomRequest?: (options: any) => (option: any) => void; } export declare class PluginFileManagerClientV2 extends Plugin<Record<string, never>, Application> { static buildInStorage: string[]; storageTypes: Map<string, StorageType>; load(): Promise<void>; registerStorageType(name: string, storageType: Omit<StorageType, 'name'>): void; getStorageType(name?: string): StorageType; private registerBuiltInStorageTypes; uploadFile(options?: { file: File; fileCollectionName?: string; storageType?: string; storageId?: number; storageRules?: StorageUploadOptions['storageRules']; dataSourceKey?: string; query?: StorageUploadOptions['query']; }): Promise<UploadFileResult>; } export default PluginFileManagerClientV2;