以编程方式在Magnolia中创建资产

时间:2016-01-13 14:42:26

标签: spring file-upload magnolia

我们正在开发一个Magnolia Blossom / Spring应用程序,其中最终用户需要在某个时刻上传图像。所以我们正在考虑将这些图像存储为Magnolia中的资产。

但是,在Spring控制器级别上传的MultipartFile准备就绪后,我还没有找到将上传的图像存储到Magnolia资产的成功方法。

我认为应该有一些更简单的方法以编程方式(即Java代码)在Magnolia中创建资源,例如,通过某些方式,您只有PNG / JPEG的字节。

以前是否有人这样做过?

1 个答案:

答案 0 :(得分:2)

Magnolia forum的帮助下,我得到了什么:

private void saveImageAsEventAsset(final MultipartFile multipartFile, final ImageInfo imageInfo, final String prefix) {
        MgnlContext.doInSystemContext(
                new MgnlContext.VoidOp() {
                    @Override
                    public void doExec() {
                        try {
                            String fileExtension = FilenameUtils.getExtension(multipartFile.getOriginalFilename());
                            String newFileName = prefix + "_" + Calendar.getInstance().getTimeInMillis() + "." + fileExtension;

                            // "Navigate" to the assets folder node
                            AssetProviderRegistry assetProviderRegistry = Components.getComponent(AssetProviderRegistry.class);
                            JcrAssetProvider jcrAssetProvider = (JcrAssetProvider) assetProviderRegistry.getProviderById(DamConstants.DEFAULT_JCR_PROVIDER_ID);
                            JcrFolder assetFolder = (JcrFolder) jcrAssetProvider.getFolder("/event");
                            Node assetFolderNode = assetFolder.getNode();

                            // Create asset node
                            Node assetNode = JcrUtils.getOrAddNode(assetFolderNode, newFileName, AssetNodeTypes.Asset.NAME);
                            assetNode.setProperty(AssetNodeTypes.Asset.ASSET_NAME, newFileName);

                            Session session = MgnlContext.getJCRSession(DamConstants.WORKSPACE);

                            // Create asset resource node
                            Node assetResourceNode = JcrUtils.getOrAddNode(assetNode, AssetNodeTypes.AssetResource.RESOURCE_NAME, AssetNodeTypes.AssetResource.NAME);
                            assetResourceNode.setProperty(AssetNodeTypes.AssetResource.DATA, session.getValueFactory().createBinary(multipartFile.getInputStream()));
                            assetResourceNode.setProperty(AssetNodeTypes.AssetResource.FILENAME, newFileName);
                            assetResourceNode.setProperty(AssetNodeTypes.AssetResource.EXTENSION, fileExtension);
                            assetResourceNode.setProperty(AssetNodeTypes.AssetResource.SIZE, Long.toString(multipartFile.getSize()));
                            assetResourceNode.setProperty(AssetNodeTypes.AssetResource.MIMETYPE, imageInfo.getMimeType());
                            assetResourceNode.setProperty(AssetNodeTypes.AssetResource.WIDTH, Long.toString(imageInfo.getWidth()));
                            assetResourceNode.setProperty(AssetNodeTypes.AssetResource.HEIGHT, Long.toString(imageInfo.getHeight()));

                            session.save();
                        } catch (Exception e) {
                            logger.debug("Could not save image as event asset", e);
                        }
                    }
                }
        );
    }

其中org.devlib.schmidt.imageinfo。 ImageInfo 参数来自先前的验证,即上传的 MultipartFile 是图像,前缀字符串只是我们需要的东西在资产名称中。