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();
}
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);
}
}
}
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;
}
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);
}
}
Aggregations