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

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

Introduction

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

Prototype

public static boolean delete(String file) 

Source Link

Usage

From source file:com.liferay.portlet.documentlibrary.sharepoint.DLSharepointStorageImpl.java

License:Open Source License

@Override
public void putDocument(SharepointRequest sharepointRequest) throws Exception {

    HttpServletRequest request = sharepointRequest.getHttpServletRequest();

    String documentPath = sharepointRequest.getRootPath();
    String parentFolderPath = getParentFolderPath(documentPath);

    long groupId = SharepointUtil.getGroupId(parentFolderPath);
    long parentFolderId = getLastFolderId(groupId, parentFolderPath,
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
    String title = getResourceName(documentPath);
    String description = StringPool.BLANK;
    String changeLog = StringPool.BLANK;

    ServiceContext serviceContext = new ServiceContext();

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

    String contentType = GetterUtil.get(request.getHeader(HttpHeaders.CONTENT_TYPE),
            ContentTypes.APPLICATION_OCTET_STREAM);

    String extension = FileUtil.getExtension(title);

    File file = null;/*w  ww  .j a  v a2 s.c  o m*/

    try {
        file = FileUtil.createTempFile(extension);

        FileUtil.write(file, request.getInputStream());

        if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
            contentType = MimeTypesUtil.getContentType(file, title);
        }

        try {
            FileEntry fileEntry = getFileEntry(sharepointRequest);

            long fileEntryId = fileEntry.getFileEntryId();

            description = fileEntry.getDescription();

            String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(FileEntry.class.getName(),
                    fileEntry.getFileEntryId());

            serviceContext.setAssetTagNames(assetTagNames);

            DLAppServiceUtil.updateFileEntry(fileEntryId, title, contentType, title, description, changeLog,
                    false, file, serviceContext);
        } catch (NoSuchFileEntryException nsfee) {
            DLAppServiceUtil.addFileEntry(groupId, parentFolderId, title, contentType, title, description,
                    changeLog, file, serviceContext);
        }
    } finally {
        FileUtil.delete(file);
    }
}

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

License:Open Source License

/**
 * Adds a file based on a byte array.//  ww  w  .  j a  v  a 2 s .  c  o m
 *
 * @param  companyId the primary key of the company
 * @param  repositoryId the primary key of the data repository (optionally
 *         {@link CompanyConstants#SYSTEM})
 * @param  fileName the file name
 * @param  bytes the files's data
 * @throws PortalException if the file's information was invalid
 * @throws SystemException if a system exception occurred
 */
public void addFile(long companyId, long repositoryId, String fileName, byte[] bytes)
        throws PortalException, SystemException {

    File file = null;

    try {
        file = FileUtil.createTempFile(bytes);

        addFile(companyId, repositoryId, fileName, file);
    } catch (IOException ioe) {
        throw new SystemException("Unable to write temporary file", ioe);
    } finally {
        FileUtil.delete(file);
    }
}

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

License:Open Source License

/**
 * Updates a file based on a byte array.
 *
 * @param  companyId the primary key of the company
 * @param  repositoryId the primary key of the data repository (optionally
 *         {@link CompanyConstants#SYSTEM})
 * @param  fileName the file name/* w w  w.j  a v  a  2  s  . c  o m*/
 * @param  versionLabel the file's new version label
 * @param  bytes the new file's data
 * @throws PortalException if the file's information was invalid
 * @throws SystemException if a system exception occurred
 */
public void updateFile(long companyId, long repositoryId, String fileName, String versionLabel, byte[] bytes)
        throws PortalException, SystemException {

    File file = null;

    try {
        file = FileUtil.createTempFile(bytes);

        updateFile(companyId, repositoryId, fileName, versionLabel, file);
    } catch (IOException ioe) {
        throw new SystemException("Unable to write temporary file", ioe);
    } finally {
        FileUtil.delete(file);
    }
}

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

License:Open Source License

