Search in sources :

Example 11 with BlobHolder

use of org.nuxeo.ecm.core.api.blobholder.BlobHolder in project nuxeo-filesystem-connectors by nuxeo.

the class FileResource method get.

@GET
public Object get() {
    BlobHolder bh = doc.getAdapter(BlobHolder.class);
    Blob blob = bh == null ? null : bh.getBlob();
    if (blob == null) {
        return Response.ok("").build();
    }
    String mimeType = blob.getMimeType();
    if (mimeType.equals("???")) {
        mimeType = "application/octet-stream";
    }
    // provide full DocumentBlobHolder context if possible, to give more info when calling the DownloadService
    Object entity = bh instanceof DocumentBlobHolder ? bh : blob;
    return Response.ok(entity).type(mimeType).build();
}
Also used : Blob(org.nuxeo.ecm.core.api.Blob) BlobHolder(org.nuxeo.ecm.core.api.blobholder.BlobHolder) DocumentBlobHolder(org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder) DocumentBlobHolder(org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder) GET(javax.ws.rs.GET)

Example 12 with BlobHolder

use of org.nuxeo.ecm.core.api.blobholder.BlobHolder in project nuxeo-filesystem-connectors by nuxeo.

the class SimpleBackend method renameItem.

@Override
public void renameItem(DocumentModel source, String destinationName) {
    source.putContextData(SOURCE_EDIT_KEYWORD, "webdav");
    if (source.isFolder()) {
        source.setPropertyValue("dc:title", destinationName);
        moveItem(source, source.getParentRef(), destinationName);
        source.putContextData("renameSource", "webdav");
        getSession().saveDocument(source);
    } else {
        source.setPropertyValue("dc:title", destinationName);
        BlobHolder bh = source.getAdapter(BlobHolder.class);
        boolean blobUpdated = false;
        if (bh != null) {
            Blob blob = bh.getBlob();
            if (blob != null) {
                blob.setFilename(destinationName);
                // as the name may have changed, reset the mime type so that the correct one will be computed
                blob.setMimeType(null);
                blobUpdated = true;
                bh.setBlob(blob);
                getSession().saveDocument(source);
            }
        }
        if (!blobUpdated) {
            source.setPropertyValue("dc:title", destinationName);
            moveItem(source, source.getParentRef(), destinationName);
            getSession().saveDocument(source);
        }
    }
}
Also used : Blob(org.nuxeo.ecm.core.api.Blob) BlobHolder(org.nuxeo.ecm.core.api.blobholder.BlobHolder)

Example 13 with BlobHolder

use of org.nuxeo.ecm.core.api.blobholder.BlobHolder in project nuxeo-filesystem-connectors by nuxeo.

the class SimpleBackend method resolveLocation.

@Override
public DocumentModel resolveLocation(String location) {
    Path resolvedLocation = parseLocation(location);
    DocumentModel doc = null;
    doc = getPathCache().get(resolvedLocation.toString());
    if (doc != null) {
        return doc;
    }
    DocumentRef docRef = new PathRef(resolvedLocation.toString());
    if (exists(docRef)) {
        doc = getSession().getDocument(docRef);
    } else {
        String encodedPath = urlEncode(resolvedLocation.toString());
        if (!resolvedLocation.toString().equals(encodedPath)) {
            DocumentRef encodedPathRef = new PathRef(encodedPath);
            if (exists(encodedPathRef)) {
                doc = getSession().getDocument(encodedPathRef);
            }
        }
        if (doc == null) {
            String filename = resolvedLocation.lastSegment();
            Path parentLocation = resolvedLocation.removeLastSegments(1);
            // first try with spaces (for create New Folder)
            String folderName = filename;
            DocumentRef folderRef = new PathRef(parentLocation.append(folderName).toString());
            if (exists(folderRef)) {
                doc = getSession().getDocument(folderRef);
            }
            // look for a child
            DocumentModel parentDocument = resolveParent(parentLocation.toString());
            if (parentDocument == null) {
                // parent doesn't exist, no use looking for a child
                return null;
            }
            List<DocumentModel> children = getChildren(parentDocument.getRef());
            for (DocumentModel child : children) {
                BlobHolder bh = child.getAdapter(BlobHolder.class);
                if (bh != null) {
                    Blob blob = bh.getBlob();
                    if (blob != null) {
                        try {
                            String blobFilename = blob.getFilename();
                            if (filename.equals(blobFilename)) {
                                doc = child;
                                break;
                            } else if (urlEncode(filename).equals(blobFilename)) {
                                doc = child;
                                break;
                            } else if (URLEncoder.encode(filename, "UTF-8").equals(blobFilename)) {
                                doc = child;
                                break;
                            } else if (encode(blobFilename.getBytes(), "ISO-8859-1").equals(filename)) {
                                doc = child;
                                break;
                            }
                        } catch (UnsupportedEncodingException e) {
                            // cannot happen for UTF-8
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }
    }
    getPathCache().put(resolvedLocation.toString(), doc);
    return doc;
}
Also used : Path(org.nuxeo.common.utils.Path) Blob(org.nuxeo.ecm.core.api.Blob) DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) PathRef(org.nuxeo.ecm.core.api.PathRef) BlobHolder(org.nuxeo.ecm.core.api.blobholder.BlobHolder) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 14 with BlobHolder

use of org.nuxeo.ecm.core.api.blobholder.BlobHolder in project nuxeo-filesystem-connectors by nuxeo.

the class SimpleBackend method createFile.

@Override
public DocumentModel createFile(String parentPath, String name, Blob content) {
    DocumentModel parent = resolveLocation(parentPath);
    if (!parent.isFolder()) {
        throw new NuxeoException("Can not create a child in a non folderish node");
    }
    try {
        cleanTrashPath(parent, name);
        DocumentModel file;
        if (Framework.isBooleanPropertyTrue(ALWAYS_CREATE_FILE_PROP)) {
            // compat for older versions, always create a File
            file = getSession().createDocumentModel(parent.getPathAsString(), name, "File");
            file.setPropertyValue("dc:title", name);
            if (content != null) {
                BlobHolder bh = file.getAdapter(BlobHolder.class);
                if (bh != null) {
                    bh.setBlob(content);
                }
            }
            file = getSession().createDocument(file);
        } else {
            // use the FileManager to create the file
            FileManager fileManager = Framework.getService(FileManager.class);
            file = fileManager.createDocumentFromBlob(getSession(), content, parent.getPathAsString(), false, name);
        }
        getPathCache().put(parseLocation(parentPath) + "/" + name, file);
        return file;
    } catch (IOException e) {
        throw new NuxeoException("Error child creating new folder", e);
    }
}
Also used : BlobHolder(org.nuxeo.ecm.core.api.blobholder.BlobHolder) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) IOException(java.io.IOException) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) FileManager(org.nuxeo.ecm.platform.filemanager.api.FileManager)

Aggregations

BlobHolder (org.nuxeo.ecm.core.api.blobholder.BlobHolder)14 Blob (org.nuxeo.ecm.core.api.Blob)10 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)8 CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)3 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)3 Test (org.junit.Test)2 FileItem (org.nuxeo.drive.adapter.FileItem)2 PathRef (org.nuxeo.ecm.core.api.PathRef)2 StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 Principal (java.security.Principal)1 Date (java.util.Date)1 ServletRequest (javax.servlet.ServletRequest)1 GET (javax.ws.rs.GET)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpPut (org.apache.http.client.methods.HttpPut)1