2012-10-04 11:44:31 -02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
2013-03-21 00:05:56 +06:00
|
|
|
|
using System.Reflection;
|
2012-10-04 13:05:31 -02:00
|
|
|
|
using System.Runtime.Serialization;
|
2012-10-11 13:11:17 -02:00
|
|
|
|
using Umbraco.Core.Models.EntityBase;
|
2012-10-04 11:44:31 -02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Models
|
|
|
|
|
|
{
|
2012-10-04 13:05:31 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents an abstract file which provides basic functionality for a File with an Alias and Name
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Serializable]
|
|
|
|
|
|
[DataContract(IsReference = true)]
|
2012-10-11 13:11:17 -02:00
|
|
|
|
public abstract class File : Entity, IFile
|
2012-10-04 11:44:31 -02:00
|
|
|
|
{
|
2013-03-21 00:05:56 +06:00
|
|
|
|
private string _path;
|
2013-03-21 08:38:18 +06:00
|
|
|
|
private string _content = string.Empty; //initialize to empty string, not null
|
2013-03-21 00:05:56 +06:00
|
|
|
|
|
2012-10-04 11:44:31 -02:00
|
|
|
|
protected File(string path)
|
|
|
|
|
|
{
|
2013-03-21 00:05:56 +06:00
|
|
|
|
_path = path;
|
2012-10-04 11:44:31 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-03-21 00:05:56 +06:00
|
|
|
|
private static readonly PropertyInfo ContentSelector = ExpressionHelper.GetPropertyInfo<File, string>(x => x.Content);
|
|
|
|
|
|
private static readonly PropertyInfo PathSelector = ExpressionHelper.GetPropertyInfo<File, string>(x => x.Path);
|
|
|
|
|
|
|
2012-10-04 11:44:31 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the Name of the File including extension
|
|
|
|
|
|
/// </summary>
|
2012-10-04 13:05:31 -02:00
|
|
|
|
[DataMember]
|
2012-10-04 11:44:31 -02:00
|
|
|
|
public virtual string Name
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2012-10-11 13:11:17 -02:00
|
|
|
|
return new FileInfo(Path).Name;
|
2012-10-04 11:44:31 -02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the Alias of the File, which is the name without the extension
|
|
|
|
|
|
/// </summary>
|
2012-10-04 13:05:31 -02:00
|
|
|
|
[DataMember]
|
2012-10-04 11:44:31 -02:00
|
|
|
|
public virtual string Alias
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
var fileInfo = new FileInfo(Path);
|
|
|
|
|
|
var name = fileInfo.Name;
|
2012-10-11 13:11:17 -02:00
|
|
|
|
int lastIndexOf = name.LastIndexOf(".", StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
|
return name.Substring(0, lastIndexOf);
|
2012-10-04 11:44:31 -02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the Path to the File from the root of the site
|
|
|
|
|
|
/// </summary>
|
2012-10-04 13:05:31 -02:00
|
|
|
|
[DataMember]
|
2013-03-21 00:05:56 +06:00
|
|
|
|
public virtual string Path
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return _path; }
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
SetPropertyValueAndDetectChanges(o =>
|
|
|
|
|
|
{
|
|
|
|
|
|
_path = value;
|
|
|
|
|
|
return _path;
|
|
|
|
|
|
}, _path, PathSelector);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-10-04 11:44:31 -02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the Content of a File
|
|
|
|
|
|
/// </summary>
|
2012-10-04 13:05:31 -02:00
|
|
|
|
[DataMember]
|
2013-03-21 00:05:56 +06:00
|
|
|
|
public virtual string Content
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return _content; }
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
SetPropertyValueAndDetectChanges(o =>
|
|
|
|
|
|
{
|
|
|
|
|
|
_content = value;
|
|
|
|
|
|
return _content;
|
|
|
|
|
|
}, _content, ContentSelector);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-10-04 11:44:31 -02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Boolean indicating whether the file could be validated
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>True if file is valid, otherwise false</returns>
|
|
|
|
|
|
public abstract bool IsValid();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|