@Override
public void updateFile(long companyId, long repositoryId, long newRepositoryId, String fileName)
        throws SystemException {

    try {/*from  w  w  w.j  ava 2 s. co  m*/
        S3Object[] s3Objects = _s3Service.listObjects(_s3Bucket, getKey(companyId, repositoryId, fileName),
                null);

        for (int i = 0; i < s3Objects.length; i++) {
            S3Object oldS3Object = s3Objects[i];

            String oldKey = oldS3Object.getKey();

            oldS3Object = _s3Service.getObject(_s3Bucket, oldKey);

            File tempFile = new File(SystemProperties.get(SystemProperties.TMP_DIR) + File.separator
                    + PortalUUIDUtil.generate());

            FileUtil.write(tempFile, oldS3Object.getDataInputStream());

            InputStream is = new FileInputStream(tempFile);

            String newPrefix = getKey(companyId, newRepositoryId);

            int x = oldKey.indexOf(CharPool.SLASH);

            x = oldKey.indexOf(CharPool.SLASH, x + 1);

            String newKey = newPrefix + oldKey.substring(x + 1, oldKey.length());

            S3Object newS3Object = new S3Object(_s3Bucket, newKey);

            newS3Object.setDataInputStream(is);

            _s3Service.putObject(_s3Bucket, newS3Object);
            _s3Service.deleteObject(_s3Bucket, oldKey);

            FileUtil.delete(tempFile);
        }
    } catch (IOException ioe) {
        throw new SystemException(ioe);
    } catch (S3ServiceException s3se) {
        throw new SystemException(s3se);
    }
}

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

License:Open Source License

public void updateFile(long companyId, long repositoryId, String fileName, String newFileName)
        throws SystemException {

    try {/*w w  w.ja  v  a 2s .co  m*/
        S3Object[] s3Objects = _s3Service.listObjects(_s3Bucket, getKey(companyId, repositoryId, fileName),
                null);

        for (int i = 0; i < s3Objects.length; i++) {
            S3Object oldS3Object = s3Objects[i];

            String oldKey = oldS3Object.getKey();

            oldS3Object = _s3Service.getObject(_s3Bucket, oldKey);

            File tempFile = new File(SystemProperties.get(SystemProperties.TMP_DIR) + File.separator
                    + PortalUUIDUtil.generate());

            FileUtil.write(tempFile, oldS3Object.getDataInputStream());

            InputStream is = new FileInputStream(tempFile);

            String newPrefix = getKey(companyId, repositoryId, newFileName);

            int x = oldKey.indexOf(StringPool.SLASH);

            x = oldKey.indexOf(CharPool.SLASH, x + 1);

            x = oldKey.indexOf(CharPool.SLASH, x + 1);

            String newKey = newPrefix + oldKey.substring(x + 1, oldKey.length());

            S3Object newS3Object = new S3Object(_s3Bucket, newKey);

            newS3Object.setDataInputStream(is);

            _s3Service.putObject(_s3Bucket, newS3Object);
            _s3Service.deleteObject(_s3Bucket, oldKey);

            FileUtil.delete(tempFile);
        }
    } catch (IOException ioe) {
        throw new SystemException(ioe);
    } catch (S3ServiceException s3se) {
        throw new SystemException(s3se);
    }
}

From source file:com.liferay.portlet.documentlibrary.util.AudioProcessorImpl.java

License:Open Source License

