Example usage for com.liferay.portal.kernel.util FileUtil createTempFile

List of usage examples for com.liferay.portal.kernel.util FileUtil createTempFile

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util FileUtil createTempFile.

Prototype

public static File createTempFile() 

Source Link

Usage

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

License:Open Source License

public FileEntry updateFileEntry(long fileEntryId, String sourceFileName, String mimeType, String title,
        String description, String changeLog, boolean majorVersion, InputStream deltaInputStream, long size,
        ServiceContext serviceContext) throws PortalException, SystemException {

    FileEntry fileEntry = dlAppLocalService.getFileEntry(fileEntryId);

    InputStream originalInputStream = null;
    File patchedFile = null;/*from   w  w w .j av a 2 s .c o m*/
    InputStream patchedInputStream = null;

    try {
        originalInputStream = fileEntry.getContentStream();

        patchedFile = FileUtil.createTempFile();

        patchFile(originalInputStream, deltaInputStream, patchedFile);

        patchedInputStream = new FileInputStream(patchedFile);

        return dlAppService.updateFileEntry(fileEntryId, sourceFileName, mimeType, title, description,
                changeLog, majorVersion, patchedInputStream, size, serviceContext);
    } catch (Exception e) {
        throw new PortalException(e);
    } finally {
        StreamUtil.cleanUp(originalInputStream);
        StreamUtil.cleanUp(patchedInputStream);

        FileUtil.delete(patchedFile);
    }
}

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

License:Open Source License

protected void patchFile(InputStream originalInputStream, InputStream deltaInputStream, File patchedFile)
        throws PortalException {

    File originalFile = null;//from   w ww .j  a v a2s  . c  o m
    FileInputStream originalFileInputStream = null;
    FileChannel originalFileChannel = null;
    FileOutputStream patchedFileOutputStream = null;
    WritableByteChannel patchedWritableByteChannel = null;
    ReadableByteChannel deltaReadableByteChannel = null;

    try {
        originalFile = FileUtil.createTempFile();

        FileUtil.write(originalFile, originalInputStream);

        originalFileInputStream = new FileInputStream(originalFile);

        originalFileChannel = originalFileInputStream.getChannel();

        patchedFileOutputStream = new FileOutputStream(patchedFile);

        patchedWritableByteChannel = Channels.newChannel(patchedFileOutputStream);

        deltaReadableByteChannel = Channels.newChannel(deltaInputStream);

        ByteChannelReader deltaByteChannelReader = new ByteChannelReader(deltaReadableByteChannel);

        DeltaUtil.patch(originalFileChannel, patchedWritableByteChannel, deltaByteChannelReader);
    } catch (Exception e) {
        throw new PortalException(e);
    } finally {
        StreamUtil.cleanUp(originalFileInputStream);
        StreamUtil.cleanUp(originalFileChannel);
        StreamUtil.cleanUp(patchedFileOutputStream);
        StreamUtil.cleanUp(patchedWritableByteChannel);
        StreamUtil.cleanUp(deltaReadableByteChannel);

        FileUtil.delete(originalFile);
    }
}

From source file:com.liferay.portlet.documentlibrary.store.DLStoreImpl.java

License:Open Source License

public void addFile(long companyId, long repositoryId, String fileName, boolean validateFileExtension,
        InputStream is) throws PortalException, SystemException {

    if (is instanceof ByteArrayFileInputStream) {
        ByteArrayFileInputStream byteArrayFileInputStream = (ByteArrayFileInputStream) is;

        File file = byteArrayFileInputStream.getFile();

        addFile(companyId, repositoryId, fileName, validateFileExtension, file);

        return;//from  ww w  . j  av a  2 s  . co m
    }

    validate(fileName, validateFileExtension, is);

    if (!PropsValues.DL_STORE_ANTIVIRUS_ENABLED || !AntivirusScannerUtil.isActive()) {

        store.addFile(companyId, repositoryId, fileName, is);
    } else {
        File tempFile = null;

        try {
            if (is.markSupported()) {
                is.mark(is.available() + 1);

                AntivirusScannerUtil.scan(is);

                is.reset();

                store.addFile(companyId, repositoryId, fileName, is);
            } else {
                tempFile = FileUtil.createTempFile();

                FileUtil.write(tempFile, is);

                AntivirusScannerUtil.scan(tempFile);

                store.addFile(companyId, repositoryId, fileName, tempFile);
            }
        } catch (IOException ioe) {
            throw new SystemException("Unable to scan file " + fileName, ioe);
        } finally {
            if (tempFile != null) {
                tempFile.delete();
            }
        }
    }
}

From source file:com.liferay.portlet.documentlibrary.store.DLStoreImpl.java

License:Open Source License

