Additional Services

File Manager

Updated: July 17, 2023

The File Manager is used to create documents from simple binaries.

From a user perspective, the Nuxeo Platform offers many ways to capture a binary so as to make it a document with a binary property. It can be done from the browser's drag and drop, from the upload REST API, from WebDAV, from CMIS, from Nuxeo Drive,  ...

The file manager service is a traditional Nuxeo Platform service that offers some methods that help standardize what happens when a file is captured in the Platform, in regard to:

  • what document type should be created
  • what is the property mapping logic
  • what is the versioning policy
  • how should the binary be processed (exploding a zip, doing some pre-persistence conversions)

Customising and Using the File Manager Service

  • The File Manager service has a plugin architecture, so that it is possible to contribute different policies depending on the mime-type of the file and the context. The default Nuxeo Platform use cases of the File Manager can be customised reading the documentation of the file manager service extension point. There are also means of controlling the versioning policy of documents updated via this channel. Finally, there is a helper for implementing binary unicity checks.
  • The File Manager can be called and used in your custom Java code with the standard service call pattern:

    ... fileManager = Framework.getService(FileManager.class);
    DocumentModel createdDoc = fileManager.createDocumentFromBlob(coreSession, blob, path, true, fileName);
    
  • And you can also use the dedicated automation operation, which provides a way to create in one REST call an document from a binary, or to create easily a document from a blob in an automation chain.

Implementing Your Own Plugin

Let's take a simple example where the document type depends on the parent folder type. In order to that in a fully automated way, you'll need to implement your own plugin. For example, we have a folder type PurchaseOrderFolder which accepts only PurchaseOrder documents as children, and an InvoiceFolder which accepts only Invoice documents.

  1. Write a class which extends org.nuxeo.ecm.platform.filemanager.service.extension.AbstractFileImporter.

    public class SampleFilemanagerPlugin extends AbstractFileImporter {
    
        private static final long serialVersionUID = 1876876876L;
    
        private static final Log log = LogFactory.getLog(SampleFilemanagerPlugin.class);
    
        @Override
        public DocumentModel create(CoreSession session, Blob content, String path,
                                    boolean overwrite, String filename, TypeManager typeService)
                throws ClientException, IOException {
    
            PathRef root = new PathRef(path);
            DocumentModel rootDoc = session.getDocument(root);
            DocumentModel doc = null;
            switch (rootDoc.getType()) {
                case "PurchaseOrderFolder":doc = createDocType(session, path, content, filename,"PurchaseOrder");break;
                case "InvoiceFolder":doc = createDocType(session, path, content, filename,"Invoice");break;
                default:break;
            }
            if (doc!=null) doc = session.createDocument(doc);
            return doc;
        }
    
        private DocumentModel createDocType(CoreSession session, String path, Blob content, String filename, String type) {
            DocumentModel doc = session.createDocumentModel(path,type,type);
            doc.setPropertyValue("dc:title",filename);
            doc.setPropertyValue("file:content", (Serializable) content);
            doc.setPropertyValue("file:filename",filename);
            return doc;
        }
    }
    

    The created method returns either a DocumentModel object or null. If null is returned, then the file manager service will try with the next plugin. Within this method, we have access to the destination path which enables us to determine the type of the folder and thus create a document with the relevant type.

  2. Add a new contribution to the file manager service.

    <component name="org.nuxeo.sample.filemanager">
        <extension target="org.nuxeo.ecm.platform.filemanager.service.FileManagerService" point="plugins">
            <plugin name="SampleImporter" class="org.nuxeo.sample.SampleFilemanagerPlugin" order="0">
                <filter>.*</filter>
            </plugin>
        </extension>
    </component>
    

    That's it!

See also the tutorial on how to change the default document type used when importing files in the Nuxeo Platform.