2012-11-07 13:56:52 -01:00
|
|
|
using System.Collections.Generic;
|
2012-10-24 11:29:51 -02:00
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
using Umbraco.Core.Persistence;
|
|
|
|
|
using Umbraco.Core.Persistence.Repositories;
|
|
|
|
|
using Umbraco.Core.Persistence.UnitOfWork;
|
2012-11-06 10:47:14 -01:00
|
|
|
using Umbraco.Core.Services;
|
2012-10-24 11:29:51 -02:00
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Services
|
|
|
|
|
{
|
2012-10-30 09:25:28 -01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Represents the File Service, which is an easy access to operations involving <see cref="IFile"/> objects like Scripts, Stylesheets and Templates
|
|
|
|
|
/// </summary>
|
2012-10-24 11:29:51 -02:00
|
|
|
public class FileService : IFileService
|
|
|
|
|
{
|
2012-11-05 10:12:17 -01:00
|
|
|
private readonly IUnitOfWork _unitOfWork;
|
2012-10-24 11:29:51 -02:00
|
|
|
|
2012-10-30 09:25:28 -01:00
|
|
|
public FileService() : this(new FileUnitOfWorkProvider())
|
2012-10-24 11:29:51 -02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public FileService(IUnitOfWorkProvider provider)
|
|
|
|
|
{
|
2012-11-05 10:12:17 -01:00
|
|
|
_unitOfWork = provider.GetUnitOfWork();
|
2012-10-24 11:29:51 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a list of all <see cref="Stylesheet"/> objects
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>An enumerable list of <see cref="Stylesheet"/> objects</returns>
|
|
|
|
|
public IEnumerable<Stylesheet> GetStylesheets(params string[] names)
|
|
|
|
|
{
|
2012-11-05 10:12:17 -01:00
|
|
|
var repository = RepositoryResolver.ResolveByType<IStylesheetRepository, Stylesheet, string>(_unitOfWork);
|
2012-10-24 11:29:51 -02:00
|
|
|
return repository.GetAll(names);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a <see cref="Stylesheet"/> object by its name
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Name of the stylesheet incl. extension</param>
|
|
|
|
|
/// <returns>A <see cref="Stylesheet"/> object</returns>
|
|
|
|
|
public Stylesheet GetStylesheetByName(string name)
|
|
|
|
|
{
|
2012-11-05 10:12:17 -01:00
|
|
|
var repository = RepositoryResolver.ResolveByType<IStylesheetRepository, Stylesheet, string>(_unitOfWork);
|
2012-10-24 11:29:51 -02:00
|
|
|
return repository.Get(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves a <see cref="Stylesheet"/>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="stylesheet"><see cref="Stylesheet"/> to save</param>
|
|
|
|
|
public void SaveStylesheet(Stylesheet stylesheet)
|
|
|
|
|
{
|
2012-11-05 10:12:17 -01:00
|
|
|
var repository = RepositoryResolver.ResolveByType<IStylesheetRepository, Stylesheet, string>(_unitOfWork);
|
2012-10-24 11:29:51 -02:00
|
|
|
repository.AddOrUpdate(stylesheet);
|
2012-11-05 10:12:17 -01:00
|
|
|
_unitOfWork.Commit();
|
2012-10-24 11:29:51 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes a stylesheet by its name
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Name incl. extension of the Stylesheet to delete</param>
|
|
|
|
|
public void DeleteStylesheet(string name)
|
|
|
|
|
{
|
2012-11-05 10:12:17 -01:00
|
|
|
var repository = RepositoryResolver.ResolveByType<IStylesheetRepository, Stylesheet, string>(_unitOfWork);
|
2012-10-24 11:29:51 -02:00
|
|
|
var stylesheet = repository.Get(name);
|
|
|
|
|
repository.Delete(stylesheet);
|
2012-11-05 10:12:17 -01:00
|
|
|
_unitOfWork.Commit();
|
2012-10-24 11:29:51 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Validates a <see cref="Stylesheet"/>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="stylesheet"><see cref="Stylesheet"/> to validate</param>
|
|
|
|
|
/// <returns>True if Stylesheet is valid, otherwise false</returns>
|
|
|
|
|
public bool ValidateStylesheet(Stylesheet stylesheet)
|
|
|
|
|
{
|
|
|
|
|
return stylesheet.IsValid() && stylesheet.IsFileValidCss();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a list of all <see cref="Script"/> objects
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>An enumerable list of <see cref="Script"/> objects</returns>
|
|
|
|
|
public IEnumerable<Script> GetScripts(params string[] names)
|
|
|
|
|
{
|
2012-11-05 10:12:17 -01:00
|
|
|
var repository = RepositoryResolver.ResolveByType<IScriptRepository, Script, string>(_unitOfWork);
|
2012-10-24 11:29:51 -02:00
|
|
|
return repository.GetAll(names);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a <see cref="Script"/> object by its name
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Name of the script incl. extension</param>
|
|
|
|
|
/// <returns>A <see cref="Script"/> object</returns>
|
|
|
|
|
public Script GetScriptByName(string name)
|
|
|
|
|
{
|
2012-11-05 10:12:17 -01:00
|
|
|
var repository = RepositoryResolver.ResolveByType<IScriptRepository, Script, string>(_unitOfWork);
|
2012-10-24 11:29:51 -02:00
|
|
|
return repository.Get(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves a <see cref="Script"/>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="script"><see cref="Script"/> to save</param>
|
|
|
|
|
public void SaveScript(Script script)
|
|
|
|
|
{
|
2012-11-05 10:12:17 -01:00
|
|
|
var repository = RepositoryResolver.ResolveByType<IScriptRepository, Script, string>(_unitOfWork);
|
2012-10-24 11:29:51 -02:00
|
|
|
repository.AddOrUpdate(script);
|
2012-11-05 10:12:17 -01:00
|
|
|
_unitOfWork.Commit();
|
2012-10-24 11:29:51 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes a script by its name
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Name incl. extension of the Script to delete</param>
|
|
|
|
|
public void DeleteScript(string name)
|
|
|
|
|
{
|
2012-11-05 10:12:17 -01:00
|
|
|
var repository = RepositoryResolver.ResolveByType<IScriptRepository, Script, string>(_unitOfWork);
|
2012-10-24 11:29:51 -02:00
|
|
|
var script = repository.Get(name);
|
|
|
|
|
repository.Delete(script);
|
2012-11-05 10:12:17 -01:00
|
|
|
_unitOfWork.Commit();
|
2012-10-24 11:29:51 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Validates a <see cref="Script"/>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="script"><see cref="Script"/> to validate</param>
|
|
|
|
|
/// <returns>True if Script is valid, otherwise false</returns>
|
|
|
|
|
public bool ValidateScript(Script script)
|
|
|
|
|
{
|
|
|
|
|
return script.IsValid();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a list of all <see cref="Template"/> objects
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>An enumerable list of <see cref="Template"/> objects</returns>
|
|
|
|
|
public IEnumerable<Template> GetTemplates(params string[] aliases)
|
|
|
|
|
{
|
2012-11-05 10:12:17 -01:00
|
|
|
var repository = RepositoryResolver.ResolveByType<ITemplateRepository, Template, string>(_unitOfWork);
|
2012-10-24 11:29:51 -02:00
|
|
|
return repository.GetAll(aliases);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a <see cref="Template"/> object by its alias
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="alias">Alias of the template</param>
|
|
|
|
|
/// <returns>A <see cref="Template"/> object</returns>
|
|
|
|
|
public Template GetTemplateByAlias(string alias)
|
|
|
|
|
{
|
2012-11-05 10:12:17 -01:00
|
|
|
var repository = RepositoryResolver.ResolveByType<ITemplateRepository, Template, string>(_unitOfWork);
|
2012-10-24 11:29:51 -02:00
|
|
|
return repository.Get(alias);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves a <see cref="Template"/>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="template"><see cref="Template"/> to save</param>
|
|
|
|
|
public void SaveTemplate(Template template)
|
|
|
|
|
{
|
2012-11-05 10:12:17 -01:00
|
|
|
var repository = RepositoryResolver.ResolveByType<ITemplateRepository, Template, string>(_unitOfWork);
|
2012-10-24 11:29:51 -02:00
|
|
|
repository.AddOrUpdate(template);
|
2012-11-05 10:12:17 -01:00
|
|
|
_unitOfWork.Commit();
|
2012-10-24 11:29:51 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes a template by its alias
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="alias">Alias of the <see cref="Template"/> to delete</param>
|
|
|
|
|
public void DeleteTemplate(string alias)
|
|
|
|
|
{
|
2012-11-05 10:12:17 -01:00
|
|
|
var repository = RepositoryResolver.ResolveByType<ITemplateRepository, Template, string>(_unitOfWork);
|
2012-10-24 11:29:51 -02:00
|
|
|
var template = repository.Get(alias);
|
|
|
|
|
repository.Delete(template);
|
2012-11-05 10:12:17 -01:00
|
|
|
_unitOfWork.Commit();
|
2012-10-24 11:29:51 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Validates a <see cref="Template"/>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="template"><see cref="Template"/> to validate</param>
|
|
|
|
|
/// <returns>True if Script is valid, otherwise false</returns>
|
|
|
|
|
public bool ValidateTemplate(Template template)
|
|
|
|
|
{
|
|
|
|
|
return template.IsValid();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|