Beispiel #1
0
        /// <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>
        public void Populate(IContentBase content, IImagingAutoFillUploadField autoFillConfig, string filepath)
        {
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }
            if (autoFillConfig == null)
            {
                throw new ArgumentNullException("autoFillConfig");
            }

            // no file = reset, file = auto-fill
            if (filepath.IsNullOrWhiteSpace())
            {
                ResetProperties(content, autoFillConfig);
            }
            else
            {
                // if anything goes wrong, just reset the properties
                try
                {
                    using (var filestream = _mediaFileSystem.OpenFile(filepath))
                    {
                        var extension = (Path.GetExtension(filepath) ?? "").TrimStart('.');
                        var size      = _mediaFileSystem.IsImageFile(extension) ? (Size?)_mediaFileSystem.GetDimensions(filestream) : null;
                        SetProperties(content, autoFillConfig, size, filestream.Length, extension);
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(typeof(UploadAutoFillProperties), "Could not populate upload auto-fill properties for file \""
                                  + filepath + "\".", ex);
                    ResetProperties(content, autoFillConfig);
                }
            }
        }