private void _generateAudio(FileVersion fileVersion) throws Exception {
    String tempFileId = DLUtil.getTempFileId(fileVersion.getFileEntryId(), fileVersion.getVersion());

    File audioTempFile = _getAudioTempFile(tempFileId, fileVersion.getExtension());
    File previewTempFile = getPreviewTempFile(tempFileId);

    try {//from w  w  w  .ja  v a  2s  .  c o  m
        if (!PrefsPropsUtil.getBoolean(PropsKeys.XUGGLER_ENABLED, PropsValues.XUGGLER_ENABLED)
                || _hasAudio(fileVersion)) {

            return;
        }

        if (_isGeneratePreview(fileVersion)) {
            File file = null;

            if (fileVersion instanceof LiferayFileVersion) {
                try {
                    LiferayFileVersion liferayFileVersion = (LiferayFileVersion) fileVersion;

                    file = liferayFileVersion.getFile(false);
                } catch (UnsupportedOperationException uoe) {
                }
            }

            if (file == null) {
                InputStream inputStream = fileVersion.getContentStream(false);

                FileUtil.write(audioTempFile, inputStream);

                file = audioTempFile;
            }

            try {
                _generateAudioXuggler(fileVersion, file, previewTempFile);
            } catch (Exception e) {
                _log.error(e, e);
            }
        }
    } catch (NoSuchFileEntryException nsfee) {
    } finally {
        _fileVersionIds.remove(fileVersion.getFileVersionId());

        FileUtil.delete(audioTempFile);
        FileUtil.delete(previewTempFile);
    }
}

From source file:com.liferay.portlet.documentlibrary.util.DLPreviewableProcessor.java

License:Open Source License

protected void storeThumbnailImage(FileVersion fileVersion, RenderedImage renderedImage, int index)
        throws Exception {

    if (!isThumbnailEnabled(index) || hasThumbnail(fileVersion, index)) {
        return;//from  w w w .j av  a 2s. c  o  m
    }

    String type = getThumbnailType(fileVersion);

    String maxHeightPropsKey = PropsKeys.DL_FILE_ENTRY_THUMBNAIL_MAX_HEIGHT;
    String maxWidthPropsKey = PropsKeys.DL_FILE_ENTRY_THUMBNAIL_MAX_WIDTH;

    if (index == THUMBNAIL_INDEX_CUSTOM_1) {
        maxHeightPropsKey = PropsKeys.DL_FILE_ENTRY_THUMBNAIL_CUSTOM_1_MAX_HEIGHT;
        maxWidthPropsKey = PropsKeys.DL_FILE_ENTRY_THUMBNAIL_CUSTOM_1_MAX_WIDTH;
    } else if (index == THUMBNAIL_INDEX_CUSTOM_2) {
        maxHeightPropsKey = PropsKeys.DL_FILE_ENTRY_THUMBNAIL_CUSTOM_2_MAX_HEIGHT;
        maxWidthPropsKey = PropsKeys.DL_FILE_ENTRY_THUMBNAIL_CUSTOM_2_MAX_WIDTH;
    }

    RenderedImage thumbnailRenderedImage = ImageToolUtil.scale(renderedImage,
            PrefsPropsUtil.getInteger(maxHeightPropsKey), PrefsPropsUtil.getInteger(maxWidthPropsKey));

    byte[] bytes = ImageToolUtil.getBytes(thumbnailRenderedImage, type);

    File file = null;

    try {
        file = FileUtil.createTempFile(bytes);

        addFileToStore(fileVersion.getCompanyId(), THUMBNAIL_PATH,
                getThumbnailFilePath(fileVersion, type, index), file);
    } finally {
        FileUtil.delete(file);
    }
}

From source file:com.liferay.portlet.documentlibrary.util.ImageProcessorImpl.java

License:Open Source License

private void _storeThumbnail(long companyId, long groupId, long fileEntryId, long fileVersionId,
        long custom1ImageId, long custom2ImageId, InputStream is, String type) throws Exception {

    StringBundler sb = new StringBundler(5);

    sb.append(getPathSegment(groupId, fileEntryId, fileVersionId, false));

    if (custom1ImageId != 0) {
        sb.append(StringPool.DASH);/*from  ww  w  . j a  v  a  2  s .c o m*/
        sb.append(1);
    } else if (custom2ImageId != 0) {
        sb.append(StringPool.DASH);
        sb.append(2);
    }

    sb.append(StringPool.PERIOD);
    sb.append(type);

    String filePath = sb.toString();

    File file = null;

    try {
        file = FileUtil.createTempFile(is);

        addFileToStore(companyId, THUMBNAIL_PATH, filePath, file);
    } finally {
        FileUtil.delete(file);
    }
}

