Inherit StorageType
Create a new class and implement the make() and delete() methods, and override hooks like getFileURL(), getFileStream(), getFileData() if necessary.
Example:
beforeLoad or load lifecycle:After registration, the storage configuration will appear in the storages resource, just like the built-in types. The configuration provided by StorageType.defaults() can be used to auto-fill forms or initialize default records.
On the client-side, you need to inform the file manager how to render the configuration form and whether there is custom upload logic. Each storage type object contains the following properties:
For uploaded files, you can display different preview content on the frontend interface based on different file types. The file manager's attachment field has a built-in browser-based file preview (embedded in an iframe), which supports previewing most file formats (such as images, videos, audio, and PDFs) directly in the browser. When a file format is not supported by the browser for preview, or when special preview interactions are required, you can extend the file type-based preview component.
For example, to extend an image file type with a carousel component, you can use the following code:
Here, attachmentFileTypes is the entry object provided in the @nocobase/client package for extending file types. Use its add method to extend a file type description object.
Each file type must implement a match() method to check if the file type meets the requirements. In the example, the method provided by the mime-match package is used to check the file's mimetype attribute. If it matches the image/* type, it is considered the file type to be processed. If no match is found, it will fall back to the built-in type handling.
The Previewer property on the type description object is the component used for previewing. When the file type matches, this component will be rendered for preview. It is generally recommended to use a dialog-type component (such as <Modal />) as the base container, and then place the preview and interactive content within it to implement the preview functionality.
attachmentFileTypesattachmentFileTypes is a global instance, imported from @nocobase/client:
attachmentFileTypes.add()Registers a new file type description object with the file type registry. The type of the description object is AttachmentFileType.
AttachmentFileTypematch()File format matching method.
The input parameter file is the data object of an uploaded file, containing relevant properties that can be used for type checking:
mimetype: mimetype descriptionextname: file extension, including the "."path: relative storage path of the fileurl: file URLReturns a boolean value indicating whether it matches.
PreviewerA React component for previewing files.
The incoming Props are:
index: The index of the file in the attachment listlist: The attachment listonSwitchIndex: A method for switching the indexThe onSwitchIndex can be passed any index from the list to switch to another file. If null is passed as the argument, the preview component will be closed directly.