using System; using System.Collections.Generic; using Umbraco.Core.Auditing; 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 IUnitOfWork _fileUnitOfWork; private readonly IDatabaseUnitOfWork _dataUnitOfWork; private readonly IStylesheetRepository _stylesheetRepository; private readonly IScriptRepository _scriptRepository; private readonly ITemplateRepository _templateRepository; public FileService(RepositoryFactory repositoryFactory) : this(new FileUnitOfWorkProvider(), new PetaPocoUnitOfWorkProvider(), repositoryFactory) { } public FileService(IUnitOfWorkProvider fileProvider, IDatabaseUnitOfWorkProvider dataProvider, RepositoryFactory repositoryFactory) { _repositoryFactory = repositoryFactory; _fileUnitOfWork = fileProvider.GetUnitOfWork(); _dataUnitOfWork = dataProvider.GetUnitOfWork(); _templateRepository = _repositoryFactory.CreateTemplateRepository(_dataUnitOfWork); _stylesheetRepository = _repositoryFactory.CreateStylesheetRepository(_fileUnitOfWork); _scriptRepository = _repositoryFactory.CreateScriptRepository(_fileUnitOfWork); } /// /// Gets a list of all objects /// /// An enumerable list of objects public IEnumerable GetStylesheets(params string[] names) { var repository = _stylesheetRepository; return repository.GetAll(names); } /// /// Gets a object by its name /// /// Name of the stylesheet incl. extension /// A object public Stylesheet GetStylesheetByName(string name) { var repository = _stylesheetRepository; return repository.Get(name); } /// /// Saves a /// /// to save /// public void SaveStylesheet(Stylesheet stylesheet, int userId = -1) { var e = new SaveEventArgs(); if (Saving != null) Saving(stylesheet, e); if (!e.Cancel) { _stylesheetRepository.AddOrUpdate(stylesheet); _fileUnitOfWork.Commit(); if (Saved != null) Saved(stylesheet, e); Audit.Add(AuditTypes.Save, string.Format("Save Stylesheet performed by user"), userId == -1 ? 0 : userId, -1); } } /// /// Deletes a stylesheet by its name /// /// Name incl. extension of the Stylesheet to delete /// public void DeleteStylesheet(string name, int userId = -1) { var stylesheet = _stylesheetRepository.Get(name); var e = new DeleteEventArgs(); if (Deleting != null) Deleting(stylesheet, e); if (!e.Cancel) { _stylesheetRepository.Delete(stylesheet); _fileUnitOfWork.Commit(); if (Deleted != null) Deleted(stylesheet, e); Audit.Add(AuditTypes.Delete, string.Format("Delete Stylesheet performed by user"), userId == -1 ? 0 : 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