2016-09-09 13:28:30 +02:00
|
|
|
|
using System.Collections.Generic;
|
2014-12-29 13:50:33 +11:00
|
|
|
|
using System.IO;
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
2019-11-08 07:51:14 +01:00
|
|
|
|
using Umbraco.Core.Composing;
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
using Umbraco.Core.IO;
|
|
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
|
2017-12-07 16:45:25 +01:00
|
|
|
|
namespace Umbraco.Core.Persistence.Repositories.Implement
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
{
|
2015-01-09 18:39:44 +11:00
|
|
|
|
internal class PartialViewRepository : FileRepository<string, IPartialView>, IPartialViewRepository
|
2017-07-20 11:21:28 +02:00
|
|
|
|
{
|
2019-11-13 21:00:54 +01:00
|
|
|
|
private readonly IIOHelper _ioHelper;
|
|
|
|
|
|
|
|
|
|
|
|
public PartialViewRepository(IFileSystems fileSystems, IIOHelper ioHelper)
|
2018-07-20 09:49:05 +02:00
|
|
|
|
: base(fileSystems.PartialViewsFileSystem)
|
2019-11-13 21:00:54 +01:00
|
|
|
|
{
|
|
|
|
|
|
_ioHelper = ioHelper;
|
|
|
|
|
|
}
|
2018-07-20 09:49:05 +02:00
|
|
|
|
|
2019-11-13 21:00:54 +01:00
|
|
|
|
protected PartialViewRepository(IFileSystem fileSystem, IIOHelper ioHelper)
|
2017-12-14 17:04:44 +01:00
|
|
|
|
: base(fileSystem)
|
2019-11-13 21:00:54 +01:00
|
|
|
|
{
|
|
|
|
|
|
_ioHelper = ioHelper;
|
|
|
|
|
|
}
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
|
2016-04-12 19:55:50 +02:00
|
|
|
|
protected virtual PartialViewType ViewType => PartialViewType.PartialView;
|
2015-09-07 12:38:46 +02:00
|
|
|
|
|
2015-01-09 18:39:44 +11:00
|
|
|
|
public override IPartialView Get(string id)
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
{
|
2015-09-08 17:48:26 +02:00
|
|
|
|
// get the relative path within the filesystem
|
|
|
|
|
|
// (though... id should be relative already)
|
|
|
|
|
|
var path = FileSystem.GetRelativePath(id);
|
|
|
|
|
|
|
|
|
|
|
|
if (FileSystem.FileExists(path) == false)
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
return null;
|
|
|
|
|
|
|
2015-09-08 17:48:26 +02:00
|
|
|
|
// content will be lazy-loaded when required
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
var created = FileSystem.GetCreated(path).UtcDateTime;
|
|
|
|
|
|
var updated = FileSystem.GetLastModified(path).UtcDateTime;
|
2015-09-08 17:48:26 +02:00
|
|
|
|
//var content = GetFileContent(path);
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
|
2017-05-30 10:50:09 +02:00
|
|
|
|
var view = new PartialView(ViewType, path, file => GetFileContent(file.OriginalPath))
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
{
|
|
|
|
|
|
//id can be the hash
|
|
|
|
|
|
Id = path.GetHashCode(),
|
|
|
|
|
|
Key = path.EncodeAsGuid(),
|
2015-09-08 17:48:26 +02:00
|
|
|
|
//Content = content,
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
CreateDate = created,
|
2014-12-22 15:14:44 +11:00
|
|
|
|
UpdateDate = updated,
|
2017-05-30 10:50:09 +02:00
|
|
|
|
VirtualPath = FileSystem.GetUrl(id)
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
};
|
|
|
|
|
|
|
2017-11-10 11:27:12 +01:00
|
|
|
|
// reset dirty initial properties (U4-1946)
|
2015-09-08 17:48:26 +02:00
|
|
|
|
view.ResetDirtyProperties(false);
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
|
2015-09-08 17:48:26 +02:00
|
|
|
|
return view;
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-07 16:45:25 +01:00
|
|
|
|
public override void Save(IPartialView entity)
|
2015-09-07 12:38:46 +02:00
|
|
|
|
{
|
|
|
|
|
|
var partialView = entity as PartialView;
|
|
|
|
|
|
if (partialView != null)
|
|
|
|
|
|
partialView.ViewType = ViewType;
|
|
|
|
|
|
|
2017-12-07 16:45:25 +01:00
|
|
|
|
base.Save(entity);
|
2015-09-07 12:38:46 +02:00
|
|
|
|
|
|
|
|
|
|
// ensure that from now on, content is lazy-loaded
|
|
|
|
|
|
if (partialView != null && partialView.GetFileContent == null)
|
2015-09-08 17:48:26 +02:00
|
|
|
|
partialView.GetFileContent = file => GetFileContent(file.OriginalPath);
|
2015-09-07 12:38:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-07 16:45:25 +01:00
|
|
|
|
public override IEnumerable<IPartialView> GetMany(params string[] ids)
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
{
|
|
|
|
|
|
//ensure they are de-duplicated, easy win if people don't do this as this can cause many excess queries
|
|
|
|
|
|
ids = ids.Distinct().ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
if (ids.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var id in ids)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return Get(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2015-01-15 11:49:32 +11:00
|
|
|
|
var files = FindAllFiles("", "*.*");
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
foreach (var file in files)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return Get(file);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-12-29 13:50:33 +11:00
|
|
|
|
|
2015-09-08 17:48:26 +02:00
|
|
|
|
private static readonly List<string> ValidExtensions = new List<string> { "cshtml" };
|
|
|
|
|
|
|
|
|
|
|
|
public virtual bool ValidatePartialView(IPartialView partialView)
|
|
|
|
|
|
{
|
|
|
|
|
|
// get full path
|
|
|
|
|
|
string fullPath;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// may throw for security reasons
|
|
|
|
|
|
fullPath = FileSystem.GetFullPath(partialView.Path);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// validate path & extension
|
2019-11-13 11:26:03 +01:00
|
|
|
|
var validDir = Constants.SystemDirectories.MvcViews;
|
2019-11-19 08:52:39 +01:00
|
|
|
|
var isValidPath = _ioHelper.VerifyEditPath(fullPath, validDir);
|
|
|
|
|
|
var isValidExtension = _ioHelper.VerifyFileExtension(fullPath, ValidExtensions);
|
2015-09-08 17:48:26 +02:00
|
|
|
|
return isValidPath && isValidExtension;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-09-09 13:28:30 +02:00
|
|
|
|
public Stream GetFileContentStream(string filepath)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (FileSystem.FileExists(filepath) == false) return null;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
return FileSystem.OpenFile(filepath);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
return null; // deal with race conds
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SetFileContent(string filepath, Stream content)
|
|
|
|
|
|
{
|
|
|
|
|
|
FileSystem.AddFile(filepath, content, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-12-29 13:50:33 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a stream that is used to write to the file
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="content"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// This ensures the stream includes a utf8 BOM
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
protected override Stream GetContentStream(string content)
|
|
|
|
|
|
{
|
|
|
|
|
|
var data = Encoding.UTF8.GetBytes(content);
|
|
|
|
|
|
var withBom = Encoding.UTF8.GetPreamble().Concat(data).ToArray();
|
|
|
|
|
|
return new MemoryStream(withBom);
|
|
|
|
|
|
}
|
Updates PartialView & PartialViewMacros models/services/repositories, streamlines their operations, fixes up other underlying problems with the FileRepository, fixes tree syncing for partial views, partial view macros and scripts, fixes scripts being created in folders, allows partial views and partial view macros to be managed and created in folders, fixes FileUnitOfWork to use a queue, publicizes some internal test classes, fixes tree syncing when dealing with invariant case, adds correct validation to the create dialogs of scripts and partial views (and partial view macros)
2014-10-22 16:44:45 +10:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|