All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.liferay.journal.web.internal.upload.ImageJournalUploadFileEntryHandler Maven / Gradle / Ivy
/**
* 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.journal.web.internal.upload;
import com.liferay.document.library.kernel.util.DLValidator;
import com.liferay.journal.service.permission.JournalArticlePermission;
import com.liferay.journal.service.permission.JournalFolderPermission;
import com.liferay.journal.service.permission.JournalPermission;
import com.liferay.portal.kernel.exception.ImageTypeException;
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.security.auth.PrincipalException;
import com.liferay.portal.kernel.security.permission.ActionKeys;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.security.permission.ResourcePermissionCheckerUtil;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.TempFileEntryUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.util.PrefsPropsUtil;
import com.liferay.upload.UniqueFileNameProvider;
import com.liferay.upload.UploadFileEntryHandler;
import java.io.IOException;
import java.io.InputStream;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* @author Eduardo García
* @author Alejandro Tardín
*/
@Component(immediate = true, service = ImageJournalUploadFileEntryHandler.class)
public class ImageJournalUploadFileEntryHandler
implements UploadFileEntryHandler {
@Override
public FileEntry upload(UploadPortletRequest uploadPortletRequest)
throws IOException, PortalException {
ThemeDisplay themeDisplay =
(ThemeDisplay)uploadPortletRequest.getAttribute(
WebKeys.THEME_DISPLAY);
long resourcePrimKey = ParamUtil.getLong(
uploadPortletRequest, "resourcePrimKey");
long folderId = ParamUtil.getLong(uploadPortletRequest, "folderId");
if (resourcePrimKey != 0) {
JournalArticlePermission.check(
themeDisplay.getPermissionChecker(), resourcePrimKey,
ActionKeys.UPDATE);
}
else if (folderId != 0) {
JournalFolderPermission.check(
themeDisplay.getPermissionChecker(),
themeDisplay.getScopeGroupId(), folderId,
ActionKeys.ADD_ARTICLE);
}
else {
_checkPermission(
themeDisplay.getScopeGroupId(),
themeDisplay.getPermissionChecker());
}
String fileName = uploadPortletRequest.getFileName(_PARAMETER_NAME);
long size = uploadPortletRequest.getSize(_PARAMETER_NAME);
_validateFile(fileName, size);
String contentType = uploadPortletRequest.getContentType(
_PARAMETER_NAME);
try (InputStream inputStream = uploadPortletRequest.getFileAsStream(
_PARAMETER_NAME)) {
String uniqueFileName = _uniqueFileNameProvider.provide(
fileName, curFileName -> _exists(themeDisplay, curFileName));
return TempFileEntryUtil.addTempFileEntry(
themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
_TEMP_FOLDER_NAME, uniqueFileName, inputStream, contentType);
}
}
private void _checkPermission(
long groupId, PermissionChecker permissionChecker)
throws PortalException {
boolean containsResourcePermission =
ResourcePermissionCheckerUtil.containsResourcePermission(
permissionChecker, JournalPermission.RESOURCE_NAME, groupId,
ActionKeys.ADD_ARTICLE);
if (!containsResourcePermission) {
throw new PrincipalException.MustHavePermission(
permissionChecker, JournalPermission.RESOURCE_NAME, groupId,
ActionKeys.ADD_ARTICLE);
}
}
private boolean _exists(ThemeDisplay themeDisplay, String curFileName) {
try {
FileEntry tempFileEntry = TempFileEntryUtil.getTempFileEntry(
themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
_TEMP_FOLDER_NAME, curFileName);
if (tempFileEntry != null) {
return true;
}
return false;
}
catch (PortalException pe) {
if (_log.isDebugEnabled()) {
_log.debug(pe, pe);
}
return false;
}
}
private void _validateFile(String fileName, long size)
throws PortalException {
_dlValidator.validateFileSize(fileName, size);
String[] imageExtensions = PrefsPropsUtil.getStringArray(
PropsKeys.JOURNAL_IMAGE_EXTENSIONS, StringPool.COMMA);
String extension = FileUtil.getExtension(fileName);
for (String imageExtension : imageExtensions) {
if (StringPool.STAR.equals(imageExtension) ||
imageExtension.equals(StringPool.PERIOD + extension)) {
return;
}
}
throw new ImageTypeException(
"Invalid image type for file name " + fileName);
}
private static final String _PARAMETER_NAME = "imageSelectorFileName";
private static final String _TEMP_FOLDER_NAME =
ImageJournalUploadFileEntryHandler.class.getName();
private static final Log _log = LogFactoryUtil.getLog(
ImageJournalUploadFileEntryHandler.class);
@Reference
private DLValidator _dlValidator;
@Reference
private UniqueFileNameProvider _uniqueFileNameProvider;
}