using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Umbraco.Core.Events;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Querying;
using Umbraco.Core.Persistence.Repositories;
using Umbraco.Core.Persistence.UnitOfWork;
namespace Umbraco.Core.Services
{
///
/// Represents the File Service, which is an easy access to operations involving objects like Scripts, Stylesheets and Templates
///
public class FileService : ScopeRepositoryService, IFileService
{
private const string PartialViewHeader = "@inherits Umbraco.Web.Mvc.UmbracoTemplatePage";
private const string PartialViewMacroHeader = "@inherits Umbraco.Web.Macros.PartialViewMacroPage";
public FileService(
IDatabaseUnitOfWorkProvider provider,
RepositoryFactory repositoryFactory,
ILogger logger,
IEventMessagesFactory eventMessagesFactory)
: base(provider, repositoryFactory, logger, eventMessagesFactory)
{ }
#region Stylesheets
///
/// Gets a list of all objects
///
/// An enumerable list of objects
public IEnumerable GetStylesheets(params string[] names)
{
using (var uow = UowProvider.GetUnitOfWork(readOnly: true))
{
var repository = RepositoryFactory.CreateStylesheetRepository(uow);
return repository.GetAll(names);
}
}
///
/// Gets a object by its name
///
/// Name of the stylesheet incl. extension
/// A object
public Stylesheet GetStylesheetByName(string name)
{
using (var uow = UowProvider.GetUnitOfWork(readOnly: true))
{
var repository = RepositoryFactory.CreateStylesheetRepository(uow);
return repository.Get(name);
}
}
///
/// Saves a
///
/// to save
///
public void SaveStylesheet(Stylesheet stylesheet, int userId = 0)
{
using (var uow = UowProvider.GetUnitOfWork())
{
if (uow.Events.DispatchCancelable(SavingStylesheet, this, new SaveEventArgs(stylesheet)))
{
uow.Commit();
return;
}
var repository = RepositoryFactory.CreateStylesheetRepository(uow);
repository.AddOrUpdate(stylesheet);
uow.Events.Dispatch(SavedStylesheet, this, new SaveEventArgs(stylesheet, false));
Audit(uow, AuditType.Save, "Save Stylesheet performed by user", userId, -1);
uow.Commit();
}
}
///
/// Deletes a stylesheet by its name
///
/// Name incl. extension of the Stylesheet to delete
///
public void DeleteStylesheet(string path, int userId = 0)
{
using (var uow = UowProvider.GetUnitOfWork())
{
var repository = RepositoryFactory.CreateStylesheetRepository(uow);
var stylesheet = repository.Get(path);
if (stylesheet == null)
{
uow.Commit();
return;
}
if (uow.Events.DispatchCancelable(DeletingStylesheet, this, new DeleteEventArgs(stylesheet)))
{
uow.Commit();
return;
}
repository.Delete(stylesheet);
uow.Events.Dispatch(DeletedStylesheet, this, new DeleteEventArgs(stylesheet, false));
Audit(uow, AuditType.Delete, string.Format("Delete Stylesheet performed by user"), userId, -1);
uow.Commit();
}
}
///
/// Validates a
///
/// to validate
/// True if Stylesheet is valid, otherwise false
public bool ValidateStylesheet(Stylesheet stylesheet)
{
using (var uow = UowProvider.GetUnitOfWork(readOnly: true))
{
var repository = RepositoryFactory.CreateStylesheetRepository(uow);
return repository.ValidateStylesheet(stylesheet);
}
}
#endregion
#region Scripts
///
/// Gets a list of all objects
///
/// An enumerable list of objects
public IEnumerable