com.liferay.message.boards.web.internal.upload.TempImageMBUploadFileEntryHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.message.boards.web.internal.upload.TempImageMBUploadFileEntryHandler.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.message.boards.web.internal.upload;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.repository.model.FileEntry;
import com.liferay.portal.kernel.security.permission.ActionKeys;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.TempFileEntryUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portlet.messageboards.service.permission.MBCategoryPermission;
import com.liferay.upload.UploadFileEntryHandler;

import java.io.IOException;
import java.io.InputStream;

import org.osgi.service.component.annotations.Component;

/**
 * @author Ambrn Chaudhary
 */
@Component(service = TempImageMBUploadFileEntryHandler.class)
public class TempImageMBUploadFileEntryHandler implements UploadFileEntryHandler {

    @Override
    public FileEntry upload(UploadPortletRequest uploadPortletRequest) throws IOException, PortalException {

        ThemeDisplay themeDisplay = (ThemeDisplay) uploadPortletRequest.getAttribute(WebKeys.THEME_DISPLAY);

        long categoryId = ParamUtil.getLong(uploadPortletRequest, "categoryId");

        MBCategoryPermission.check(themeDisplay.getPermissionChecker(), themeDisplay.getScopeGroupId(), categoryId,
                ActionKeys.ADD_FILE);

        String fileName = uploadPortletRequest.getFileName(_PARAMETER_NAME);
        String contentType = uploadPortletRequest.getContentType(_PARAMETER_NAME);

        try (InputStream inputStream = uploadPortletRequest.getFileAsStream(_PARAMETER_NAME)) {

            String tempFileName = TempFileEntryUtil.getTempFileName(fileName);

            return TempFileEntryUtil.addTempFileEntry(themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
                    _TEMP_FOLDER_NAME, tempFileName, inputStream, contentType);
        }
    }

    private static final String _PARAMETER_NAME = "imageSelectorFileName";

    private static final String _TEMP_FOLDER_NAME = TempImageMBUploadFileEntryHandler.class.getName();

}