using System.Runtime.Serialization;
namespace Umbraco.Cms.Core.Models;
///
/// Represents a Media object
///
[Serializable]
[DataContract(IsReference = true)]
public class Media : ContentBase, IMedia
{
///
/// Constructor for creating a Media object
///
/// name of the Media object
/// Parent object
/// MediaType for the current Media object
public Media(string? name, IMedia? parent, IMediaType mediaType)
: this(name, parent, mediaType, new PropertyCollection())
{
}
///
/// Constructor for creating a Media object
///
/// name of the Media object
/// Parent object
/// MediaType for the current Media object
/// Collection of properties
public Media(string? name, IMedia? parent, IMediaType mediaType, IPropertyCollection properties)
: base(name, parent, mediaType, properties)
{
}
///
/// Constructor for creating a Media object
///
/// name of the Media object
/// Id of the Parent IMedia
/// MediaType for the current Media object
public Media(string? name, int parentId, IMediaType? mediaType)
: this(name, parentId, mediaType, new PropertyCollection())
{
}
///
/// Constructor for creating a Media object
///
/// Name of the Media object
/// Id of the Parent IMedia
/// MediaType for the current Media object
/// Collection of properties
public Media(string? name, int parentId, IMediaType? mediaType, IPropertyCollection properties)
: base(name, parentId, mediaType, properties)
{
}
///
/// Changes the for the current Media object
///
/// New MediaType for this Media
/// Leaves PropertyTypes intact after change
internal void ChangeContentType(IMediaType mediaType) => ChangeContentType(mediaType, false);
///
/// Changes the for the current Media object and removes PropertyTypes,
/// which are not part of the new MediaType.
///
/// New MediaType for this Media
/// Boolean indicating whether to clear PropertyTypes upon change
internal void ChangeContentType(IMediaType mediaType, bool clearProperties)
{
ChangeContentType(new SimpleContentType(mediaType));
if (clearProperties)
{
Properties.EnsureCleanPropertyTypes(mediaType.CompositionPropertyTypes);
}
else
{
Properties.EnsurePropertyTypes(mediaType.CompositionPropertyTypes);
}
Properties.ClearCollectionChangedEvents(); // be sure not to double add
Properties.CollectionChanged += PropertiesChanged;
}
}