importing more stuff from Belle proj

This commit is contained in:
Shannon Deminick
2013-05-26 19:54:50 -10:00
parent 8b841e19eb
commit faae0b85cb
17 changed files with 741 additions and 298 deletions

View File

@@ -16,7 +16,6 @@ namespace Umbraco.Core.Manifest
internal class ManifestParser
{
private readonly DirectoryInfo _pluginsDir;
private static readonly Regex Token = new Regex("(\"##\\w+?##\")", RegexOptions.Compiled);
//used to strip comments
private static readonly Regex Comments = new Regex("(/\\*.*\\*/)", RegexOptions.Compiled);
@@ -47,21 +46,6 @@ namespace Umbraco.Core.Manifest
return CreateManifests(manifestFileContents.ToArray());
}
/// <summary>
/// Processes all found manifest files and outputs the main.js file containing all plugin manifests
/// </summary>
public string GetJavascriptInitialization(JObject umbracoConfig, JArray umbracoInit)
{
foreach (var m in GetManifests())
{
MergeJObjects(umbracoConfig, m.JavaScriptConfig, true);
MergeJArrays(umbracoInit, m.JavaScriptInitialize);
}
return ParseMain(umbracoConfig.ToString(), umbracoInit.ToString());
}
/// <summary>
/// Get the file contents from all declared manifest files
/// </summary>
@@ -250,42 +234,6 @@ namespace Umbraco.Core.Manifest
}
}
///// <summary>
///// Returns the default config as a JObject
///// </summary>
///// <returns></returns>
//internal static JObject GetDefaultConfig()
//{
// var config = JsResources.RequireJsConfig;
// var jObj = JsonConvert.DeserializeObject<JObject>(config);
// return jObj;
//}
///// <summary>
///// Returns the default config as a JArray
///// </summary>
///// <returns></returns>
//internal static JArray GetDefaultInitialization()
//{
// var init = JsResources.RequireJsInitialize;
// var jArr = JsonConvert.DeserializeObject<JArray>(init);
// return jArr;
//}
/// <summary>
/// Parses the JsResources.Main and replaces the replacement tokens accordingly.
/// </summary>
/// <param name="replacements"></param>
/// <returns></returns>
internal static string ParseMain(params string[] replacements)
{
var count = 0;
return Token.Replace(Resources.Main, match =>
{
var replaced = replacements[count];
count++;
return replaced;
});
}
}
}

View File

@@ -173,11 +173,6 @@
<Compile Include="Manifest\ManifestParser.cs" />
<Compile Include="Manifest\PackageManifest.cs" />
<Compile Include="Manifest\PropertyEditorConverter.cs" />
<Compile Include="Manifest\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Media\MediaSubfolderCounter.cs" />
<Compile Include="Models\ContentBase.cs" />
<Compile Include="Models\ContentExtensions.cs" />
@@ -818,18 +813,11 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Manifest\Main.js" />
<Content Include="Strings\Notes.txt" />
</ItemGroup>
<ItemGroup>
<Folder Include="Packaging\" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Manifest\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Newtonsoft.Json.Linq;
using Umbraco.Core.Manifest;
using Umbraco.Web.UI.JavaScript;
namespace Umbraco.Tests.AngularIntegration
{
[TestFixture]
public class RequireJsInitTests
{
[Test]
public void Get_Default_Config()
{
var config = RequireJsInit.GetDefaultConfig();
var paths = config.Properties().SingleOrDefault(x => x.Name == "paths");
var shim = config.Properties().SingleOrDefault(x => x.Name == "shim");
Assert.IsNotNull(paths);
Assert.AreEqual(typeof(JProperty), paths.GetType());
Assert.IsNotNull(shim);
Assert.AreEqual(typeof(JProperty), shim.GetType());
}
[Test]
public void Get_Default_Init()
{
var init = RequireJsInit.GetDefaultInitialization();
Assert.IsTrue(init.Any());
}
[Test]
public void Parse_Main()
{
var result = RequireJsInit.ParseMain("{Hello}", "[World]");
Assert.IsTrue(result.StartsWith("require.config({Hello});"));
Assert.IsTrue(result.Contains("require([World]"));
}
}
}

View File

