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

com.liferay.adaptive.media.image.internal.configuration.AMImageAttributeMapping Maven / Gradle / Ivy

/**
 * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
 * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
 */

package com.liferay.adaptive.media.image.internal.configuration;

import com.liferay.adaptive.media.AMAttribute;
import com.liferay.adaptive.media.image.processor.AMImageAttribute;
import com.liferay.adaptive.media.image.processor.AMImageProcessor;
import com.liferay.portal.kernel.repository.model.FileVersion;
import com.liferay.portal.kernel.util.HashMapBuilder;

import java.util.Map;
import java.util.Optional;

/**
 * Gives convenient access to a set of media attributes. It offers a type-safe
 * interface to access attribute values, accepting only attributes of the
 * correct type (those for adaptive images), and returning values of the correct
 * type.
 *
 * @author Adolfo Pérez
 */
public class AMImageAttributeMapping {

	public static AMImageAttributeMapping fromFileVersion(
		FileVersion fileVersion) {

		return new AMImageAttributeMapping(
			HashMapBuilder., Optional>put(
				AMAttribute.getConfigurationUuidAMAttribute(), Optional.empty()
			).put(
				AMAttribute.getContentLengthAMAttribute(),
				Optional.of(fileVersion.getSize())
			).put(
				AMAttribute.getContentTypeAMAttribute(),
				Optional.of(fileVersion.getMimeType())
			).put(
				AMAttribute.getFileNameAMAttribute(),
				Optional.of(fileVersion.getFileName())
			).put(
				AMImageAttribute.AM_IMAGE_ATTRIBUTE_HEIGHT, Optional.empty()
			).put(
				AMImageAttribute.AM_IMAGE_ATTRIBUTE_WIDTH, Optional.empty()
			).build());
	}

	/**
	 * Returns an {@link AMImageAttributeMapping} that uses the map as the
	 * underlying attribute storage.
	 *
	 * @param  properties the map to get the properties from
	 * @return a non-null mapping that provides type-safe access to
	 *         an underlying map
	 */
	public static AMImageAttributeMapping fromProperties(
		Map properties) {

		if (properties == null) {
			throw new IllegalArgumentException("Properties map is null");
		}

		return new AMImageAttributeMapping(
			HashMapBuilder., Optional>put(
				AMAttribute.getConfigurationUuidAMAttribute(),
				_getValueOptional(
					properties, AMAttribute.getConfigurationUuidAMAttribute())
			).put(
				AMAttribute.getContentLengthAMAttribute(),
				_getValueOptional(
					properties, AMAttribute.getContentLengthAMAttribute())
			).put(
				AMAttribute.getContentTypeAMAttribute(),
				_getValueOptional(
					properties, AMAttribute.getContentTypeAMAttribute())
			).put(
				AMAttribute.getFileNameAMAttribute(),
				_getValueOptional(
					properties, AMAttribute.getFileNameAMAttribute())
			).put(
				AMImageAttribute.AM_IMAGE_ATTRIBUTE_HEIGHT,
				_getValueOptional(
					properties, AMImageAttribute.AM_IMAGE_ATTRIBUTE_HEIGHT)
			).put(
				AMImageAttribute.AM_IMAGE_ATTRIBUTE_WIDTH,
				_getValueOptional(
					properties, AMImageAttribute.AM_IMAGE_ATTRIBUTE_WIDTH)
			).build());
	}

	/**
	 * Returns an {@link Optional} instance that contains the value of the
	 * attribute (if any) in this mapping.
	 *
	 * @param  amAttribute a non null attribute
	 * @return a non-null optional that contains the
	 *         (non-null) value (if any)
	 */
	public  Optional getValueOptional(
		AMAttribute amAttribute) {

		if (amAttribute == null) {
			throw new IllegalArgumentException(
				"Adaptive media attribute is null");
		}

		return (Optional)_optionals.get(amAttribute);
	}

	protected AMImageAttributeMapping(
		Map, Optional> optionals) {

		_optionals = optionals;
	}

	private static  Optional _getValueOptional(
		Map properties,
		AMAttribute amAttribute) {

		String value = properties.get(amAttribute.getName());

		if (value == null) {
			return Optional.empty();
		}

		return Optional.of(amAttribute.convert(value));
	}

	private final Map, Optional> _optionals;

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy