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(String extension) 

Source Link

Usage

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

License:Open Source License

/**
 * Updates a file entry and associated metadata based on a byte array
 * object. If the file data is <code>null</code>, then only the associated
 * metadata (i.e., <code>title</code>, <code>description</code>, and
 * parameters in the <code>serviceContext</code>) will be updated.
 *
 * <p>//w  ww.ja  v a2  s .  com
 * This method takes two file names, the <code>sourceFileName</code> and the
 * <code>title</code>. The <code>sourceFileName</code> corresponds to the
 * name of the actual file being uploaded. The <code>title</code>
 * corresponds to a name the client wishes to assign this file after it has
 * been uploaded to the portal.
 * </p>
 *
 * @param  fileEntryId the primary key of the file entry
 * @param  sourceFileName the original file's name (optionally
 *         <code>null</code>)
 * @param  mimeType the file's MIME type (optionally <code>null</code>)
 * @param  title the new name to be assigned to the file (optionally <code>
 *         <code>null</code></code>)
 * @param  description the file's new description
 * @param  changeLog the file's version change log (optionally
 *         <code>null</code>)
 * @param  majorVersion whether the new file version is a major version
 * @param  bytes the file's data (optionally <code>null</code>)
 * @param  serviceContext the service context to be applied. Can set the
 *         asset category IDs, asset tag names, and expando bridge
 *         attributes for the file entry. In a Liferay repository, it may
 *         include:  <ul> <li> fileEntryTypeId - ID for a custom file entry
 *         type </li> <li> fieldsMap - mapping for fields associated with a
 *         custom file entry type </li> </ul>
 * @return the file entry
 * @throws PortalException if the file entry could not be found
 * @throws SystemException if a system exception occurred
 */
public FileEntry updateFileEntry(long fileEntryId, String sourceFileName, String mimeType, String title,
        String description, String changeLog, boolean majorVersion, byte[] bytes, ServiceContext serviceContext)
        throws PortalException, SystemException {

    File file = null;

    try {
        if ((bytes != null) && (bytes.length > 0)) {
            file = FileUtil.createTempFile(bytes);
        }

        return updateFileEntry(fileEntryId, sourceFileName, mimeType, title, description, changeLog,
                majorVersion, file, serviceContext);
    } catch (IOException ioe) {
        throw new SystemException("Unable to write temporary file", ioe);
    } finally {
        FileUtil.delete(file);
    }
}

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

License:Open Source License

