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.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(IScopeUnitOfWorkProvider uowProvider, ILogger logger, IEventMessagesFactory eventMessagesFactory)
: base(uowProvider, 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.CreateUnitOfWork(readOnly: true))
{
var repository = uow.CreateRepository();
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.CreateUnitOfWork(readOnly: true))
{
var repository = uow.CreateRepository();
return repository.Get(name);
}
}
///
/// Saves a
///
/// to save
///
public void SaveStylesheet(Stylesheet stylesheet, int userId = 0)
{
using (var uow = UowProvider.CreateUnitOfWork())
{
var saveEventArgs = new SaveEventArgs(stylesheet);
if (uow.Events.DispatchCancelable(SavingStylesheet, this, saveEventArgs))
{
uow.Complete();
return;
}
var repository = uow.CreateRepository();
repository.AddOrUpdate(stylesheet);
saveEventArgs.CanCancel = false;
uow.Events.Dispatch(SavedStylesheet, this, saveEventArgs);
Audit(uow, AuditType.Save, "Save Stylesheet performed by user", userId, -1);
uow.Complete();
}
}
///
/// 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.CreateUnitOfWork())
{
var repository = uow.CreateRepository();
var stylesheet = repository.Get(path);
if (stylesheet == null)
{
uow.Complete();
return;
}
var deleteEventArgs = new DeleteEventArgs(stylesheet);
if (uow.Events.DispatchCancelable(DeletingStylesheet, this, deleteEventArgs))
{
uow.Complete();
return; // causes rollback
}
repository.Delete(stylesheet);
deleteEventArgs.CanCancel = false;
uow.Events.Dispatch(DeletedStylesheet, this, deleteEventArgs);
Audit(uow, AuditType.Delete, "Delete Stylesheet performed by user", userId, -1);
uow.Complete();
}
}
///
/// Validates a
///
/// to validate
/// True if Stylesheet is valid, otherwise false
public bool ValidateStylesheet(Stylesheet stylesheet)
{
using (var uow = UowProvider.CreateUnitOfWork(readOnly: true))
{
var repository = uow.CreateRepository();
return repository.ValidateStylesheet(stylesheet);
}
}
public Stream GetStylesheetFileContentStream(string filepath)
{
using (var uow = UowProvider.CreateUnitOfWork(readOnly: true))
{
var repository = uow.CreateRepository();
return repository.GetFileContentStream(filepath);
}
}
public void SetStylesheetFileContent(string filepath, Stream content)
{
using (var uow = UowProvider.CreateUnitOfWork())
{
var repository = uow.CreateRepository();
repository.SetFileContent(filepath, content);
uow.Complete();
}
}
public long GetStylesheetFileSize(string filepath)
{
using (var uow = UowProvider.CreateUnitOfWork(readOnly: true))
{
var repository = uow.CreateRepository();
return repository.GetFileSize(filepath);
}
}
#endregion
#region Scripts
///
/// Gets a list of all objects
///
/// An enumerable list of objects
public IEnumerable