Search in sources :

Example 1 with BlobProvider

use of org.nuxeo.ecm.core.blob.BlobProvider in project nuxeo-drive-server by nuxeo.

the class DefaultFileSystemItemFactory method isFileSystemItem.

/**
 * The default factory considers that a {@link DocumentModel} is adaptable as a {@link FileSystemItem} if:
 * <ul>
 * <li>It is not a version</li>
 * <li>AND it is not HiddenInNavigation</li>
 * <li>AND it is not in the trash, unless {@code includeDeleted} is true</li>
 * <li>AND it is Folderish or it can be adapted as a {@link BlobHolder} with a blob</li>
 * <li>AND its blob is not backed by an extended blob provider</li>
 * <li>AND it is not a synchronization root registered for the current user, unless {@code relaxSyncRootConstraint}
 * is true</li>
 * </ul>
 */
@Override
public boolean isFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) {
    // Check version
    if (doc.isVersion()) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Document %s is a version, it cannot be adapted as a FileSystemItem.", doc.getId()));
        }
        return false;
    }
    // Check Collections
    if (CollectionConstants.COLLECTIONS_TYPE.equals(doc.getType())) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Document %s is the collection root folder (type=%s, path=%s), it cannot be adapted as a FileSystemItem.", doc.getId(), CollectionConstants.COLLECTIONS_TYPE, doc.getPathAsString()));
        }
        return false;
    }
    // Check HiddenInNavigation
    if (doc.hasFacet("HiddenInNavigation")) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Document %s is HiddenInNavigation, it cannot be adapted as a FileSystemItem.", doc.getId()));
        }
        return false;
    }
    // Check if document is in the trash
    if (!includeDeleted && doc.isTrashed()) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Document %s is trashed, it cannot be adapted as a FileSystemItem.", doc.getId()));
        }
        return false;
    }
    // Try to fetch blob
    Blob blob = null;
    try {
        blob = getBlob(doc);
    } catch (NuxeoException e) {
        log.error(String.format("Error while fetching blob for document %s, it cannot be adapted as a FileSystemItem.", doc.getId()), e);
        return false;
    }
    // Check Folderish or BlobHolder with a blob
    if (!doc.isFolder() && blob == null) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Document %s is not Folderish nor a BlobHolder with a blob, it cannot be adapted as a FileSystemItem.", doc.getId()));
        }
        return false;
    }
    // Check for blobs backed by extended blob providers (ex: Google Drive)
    if (!doc.isFolder()) {
        BlobManager blobManager = Framework.getService(BlobManager.class);
        BlobProvider blobProvider = blobManager.getBlobProvider(blob);
        if (blobProvider != null && (!blobProvider.supportsUserUpdate() || blobProvider.getBinaryManager() == null)) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Blob for Document %s is backed by a BlobProvider preventing updates, it cannot be adapted as a FileSystemItem.", doc.getId()));
            }
            return false;
        }
    }
    if (!relaxSyncRootConstraint && doc.isFolder()) {
        // Check not a synchronization root registered for the current user
        NuxeoDriveManager nuxeoDriveManager = Framework.getService(NuxeoDriveManager.class);
        Principal principal = doc.getCoreSession().getPrincipal();
        boolean isSyncRoot = nuxeoDriveManager.isSynchronizationRoot(principal, doc);
        if (isSyncRoot) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Document %s is a registered synchronization root for user %s, it cannot be adapted as a DefaultFileSystemItem.", doc.getId(), principal.getName()));
            }
            return false;
        }
    }
    return true;
}
Also used : Blob(org.nuxeo.ecm.core.api.Blob) BlobManager(org.nuxeo.ecm.core.blob.BlobManager) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) NuxeoDriveManager(org.nuxeo.drive.service.NuxeoDriveManager) Principal(java.security.Principal) BlobProvider(org.nuxeo.ecm.core.blob.BlobProvider)

Aggregations

Principal (java.security.Principal)1 NuxeoDriveManager (org.nuxeo.drive.service.NuxeoDriveManager)1 Blob (org.nuxeo.ecm.core.api.Blob)1 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)1 BlobManager (org.nuxeo.ecm.core.blob.BlobManager)1 BlobProvider (org.nuxeo.ecm.core.blob.BlobProvider)1