Example usage for com.liferay.portal.kernel.io ByteArrayFileInputStream getFile

List of usage examples for com.liferay.portal.kernel.io ByteArrayFileInputStream getFile

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.io ByteArrayFileInputStream getFile.

Prototype

public File getFile() 

Source Link

Usage

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   w  w  w.  jav 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 av a 2  s.  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.privatemessaging.portlet.PrivateMessagingPortlet.java

License:Open Source License

protected void validateAttachment(String fileName, InputStream inputStream) throws Exception {

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

        File file = byteArrayFileInputStream.getFile();

        if ((PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)
                && ((file == null) || (file.length() > PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {

            throw new FileSizeException(fileName);
        }//  ww w.  ja  v a  2 s . co m
    }

    if (!isValidName(fileName)) {
        throw new FileNameException(fileName);
    }

    String[] fileExtensions = PrefsPropsUtil.getStringArray(PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA);

    boolean validFileExtension = false;

    for (String fileExtension : fileExtensions) {
        if (StringPool.STAR.equals(fileExtension) || StringUtil.endsWith(fileName, fileExtension)) {

            validFileExtension = true;

            break;
        }
    }

    if (!validFileExtension) {
        throw new FileExtensionException(fileName);
    }
}