public static Map<String, Serializable> getMapFromBlob(Blob blob) { Map<String, Serializable> map = new HashMap<String, Serializable>(); if (blob == null) { map.put(FILE_NAME, null); map.put(MIME_TYPE, null); map.put(ENCODING, null); map.put(LENGTH, null); map.put(DIGEST, null); } else { // cannot return uri for blob for now: no edit implemented map.put(FILE_NAME, blob.getFilename()); map.put(MIME_TYPE, blob.getMimeType()); map.put(ENCODING, blob.getEncoding()); map.put(LENGTH, blob.getLength()); map.put(DIGEST, blob.getDigest()); } return map; }
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); } }
@Override public File getFile() { return blob.getFile(); }
protected void checkBlob(Document doc, BlobAccessor accessor) { Blob blob = accessor.getBlob(); if (!(blob instanceof ManagedBlob)) { return; } // compare current provider with expected String expectedProviderId = getProviderId(doc, blob, accessor.getXPath()); if (((ManagedBlob) blob).getProviderId().equals(expectedProviderId)) { return; } // re-write blob // TODO add APIs so that blob providers can copy blobs efficiently from each other Blob newBlob; try (InputStream in = blob.getStream()) { newBlob = Blobs.createBlob(in, blob.getMimeType(), blob.getEncoding()); newBlob.setFilename(blob.getFilename()); newBlob.setDigest(blob.getDigest()); } catch (IOException e) { throw new RuntimeException(e); } accessor.setBlob(newBlob); }
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; }
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; } }
public BlobWrapper(Blob blob) { this.blob = blob; filename = blob.getFilename(); }
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 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()); } }
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); }
/** * @param childComment * @return */ protected JSONArray buildFilesInfos(DocumentModel childComment) { // Attached files List<Map<String, Object>> attachedFiles = (List<Map<String, Object>>) childComment.getPropertyValue("files:files"); JSONArray files = new JSONArray(); int index = 0; for (Map<String, Object> attachedFile : attachedFiles) { // File infos JSONObject fileInfos = new JSONObject(); Blob file = (Blob) attachedFile.get("file"); fileInfos.element("index", index); fileInfos.element("filename", file.getFilename()); fileInfos.element("mime-type", file.getMimeType()); fileInfos.element("length", file.getLength()); files.add(fileInfos); index++; } return files; }
@Override public String getXmlFileContent() throws IOException { try { Blob xml = safeGet(Blob.class, "file:content", null); if (xml.getEncoding() == null || "".equals(xml.getEncoding())) { xml.setEncoding("utf-8"); } return xml.getString(); } catch (IOException e) { log.error("Error while reading blob", e); return ""; } }
@Override public String getMimeType() { return blob.getMimeType(); }
@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; } }
/** * Gets a new blob on a temporary file which is a copy of the blob's. * * @since 7.4 */ protected Blob getTemporaryBlob(Blob blob) throws IOException { String ext = FilenameUtils.getExtension(blob.getFilename()); if (!VALID_EXT.matcher(ext).matches()) { ext = "tmp"; } Blob newBlob = new FileBlob('.' + ext); File tmp = newBlob.getFile(); File file = blob.getFile(); if (file == null) { try (InputStream in = blob.getStream()) { Files.copy(in, tmp.toPath(), StandardCopyOption.REPLACE_EXISTING); } } else { // do a copy Files.copy(file.toPath(), tmp.toPath(), StandardCopyOption.REPLACE_EXISTING); } return newBlob; }
/** * Check if blob is a pdf. */ protected void checkPdf(Blob blob) throws OperationException { if (!"application/pdf".equals(blob.getMimeType())) { throw new OperationException("Blob " + blob.getFilename() + " is not a PDF."); } } }
@Override public InputStream getStream() throws IOException { return blob.getStream(); }
public void createPictureimpl(String description, String tag, String title, Integer maxsize, String filename, Integer width, Integer height, Integer depth, Blob fileContent) throws IOException { if (fileContent.getFilename() == null) { fileContent.setFilename(filename); } if (maxsize == null) { maxsize = 0; } PictureConversion pictureConversion = new PictureConversion(title, description, tag, maxsize); pictureConversion.setChainId("Image.Blob.Resize"); PictureView view = getImagingService().computeViewFor(fileContent, pictureConversion, getImageInfo(), true); addPictureView(view); }
public static VideoInfo getVideoInfo(Blob video) { if (video == null || video.getLength() == 0) { return null; } try { ExecResult result; try (CloseableFile cf = video.getCloseableFile("." + FilenameUtils.getExtension(video.getFilename()))) { CommandLineExecutorService cles = Framework.getService(CommandLineExecutorService.class); CmdParameters params = cles.getDefaultCmdParameters(); params.addNamedParameter("inFilePath", cf.getFile().getAbsolutePath()); // read the duration with a first command to adjust the best rate: result = cles.execCommand(FFMPEG_INFO_COMMAND_LINE, params); } if (!result.isSuccessful()) { throw result.getError(); } return VideoInfo.fromFFmpegOutput(result.getOutput()); } catch (CommandNotAvailable | CommandException | IOException e) { throw new NuxeoException(e); } }