Skip to content

Commit f629ab0

Browse files
committed
get web templates from github
1 parent 348c063 commit f629ab0

10 files changed

+417
-36
lines changed

OpenContent/App_LocalResources/EditGlobalSettings.ascx.resx

+5-2
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,16 @@
144144
<data name="lGoogleApiKey.Text" xml:space="preserve">
145145
<value>Google Api Key</value>
146146
</data>
147-
<data name="lFastHandlebars.Text" xml:space="preserve">
148-
<value>Fast Handlebars. Case sensitive variables ! (Beta)</value>
147+
<data name="lLegacyHandlebars.Text" xml:space="preserve">
148+
<value>Legacy Handlebars. Case unsensitive variables. Slower.</value>
149149
</data>
150150
<data name="lSaveXml.Text" xml:space="preserve">
151151
<value>Save Xml</value>
152152
</data>
153153
<data name="cmdUpgradeXml.Text" xml:space="preserve">
154154
<value>Generate Xml</value>
155155
</data>
156+
<data name="lGithubRepository.Text" xml:space="preserve">
157+
<value>Github Templates Repository</value>
158+
</data>
156159
</root>
+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
6+
namespace Satrabel.OpenContent.Components.Github
7+
{
8+
9+
// <auto-generated />
10+
//
11+
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
12+
//
13+
// using QuickType;
14+
//
15+
// var contents = Contents.FromJson(jsonString);
16+
17+
using System;
18+
using System.Collections.Generic;
19+
20+
using System.Globalization;
21+
using Newtonsoft.Json;
22+
using Newtonsoft.Json.Converters;
23+
24+
public partial class Contents
25+
{
26+
[JsonProperty("name")]
27+
public string Name { get; set; }
28+
29+
[JsonProperty("path")]
30+
public string Path { get; set; }
31+
32+
[JsonProperty("sha")]
33+
public string Sha { get; set; }
34+
35+
[JsonProperty("size")]
36+
public long Size { get; set; }
37+
38+
[JsonProperty("url")]
39+
public Uri Url { get; set; }
40+
41+
[JsonProperty("html_url")]
42+
public Uri HtmlUrl { get; set; }
43+
44+
[JsonProperty("git_url")]
45+
public Uri GitUrl { get; set; }
46+
47+
[JsonProperty("download_url")]
48+
public Uri DownloadUrl { get; set; }
49+
50+
[JsonProperty("type")]
51+
public TypeEnum Type { get; set; }
52+
53+
[JsonProperty("_links")]
54+
public Links Links { get; set; }
55+
}
56+
57+
public partial class Links
58+
{
59+
[JsonProperty("self")]
60+
public Uri Self { get; set; }
61+
62+
[JsonProperty("git")]
63+
public Uri Git { get; set; }
64+
65+
[JsonProperty("html")]
66+
public Uri Html { get; set; }
67+
}
68+
69+
public enum TypeEnum { Dir, File };
70+
71+
public partial class Contents
72+
{
73+
public static List<Contents> FromJson(string json) => JsonConvert.DeserializeObject<List<Contents>>(json, Converter.Settings);
74+
}
75+
76+
public static class Serialize
77+
{
78+
public static string ToJson(this List<Contents> self) => JsonConvert.SerializeObject(self, Converter.Settings);
79+
}
80+
81+
internal static class Converter
82+
{
83+
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
84+
{
85+
//MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
86+
DateParseHandling = DateParseHandling.None,
87+
Converters =
88+
{
89+
TypeEnumConverter.Singleton,
90+
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
91+
},
92+
};
93+
}
94+
95+
internal class TypeEnumConverter : JsonConverter
96+
{
97+
public override bool CanConvert(Type t) => t == typeof(TypeEnum) || t == typeof(TypeEnum?);
98+
99+
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
100+
{
101+
if (reader.TokenType == JsonToken.Null) return null;
102+
var value = serializer.Deserialize<string>(reader);
103+
switch (value)
104+
{
105+
case "dir":
106+
return TypeEnum.Dir;
107+
case "file":
108+
return TypeEnum.File;
109+
}
110+
throw new Exception("Cannot unmarshal type TypeEnum");
111+
}
112+
113+
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
114+
{
115+
if (untypedValue == null)
116+
{
117+
serializer.Serialize(writer, null);
118+
return;
119+
}
120+
var value = (TypeEnum)untypedValue;
121+
switch (value)
122+
{
123+
case TypeEnum.Dir:
124+
serializer.Serialize(writer, "dir");
125+
return;
126+
case TypeEnum.File:
127+
serializer.Serialize(writer, "file");
128+
return;
129+
}
130+
throw new Exception("Cannot marshal type TypeEnum");
131+
}
132+
133+
public static readonly TypeEnumConverter Singleton = new TypeEnumConverter();
134+
}
135+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
using System.Net;
6+
using System.Net.Http;
7+
using System.Net.Http.Headers;
8+
using Newtonsoft.Json;
9+
using Newtonsoft.Json.Linq;
10+
using System.Threading.Tasks;
11+
using Satrabel.OpenContent.Components.Github;
12+
using DotNetNuke.Services.FileSystem;
13+
using System.IO;
14+
using DotNetNuke.Entities.Host;
15+
16+
namespace Satrabel.OpenContent.Components
17+
{
18+
public static class GithubTemplateUtils
19+
{
20+
// 1. api call to template list: https://api.github.com/repos/sachatrauwaen/OpenContent-Templates/contents (type='dir')
21+
22+
// 2. per template ophalen manifest:
23+
// 3. uit manifest (master branch) de naam, onmschrijving en evt. afbeelding lezen
24+
// inhoud schema file(is json):
25+
// https://raw.githubusercontent.com/sachatrauwaen/OpenContent-Templates/master/Bootstrap3Accordion/manifest.json
26+
27+
// 4. lijst templates opbouwen voor tonen
28+
29+
// vervolgens:
30+
// 5. template laten kiezen
31+
// 6. om eigen/locale template naam vragen plus locatie (radio button)? host map /skin map / portal map
32+
33+
// 7. alle files uit het gekozen template downloaden naar locale template map
34+
// JSON lijst van files in OC template, voor download in locale map
35+
// https://api.github.com/repos/sachatrauwaen/OpenContent-Templates/contents/Bootstrap3Accordion
36+
37+
38+
private static string GetGitRepository(int portalId)
39+
{
40+
var globalSettingsRepository = App.Services.CreateGlobalSettingsRepository(portalId);
41+
return globalSettingsRepository.GetGithubRepository();
42+
}
43+
44+
// Git templates
45+
public static List<Contents> GetTemplateList(int portalId)
46+
{
47+
// we need to force the protocol to TLS 1.2
48+
if (ServicePointManager.SecurityProtocol != SecurityProtocolType.Tls12)
49+
{
50+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
51+
}
52+
List<Contents> contents = null;
53+
string url = "https://api.github.com/repos/"+GetGitRepository(portalId)+"/contents";
54+
HttpClient client = new HttpClient();
55+
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
56+
var response = client.GetStringAsync(new Uri(url)).Result;
57+
if (response != null)
58+
{
59+
//content = JArray.Parse(response);
60+
contents = Contents.FromJson(response);
61+
}
62+
return contents;
63+
}
64+
65+
public static List<Contents> GetFileList(int portalId, string path)
66+
{
67+
// we need to force the protocol to TLS 1.2
68+
if (ServicePointManager.SecurityProtocol != SecurityProtocolType.Tls12)
69+
{
70+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
71+
}
72+
List<Contents> contents = null;
73+
string url = "https://api.github.com/repos/"+GetGitRepository(portalId)+"/contents/" + path;
74+
HttpClient client = new HttpClient();
75+
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
76+
var response = client.GetStringAsync(new Uri(url)).Result;
77+
if (response != null)
78+
{
79+
contents = Contents.FromJson(response);
80+
}
81+
return contents;
82+
}
83+
84+
// all registed github templates (datasource for the repeater)
85+
public static List<Contents> ProcessGithubTemplatesNames(int portalId)
86+
{
87+
return GetTemplateList(portalId)
88+
.Where(t => t.Type == Components.Github.TypeEnum.Dir)
89+
.OrderBy(t => t.Name).ToList();
90+
}
91+
92+
/*
93+
public static JObject GetManifestFile(string templatename)
94+
{
95+
JObject manifest = null;
96+
string manfesturl = "https://raw.githubusercontent.com/schotman/OpenContent-Templates/gitTemplates/" + templatename + "/manifest.json";
97+
98+
// "https://raw.githubusercontent.com/sachatrauwaen/OpenContent-Templates/master/" + tempatename + "/manifest.json";
99+
// https://raw.githubusercontent.com/schotman/OpenContent-Templates/gitTemplates/Bootstrap3Columns/manifest.json
100+
101+
HttpClient client = new HttpClient();
102+
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
103+
104+
Uri uri = new Uri(manfesturl);
105+
Task<HttpResponseMessage> getManifest = client.GetAsync(uri);
106+
107+
var response = getManifest.GetAwaiter().GetResult();
108+
109+
if (response.IsSuccessStatusCode)
110+
{
111+
Task<string> content = response.Content.ReadAsStringAsync();
112+
content.GetAwaiter().GetResult();
113+
var c1 = content.Result;
114+
manifest = JObject.Parse(c1);
115+
}
116+
return manifest;
117+
}
118+
*/
119+
public static void SaveFileContent(Contents file, IFolderInfo folder)
120+
{
121+
HttpClient client = new HttpClient();
122+
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
123+
Task<HttpResponseMessage> getManifest = client.GetAsync(file.DownloadUrl);
124+
var response = getManifest.GetAwaiter().GetResult();
125+
if (response.IsSuccessStatusCode)
126+
{
127+
var content = response.Content.ReadAsStringAsync();
128+
var res = content.GetAwaiter().GetResult();
129+
File.WriteAllText(folder.PhysicalPath + file.Name, res);
130+
}
131+
}
132+
133+
public static string ImportFromGithub(int portalId, string name, string path, string newTemplateName)
134+
{
135+
string strMessage = "";
136+
try
137+
{
138+
var folder = FolderManager.Instance.GetFolder(portalId, "OpenContent/Templates");
139+
if (folder == null)
140+
{
141+
folder = FolderManager.Instance.AddFolder(portalId, "OpenContent/Templates");
142+
}
143+
if (String.IsNullOrEmpty(newTemplateName))
144+
{
145+
newTemplateName = name;
146+
}
147+
string folderName = "OpenContent/Templates/" + newTemplateName;
148+
folder = FolderManager.Instance.GetFolder(portalId, folderName);
149+
if (folder != null)
150+
{
151+
throw new Exception("Template already exist " + folder.FolderName);
152+
}
153+
folder = FolderManager.Instance.AddFolder(portalId, folderName);
154+
var fileList = GetFileList(portalId, path).Where(f => f.Type == TypeEnum.File);
155+
foreach (var file in fileList)
156+
{
157+
SaveFileContent(file, folder);
158+
}
159+
return OpenContentUtils.GetDefaultTemplate(folder.PhysicalPath);
160+
}
161+
catch (PermissionsNotMetException)
162+
{
163+
//Logger.Warn(exc);
164+
strMessage = String.Format(App.Services.Localizer.GetString("InsufficientFolderPermission"), "OpenContent/Templates");
165+
}
166+
catch (NoSpaceAvailableException)
167+
{
168+
//Logger.Warn(exc);
169+
strMessage = String.Format(App.Services.Localizer.GetString("DiskSpaceExceeded"), name);
170+
}
171+
catch (InvalidFileExtensionException)
172+
{
173+
//Logger.Warn(exc);
174+
strMessage = String.Format(App.Services.Localizer.GetString("RestrictedFileType"), name, Host.AllowedExtensionWhitelist.ToDisplayString());
175+
}
176+
catch (Exception exc)
177+
{
178+
//Logger.Error(exc);
179+
strMessage = String.Format(App.Services.Localizer.GetString("SaveFileError") + " - " + exc.Message, name);
180+
}
181+
if (!String.IsNullOrEmpty(strMessage))
182+
{
183+
throw new Exception(strMessage);
184+
}
185+
return "";
186+
}
187+
}
188+
}

0 commit comments

Comments
 (0)