public void updateFile(long companyId, long repositoryId, String fileName, String fileExtension,
        boolean validateFileExtension, String versionLabel, String sourceFileName, InputStream is)
        throws PortalException, SystemException {

    if (is instanceof ByteArrayFileInputStream) {
        ByteArrayFileInputStream byteArrayFileInputStream = (ByteArrayFileInputStream) is;

        File file = byteArrayFileInputStream.getFile();

        updateFile(companyId, repositoryId, fileName, fileExtension, validateFileExtension, versionLabel,
                sourceFileName, file);//from   ww w  .j  a v a 2s .co m

        return;
    }

    validate(fileName, fileExtension, sourceFileName, validateFileExtension, is);

    if (!PropsValues.DL_STORE_ANTIVIRUS_ENABLED || !AntivirusScannerUtil.isActive()) {

        store.updateFile(companyId, repositoryId, fileName, versionLabel, is);
    } else {
        File tempFile = null;

        try {
            if (is.markSupported()) {
                is.mark(is.available() + 1);

                AntivirusScannerUtil.scan(is);

                is.reset();

                store.updateFile(companyId, repositoryId, fileName, versionLabel, is);
            } else {
                tempFile = FileUtil.createTempFile();

                FileUtil.write(tempFile, is);

                AntivirusScannerUtil.scan(tempFile);

                store.updateFile(companyId, repositoryId, fileName, versionLabel, tempFile);
            }
        } catch (IOException ioe) {
            throw new SystemException("Unable to scan file " + fileName, ioe);
        } finally {
            if (tempFile != null) {
                tempFile.delete();
            }
        }
    }
}

From source file:com.liferay.sync.service.impl.SyncDLObjectServiceImpl.java

License:Open Source License

@Override
public SyncDLObject patchFileEntry(long fileEntryId, String sourceVersion, String sourceFileName,
        String mimeType, String title, String description, String changeLog, boolean majorVersion,
        File deltaFile, String checksum, ServiceContext serviceContext) throws PortalException {

    File patchedFile = null;/*  ww w. ja v a  2 s  .c om*/

    try {
        File sourceFile = dlFileEntryLocalService.getFile(getUserId(), fileEntryId, sourceVersion, false);

        patchedFile = FileUtil.createTempFile();

        SyncUtil.patchFile(sourceFile, deltaFile, patchedFile);

        SyncDLObject syncDLObject = updateFileEntry(fileEntryId, sourceFileName, mimeType, title, description,
                changeLog, majorVersion, patchedFile, checksum, serviceContext);

        if (PortletPropsValues.SYNC_FILE_DIFF_CACHE_ENABLED) {
            DLFileVersion sourceDLFileVersion = dlFileVersionLocalService.getFileVersion(fileEntryId,
                    sourceVersion);
            DLFileVersion targetDLFileVersion = dlFileVersionLocalService.getFileVersion(fileEntryId,
                    syncDLObject.getVersion());

            syncDLFileVersionDiffLocalService.addSyncDLFileVersionDiff(fileEntryId,
                    sourceDLFileVersion.getFileVersionId(), targetDLFileVersion.getFileVersionId(), deltaFile);
        }

        return syncDLObject;
    } catch (PortalException pe) {
        throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    } finally {
        FileUtil.delete(patchedFile);
    }
}

From source file:com.liferay.sync.util.SyncUtil.java

License:Open Source License

public static File getFileDelta(File sourceFile, File targetFile) throws PortalException {

    File deltaFile = null;/* ww  w.  j a  va2 s . c  o m*/

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

    try {
        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);
    }

    FileInputStream targetFileInputStream = null;
    ReadableByteChannel targetReadableByteChannel = null;
    InputStream checksumsInputStream = null;
    ReadableByteChannel checksumsReadableByteChannel = null;
    OutputStream deltaOutputStream = null;
    WritableByteChannel deltaOutputStreamWritableByteChannel = null;

    try {
        targetFileInputStream = new FileInputStream(targetFile);

        targetReadableByteChannel = targetFileInputStream.getChannel();

        checksumsInputStream = new FileInputStream(checksumsFile);

        checksumsReadableByteChannel = Channels.newChannel(checksumsInputStream);

        ByteChannelReader checksumsByteChannelReader = new ByteChannelReader(checksumsReadableByteChannel);

        deltaFile = FileUtil.createTempFile();

        deltaOutputStream = new FileOutputStream(deltaFile);

        deltaOutputStreamWritableByteChannel = Channels.newChannel(deltaOutputStream);

        ByteChannelWriter deltaByteChannelWriter = new ByteChannelWriter(deltaOutputStreamWritableByteChannel);

        DeltaUtil.delta(targetReadableByteChannel, checksumsByteChannelReader, deltaByteChannelWriter);

        deltaByteChannelWriter.finish();
    } catch (Exception e) {
        throw new PortalException(e);
    } finally {
        StreamUtil.cleanUp(targetFileInputStream);
        StreamUtil.cleanUp(targetReadableByteChannel);
        StreamUtil.cleanUp(checksumsInputStream);
        StreamUtil.cleanUp(checksumsReadableByteChannel);
        StreamUtil.cleanUp(deltaOutputStream);
        StreamUtil.cleanUp(deltaOutputStreamWritableByteChannel);

        FileUtil.delete(checksumsFile);
    }

    return deltaFile;
}