@Override public void setMimeType(String mimeType) { blob.setMimeType(mimeType); }
protected void setMemberValue(Blob blob, String name, Object value) throws PropertyNotFoundException { if (NAME.equals(name)) { blob.setFilename((String) value); } else if (MIME_TYPE.equals(name)) { blob.setMimeType((String) value); } else if (ENCODING.equals(name)) { blob.setEncoding((String) value); } else if (DIGEST.equals(name)) { blob.setDigest((String) value); } else { throw new PropertyNotFoundException(name); } }
public static Blob getBlobFromMap(Map<String, Object> mapValue) { if (mapValue == null) { return null; } String uri = (String) mapValue.get(URI); if (uri == null || "".equals(uri)) { return null; } String filename = (String) mapValue.get(FILE_NAME); String mimeType = (String) mapValue.get(MIME_TYPE); String encoding = (String) mapValue.get(ENCODING); String digest = (String) mapValue.get(DIGEST); try { BlobHolderAdapterService service = Framework.getService(BlobHolderAdapterService.class); if (service == null) { throw new NuxeoException("BlobHolderAdapterService not found"); } Blob blob = service.getExternalBlobForUri(uri); if (filename != null) { blob.setFilename(filename); } blob.setMimeType(mimeType); blob.setEncoding(encoding); // TODO maybe check if digest is still a match to the retrieved blob blob.setDigest(digest); return blob; } catch (IOException e) { throw new NuxeoException(e); } }
protected void adjustBlobName(String filename, Blob blob, String mimeType) { if (StringUtils.isBlank(filename)) { filename = "file_" + System.currentTimeMillis(); } else { filename = FilenameUtils.removeExtension(FilenameUtils.getName(filename)); } String extension = Framework.getService(MimetypeRegistry.class) .getExtensionsFromMimetypeName(mimeType) .stream() .findFirst() .orElse("bin"); blob.setFilename(filename + "." + extension); blob.setMimeType(mimeType); }
protected List<Blob> loadBlobs(List<Map<String, String>> blobInfos) { log.debug("Loading blobs from the file system: " + blobInfos); List<Blob> blobs = new ArrayList<>(); for (Map<String, String> info : blobInfos) { File blobFile = new File(cacheDir, info.get("file")); Blob blob = new FileBlob(blobFile); blob.setEncoding(info.get("encoding")); blob.setMimeType(info.get("mimetype")); blob.setFilename(info.get("filename")); blob.setDigest(info.get("digest")); blobs.add(blob); } return blobs; }
protected void updateResultBlobMimeType(BlobHolder resultBh, ConverterDescriptor desc) { Blob mainBlob = resultBh.getBlob(); if (mainBlob == null) { return; } String mimeType = mainBlob.getMimeType(); if (StringUtils.isBlank(mimeType) || mimeType.equals("application/octet-stream")) { mainBlob.setMimeType(desc.getDestinationMimeType()); } }
@Override public List<PictureView> computeViewsFor(Blob blob, List<PictureConversion> pictureConversions, ImageInfo imageInfo, boolean convert) throws IOException { String mimeType = blob.getMimeType(); if (mimeType == null) { blob.setMimeType(getImageMimeType(blob)); } if (imageInfo == null) { imageInfo = getImageInfo(blob); } List<PictureView> views = new ArrayList<>(); for (PictureConversion pictureConversion : pictureConversions) { views.add(computeView(blob, pictureConversion, imageInfo, convert)); } return views; }
/** * Gets a temporary blob for the given temporary path. * <p> * The temporary blob is backed by a temporary file in a new location. The old file is removed. * * @param path the path to a temporary file * @param mimeType the blob MIME type * @return a temporary {@link Blob} */ public static Blob getTemporaryBlob(String path, String mimeType) { String ext = "." + FilenameUtils.getExtension(path); Blob blob; try { blob = new FileBlob(ext); // automatically tracked for removal Files.move(Paths.get(path), blob.getFile().toPath(), REPLACE_EXISTING); } catch (IOException e) { throw new NuxeoException(e); } blob.setMimeType(mimeType); blob.setFilename(FilenameUtils.getName(path)); return blob; }
@PUT public Response put() { if (backend.isLocked(doc.getRef()) && !backend.canUnlock(doc.getRef())) { return Response.status(423).build(); } try { Blob content = Blobs.createBlob(request.getInputStream()); String contentType = request.getContentType(); if (contentType == null) { contentType = "application/octet-stream"; } content.setMimeType(contentType); content.setFilename(name); backend.updateDocument(doc, name, content); try { return Response.created(new URI(URLEncoder.encode(path, "UTF8"))).build(); } catch (URISyntaxException e) { throw new NuxeoException(e); } } catch (IOException e) { log.error("Error during PUT method execution", e); return Response.status(409).build(); } }
public Blob getBlob(String path) throws IOException, MimetypeNotFoundException, MimetypeDetectionException, ConversionException { String filePath = key + path; File file = new File(filePath); Blob blob = Blobs.createBlob(file); String mimeType = getMimeTypeService().getMimetypeFromBlob(blob); blob.setMimeType(mimeType); blob.setFilename(path); return blob; }
protected Blob saveInTempFile(PDDocument PdfDoc) throws IOException, COSVisitorException { Blob blob = Blobs.createBlobWithExtension(".pdf"); // creates a tracked temporary file blob.setMimeType(MIME_TYPE); PdfDoc.save(blob.getFile()); return blob; }
@Override public PictureView computeViewFor(Blob blob, PictureConversion pictureConversion, ImageInfo imageInfo, boolean convert) throws IOException { String mimeType = blob.getMimeType(); if (mimeType == null) { blob.setMimeType(getImageMimeType(blob)); } if (imageInfo == null) { imageInfo = getImageInfo(blob); } return computeView(blob, pictureConversion, imageInfo, convert); }
protected void setBlobAttributes(Blob result, TemplateBasedDocument templateBasedDocument) { // try to guess mimetype and extension of the resulting Blob MimetypeRegistry mreg = Framework.getService(MimetypeRegistry.class); String mimetype = "text/html"; String extension = ".html"; if (mreg != null) { String found_mimetype = guessMimeType(result, mreg); if (found_mimetype != null) { mimetype = found_mimetype; List<String> extensions = mreg.getExtensionsFromMimetypeName(mimetype); if (extensions != null && extensions.size() > 0) { extension = "." + extensions.get(0); } } } if ("text/xml".equalsIgnoreCase(mimetype)) { // because MimetypeRegistry return a stupid result for XML extension = ".xml"; } result.setMimeType(mimetype); String targetFileName = FileUtils.getFileNameNoExt(templateBasedDocument.getAdaptedDoc().getTitle()); result.setFilename(targetFileName + extension); }
public static FileBlob saveInTempFile(PDDocument inPdfDoc, String inFileName) throws IOException, COSVisitorException { Blob result = Blobs.createBlobWithExtension(".pdf"); File resultFile = result.getFile(); inPdfDoc.save(result.getFile()); result.setMimeType("application/pdf"); if (StringUtils.isNotBlank(inFileName)) { result.setFilename(inFileName); } FileBlob fb = new FileBlob(resultFile); fb.setMimeType("application/pdf"); return fb; }
@Override public String exportWorklistAsZip(List<DocumentModel> documents, boolean exportAllBlobs) { Blob blob = null; try { DownloadService downloadService = Framework.getService(DownloadService.class); DocumentListZipExporter zipExporter = new DocumentListZipExporter(); blob = zipExporter.exportWorklistAsZip(documents, documentManager, exportAllBlobs); if (blob == null) { // empty zip file, do nothing facesMessages.add(StatusMessage.Severity.INFO, messages.get("label.clipboard.emptyDocuments")); return null; } blob.setMimeType("application/zip"); blob.setFilename("clipboard.zip"); String key = downloadService.storeBlobs(Collections.singletonList(blob)); String url = BaseURL.getBaseURL() + downloadService.getDownloadUrl(key); ExternalContext context = FacesContext.getCurrentInstance().getExternalContext(); context.redirect(url); return ""; } catch (IOException io) { if (blob != null) { blob.getFile().delete(); } throw new NuxeoException("Error while redirecting for clipboard content", io); } } }
protected String findConverter(Blob blob, String destMimeType) { MimetypeRegistry mtr = Framework.getService(MimetypeRegistry.class); String srcMt = mtr.getMimetypeFromFilenameAndBlobWithDefault(blob.getFilename(), blob, blob.getMimeType()); blob.setMimeType(srcMt); ConversionService cs = Framework.getService(ConversionService.class); return cs.getConverterName(srcMt, destMimeType); }
protected Blob getBlob(FileItem item) { try { Blob blob; if (item.isInMemory()) { blob = Blobs.createBlob(item.get()); } else { File file; if (item instanceof DiskFileItem // && (file = ((DiskFileItem) item).getStoreLocation()) != null) { // move the file to a temporary blob we own blob = Blobs.createBlobWithExtension(null); Files.move(file.toPath(), blob.getFile().toPath(), REPLACE_EXISTING); } else { // if we couldn't get to the file, use the InputStream try (InputStream in = item.getInputStream()) { blob = Blobs.createBlob(in); } } } blob.setMimeType(defaultIfEmpty(item.getContentType(), "application/octet-stream")); blob.setFilename(item.getName()); return blob; } catch (IOException e) { throw new NuxeoException("Failed to get blob data", e); } }
protected Blob applyConverter(Blob blob, String converter, String destMimeType, Map<String, Serializable> params) { ConversionService cs = Framework.getService(ConversionService.class); BlobHolder bh = cs.convert(converter, new SimpleBlobHolder(blob), params); if (bh == null || bh.getBlob() == null) { return blob; } else { Blob result = bh.getBlob(); MimetypeRegistry mtr = Framework.getService(MimetypeRegistry.class); String filename = FileUtils.getFileNameNoExt(blob.getFilename()); filename = filename + "." + mtr.getExtensionsFromMimetypeName(destMimeType).get(0); result.setFilename(filename); if (result.getMimeType() == null) { result.setMimeType(destMimeType); } return result; } }
@Override public DocumentModel moveItem(DocumentModel source, DocumentRef targetParentRef, String name) { cleanTrashPath(targetParentRef, name); source.setPropertyValue("dc:title", name); BlobHolder blobHolder = source.getAdapter(BlobHolder.class); if (blobHolder != null) { Blob blob = blobHolder.getBlob(); if (blob != null) { blob.setFilename(name); // as the name may have changed, reset the mime type so that the correct one will be computed blob.setMimeType(null); blobHolder.setBlob(blob); } } source = getSession().saveDocument(source); DocumentModel model = getSession().move(source.getRef(), targetParentRef, name); return model; }
@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); } } }