com.liferay.blogs.web.internal.upload.TempImageBlogsUploadFileEntryHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.blogs.web.internal.upload.TempImageBlogsUploadFileEntryHandler.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.blogs.web.internal.upload;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.repository.model.FileEntry;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.TempFileEntryUtil;
import com.liferay.upload.UniqueFileNameProvider;

import java.io.InputStream;

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

/**
 * @author Alejandro Tardn
 */
@Component(service = TempImageBlogsUploadFileEntryHandler.class)
public class TempImageBlogsUploadFileEntryHandler extends ImageBlogsUploadFileEntryHandler {

    @Override
    protected FileEntry addFileEntry(String fileName, String contentType, InputStream inputStream,
            ThemeDisplay themeDisplay) throws PortalException {

        String uniqueFileName = _uniqueFileNameProvider.provide(fileName,
                curFileName -> _exists(themeDisplay, curFileName));

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

    private boolean _exists(ThemeDisplay themeDisplay, String curFileName) {
        try {
            if (TempFileEntryUtil.getTempFileEntry(themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
                    _TEMP_FOLDER_NAME, curFileName) != null) {

                return true;
            }

            return false;
        } catch (PortalException pe) {
            if (_log.isDebugEnabled()) {
                _log.debug(pe, pe);
            }

            return false;
        }
    }

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

    private static final Log _log = LogFactoryUtil.getLog(TempImageBlogsUploadFileEntryHandler.class);

    @Reference
    private UniqueFileNameProvider _uniqueFileNameProvider;

}