Example usage for com.liferay.portal.kernel.zip ZipReader getEntryAsInputStream

List of usage examples for com.liferay.portal.kernel.zip ZipReader getEntryAsInputStream

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.zip ZipReader getEntryAsInputStream.

Prototype

public InputStream getEntryAsInputStream(String name);

Source Link

Usage

From source file:com.liferay.knowledgebase.admin.importer.KBArticleImporter.java

License:Open Source License

protected Map<String, String> getMetadata(ZipReader zipReader) throws KBArticleImportException {

    InputStream inputStream = null;

    try {//from   w w  w. j a v  a2 s  . c om
        inputStream = zipReader.getEntryAsInputStream(".METADATA");

        if (inputStream == null) {
            return Collections.emptyMap();
        }

        Properties properties = new Properties();

        properties.load(inputStream);

        Map<String, String> metadata = new HashMap<String, String>(properties.size());

        for (Object key : properties.keySet()) {
            Object value = properties.get(key);

            if (value != null) {
                metadata.put(key.toString(), value.toString());
            }
        }

        return metadata;
    } catch (IOException ioe) {
        throw new KBArticleImportException(ioe);
    } finally {
        StreamUtil.cleanUp(inputStream);
    }
}

From source file:com.liferay.knowledgebase.admin.importer.util.KBArticleImporterUtil.java

License:Open Source License

public static FileEntry addImageFileEntry(String imageFileName, long userId, KBArticle kbArticle,
        ZipReader zipReader, Map<String, FileEntry> fileEntriesMap) throws PortalException {

    try {//from   w w w.  j  a  va  2 s .  co  m
        validateImageFileExtension(imageFileName);
    } catch (KBArticleImportException kbaie) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unsupported image file suffix used in ZIP file " + imageFileName);
        }
    }

    try {
        return addImageFileEntry(userId, kbArticle, imageFileName, zipReader.getEntryAsInputStream(
                PortletPropsValues.MARKDOWN_IMPORTER_IMAGE_FOLDER + imageFileName), fileEntriesMap);
    } catch (Exception e) {
        StringBuilder sb = new StringBuilder(4);

        sb.append("Unable to import image file ");
        sb.append(imageFileName);
        sb.append(": ");
        sb.append(e.getLocalizedMessage());

        throw new KBArticleImportException(sb.toString());
    }
}

From source file:com.liferay.portlet.wiki.importers.mediawiki.MediaWikiImporter.java

License:Open Source License

protected void processImages(long userId, WikiNode node, InputStream imagesInputStream) throws Exception {

    if (imagesInputStream == null) {
        return;//from w w w. j a  va2s .c o  m
    }

    ProgressTracker progressTracker = ProgressTrackerThreadLocal.getProgressTracker();

    int count = 0;

    ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(imagesInputStream);

    List<String> entries = zipReader.getEntries();

    int total = entries.size();

    if (total > 0) {
        try {
            WikiPageLocalServiceUtil.getPage(node.getNodeId(), SHARED_IMAGES_TITLE);
        } catch (NoSuchPageException nspe) {
            ServiceContext serviceContext = new ServiceContext();

            serviceContext.setAddGroupPermissions(true);
            serviceContext.setAddGuestPermissions(true);

            WikiPageLocalServiceUtil.addPage(userId, node.getNodeId(), SHARED_IMAGES_TITLE,
                    SHARED_IMAGES_CONTENT, null, true, serviceContext);
        }
    }

    List<ObjectValuePair<String, InputStream>> inputStreamOVPs = new ArrayList<ObjectValuePair<String, InputStream>>();

    try {
        int percentage = 50;

        for (int i = 0; i < entries.size(); i++) {
            String entry = entries.get(i);

            String key = entry;

            InputStream inputStream = zipReader.getEntryAsInputStream(entry);

            String[] paths = StringUtil.split(key, CharPool.SLASH);

            if (!isValidImage(paths, inputStream)) {
                if (_log.isInfoEnabled()) {
                    _log.info("Ignoring " + key);
                }

                continue;
            }

            String fileName = StringUtil.toLowerCase(paths[paths.length - 1]);

            ObjectValuePair<String, InputStream> inputStreamOVP = new ObjectValuePair<String, InputStream>(
                    fileName, inputStream);

            inputStreamOVPs.add(inputStreamOVP);

            count++;

            if ((i % 5) == 0) {
                WikiPageLocalServiceUtil.addPageAttachments(userId, node.getNodeId(), SHARED_IMAGES_TITLE,
                        inputStreamOVPs);

                inputStreamOVPs.clear();

                percentage = Math.min(50 + (i * 50) / total, 99);

                progressTracker.setPercent(percentage);
            }
        }

        if (!inputStreamOVPs.isEmpty()) {
            WikiPageLocalServiceUtil.addPageAttachments(userId, node.getNodeId(), SHARED_IMAGES_TITLE,
                    inputStreamOVPs);
        }
    } finally {
        for (ObjectValuePair<String, InputStream> inputStreamOVP : inputStreamOVPs) {

            InputStream inputStream = inputStreamOVP.getValue();

            StreamUtil.cleanUp(inputStream);
        }
    }

    zipReader.close();

    if (_log.isInfoEnabled()) {
        _log.info("Imported " + count + " images into " + node.getName());
    }
}