From source file:com.liferay.portlet.documentlibrary.util.PDFProcessorImpl.java

License:Open Source License

private void _generateImagesIM(FileVersion fileVersion, File file, boolean thumbnail) throws Exception {

    // Generate images

    String tempFileId = DLUtil.getTempFileId(fileVersion.getFileEntryId(), fileVersion.getVersion());

    IMOperation imOperation = new IMOperation();

    imOperation.alpha("off");

    imOperation.density(PropsValues.DL_FILE_ENTRY_PREVIEW_DOCUMENT_DPI,
            PropsValues.DL_FILE_ENTRY_PREVIEW_DOCUMENT_DPI);

    if (PropsValues.DL_FILE_ENTRY_PREVIEW_DOCUMENT_MAX_HEIGHT != 0) {
        imOperation.adaptiveResize(PropsValues.DL_FILE_ENTRY_PREVIEW_DOCUMENT_MAX_WIDTH,
                PropsValues.DL_FILE_ENTRY_PREVIEW_DOCUMENT_MAX_HEIGHT);
    } else {/*w  w  w. j  ava2  s .c  om*/
        imOperation.adaptiveResize(PropsValues.DL_FILE_ENTRY_PREVIEW_DOCUMENT_MAX_WIDTH);
    }

    imOperation.depth(PropsValues.DL_FILE_ENTRY_PREVIEW_DOCUMENT_DEPTH);

    if (thumbnail) {
        imOperation.addImage(file.getPath() + "[0]");
        imOperation.addImage(getThumbnailTempFilePath(tempFileId));
    } else {
        imOperation.addImage(file.getPath());
        imOperation.addImage(getPreviewTempFilePath(tempFileId, -1));
    }

    if (_log.isInfoEnabled()) {
        _log.info("Excecuting command 'convert " + imOperation + "'");
    }

    if (PropsValues.DL_FILE_ENTRY_PREVIEW_FORK_PROCESS_ENABLED) {
        ProcessCallable<String> processCallable = new ImageMagickProcessCallable(_globalSearchPath,
                imOperation.getCmdArgs());

        ProcessExecutor.execute(processCallable, ClassPathUtil.getPortalClassPath());
    } else {
        _convertCmd.run(imOperation);
    }

    // Store images

    if (thumbnail) {
        File thumbnailTempFile = getThumbnailTempFile(tempFileId);

        try {
            storeThumbnailImages(fileVersion, thumbnailTempFile);
        } finally {
            FileUtil.delete(thumbnailTempFile);
        }
    } else {

        // ImageMagick converts single page PDFs without appending an
        // index. Rename file for consistency.

        File singlePagePreviewFile = getPreviewTempFile(tempFileId, -1);

        if (singlePagePreviewFile.exists()) {
            singlePagePreviewFile.renameTo(getPreviewTempFile(tempFileId, 1));
        }

        int total = getPreviewTempFileCount(fileVersion);

        for (int i = 0; i < total; i++) {
            File previewTempFile = getPreviewTempFile(tempFileId, i + 1);

            try {
                addFileToStore(fileVersion.getCompanyId(), PREVIEW_PATH, getPreviewFilePath(fileVersion, i + 1),
                        previewTempFile);
            } finally {
                FileUtil.delete(previewTempFile);
            }
        }
    }
}

From source file:com.liferay.portlet.documentlibrary.util.PDFProcessorImpl.java

License:Open Source License

private void _generateImagesIM(FileVersion fileVersion, InputStream inputStream) throws Exception {

    File file = FileUtil.createTempFile(inputStream);

    try {//from  www  .  j  a v  a 2s. c  o m
        _generateImagesIM(fileVersion, file);
    } finally {
        FileUtil.delete(file);
    }
}