From 0fbf3b2fdc785578e2cf1e3887026d66f96fb170 Mon Sep 17 00:00:00 2001 From: "Morten@Thinkpad-X220" Date: Tue, 9 Oct 2012 08:12:03 -0200 Subject: [PATCH] Adds Script repository and some edits to the Script model U4-965 Refactoring RepositoryResolver to use the base repository instead of the Queryable repo. --- src/Umbraco.Core/Models/Script.cs | 8 +- .../Repositories/ScriptRepository.cs | 100 ++++++++++++++++++ .../Persistence/RepositoryResolver.cs | 7 +- src/Umbraco.Core/Umbraco.Core.csproj | 1 + 4 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 src/Umbraco.Core/Persistence/Repositories/ScriptRepository.cs diff --git a/src/Umbraco.Core/Models/Script.cs b/src/Umbraco.Core/Models/Script.cs index 415ddd48ce..7bead00a3a 100644 --- a/src/Umbraco.Core/Models/Script.cs +++ b/src/Umbraco.Core/Models/Script.cs @@ -34,15 +34,15 @@ namespace Umbraco.Core.Models //See codeEditorSave.asmx.cs for reference. var exts = UmbracoSettings.ScriptFileTypes.Split(',').ToList(); - if (UmbracoSettings.DefaultRenderingEngine == RenderingEngine.Mvc) + /*if (UmbracoSettings.DefaultRenderingEngine == RenderingEngine.Mvc) { exts.Add("cshtml"); exts.Add("vbhtml"); - } + }*/ var dirs = SystemDirectories.Scripts; - if (UmbracoSettings.DefaultRenderingEngine == RenderingEngine.Mvc) - dirs += "," + SystemDirectories.MvcViews; + /*if (UmbracoSettings.DefaultRenderingEngine == RenderingEngine.Mvc) + dirs += "," + SystemDirectories.MvcViews;*/ //Validate file var validFile = IOHelper.ValidateEditPath(Path, dirs.Split(',')); diff --git a/src/Umbraco.Core/Persistence/Repositories/ScriptRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ScriptRepository.cs new file mode 100644 index 0000000000..b4a23d9d0f --- /dev/null +++ b/src/Umbraco.Core/Persistence/Repositories/ScriptRepository.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using Umbraco.Core.IO; +using Umbraco.Core.Models; +using Umbraco.Core.Persistence.UnitOfWork; + +namespace Umbraco.Core.Persistence.Repositories +{ + /// + /// Represents the Script Repository + /// + internal class ScriptRepository : IScriptRepository + { + private IUnitOfWork _work; + private readonly IFileSystem _fileSystem; + + public ScriptRepository(IUnitOfWork work) + { + _work = work; + _fileSystem = FileSystemProviderManager.Current.GetFileSystemProvider("script"); + } + + /// + /// Returns the Unit of Work added to the repository + /// + protected IUnitOfWork UnitOfWork + { + get { return _work; } + } + + #region Implementation of IRepository + + public void AddOrUpdate(Script entity) + { + var stream = new MemoryStream(Encoding.UTF8.GetBytes(entity.Content)); + _fileSystem.AddFile(entity.Name, stream, true); + } + + public void Delete(Script entity) + { + if (_fileSystem.FileExists(entity.Name)) + { + _fileSystem.DeleteFile(entity.Name); + } + } + + public Script Get(string id) + { + if(!_fileSystem.FileExists(id)) + { + throw new Exception(string.Format("The file {0} was not found", id)); + } + + var stream = _fileSystem.OpenFile(id); + byte[] bytes = new byte[stream.Length]; + stream.Position = 0; + stream.Read(bytes, 0, (int)stream.Length); + var content = Encoding.UTF8.GetString(bytes); + + var path = _fileSystem.GetRelativePath(id); + + var script = new Script(path) {Content = content}; + return script; + } + + public IEnumerable