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