using System; using System.Collections.Generic; using System.IO; using System.Runtime.Remoting.Messaging; using System.Text; using System.Web; using Umbraco.Core.Auditing; using Umbraco.Core.Events; using Umbraco.Core.IO; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.Persistence; 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 : IFileService { private readonly RepositoryFactory _repositoryFactory; private readonly IUnitOfWorkProvider _fileUowProvider; private readonly IDatabaseUnitOfWorkProvider _dataUowProvider; private readonly IMacroService _macroService; public FileService() : this(new RepositoryFactory()) { } public FileService(RepositoryFactory repositoryFactory) : this(new FileUnitOfWorkProvider(), new PetaPocoUnitOfWorkProvider(), repositoryFactory, new MacroService()) { } public FileService(IUnitOfWorkProvider fileProvider, IDatabaseUnitOfWorkProvider dataProvider, RepositoryFactory repositoryFactory, IMacroService macroService) { _repositoryFactory = repositoryFactory; _macroService = macroService; _fileUowProvider = fileProvider; _dataUowProvider = dataProvider; } /// /// Gets a list of all objects /// /// An enumerable list of objects public IEnumerable GetStylesheets(params string[] names) { using (var repository = _repositoryFactory.CreateStylesheetRepository(_fileUowProvider.GetUnitOfWork(), _dataUowProvider.GetUnitOfWork())) { 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 repository = _repositoryFactory.CreateStylesheetRepository(_fileUowProvider.GetUnitOfWork(), _dataUowProvider.GetUnitOfWork())) { return repository.Get(name); } } /// /// Saves a /// /// to save /// public void SaveStylesheet(Stylesheet stylesheet, int userId = 0) { if (SavingStylesheet.IsRaisedEventCancelled(new SaveEventArgs(stylesheet), this)) return; var uow = _fileUowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateStylesheetRepository(uow, _dataUowProvider.GetUnitOfWork())) { repository.AddOrUpdate(stylesheet); uow.Commit(); SavedStylesheet.RaiseEvent(new SaveEventArgs(stylesheet, false), this); } Audit.Add(AuditTypes.Save, string.Format("Save Stylesheet performed by user"), userId, -1); } /// /// Deletes a stylesheet by its name /// /// Name incl. extension of the Stylesheet to delete /// public void DeleteStylesheet(string name, int userId = 0) { var uow = _fileUowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateStylesheetRepository(uow, _dataUowProvider.GetUnitOfWork())) { var stylesheet = repository.Get(name); if (DeletingStylesheet.IsRaisedEventCancelled(new DeleteEventArgs(stylesheet), this)) return; repository.Delete(stylesheet); uow.Commit(); DeletedStylesheet.RaiseEvent(new DeleteEventArgs(stylesheet, false), this); Audit.Add(AuditTypes.Delete, string.Format("Delete Stylesheet performed by user"), userId, -1); } } /// /// Validates a /// /// to validate /// True if Stylesheet is valid, otherwise false public bool ValidateStylesheet(Stylesheet stylesheet) { return stylesheet.IsValid() && stylesheet.IsFileValidCss(); } /// /// Gets a list of all objects /// /// An enumerable list of objects public IEnumerable