@Override public Blob getBlob(String uri) throws PropertyException, IOException { String localPath = getLocalName(uri); String path = getFileAbsolutePath(localPath); File file = new File(path); if (!file.exists()) { throw new PropertyException(String.format("Cannot find file at '%s'", path)); } Blob blob = Blobs.createBlob(file); blob.setFilename(file.getName()); return blob; } }
/** * Zip a list of blob. * * @param blobs the blob list * @param fileName if no filename is given, the first blob's filename will be used * @return a zip containing the list of blob * @throws IOException */ public static Blob zip(List<Blob> blobs, String fileName) throws IOException { if (fileName == null || (fileName = fileName.trim()).length() == 0) { fileName = blobs.isEmpty() ? null : blobs.get(0).getFilename(); } File file = Framework.createTempFile("nxops-createzip-", ".tmp"); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file)); Framework.trackFile(file, file); try { zip(blobs, out); } finally { out.finish(); out.close(); } return Blobs.createBlob(file, "application/zip", null, fileName); }
@Override public Blob getBlob() { String string = (String) doc.getProperty(xPath).getValue(); if (string == null) { return null; } Blob blob = Blobs.createBlob(string, mt); String ext = ".txt"; if ("text/html".equals(mt)) { ext = ".html"; } else if ("text/xml".equals(mt)) { ext = ".xml"; } else if ("text/x-web-markdown".equals(mt)) { ext = ".md"; } String title = doc.getTitle(); if (!title.endsWith(ext)) { title = title.concat(ext); } blob.setFilename(title); return blob; }
/** * Zip the given blob. * * @param blob the blob * @param filename if no filename is given, the blob's filename will be used * @return a zip containing the blob * @throws IOException */ public static Blob zip(Blob blob, String filename) throws IOException { if (filename == null || (filename = filename.trim()).length() == 0) { filename = blob.getFilename(); } File file = Framework.createTempFile("nxops-createzip-", ".tmp"); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file)); Framework.trackFile(file, file); try { zip(blob, out); } finally { out.finish(); out.close(); } return Blobs.createBlob(file, "application/zip", null, filename); }
@Override public DocumentModel createFile(String parentPath, String name) { Blob blob = Blobs.createBlob("", "application/octet-stream"); return createFile(parentPath, name, blob); }
public Object getValue(DocumentModel doc) { try { Blob blob = (Blob) doc.getPropertyValue("file:content"); if (blob != null) { return blob; } } catch (PropertyNotFoundException e) { // ignore } return Blobs.createBlob(""); } };
/** * @deprecated Use addBinaryFileFromPlugin(Blob, String, String) to avoid loading the data in memory as a Bytes * array */ @Deprecated public String addBinaryFileFromPlugin(byte[] content, String mimetype, String fullName, String morePath) { Blob blob = Blobs.createBlob(content); return addBinaryFileFromPlugin(blob, fullName, morePath); }
@Override protected BlobHolder buildResult(List<String> cmdOutput, CmdParameters cmdParams) throws ConversionException { StringBuilder sb = new StringBuilder(); if (cmdOutput != null) { for (String out : cmdOutput) { if (out != null && out.trim().length() > 0) { sb.append(out.trim()); sb.append('\n'); } } } return new SimpleCachableBlobHolder(Blobs.createBlob(sb.toString())); }
/** * Writes a summary file and puts it in the archive. */ private void addSummaryToZip(ZipOutputStream out, byte[] data, StringBuilder sb) throws IOException { Blob content = Blobs.createBlob(sb.toString()); BufferedInputStream buffi = new BufferedInputStream(content.getStream(), BUFFER); ZipEntry entry = new ZipEntry(SUMMARY_FILENAME); out.putNextEntry(entry); int count = buffi.read(data, 0, BUFFER); while (count != -1) { out.write(data, 0, count); count = buffi.read(data, 0, BUFFER); } out.closeEntry(); buffi.close(); }
@Override public void load(String path) throws IOException { blobs = new ArrayList<>(); File base = new File(path); if (base.isDirectory()) { addDirectoryToList(base, ""); } else { File file = new File(path); Blob mainBlob = Blobs.createBlob(file); mainBlob.setFilename(file.getName()); blobs.add(mainBlob); } orderIndexPageFirst(blobs); }
@Override public String getImageMimeType(File file) { try { MimetypeRegistry mimetypeRegistry = Framework.getService(MimetypeRegistry.class); if (file.getName() != null) { return mimetypeRegistry.getMimetypeFromFilenameAndBlobWithDefault(file.getName(), Blobs.createBlob(file), "image/jpeg"); } else { return mimetypeRegistry.getMimetypeFromFile(file); } } catch (MimetypeNotFoundException | MimetypeDetectionException | IOException e) { log.error("Unable to retrieve mime type", e); } return null; }
private Blob createBlob(ZipEntry entry) throws IOException { if (zipFile != null) { return new ZipEntryBlob(zipFile, entry); } else { // should decompress since this is a generic stream // FileBlob always closes the stream, but we don't want that // so wrap it in a CloseShieldInputStream try (CloseShieldInputStream ncin = new CloseShieldInputStream(in)) { Blob blob = Blobs.createBlob(ncin); filesToDelete.add(blob.getFile()); return blob; } } }
@OperationMethod(collector = BlobCollector.class) public Blob run(DocumentModel doc) throws OperationException, RenderingException, TemplateException, IOException { String content = RenderingService.getInstance().render(type, template, ctx); return Blobs.createBlob(content, mimeType, null, name); }
@OperationMethod public Blob run(DocumentModelList docs) throws OperationException, RenderingException, TemplateException, IOException { String content = RenderingService.getInstance().render(type, template, ctx); return Blobs.createBlob(content, mimeType, charset, name); }
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 appendPDFs(PDFMergerUtility ut) throws IOException, COSVisitorException { File tempFile = Framework.createTempFile(filename, ".pdf"); ut.setDestinationFileName(tempFile.getAbsolutePath()); ut.mergeDocuments(); Blob fb = Blobs.createBlob(tempFile); Framework.trackFile(tempFile, fb); fb.setFilename(filename); return fb; }
public static Blob readBlob(HttpServletRequest request, BodyPart part) throws MessagingException, IOException { String ctype = part.getContentType(); String fname = part.getFileName(); InputStream pin = part.getInputStream(); final File tmp = Framework.createTempFile("nx-automation-upload-", ".tmp"); FileUtils.copyInputStreamToFile(pin, tmp); Blob blob = Blobs.createBlob(tmp, ctype, null, fname); RequestContext.getActiveContext(request).addRequestCleanupHandler(req -> tmp.delete()); return blob; }
@OperationMethod public Blob run(Blob blob) { SimpleBlobHolder bh = new SimpleBlobHolder(blob); Map<String, Serializable> parameters = new HashMap<String, Serializable>(); parameters.put(WIDTH, maxWidth); parameters.put(HEIGHT, maxHeigh); if (!service.isConverterAvailable(PICTURE_RESIZE_CONVERTER).isAvailable()) { return blob; } BlobHolder result = service.convert(PICTURE_RESIZE_CONVERTER, bh, parameters); if (result != null) { return result.getBlob(); } else { return Blobs.createBlob("Converter did not return any result"); } }
@OperationMethod(collector = BlobCollector.class) public Blob run(DocumentModel doc) { Blob blob = (Blob) doc.getPropertyValue(xpath); if (blob == null) { BlobHolder bh = doc.getAdapter(BlobHolder.class); if (bh != null) { blob = bh.getBlob(); } } // cannot return null since it may break the next operation if (blob == null) { // create an empty blob blob = Blobs.createBlob(""); blob.setFilename(doc.getName() + ".null"); } return blob; }
@Override public void populate(CoreSession session) { super.populate(session); DocumentModel doc = session.createDocumentModel("/", "testBlob", "File"); doc.setPropertyValue("file:content", (Serializable) Blobs.createBlob("one")); session.createDocument(doc); File docFile = FileUtils.getResourceFileFromContext("hello.doc"); DocumentModel doc2 = session.createDocumentModel("/", "testBlob2", "File"); doc2.setPropertyValue("file:content", new FileBlob(docFile)); session.createDocument(doc2); TransactionHelper.commitOrRollbackTransaction(); TransactionHelper.startTransaction(); } }