@@ -0,0 +1,33 @@
using System.Collections.Generic;
using NUnit.Framework;
using Umbraco.Web.UI.JavaScript;
namespace Umbraco.Tests.AngularIntegration
{
[TestFixture]
public class ServerVariablesParserTests
{
[Test]
public void Parse()
{
var d = new Dictionary<string, object>();
d.Add("test1", "Test 1");
d.Add("test2", "Test 2");
d.Add("test3", "Test 3");
d.Add("test4", "Test 4");
d.Add("test5", "Test 5");
var output = ServerVariablesParser.Parse(d);
Assert.IsTrue(output.Contains(@"Umbraco.Sys.ServerVariables = {
""test1"": ""Test 1"",
""test2"": ""Test 2"",
""test3"": ""Test 3"",
""test4"": ""Test 4"",
""test5"": ""Test 5""
} ;"));
}
}
}

View File

@@ -0,0 +1,157 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Umbraco.Core.Manifest;
namespace Umbraco.Belle.Tests
{
[TestFixture]
public class ManifestParserTests
{
[Test]
public void Parse_Property_Editors()
{
var a = JsonConvert.DeserializeObject<JArray>(@"[
{
id: '0EEBB7CE-51BA-4F6B-9D9C-78BB3314366C',
name: 'Test 1',
editor: {
view: '~/App_Plugins/MyPackage/PropertyEditors/MyEditor.html',
valueType: 'int',
validation: [
{
type: 'Required'
},
{
type: 'Regex',
value: '\\d*'
},
]
}
},
{
id: '1FCF5C39-5FC7-4BCE-AFBE-6500D9EBA261',
name: 'Test 2',
editor: {
view: '~/App_Plugins/MyPackage/PropertyEditors/CsvEditor.html'
}
},
]");
var parser = ManifestParser.GetPropertyEditors(a);
Assert.AreEqual(2, parser.Count());
Assert.AreEqual(new Guid("0EEBB7CE-51BA-4F6B-9D9C-78BB3314366C"), parser.ElementAt(0).Id);
Assert.AreEqual("Test 1", parser.ElementAt(0).Name);
Assert.AreEqual("~/App_Plugins/MyPackage/PropertyEditors/MyEditor.html", parser.ElementAt(0).ValueEditor.View);
Assert.AreEqual("int", parser.ElementAt(0).ValueEditor.ValueType);
Assert.AreEqual(2, parser.ElementAt(0).ValueEditor.Validators.Count());
Assert.AreEqual(new Guid("1FCF5C39-5FC7-4BCE-AFBE-6500D9EBA261"), parser.ElementAt(1).Id);
Assert.AreEqual("Test 2", parser.ElementAt(1).Name);
}
[Test]
public void Merge_JArrays()
{
var obj1 = JArray.FromObject(new[] { "test1", "test2", "test3" });
var obj2 = JArray.FromObject(new[] { "test1", "test2", "test3", "test4" });
ManifestParser.MergeJArrays(obj1, obj2);
Assert.AreEqual(4, obj1.Count());
}
[Test]
public void Merge_JObjects_Replace_Original()
{
var obj1 = JObject.FromObject(new
{
Property1 = "Value1",
Property2 = "Value2",
Property3 = "Value3"
});
var obj2 = JObject.FromObject(new
{
Property3 = "Value3/2",
Property4 = "Value4",
Property5 = "Value5"
});
ManifestParser.MergeJObjects(obj1, obj2);
Assert.AreEqual(5, obj1.Properties().Count());
Assert.AreEqual("Value3/2", obj1.Properties().ElementAt(2).Value.Value<string>());
}
[Test]
public void Merge_JObjects_Keep_Original()
{
var obj1 = JObject.FromObject(new
{
Property1 = "Value1",
Property2 = "Value2",
Property3 = "Value3"
});
var obj2 = JObject.FromObject(new
{
Property3 = "Value3/2",
Property4 = "Value4",
Property5 = "Value5"
});
ManifestParser.MergeJObjects(obj1, obj2, true);
Assert.AreEqual(5, obj1.Properties().Count());
Assert.AreEqual("Value3", obj1.Properties().ElementAt(2).Value.Value<string>());
}
[TestCase("C:\\Test", "C:\\Test\\MyFolder\\AnotherFolder", 2)]
[TestCase("C:\\Test", "C:\\Test\\MyFolder\\AnotherFolder\\YetAnother", 3)]
[TestCase("C:\\Test", "C:\\Test\\", 0)]
public void Get_Folder_Depth(string baseFolder, string currFolder, int expected)
{
Assert.AreEqual(expected,
ManifestParser.FolderDepth(
new DirectoryInfo(baseFolder),
new DirectoryInfo(currFolder)));
}
//[Test]
//public void Parse_Property_Editor()
//{
//}
[Test]
public void Create_Manifest_From_File_Content()
{
var content1 = "{}";
var content2 = "{config: {}, init: []}";
var content3 = "{config: {paths: {blah: 'mypath.js'}, shim: {'blah' : {'exports': 'blah'}}}, init: []}";
var content4 = "{propertyEditors: [], config: {paths: {blah: 'mypath.js'}, shim: {'blah' : {'exports': 'blah'}}}, init: []}";
var result = ManifestParser.CreateManifests(null, content1, content2, content3, content4);
Assert.AreEqual(4, result.Count());
Assert.AreEqual(0, result.ElementAt(1).JavaScriptConfig.Properties().Count());
Assert.AreEqual(2, result.ElementAt(2).JavaScriptConfig.Properties().Count());
Assert.AreEqual(2, result.ElementAt(3).JavaScriptConfig.Properties().Count());
}
}
}

View File

@@ -145,6 +145,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AngularIntegration\RequireJsInitTests.cs" />
<Compile Include="AngularIntegration\ServerVariablesParserTests.cs" />
<Compile Include="Auditing\AuditTests.cs" />
<Compile Include="BootManagers\CoreBootManagerTests.cs" />
<Compile Include="BusinessLogic\DictionaryTest.cs" />
@@ -188,6 +190,7 @@
<Compile Include="Configurations\FileSystemProviderTests.cs" />
<Compile Include="CoreXml\FrameworkXmlTests.cs" />
<Compile Include="Integration\CreateContent.cs" />
<Compile Include="Manifest\ManifestParserTests.cs" />
<Compile Include="TestHelpers\BaseSeleniumTest.cs" />
<Compile Include="Integration\InstallPackage.cs" />
<Compile Include="PublishedCache\PublishedMediaCacheTests.cs" />

View File

@@ -0,0 +1,33 @@
{
/*NOTE: This is actually /Belle/js because we are loading in requireJs from /Belle already*/
baseUrl: 'js',
waitSeconds: 120,
paths: {
jquery: '../lib/jquery/jquery-1.8.2.min',
jqueryCookie: '../lib/jquery/jquery.cookie',
bootstrap: '../lib/bootstrap/js/bootstrap',
underscore: '../lib/underscore/underscore',
angular: '../lib/angular/angular',
angularResource: '../lib/angular/angular-resource',
statemanager: '../lib/angular/statemanager',
text: '../lib/require/text',
async: '../lib/require/async',
namespaceMgr: '../lib/Umbraco/NamespaceManager',
myApp: '../../../Content/JavaScript/myApp'
},
shim: {
'angular' : {'exports' : 'angular'},
'angular-resource': { deps: ['angular'] },
'statemanager': { deps: ['angular'] },
'bootstrap': { deps: ['jquery'] },
'jqueryCookie': { deps: ['jquery'] },
'angular-statemanager' : {deps:['angular']},
'underscore': {exports: '_'}
},
priority: [
"angular"
],
urlArgs: 'v=1.1'
}

View File

@@ -0,0 +1,79 @@
using System.IO;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Umbraco.Core.Manifest;
namespace Umbraco.Web.UI.JavaScript
{
/// <summary>
/// Reads from all defined manifests and ensures that any of their initialization is output with the
/// main Umbraco initialization output.
/// </summary>
internal class RequireJsInit
{
private readonly DirectoryInfo _pluginsDir;
private readonly ManifestParser _parser;
public RequireJsInit(DirectoryInfo pluginsDir)
{
_pluginsDir = pluginsDir;
_parser = new ManifestParser(_pluginsDir);
}
private static readonly Regex Token = new Regex("(\"##\\w+?##\")", RegexOptions.Compiled);
/// <summary>
/// Processes all found manifest files and outputs the main.js file containing all plugin manifests
/// </summary>
public string GetJavascriptInitialization(JObject umbracoConfig, JArray umbracoInit)
{
foreach (var m in _parser.GetManifests())
{
ManifestParser.MergeJObjects(umbracoConfig, m.JavaScriptConfig, true);
ManifestParser.MergeJArrays(umbracoInit, m.JavaScriptInitialize);
}
return ParseMain(umbracoConfig.ToString(), umbracoInit.ToString());
}
/// <summary>
/// Returns the default config as a JObject
/// </summary>
/// <returns></returns>
internal static JObject GetDefaultConfig()
{
var config = Resources.RequireJsConfig;
var jObj = JsonConvert.DeserializeObject<JObject>(config);
return jObj;
}
/// <summary>
/// Returns the default config as a JArray
/// </summary>
/// <returns></returns>
internal static JArray GetDefaultInitialization()
{
var init = Resources.RequireJsInitialize;
var jArr = JsonConvert.DeserializeObject<JArray>(init);
return jArr;
}
/// <summary>
/// Parses the JsResources.Main and replaces the replacement tokens accordingly.
/// </summary>
/// <param name="replacements"></param>
/// <returns></returns>
internal static string ParseMain(params string[] replacements)
{
var count = 0;
return Token.Replace(Resources.Main, match =>
{
var replaced = replacements[count];
count++;
return replaced;
});
}
}
}

View File

@@ -0,0 +1,7 @@
[
'angular',
'jquery',
'underscore',
'namespaceMgr',
'myApp'
]

View File

@@ -8,7 +8,7 @@
// </auto-generated>
//------------------------------------------------------------------------------
namespace Umbraco.Core.Manifest {
namespace Umbraco.Web.UI.JavaScript {
using System;
@@ -39,7 +39,7 @@ namespace Umbraco.Core.Manifest {
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Umbraco.Core.Manifest.Resources", typeof(Resources).Assembly);
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Umbraco.Web.UI.JavaScript.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
@@ -80,5 +80,59 @@ namespace Umbraco.Core.Manifest {
return ResourceManager.GetString("Main", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to {
///
/// /*NOTE: This is actually /Belle/js because we are loading in requireJs from /Belle already*/
/// baseUrl: &apos;js&apos;,
///
/// waitSeconds: 120,
/// paths: {
/// jquery: &apos;../lib/jquery/jquery-1.8.2.min&apos;,
/// jqueryCookie: &apos;../lib/jquery/jquery.cookie&apos;,
/// bootstrap: &apos;../lib/bootstrap/js/bootstrap&apos;,
/// underscore: &apos;../lib/underscore/underscore&apos;,
/// angular: &apos;../lib/angular/angular&apos;,
/// angularResource: &apos;../lib/angular/angular-resource&apos;,
/// statemanager: &apos;../ [rest of string was truncated]&quot;;.
/// </summary>
internal static string RequireJsConfig {
get {
return ResourceManager.GetString("RequireJsConfig", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to [
/// &apos;angular&apos;,
/// &apos;jquery&apos;,
/// &apos;underscore&apos;,
/// &apos;namespaceMgr&apos;,
/// &apos;myApp&apos;
///].
/// </summary>
internal static string RequireJsInitialize {
get {
return ResourceManager.GetString("RequireJsInitialize", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to //TODO: This would be nicer as an angular module so it can be injected into stuff... that&apos;d be heaps nicer, but
///// how to do that when this is not a regular JS file, it is a server side JS file and RequireJS seems to only want
///// to force load JS files ?
///
/////create the namespace (NOTE: This loads before any dependencies so we don&apos;t have a namespace mgr so we just create it manually)
///var Umbraco = {};
///Umbraco.Sys = {};
/////define a global static object
///Umbraco.Sys.ServerVariables = ##Variables## ;.
/// </summary>
internal static string ServerVariables {
get {
return ResourceManager.GetString("ServerVariables", resourceCulture);
}
}
}
}

View File

@@ -121,4 +121,13 @@
<data name="Main" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Main.js;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="RequireJsConfig" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>requirejsconfig.js;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
<data name="RequireJsInitialize" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>requirejsinitialize.js;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
<data name="ServerVariables" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>servervariables.js;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
</root>

View File

@@ -0,0 +1,9 @@
//TODO: This would be nicer as an angular module so it can be injected into stuff... that'd be heaps nicer, but
// how to do that when this is not a regular JS file, it is a server side JS file and RequireJS seems to only want
// to force load JS files ?
//create the namespace (NOTE: This loads before any dependencies so we don't have a namespace mgr so we just create it manually)
var Umbraco = {};
Umbraco.Sys = {};
//define a global static object
Umbraco.Sys.ServerVariables = ##Variables## ;

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
namespace Umbraco.Web.UI.JavaScript
{
internal class ServerVariablesParser
{
/// <summary>
/// Can allow developers to add custom variables on startup
/// </summary>
internal static EventHandler<Dictionary<string, object>> Parsing;
internal const string Token = "##Variables##";
internal static string Parse(Dictionary<string, object> items)
{
var vars = JsResources.ServerVariables;
if (Parsing != null)
{
Parsing(null, items);
}
var json = JObject.FromObject(items);
return vars.Replace(Token, json.ToString());
}
}
}

View File

@@ -292,6 +292,12 @@
<Compile Include="Cache\UserCacheRefresher.cs" />
<Compile Include="Cache\UserTypeCacheRefresher.cs" />
<Compile Include="Configuration\WebRouting.cs" />
<Compile Include="UI\JavaScript\RequireJsInit.cs" />
<Compile Include="UI\JavaScript\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Models\ContentEditing\ContentItemBase.cs" />
<Compile Include="Models\ContentEditing\ContentItemDisplay.cs" />
<Compile Include="Models\ContentEditing\ContentItemDto.cs" />
@@ -374,6 +380,7 @@
<Compile Include="Search\LuceneIndexerExtensions.cs" />
<Compile Include="Security\ValidateRequestAttempt.cs" />
<Compile Include="Security\WebSecurity.cs" />
<Compile Include="UI\JavaScript\ServerVariablesParser.cs" />
<Compile Include="umbraco.presentation\LegacyClasses.cs" />
<Compile Include="umbraco.presentation\umbraco\controls\ContentTypeControlNew.ascx.cs">
<SubType>ASPXCodeBehind</SubType>
@@ -1794,6 +1801,10 @@
<Compile Include="WebServices\UmbracoWebService.cs">
<SubType>Component</SubType>
</Compile>
<EmbeddedResource Include="UI\JavaScript\Main.js" />
<EmbeddedResource Include="UI\JavaScript\RequireJsConfig.js" />
<EmbeddedResource Include="UI\JavaScript\RequireJsInitialize.js" />
<EmbeddedResource Include="UI\JavaScript\ServerVariables.js" />
<Content Include="umbraco.presentation\umbracobase\readme.txt" />
<Content Include="umbraco.presentation\umbraco\controls\Tree\CustomTreeService.asmx" />
<Content Include="umbraco.presentation\umbraco\developer\RelationTypes\EditRelationType.aspx" />
@@ -2010,6 +2021,10 @@
<Generator>MSDiscoCodeGenerator</Generator>
<LastGenOutput>Reference.cs</LastGenOutput>
</None>
<EmbeddedResource Include="UI\JavaScript\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Include="Mvc\Strings.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Strings.Designer.cs</LastGenOutput>

View File

@@ -1,103 +1,103 @@
//using System.IO;
//using System.Net;
//using System.Net.Http;
//using System.Threading.Tasks;
//using System.Web;
//using System.Web.Http;
//using System.Web.Http.Controllers;
//using System.Web.Http.ModelBinding;
//using Newtonsoft.Json;
//using Umbraco.Web.Models.ContentEditing;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.ModelBinding;
using Newtonsoft.Json;
using Umbraco.Web.Models.ContentEditing;
//namespace Umbraco.Web.WebApi
//{
// /// <summary>
// /// Binds the content model to the controller action for the posted multi-part Post
// /// </summary>
// internal class ContentItemBinder : IModelBinder
// {
// public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
// {
// //NOTE: Validation is done in the filter
// if (!actionContext.Request.Content.IsMimeMultipartContent())
// {
// throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
// }
namespace Umbraco.Web.WebApi
{
/// <summary>
/// Binds the content model to the controller action for the posted multi-part Post
/// </summary>
internal class ContentItemBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
//NOTE: Validation is done in the filter
if (!actionContext.Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
// var root = HttpContext.Current.Server.MapPath("~/App_Data/TEMP/FileUploads");
// //ensure it exists
// Directory.CreateDirectory(root);
// var provider = new MultipartFormDataStreamProvider(root);
var root = HttpContext.Current.Server.MapPath("~/App_Data/TEMP/FileUploads");
//ensure it exists
Directory.CreateDirectory(root);
var provider = new MultipartFormDataStreamProvider(root);
// var task = Task.Run(() => GetModel(actionContext.Request.Content, provider))
// .ContinueWith(x =>
// {
// if (x.IsFaulted && x.Exception != null)
// {
// throw x.Exception;
// }
// bindingContext.Model = x.Result;
// });
// task.Wait();
// return bindingContext.Model != null;
// }
var task = Task.Run(() => GetModel(actionContext.Request.Content, provider))
.ContinueWith(x =>
{
if (x.IsFaulted && x.Exception != null)
{
throw x.Exception;
}
bindingContext.Model = x.Result;
});
// /// <summary>
// /// Builds the model from the request contents
// /// </summary>
// /// <param name="content"></param>
// /// <param name="provider"></param>
// /// <returns></returns>
// private async Task<ContentItemSave> GetModel(HttpContent content, MultipartFormDataStreamProvider provider)
// {
// var result = await content.ReadAsMultipartAsync(provider);
task.Wait();
// if (result.FormData["contentItem"] == null)
// {
// throw new HttpResponseException(
// new HttpResponseMessage(HttpStatusCode.BadRequest)
// {
// ReasonPhrase = "The request was not formatted correctly and is missing the 'contentItem' parameter"
// });
// }
// //get the string json from the request
// var contentItem = result.FormData["contentItem"];
return bindingContext.Model != null;
}
// //transform the json into an object
// var model = JsonConvert.DeserializeObject<ContentItemSave>(contentItem);
/// <summary>
/// Builds the model from the request contents
/// </summary>
/// <param name="content"></param>
/// <param name="provider"></param>
/// <returns></returns>
private async Task<ContentItemSave> GetModel(HttpContent content, MultipartFormDataStreamProvider provider)
{
var result = await content.ReadAsMultipartAsync(provider);
// //get the files
// foreach (var file in result.FileData)
// {
// var parts = file.Headers.ContentDisposition.Name.Trim(new char[] {'\"'}).Split('_');
// if (parts.Length != 2)
// {
// throw new HttpResponseException(
// new HttpResponseMessage(HttpStatusCode.BadRequest)
// {
// ReasonPhrase = "The request was not formatted correctly the file name's must be underscore delimited"
// });
// }
// int propertyId;
// if (!int.TryParse(parts[1], out propertyId))
// {
// throw new HttpResponseException(
// new HttpResponseMessage(HttpStatusCode.BadRequest)
// {
// ReasonPhrase = "The request was not formatted correctly the file name's 2nd part must be an integer"
// });
// }
// model.UploadedFiles.Add(new ContentItemFile
// {
// FilePath = file.LocalFileName,
// PropertyId = propertyId
// });
// }
if (result.FormData["contentItem"] == null)
{
throw new HttpResponseException(
new HttpResponseMessage(HttpStatusCode.BadRequest)
{
ReasonPhrase = "The request was not formatted correctly and is missing the 'contentItem' parameter"
});
}
// return model;
// }
// }
//}
//get the string json from the request
var contentItem = result.FormData["contentItem"];
//transform the json into an object
var model = JsonConvert.DeserializeObject<ContentItemSave>(contentItem);
//get the files
foreach (var file in result.FileData)
{
var parts = file.Headers.ContentDisposition.Name.Trim(new char[] { '\"' }).Split('_');
if (parts.Length != 2)
{
throw new HttpResponseException(
new HttpResponseMessage(HttpStatusCode.BadRequest)
{
ReasonPhrase = "The request was not formatted correctly the file name's must be underscore delimited"
});
}
int propertyId;
if (!int.TryParse(parts[1], out propertyId))
{
throw new HttpResponseException(
new HttpResponseMessage(HttpStatusCode.BadRequest)
{
ReasonPhrase = "The request was not formatted correctly the file name's 2nd part must be an integer"
});
}
model.UploadedFiles.Add(new ContentItemFile
{
FilePath = file.LocalFileName,
PropertyId = propertyId
});
}
return model;
}
}
}

View File

@@ -1,150 +1,180 @@
//using System.Linq;
//using System.Net;
//using System.Net.Http;
//using System.Web.Http.Controllers;
//using System.Web.Http.Filters;
//using Umbraco.Core.PropertyEditors;
//using Umbraco.Web.Models.ContentEditing;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using Umbraco.Core;
using Umbraco.Core.PropertyEditors;
using Umbraco.Web.Models.ContentEditing;
//namespace Umbraco.Web.WebApi.Filters
//{
// /// <summary>
// /// Validates the content item
// /// </summary>
// /// <remarks>
// /// There's various validation happening here both value validation and structure validation
// /// to ensure that malicious folks are not trying to post invalid values or to invalid properties.
// /// </remarks>
// internal class ContentItemValidationFilterAttribute : ActionFilterAttribute
// {
// /// <summary>
// /// Returns true so that other filters can execute along with this one
// /// </summary>
// public override bool AllowMultiple
// {
// get { return true; }
// }
namespace Umbraco.Web.WebApi.Filters
{
/// <summary>
/// Validates the content item
/// </summary>
/// <remarks>
/// There's various validation happening here both value validation and structure validation
/// to ensure that malicious folks are not trying to post invalid values or to invalid properties.
/// </remarks>
internal class ContentItemValidationFilterAttribute : ActionFilterAttribute
{
private readonly ApplicationContext _applicationContext;
// /// <summary>
// /// Performs the validation
// /// </summary>
// /// <param name="actionContext"></param>
// public override void OnActionExecuting(HttpActionContext actionContext)
// {
// var contentItem = actionContext.ActionArguments["contentItem"] as ContentItemSave;
// if (contentItem == null)
// {
// actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No " + typeof(ContentItemSave) + " found in request");
// return;
// }
public ContentItemValidationFilterAttribute(ApplicationContext applicationContext)
{
_applicationContext = applicationContext;
}
// //now do each validation step
// ContentItemDto existingContent;
// if (!ValidateExistingContent(contentItem, actionContext, out existingContent)) return;
// if (!ValidateProperties(contentItem, existingContent, actionContext)) return;
// if (!ValidateData(contentItem, existingContent, actionContext)) return;
// }
public ContentItemValidationFilterAttribute()
: this(ApplicationContext.Current)
{
}
// /// <summary>
// /// Ensure the content exists
// /// </summary>
// /// <param name="postedItem"></param>
// /// <param name="actionContext"></param>
// /// <param name="found"></param>
// /// <returns></returns>
// private bool ValidateExistingContent(ContentItemSave postedItem, HttpActionContext actionContext, out ContentItemDto found)
// {
// //TODO: We need to of course change this to the real umbraco api
// found = TestContentService.GetContentItem(postedItem.Id);
// if (found == null)
// {
// var message = string.Format("content with id: {0} was not found", postedItem.Id);
// actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
// return false;
// }
// return true;
// }
/// <summary>
/// Returns true so that other filters can execute along with this one
/// </summary>
public override bool AllowMultiple
{
get { return true; }
}
// /// <summary>
// /// Ensure all of the ids in the post are valid
// /// </summary>
// /// <param name="postedItem"></param>
// /// <param name="actionContext"></param>
// /// <param name="realItem"></param>
// /// <returns></returns>
// private bool ValidateProperties(ContentItemSave postedItem, ContentItemDto realItem, HttpActionContext actionContext)
// {
// foreach (var p in postedItem.Properties)
// {
// //ensure the property actually exists in our server side properties
// if (!realItem.Properties.Contains(p))
// {
// //TODO: Do we return errors here ? If someone deletes a property whilst their editing then should we just
// //save the property data that remains? Or inform them they need to reload... not sure. This problem exists currently too i think.
/// <summary>
/// Performs the validation
/// </summary>
/// <param name="actionContext"></param>
public override void OnActionExecuting(HttpActionContext actionContext)
{
var contentItem = actionContext.ActionArguments["contentItem"] as ContentItemSave;
if (contentItem == null)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No " + typeof(ContentItemSave) + " found in request");
return;
}
// var message = string.Format("property with id: {0} was not found", p.Id);
// actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
// return false;
// }
// }
// return true;
// }
//now do each validation step
ContentItemDto existingContent;
if (!ValidateExistingContent(contentItem, actionContext, out existingContent)) return;
if (!ValidateProperties(contentItem, existingContent, actionContext)) return;
if (!ValidateData(contentItem, existingContent, actionContext)) return;
}
// //TODO: Validate the property type data
// private bool ValidateData(ContentItemSave postedItem, ContentItemDto realItem, HttpActionContext actionContext)
// {
// foreach (var p in realItem.Properties)
// {
// var editor = PropertyEditorResolver.Current.GetById(p.DataType.ControlId);
// if (editor == null)
// {
// var message = string.Format("The property editor with id: {0} was not found for property with id {1}", p.DataType.ControlId, p.Id);
// actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
// return false;
// }
/// <summary>
/// Ensure the content exists
/// </summary>
/// <param name="postedItem"></param>
/// <param name="actionContext"></param>
/// <param name="found"></param>
/// <returns></returns>
private bool ValidateExistingContent(ContentItemSave postedItem, HttpActionContext actionContext, out ContentItemDto found)
{
var item = _applicationContext.Services.ContentService.GetById(postedItem.Id);
if (item == null)
{
var message = string.Format("content with id: {0} was not found", postedItem.Id);
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
found = null;
return false;
}
// //get the posted value for this property
// var postedValue = postedItem.Properties.Single(x => x.Id == p.Id).Value;
//TODO: Convert this over to a mapping engine, just need people to vote on podio for my question about that.
found = new ContentItemDto
{
Id = item.Id,
Properties = item.Properties.Select(p => new ContentPropertyDto
{
Alias = p.Alias,
Description = p.PropertyType.Description,
Label = p.PropertyType.Name,
Id = p.Id,
DataType = _applicationContext.Services.DataTypeService.GetDataTypeDefinitionById(p.PropertyType.DataTypeDefinitionId)
}).ToList()
};
return true;
}
// //get the pre-values for this property
// var preValues = TestContentService.GetPreValue(p.DataType.Id);
/// <summary>
/// Ensure all of the ids in the post are valid
/// </summary>
/// <param name="postedItem"></param>
/// <param name="actionContext"></param>
/// <param name="realItem"></param>
/// <returns></returns>
private bool ValidateProperties(ContentItemSave postedItem, ContentItemDto realItem, HttpActionContext actionContext)
{
foreach (var p in postedItem.Properties)
{
//ensure the property actually exists in our server side properties
if (!realItem.Properties.Contains(p))
{
//TODO: Do we return errors here ? If someone deletes a property whilst their editing then should we just
//save the property data that remains? Or inform them they need to reload... not sure. This problem exists currently too i think.
// //TODO: when we figure out how to 'override' certain pre-value properties we'll either need to:
// // * Combine the preValues with the overridden values stored with the document type property (but how to combine?)
// // * Or, pass in the overridden values stored with the doc type property separately
var message = string.Format("property with id: {0} was not found", p.Id);
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
return false;
}
}
return true;
}
// foreach (var v in editor.ValueEditor.Validators)
// {
// foreach (var result in v.Validate(postedValue, preValues, editor))
// {
// //if there are no member names supplied then we assume that the validation message is for the overall property
// // not a sub field on the property editor
// if (!result.MemberNames.Any())
// {
// //add a model state error for the entire property
// actionContext.ModelState.AddModelError(p.Alias, result.ErrorMessage);
// }
// else
// {
// //there's assigned field names so we'll combine the field name with the property name
// // so that we can try to match it up to a real sub field of this editor
// foreach (var field in result.MemberNames)
// {
// actionContext.ModelState.AddModelError(string.Format("{0}.{1}", p.Alias, field), result.ErrorMessage);
// }
// }
// }
// }
// }
//TODO: Validate the property type data
private bool ValidateData(ContentItemSave postedItem, ContentItemDto realItem, HttpActionContext actionContext)
{
foreach (var p in realItem.Properties)
{
var editor = PropertyEditorResolver.Current.GetById(p.DataType.ControlId);
if (editor == null)
{
var message = string.Format("The property editor with id: {0} was not found for property with id {1}", p.DataType.ControlId, p.Id);
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
return false;
}
// //create the response if there any errors
// if (!actionContext.ModelState.IsValid)
// {
// actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Forbidden, actionContext.ModelState);
// }
//get the posted value for this property
var postedValue = postedItem.Properties.Single(x => x.Id == p.Id).Value;
// return actionContext.ModelState.IsValid;
// }
//get the pre-values for this property
var preValues = _applicationContext.Services.DataTypeService.GetPreValueAsString(p.DataType.Id);
// }
//}
//TODO: when we figure out how to 'override' certain pre-value properties we'll either need to:
// * Combine the preValues with the overridden values stored with the document type property (but how to combine?)
// * Or, pass in the overridden values stored with the doc type property separately
foreach (var v in editor.ValueEditor.Validators)
{
foreach (var result in v.Validate(postedValue, preValues, editor))
{
//if there are no member names supplied then we assume that the validation message is for the overall property
// not a sub field on the property editor
if (!result.MemberNames.Any())
{
//add a model state error for the entire property
actionContext.ModelState.AddModelError(p.Alias, result.ErrorMessage);
}
else
{
//there's assigned field names so we'll combine the field name with the property name
// so that we can try to match it up to a real sub field of this editor
foreach (var field in result.MemberNames)
{
actionContext.ModelState.AddModelError(string.Format("{0}.{1}", p.Alias, field), result.ErrorMessage);
}
}
}
}
}
//create the response if there any errors
if (!actionContext.ModelState.IsValid)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Forbidden, actionContext.ModelState);
}
return actionContext.ModelState.IsValid;
}
}
}