Exemple #1
0
        private static void ResetProperties(IContentBase content, ImagingAutoFillUploadField autoFillConfig, string?culture, string?segment)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }
            if (autoFillConfig == null)
            {
                throw new ArgumentNullException(nameof(autoFillConfig));
            }

            if (content.Properties.Contains(autoFillConfig.WidthFieldAlias))
            {
                content.Properties[autoFillConfig.WidthFieldAlias]?.SetValue(string.Empty, culture, segment);
            }

            if (content.Properties.Contains(autoFillConfig.HeightFieldAlias))
            {
                content.Properties[autoFillConfig.HeightFieldAlias]?.SetValue(string.Empty, culture, segment);
            }

            if (content.Properties.Contains(autoFillConfig.LengthFieldAlias))
            {
                content.Properties[autoFillConfig.LengthFieldAlias]?.SetValue(string.Empty, culture, segment);
            }

            if (content.Properties.Contains(autoFillConfig.ExtensionFieldAlias))
            {
                content.Properties[autoFillConfig.ExtensionFieldAlias]?.SetValue(string.Empty, culture, segment);
            }
        }
Exemple #2
0
        private static void SetProperties(IContentBase content, ImagingAutoFillUploadField autoFillConfig, Size?size, long?length, string extension, string?culture, string?segment)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }
            if (autoFillConfig == null)
            {
                throw new ArgumentNullException(nameof(autoFillConfig));
            }

            if (!string.IsNullOrWhiteSpace(autoFillConfig.WidthFieldAlias) && content.Properties.Contains(autoFillConfig.WidthFieldAlias))
            {
                content.Properties[autoFillConfig.WidthFieldAlias] !.SetValue(size.HasValue ? size.Value.Width.ToInvariantString() : string.Empty, culture, segment);
            }

            if (!string.IsNullOrWhiteSpace(autoFillConfig.HeightFieldAlias) && content.Properties.Contains(autoFillConfig.HeightFieldAlias))
            {
                content.Properties[autoFillConfig.HeightFieldAlias] !.SetValue(size.HasValue ? size.Value.Height.ToInvariantString() : string.Empty, culture, segment);
            }

            if (!string.IsNullOrWhiteSpace(autoFillConfig.LengthFieldAlias) && content.Properties.Contains(autoFillConfig.LengthFieldAlias))
            {
                content.Properties[autoFillConfig.LengthFieldAlias] !.SetValue(length, culture, segment);
            }

            if (!string.IsNullOrWhiteSpace(autoFillConfig.ExtensionFieldAlias) && content.Properties.Contains(autoFillConfig.ExtensionFieldAlias))
            {
                content.Properties[autoFillConfig.ExtensionFieldAlias] !.SetValue(extension, culture, segment);
            }
        }
Exemple #3
0
        private void SetProperties(IContentBase content, ImagingAutoFillUploadField autoFillConfig, string filepath, Stream?filestream, string?culture, string?segment)
        {
            var extension = (Path.GetExtension(filepath) ?? string.Empty).TrimStart(Constants.CharArrays.Period);

            var size = _imageUrlGenerator.IsSupportedImageFormat(extension)
                ? _imageDimensionExtractor.GetDimensions(filestream) ?? (Size?)new Size(Constants.Conventions.Media.DefaultSize, Constants.Conventions.Media.DefaultSize)
                : null;

            SetProperties(content, autoFillConfig, size, filestream?.Length, extension, culture, segment);
        }
Exemple #4
0
        /// <summary>
        /// Resets the auto-fill properties of a content item, for a specified auto-fill configuration.
        /// </summary>
        /// <param name="content">The content item.</param>
        /// <param name="autoFillConfig">The auto-fill configuration.</param>
        /// <param name="culture">Variation language.</param>
        /// <param name="segment">Variation segment.</param>
        public void Reset(IContentBase content, ImagingAutoFillUploadField autoFillConfig, string?culture, string?segment)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }
            if (autoFillConfig == null)
            {
                throw new ArgumentNullException(nameof(autoFillConfig));
            }

            ResetProperties(content, autoFillConfig, culture, segment);
        }
Exemple #5
0
        /// <summary>
        /// Populates the auto-fill properties of a content item.
        /// </summary>
        /// <param name="content">The content item.</param>
        /// <param name="autoFillConfig"></param>
        /// <param name="filepath">The filesystem-relative filepath, or null to clear properties.</param>
        /// <param name="filestream">The stream containing the file data.</param>
        /// <param name="culture">Variation language.</param>
        /// <param name="segment">Variation segment.</param>
        public void Populate(IContentBase content, ImagingAutoFillUploadField autoFillConfig, string filepath, Stream filestream, string culture, string segment)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }
            if (autoFillConfig == null)
            {
                throw new ArgumentNullException(nameof(autoFillConfig));
            }

            // no file = reset, file = auto-fill
            if (filepath.IsNullOrWhiteSpace() || filestream == null)
            {
                ResetProperties(content, autoFillConfig, culture, segment);
            }
            else
            {
                SetProperties(content, autoFillConfig, filepath, filestream, culture, segment);
            }
        }
    /// <summary>
    ///     Populates the auto-fill properties of a content item, for a specified auto-fill configuration.
    /// </summary>
    /// <param name="content">The content item.</param>
    /// <param name="autoFillConfig">The auto-fill configuration.</param>
    /// <param name="filepath">The filesystem path to the uploaded file.</param>
    /// <remarks>The <paramref name="filepath" /> parameter is the path relative to the filesystem.</remarks>
    /// <param name="culture">Variation language.</param>
    /// <param name="segment">Variation segment.</param>
    public void Populate(IContentBase content, ImagingAutoFillUploadField autoFillConfig, string filepath, string?culture, string?segment)
    {
        if (content == null)
        {
            throw new ArgumentNullException(nameof(content));
        }

        if (autoFillConfig == null)
        {
            throw new ArgumentNullException(nameof(autoFillConfig));
        }

        // no file = reset, file = auto-fill
        if (filepath.IsNullOrWhiteSpace())
        {
            ResetProperties(content, autoFillConfig, culture, segment);
        }
        else
        {
            // it might not exist if the media item has been created programatically but doesn't have a file persisted yet.
            if (_mediaFileManager.FileSystem.FileExists(filepath))
            {
                // if anything goes wrong, just reset the properties
                try
                {
                    using (Stream filestream = _mediaFileManager.FileSystem.OpenFile(filepath))
                    {
                        SetProperties(content, autoFillConfig, filepath, filestream, culture, segment);
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Could not populate upload auto-fill properties for file '{File}'.", filepath);
                    ResetProperties(content, autoFillConfig, culture, segment);
                }
            }
        }
    }