/
Startup.cs
81 lines (68 loc) · 3.15 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Configuration;
using System.Text;
using Umbraco.Core;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web.Routing;
using UmbracoContentFiles.RequestPipeline;
namespace UmbracoContentFiles.Events
{
public class Startup : IApplicationEventHandler
{
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
UrlProviderResolver.Current.InsertTypeBefore<DefaultUrlProvider, FileUrlProvider>();
}
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
// Determine if Umbraco has been configured/installed before continuing..
if (applicationContext.IsConfigured)
{
return;
}
var contentService = applicationContext.Services.ContentService;
var textFileContentEvents = new FileContentEvents(contentService);
ContentService.Published += textFileContentEvents.Published;
ContentService.UnPublished += textFileContentEvents.UnPublished;
var contentTypeService = applicationContext.Services.ContentTypeService;
try
{
var textFileContentType = contentTypeService.GetContentType("contentFile");
if (textFileContentType == null)
{
var textboxMultipleDef = new DataTypeDefinition(-1, "Umbraco.TextboxMultiple");
var fileService = applicationContext.Services.FileService;
var template = new Template("Content File", "ContentFile");
var sbTemplate = new StringBuilder();
sbTemplate.AppendLine("@inherits Umbraco.Web.Mvc.UmbracoViewPage<Umbraco.Web.Models.RenderModel>");
sbTemplate.AppendLine("@{ Layout = null; }");
sbTemplate.Append("@Model.Content.GetPropertyValue(\"fileContent\")");
template.Content = sbTemplate.ToString();
fileService.SaveTemplate(template);
var contentType = new ContentType(-1)
{
Alias = "contentFile",
Name = "Content File",
Icon = "icon-files",
AllowedTemplates = new ITemplate[] { template }
};
var propertyType = new PropertyType(textboxMultipleDef, "fileContent");
propertyType.Name = "Content";
contentType.AddPropertyGroup("Content");
contentType.AddPropertyType(propertyType, "Content");
contentTypeService.Save(contentType);
}
}
catch(Exception ex)
{
LogHelper.Error<Startup>("Cannot setup file content type", ex);
}
}
}
}