Beispiel #1
0
        /// <inheritdoc />
        public override object?ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object?source, bool preview)
        {
            if (source == null)
            {
                return(null);
            }
            var sourceString = source.ToString() !;

            ImageCropperValue?value;

            try
            {
                value = JsonConvert.DeserializeObject <ImageCropperValue>(sourceString, ImageCropperValueJsonSerializerSettings);
            }
            catch (Exception ex)
            {
                // cannot deserialize, assume it may be a raw image URL
                _logger.LogError(ex, "Could not deserialize string '{JsonString}' into an image cropper value.", sourceString);
                value = new ImageCropperValue {
                    Src = sourceString
                };
            }

            value?.ApplyConfiguration(propertyType.DataType.ConfigurationAs <ImageCropperConfiguration>() !);

            return(value);
        }
        public override object?ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object?inter, bool preview)
        {
            var isMultiple = IsMultipleDataType(propertyType.DataType);

            if (string.IsNullOrEmpty(inter?.ToString()))
            {
                // Short-circuit on empty value
                return(isMultiple ? Enumerable.Empty <MediaWithCrops>() : null);
            }

            var mediaItems        = new List <MediaWithCrops>();
            var dtos              = MediaPicker3PropertyEditor.MediaPicker3PropertyValueEditor.Deserialize(_jsonSerializer, inter);
            var configuration     = propertyType.DataType.ConfigurationAs <MediaPicker3Configuration>();
            var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();

            foreach (var dto in dtos)
            {
                var mediaItem = publishedSnapshot.Media?.GetById(preview, dto.MediaKey);
                if (mediaItem != null)
                {
                    var localCrops = new ImageCropperValue
                    {
                        Crops      = dto.Crops,
                        FocalPoint = dto.FocalPoint,
                        Src        = mediaItem.Url(_publishedUrlProvider)
                    };

                    localCrops.ApplyConfiguration(configuration);

                    // TODO: This should be optimized/cached, as calling Activator.CreateInstance is slow
                    var mediaWithCropsType = typeof(MediaWithCrops <>).MakeGenericType(mediaItem.GetType());
                    var mediaWithCrops     = (MediaWithCrops)Activator.CreateInstance(mediaWithCropsType, mediaItem, _publishedValueFallback, localCrops) !;

                    mediaItems.Add(mediaWithCrops);

                    if (!isMultiple)
                    {
                        // Short-circuit on single item
                        break;
                    }
                }
            }

            return(isMultiple ? mediaItems : mediaItems.FirstOrDefault());
        }