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.
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, if you want to integrate a custom online preview for Office files, you can use the following code:
Here, filePreviewTypes is the entry object provided by @nocobase/plugin-file-manager/client for extending file previews. Use its add method to extend a file type descriptor object.
Each file type must implement a match() method to check whether the file type meets the requirements. In the example, matchMimetype is used to check the file's mimetype attribute. If it matches the docx type, it is considered the file type to be handled. If it does not match, the built-in type handling will be used.
The Previewer property on the type descriptor object is the component used for previewing. When the file type matches, this component will be rendered in the preview dialog. You can return any React view (such as an iframe, player, or chart).
filePreviewTypesfilePreviewTypes is a global instance, imported from @nocobase/plugin-file-manager/client:
filePreviewTypes.add()Register a new file type descriptor object with the file type registry. The type of the descriptor object is FilePreviewType.
FilePreviewTypematch()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 "."path: relative storage path of the fileurl: file URLReturns a boolean value indicating whether it matches.
getThumbnailURLReturns the thumbnail URL used in the file list. If the return value is empty, the built-in placeholder image will be used.
PreviewerA React component for previewing files.
The incoming Props are:
file: the current file object (may be a string URL or an object containing url/preview)index: index of the file in the listlist: file list