From source file:com.liferay.wiki.importer.impl.mediawiki.MediaWikiImporter.java

License:Open Source License

protected void processImages(long userId, WikiNode node, InputStream imagesInputStream) throws Exception {

    if (imagesInputStream == null) {
        return;//w  w w. j a v a 2s. co  m
    }

    ProgressTracker progressTracker = ProgressTrackerThreadLocal.getProgressTracker();

    int count = 0;

    ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(imagesInputStream);

    List<String> entries = zipReader.getEntries();

    if (entries == null) {
        throw new ImportFilesException();
    }

    int total = entries.size();

    if (total > 0) {
        try {
            _wikiPageLocalService.getPage(node.getNodeId(), SHARED_IMAGES_TITLE);
        } catch (NoSuchPageException nspe) {
            ServiceContext serviceContext = new ServiceContext();

            serviceContext.setAddGroupPermissions(true);
            serviceContext.setAddGuestPermissions(true);

            _wikiPageLocalService.addPage(userId, node.getNodeId(), SHARED_IMAGES_TITLE, SHARED_IMAGES_CONTENT,
                    null, true, serviceContext);
        }
    }

    List<ObjectValuePair<String, InputStream>> inputStreamOVPs = new ArrayList<>();

    try {
        int percentage = 50;

        for (int i = 0; i < entries.size(); i++) {
            String entry = entries.get(i);

            String key = entry;

            InputStream inputStream = zipReader.getEntryAsInputStream(entry);

            String[] paths = StringUtil.split(key, CharPool.SLASH);

            if (!isValidImage(paths, inputStream)) {
                if (_log.isInfoEnabled()) {
                    _log.info("Ignoring " + key);
                }

                continue;
            }

            String fileName = StringUtil.toLowerCase(paths[paths.length - 1]);

            ObjectValuePair<String, InputStream> inputStreamOVP = new ObjectValuePair<>(fileName, inputStream);

            inputStreamOVPs.add(inputStreamOVP);

            count++;

            if ((i % 5) == 0) {
                _wikiPageLocalService.addPageAttachments(userId, node.getNodeId(), SHARED_IMAGES_TITLE,
                        inputStreamOVPs);

                inputStreamOVPs.clear();

                percentage = Math.min(50 + (i * 50) / total, 99);

                if (progressTracker != null) {
                    progressTracker.setPercent(percentage);
                }
            }
        }

        if (!inputStreamOVPs.isEmpty()) {
            _wikiPageLocalService.addPageAttachments(userId, node.getNodeId(), SHARED_IMAGES_TITLE,
                    inputStreamOVPs);
        }
    } finally {
        for (ObjectValuePair<String, InputStream> inputStreamOVP : inputStreamOVPs) {

            InputStream inputStream = inputStreamOVP.getValue();

            StreamUtil.cleanUp(inputStream);
        }
    }

    zipReader.close();

    if (_log.isInfoEnabled()) {
        _log.info("Imported " + count + " images into " + node.getName());
    }
}