@Override
public Tree[] moveDocument(SharepointRequest sharepointRequest) throws Exception {

    String parentFolderPath = sharepointRequest.getRootPath();

    long groupId = SharepointUtil.getGroupId(parentFolderPath);

    Folder folder = null;// w  ww .j  ava2s  . co m
    FileEntry fileEntry = null;

    try {
        long parentFolderId = getLastFolderId(groupId, parentFolderPath,
                DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);

        folder = DLAppServiceUtil.getFolder(parentFolderId);
    } catch (Exception e1) {
        if (e1 instanceof NoSuchFolderException) {
            try {
                fileEntry = getFileEntry(sharepointRequest);
            } catch (Exception e2) {
            }
        }
    }

    Tree movedDocsTree = new Tree();
    Tree movedDirsTree = new Tree();

    String newPath = sharepointRequest.getParameterValue("newUrl");
    String newParentFolderPath = getParentFolderPath(newPath);

    long newGroupId = SharepointUtil.getGroupId(newParentFolderPath);

    long newParentFolderId = getLastFolderId(newGroupId, newParentFolderPath,
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);

    String newName = getResourceName(newPath);

    ServiceContext serviceContext = new ServiceContext();

    if (fileEntry != null) {
        File file = null;

        try {
            long fileEntryId = fileEntry.getFileEntryId();

            long folderId = fileEntry.getFolderId();
            String mimeType = fileEntry.getMimeType();
            String description = fileEntry.getDescription();
            String changeLog = StringPool.BLANK;

            InputStream is = fileEntry.getContentStream();

            file = FileUtil.createTempFile(is);

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

            serviceContext.setAssetTagNames(assetTagNames);

            fileEntry = DLAppServiceUtil.updateFileEntry(fileEntryId, newName, mimeType, newName, description,
                    changeLog, false, file, serviceContext);

            if (folderId != newParentFolderId) {
                fileEntry = DLAppServiceUtil.moveFileEntry(fileEntryId, newParentFolderId, serviceContext);
            }

            Tree documentTree = getFileEntryTree(fileEntry, newParentFolderPath);

            movedDocsTree.addChild(documentTree);
        } finally {
            FileUtil.delete(file);
        }
    } else if (folder != null) {
        long folderId = folder.getFolderId();

        folder = DLAppServiceUtil.moveFolder(folderId, newParentFolderId, serviceContext);

        Tree folderTree = getFolderTree(folder, newParentFolderPath);

        movedDirsTree.addChild(folderTree);
    }

    return new Tree[] { movedDocsTree, movedDirsTree };
}

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;/*from w w w  .j  ava2s  .  co 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./*from  ww  w  .  java  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/*www  .j  ava 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.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;/* w w w. j a v a 2  s . c om*/
    }

    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);/* w  w w  .j  a v a2s . 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, InputStream inputStream) throws Exception {

    File file = FileUtil.createTempFile(inputStream);

    try {//from  w ww  .j  av  a 2 s  . c om
        _generateImagesIM(fileVersion, file);
    } finally {
        FileUtil.delete(file);
    }
}

From source file:com.liferay.portlet.documentlibrary.webdav.DLWebDAVStorageImpl.java

License:Open Source License

@Override
public int copySimpleResource(WebDAVRequest webDavRequest, Resource resource, String destination,
        boolean overwrite) throws WebDAVException {

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

    try {
        String[] destinationArray = WebDAVUtil.getPathArray(destination, true);

        long companyId = webDavRequest.getCompanyId();

        long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;

        try {
            parentFolderId = getParentFolderId(companyId, destinationArray);
        } catch (NoSuchFolderException nsfe) {
            return HttpServletResponse.SC_CONFLICT;
        }

        FileEntry fileEntry = (FileEntry) resource.getModel();

        long groupId = WebDAVUtil.getGroupId(companyId, destination);
        String mimeType = fileEntry.getMimeType();
        String title = WebDAVUtil.getResourceName(destinationArray);
        String description = fileEntry.getDescription();
        String changeLog = StringPool.BLANK;

        InputStream is = fileEntry.getContentStream();

        file = FileUtil.createTempFile(is);

        ServiceContext serviceContext = new ServiceContext();

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

        int status = HttpServletResponse.SC_CREATED;

        if (overwrite) {
            if (deleteResource(groupId, parentFolderId, title, webDavRequest.getLockUuid())) {

                status = HttpServletResponse.SC_NO_CONTENT;
            }
        }

        DLAppServiceUtil.addFileEntry(groupId, parentFolderId, title, mimeType, title, description, changeLog,
                file, serviceContext);

        return status;
    } catch (DuplicateFileException dfe) {
        return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (DuplicateFolderNameException dfne) {
        return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (LockException le) {
        return WebDAVUtil.SC_LOCKED;
    } catch (PrincipalException pe) {
        return HttpServletResponse.SC_FORBIDDEN;
    } catch (Exception e) {
        throw new WebDAVException(e);
    } finally {
        FileUtil.delete(file);
    }
}

From source file:com.liferay.portlet.documentlibrary.webdav.DLWebDAVStorageImpl.java

License:Open Source License

@Override
public Status lockResource(WebDAVRequest webDavRequest, String owner, long timeout) throws WebDAVException {

    Resource resource = getResource(webDavRequest);

    Lock lock = null;/*w ww . ja  va  2  s .co m*/
    int status = HttpServletResponse.SC_OK;

    try {
        if (resource == null) {
            status = HttpServletResponse.SC_CREATED;

            HttpServletRequest request = webDavRequest.getHttpServletRequest();

            String[] pathArray = webDavRequest.getPathArray();

            long companyId = webDavRequest.getCompanyId();
            long groupId = webDavRequest.getGroupId();
            long parentFolderId = getParentFolderId(companyId, pathArray);
            String title = WebDAVUtil.getResourceName(pathArray);

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

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

            String description = StringPool.BLANK;
            String changeLog = StringPool.BLANK;

            File file = FileUtil.createTempFile(FileUtil.getExtension(title));

            file.createNewFile();

            ServiceContext serviceContext = new ServiceContext();

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

            FileEntry fileEntry = DLAppServiceUtil.addFileEntry(groupId, parentFolderId, title, contentType,
                    title, description, changeLog, file, serviceContext);

            resource = toResource(webDavRequest, fileEntry, false);
        }

        if (resource instanceof DLFileEntryResourceImpl) {
            FileEntry fileEntry = (FileEntry) resource.getModel();

            lock = DLAppServiceUtil.lockFileEntry(fileEntry.getFileEntryId(), owner, timeout);
        } else {
            boolean inheritable = false;

            long depth = WebDAVUtil.getDepth(webDavRequest.getHttpServletRequest());

            if (depth != 0) {
                inheritable = true;
            }

            Folder folder = (Folder) resource.getModel();

            lock = DLAppServiceUtil.lockFolder(folder.getRepositoryId(), folder.getFolderId(), owner,
                    inheritable, timeout);
        }
    } catch (Exception e) {

        // DuplicateLock is 423 not 501

        if (!(e instanceof DuplicateLockException)) {
            throw new WebDAVException(e);
        }

        status = WebDAVUtil.SC_LOCKED;
    }

    return new Status(lock, status);
}