Example usage for com.liferay.portal.kernel.repository.model FileEntry getContentStream

List of usage examples for com.liferay.portal.kernel.repository.model FileEntry getContentStream

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.repository.model FileEntry getContentStream.

Prototype

public InputStream getContentStream(String version) throws PortalException;

Source Link

Usage

From source file:com.liferay.portlet.documentlibrary.action.CompareVersionsAction.java

License:Open Source License

protected void compareVersions(RenderRequest renderRequest) throws Exception {

    long fileEntryId = ParamUtil.getLong(renderRequest, "fileEntryId");

    String sourceVersion = ParamUtil.getString(renderRequest, "sourceVersion");
    String targetVersion = ParamUtil.getString(renderRequest, "targetVersion");

    FileEntry fileEntry = DLAppServiceUtil.getFileEntry(fileEntryId);

    String extension = fileEntry.getExtension();

    FileVersion sourceFileVersion = fileEntry.getFileVersion(sourceVersion);

    String sourceTitle = sourceFileVersion.getTitle();

    FileVersion targetFileVersion = fileEntry.getFileVersion(targetVersion);

    String targetTitle = targetFileVersion.getTitle();

    InputStream sourceIs = fileEntry.getContentStream(sourceVersion);
    InputStream targetIs = fileEntry.getContentStream(targetVersion);

    if (extension.equals("htm") || extension.equals("html") || extension.equals("xml")) {

        String escapedSource = HtmlUtil.escape(StringUtil.read(sourceIs));
        String escapedTarget = HtmlUtil.escape(StringUtil.read(targetIs));

        sourceIs = new UnsyncByteArrayInputStream(escapedSource.getBytes(StringPool.UTF8));
        targetIs = new UnsyncByteArrayInputStream(escapedTarget.getBytes(StringPool.UTF8));
    }/*from  w w  w .  jav a 2  s . c o  m*/

    if (DocumentConversionUtil.isEnabled() && DocumentConversionUtil.isConvertBeforeCompare(extension)) {

        String sourceTempFileId = DLUtil.getTempFileId(fileEntryId, sourceVersion);
        String targetTempFileId = DLUtil.getTempFileId(fileEntryId, targetVersion);

        sourceIs = new FileInputStream(
                DocumentConversionUtil.convert(sourceTempFileId, sourceIs, extension, "txt"));
        targetIs = new FileInputStream(
                DocumentConversionUtil.convert(targetTempFileId, targetIs, extension, "txt"));
    }

    List<DiffResult>[] diffResults = DiffUtil.diff(new InputStreamReader(sourceIs),
            new InputStreamReader(targetIs));

    renderRequest.setAttribute(WebKeys.SOURCE_NAME, sourceTitle + StringPool.SPACE + sourceVersion);
    renderRequest.setAttribute(WebKeys.TARGET_NAME, targetTitle + StringPool.SPACE + targetVersion);
    renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
}

From source file:com.liferay.portlet.documentlibrary.service.impl.DLSyncServiceImpl.java

License:Open Source License

public InputStream getFileDeltaAsStream(long fileEntryId, String sourceVersion, String destinationVersion)
        throws PortalException, SystemException {

    InputStream deltaInputStream = null;

    FileEntry fileEntry = dlAppLocalService.getFileEntry(fileEntryId);

    InputStream sourceInputStream = null;
    File sourceFile = FileUtil.createTempFile();
    FileInputStream sourceFileInputStream = null;
    FileChannel sourceFileChannel = null;
    File checksumsFile = FileUtil.createTempFile();
    OutputStream checksumsOutputStream = null;
    WritableByteChannel checksumsWritableByteChannel = null;

    try {/*from  ww  w  . j a  v  a2 s. c o  m*/
        sourceInputStream = fileEntry.getContentStream(sourceVersion);

        FileUtil.write(sourceFile, sourceInputStream);

        sourceFileInputStream = new FileInputStream(sourceFile);

        sourceFileChannel = sourceFileInputStream.getChannel();

        checksumsOutputStream = new FileOutputStream(checksumsFile);

        checksumsWritableByteChannel = Channels.newChannel(checksumsOutputStream);

        ByteChannelWriter checksumsByteChannelWriter = new ByteChannelWriter(checksumsWritableByteChannel);

        DeltaUtil.checksums(sourceFileChannel, checksumsByteChannelWriter);

        checksumsByteChannelWriter.finish();
    } catch (Exception e) {
        throw new PortalException(e);
    } finally {
        StreamUtil.cleanUp(sourceFileInputStream);
        StreamUtil.cleanUp(sourceFileChannel);
        StreamUtil.cleanUp(checksumsOutputStream);
        StreamUtil.cleanUp(checksumsWritableByteChannel);

        FileUtil.delete(sourceFile);
    }

    InputStream destinationInputStream = null;
    ReadableByteChannel destinationReadableByteChannel = null;
    InputStream checksumsInputStream = null;
    ReadableByteChannel checksumsReadableByteChannel = null;
    OutputStream deltaOutputStream = null;
    WritableByteChannel deltaOutputStreamWritableByteChannel = null;

    try {
        destinationInputStream = fileEntry.getContentStream(destinationVersion);

        destinationReadableByteChannel = Channels.newChannel(destinationInputStream);

        checksumsInputStream = new FileInputStream(checksumsFile);

        checksumsReadableByteChannel = Channels.newChannel(checksumsInputStream);

        ByteChannelReader checksumsByteChannelReader = new ByteChannelReader(checksumsReadableByteChannel);

        File deltaFile = FileUtil.createTempFile();

        deltaOutputStream = new FileOutputStream(deltaFile);

        deltaOutputStreamWritableByteChannel = Channels.newChannel(deltaOutputStream);

        ByteChannelWriter deltaByteChannelWriter = new ByteChannelWriter(deltaOutputStreamWritableByteChannel);

        DeltaUtil.delta(destinationReadableByteChannel, checksumsByteChannelReader, deltaByteChannelWriter);

        deltaByteChannelWriter.finish();

        deltaInputStream = new FileInputStream(deltaFile);
    } catch (Exception e) {
        throw new PortalException(e);
    } finally {
        StreamUtil.cleanUp(destinationInputStream);
        StreamUtil.cleanUp(destinationReadableByteChannel);
        StreamUtil.cleanUp(checksumsInputStream);
        StreamUtil.cleanUp(checksumsReadableByteChannel);
        StreamUtil.cleanUp(deltaOutputStream);
        StreamUtil.cleanUp(deltaOutputStreamWritableByteChannel);

        FileUtil.delete(checksumsFile);
    }

    return deltaInputStream;
}