示例#1
0
        static void AutoFillProperties(IContentBase model)
        {
            foreach (var p in model.Properties.Where(x => x.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.ImageCropperAlias))
            {
                var uploadFieldConfigNode =
                    UmbracoConfig.For.UmbracoSettings().Content.ImageAutoFillProperties
                    .FirstOrDefault(x => x.Alias == p.Alias);

                if (uploadFieldConfigNode != null)
                {
                    if (p.Value != null)
                    {
                        JObject json = null;
                        try
                        {
                            json = JObject.Parse((string)p.Value);
                        }
                        catch (JsonException)
                        {
                            //note: we are swallowing this exception because in some cases a normal string/non json value will be passed in which will just be the
                            // file path like /media/23454/hello.jpg
                            // This will happen everytime an image is uploaded via the folder browser and we don't really want to pollute the log since it's not actually
                            // a problem and we take care of this below.
                            // see: http://issues.umbraco.org/issue/U4-4756
                        }
                        if (json != null && json["src"] != null)
                        {
                            model.PopulateFileMetaDataProperties(uploadFieldConfigNode, json["src"].Value <string>());
                        }
                        else if (p.Value is string)
                        {
                            var src    = p.Value == null ? string.Empty : p.Value.ToString();
                            var config = ApplicationContext.Current.Services.DataTypeService.GetPreValuesByDataTypeId(p.PropertyType.DataTypeDefinitionId).FirstOrDefault();
                            var crops  = string.IsNullOrEmpty(config) == false ? config : "[]";
                            p.Value = "{src: '" + p.Value + "', crops: " + crops + "}";
                            //Only provide the source path, not the whole JSON value
                            model.PopulateFileMetaDataProperties(uploadFieldConfigNode, src);
                        }
                    }
                    else
                    {
                        model.ResetFileMetaDataProperties(uploadFieldConfigNode);
                    }
                }
            }
        }