All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.portal.image.FileSystemHook 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.portal.image;

import com.liferay.document.library.kernel.exception.NoSuchFileException;
import com.liferay.petra.string.StringBundler;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.model.Image;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.util.PropsValues;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Jorge Ferrer
 */
public class FileSystemHook extends BaseHook {

	public FileSystemHook() throws IOException {
		_rootDir = new File(PropsValues.IMAGE_HOOK_FILE_SYSTEM_ROOT_DIR);

		FileUtil.mkdirs(_rootDir);
	}

	@Override
	public void deleteImage(Image image) {
		FileUtil.delete(getFile(image.getImageId(), image.getType()));
	}

	@Override
	public byte[] getImageAsBytes(Image image) throws PortalException {
		try {
			File file = getFile(image.getImageId(), image.getType());

			if (!file.exists()) {
				throw new NoSuchFileException(file.getPath());
			}

			return FileUtil.getBytes(file);
		}
		catch (IOException ioException) {
			throw new SystemException(ioException);
		}
	}

	@Override
	public InputStream getImageAsStream(Image image) throws PortalException {
		try {
			File file = getFile(image.getImageId(), image.getType());

			if (!file.exists()) {
				throw new NoSuchFileException(file.getPath());
			}

			return new FileInputStream(file);
		}
		catch (IOException ioException) {
			throw new SystemException(ioException);
		}
	}

	@Override
	public void updateImage(Image image, String type, byte[] bytes) {
		try {
			FileUtil.write(getFile(image.getImageId(), type), bytes);
		}
		catch (IOException ioException) {
			throw new SystemException(ioException);
		}
	}

	protected String buildPath(String fileNameFragment) {
		int fileNameFragmentLength = fileNameFragment.length();

		if (fileNameFragmentLength <= 2) {
			return StringPool.BLANK;
		}

		StringBundler sb = new StringBundler(
			(fileNameFragmentLength / 2) + fileNameFragmentLength);

		for (int i = 0; i < fileNameFragmentLength; i += 2) {
			if ((i + 2) < fileNameFragmentLength) {
				sb.append(StringPool.SLASH);
				sb.append(fileNameFragment.substring(i, i + 2));
			}
		}

		return sb.toString();
	}

	protected File getFile(long imageId, String type) {
		String path = buildPath(String.valueOf(imageId));

		return new File(
			StringBundler.concat(
				_rootDir, StringPool.SLASH, path, StringPool.SLASH, imageId,
				StringPool.PERIOD, type));
	}

	private final File _rootDir;

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy