-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageDescriptor.cs
279 lines (235 loc) · 11.8 KB
/
ImageDescriptor.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using System.IO;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.PixelFormats;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Runtime.Serialization;
//using System.Runtime.Serialization.Formatters.Soap;
//using System.Runtime.Serialization.Formatters.Binary;
namespace CSharp_Image_Action
{
[Serializable]
public class DirectoryDescriptor
{
[JsonPropertyName("GalleryName")]
public string GalleryName { get => directoryName; }
[JsonPropertyName("DirectoryName")]
public string DirectoryName { get => "/" + directoryName; }
protected string directoryName;
[JsonPropertyName("FullDirectoryPath")]
public string FullPath { get => "/" + fullPath.Replace(GitHubRepoRoot.FullName,"").Replace("\\","/"); }
private string fullPath;
[JsonPropertyName("GalleryIndexHTMLFile")]
public string GalleryIndexHTMLFile { get => "/" + indexfilename.FullName.Replace(GitHubRepoRoot.FullName,"").Replace("\\","/").Replace(".md",".html"); }
[JsonPropertyName("GalleryIndexMarkdownFile")]
public string IndexFileName { get => "/" + indexfilename.FullName.Replace(GitHubRepoRoot.FullName,"").Replace("\\","/"); }
protected System.IO.FileInfo indexfilename;
[JsonPropertyName("GalleryThumbnail")]
public string GalleryThumbnail { get; set; }
[JsonPropertyName("PhotoGalleries")] //JsonExtensionData
public List<DirectoryDescriptor> Directories { get => directories; set => directories = value; }
private System.Collections.Generic.List<DirectoryDescriptor> directories = new System.Collections.Generic.List<DirectoryDescriptor>();
[JsonPropertyName("Images")]
public List<ImageDescriptor> Images { get => images; set => images = value; }
private System.Collections.Generic.List<ImageDescriptor> images = new System.Collections.Generic.List<ImageDescriptor>();
// Ensures ever run of this has some changes
[JsonPropertyName("GeneratedDateTime")]
public string GeneratedDateTime { get => System.DateTime.UtcNow.ToString(); }
private DirectoryInfo GitHubRepoRoot;
public DirectoryDescriptor(string directoryname, string fullpath)
{
this.directoryName = directoryname;
this.fullPath = fullpath;
}
public void FixUpPaths(DirectoryInfo di)
{
GitHubRepoRoot = di;
foreach(DirectoryDescriptor dd in directories)
{
dd.FixUpPaths(di);
}
bool first = true;
foreach(ImageDescriptor i in images)
{
i.FixUpPaths(di);
if(first || i.Name.ToLower().StartsWith("thumbnail"))
{
GalleryThumbnail = i.JSON_ThumbnailFile;
first = false; // we need to have a thumbnail, so it's the first image
}
}
}
// Only 1 directory deep support currently. not a problem because Jekyll doesn't support it
public void SaveMDFiles(string Domain, System.IO.DirectoryInfo ImagesDirectory, PaulsOKWrapper github)
{
if(directoryName == ImagesDirectory.Name)
{
indexfilename = new FileInfo(ImagesDirectory.FullName + "\\Index.md");
}else{
indexfilename = new FileInfo(ImagesDirectory.FullName + "\\" + directoryName + ".md");
}
foreach(DirectoryDescriptor dd in directories)
{
dd.SaveMDFiles(Domain, ImagesDirectory, github);
}
//we need to overwrite them even if they exist (but we need some fortmat to allow edits)
{
CreateIndexFile( Domain, ImagesDirectory, indexfilename);
System.Console.WriteLine("Create : " + indexfilename.FullName);
}
if(github.DoGitHubStuff && github.ImagesWereCommtted)
{
System.Threading.Tasks.ValueTask<bool> task = github.ImmediatlyAddorUpdateTextFile(indexfilename);
//this awaits the result of the above async call
var result = task.GetAwaiter().GetResult();
}
}
public void CreateIndexFile( string Domain, System.IO.DirectoryInfo ImagesDirectory, System.IO.FileInfo fi)
{
System.IO.TextWriter tw = fi.CreateText();
//bool DirectoryListing = false;
tw.WriteLine("---");
if(directoryName == ImagesDirectory.Name)
{
tw.WriteLine("permalink: /" + ImagesDirectory.Name + "/index.html");
tw.WriteLine("title: /" + ImagesDirectory.Name + "/index" );
tw.WriteLine("page_variable: " + this.directoryName );
//DirectoryListing = true;
}else{
tw.WriteLine("permalink: /" + ImagesDirectory.Name + "/" + this.directoryName + ".html");
tw.WriteLine("title: /" + ImagesDirectory.Name + "/" + this.directoryName );
tw.WriteLine("page_variable: " + ImagesDirectory.Name + "/" + this.directoryName );
}
tw.WriteLine("Gallery_Page: " + this.directoryName );
tw.WriteLine("Gallery_Name: " + this.directoryName );
tw.WriteLine("---");
tw.WriteLine();
tw.WriteLine();
tw.WriteLine("{% include header.md %}");
tw.WriteLine();
tw.WriteLine("# " + this.directoryName);
tw.WriteLine();
tw.WriteLine("----");
tw.WriteLine("<div class=\"image-container-" + this.directoryName + " ImgContainer\">");
foreach(ImageDescriptor i in images)
{
WriteImage( tw, i, Domain, ImagesDirectory);
}
tw.WriteLine("</div>");
tw.WriteLine("----");
tw.WriteLine();
foreach(DirectoryDescriptor d in Directories)
{
WriteDirectory( tw, d, ImagesDirectory);
}
tw.WriteLine();
tw.WriteLine("[Gallery]( ./index.html)");
tw.WriteLine(" or ");
tw.WriteLine("[Home]( ../)");
tw.WriteLine();
tw.WriteLine("----");
tw.WriteLine();
//if(!DirectoryListing)
{
tw.WriteLine("<script>");
tw.WriteLine();
tw.WriteLine("{% include single-gallery.js %}");
tw.WriteLine();
tw.WriteLine("SetupGallery(\".image-container-" + this.directoryName + " ImgContainer\");");
tw.WriteLine();
tw.WriteLine("</script>");
tw.WriteLine();
}
tw.WriteLine("{% include footer.md %}");
tw.WriteLine();
tw.WriteLine("<!-- created on " + System.DateTime.Now.ToString("MM/dd/yyyy h:mm tt") + " -->");
tw.Flush();
tw.Close();
}
public void WriteImage(System.IO.TextWriter textWriter, ImageDescriptor id, string Domain, System.IO.DirectoryInfo ImagesDirectory)
{
textWriter.WriteLine("<a href=\"" + id.ReSizedFileInfo.FullName.Replace(ImagesDirectory.FullName,"").Replace(@"\","/") + "\" data-fancybox=\"" + id.ThumbNailFile.FullName.Replace(GitHubRepoRoot.FullName,"").Replace(@"\","/") + "\" data-caption=\"" + id.Name + "\">");
textWriter.WriteLine(" <img class=\"image-thumb\" src=\"" + Domain + "/" + id.ThumbNailFile.FullName.Replace(GitHubRepoRoot.FullName,"").Replace(@"\","/") + "\" alt=\"" + id.Name + "\" />");
textWriter.WriteLine("</a>");
//textWriter.WriteLine("[![" + id.Name + "](" /* + Domain + "/" */ + id.ThumbNailFile.FullName.Replace(GitHubRepoRoot.FullName,"").Replace(@"\","/") + ")](" + id.ReSizedFileInfo.FullName.Replace(ImagesDirectory.FullName,"").Replace(@"\","/") + ")" );
}
public void WriteDirectory(System.IO.TextWriter textWriter, DirectoryDescriptor dd, System.IO.DirectoryInfo ImagesDirectory)
{
textWriter.WriteLine("### [" + dd.directoryName + "]( ./"+ dd.indexfilename.Name.Replace(".md",".html") + ")");
if(dd.GalleryThumbnail != null)
{
var inlinelater = dd.GalleryThumbnail.Replace(ImagesDirectory.Name,"").Replace(@"\","/");
textWriter.WriteLine("[ ![" + dd.directoryName + "-Thumbnail](" + inlinelater + ")]( ./"+ dd.indexfilename.Name.Replace(".md",".html") + ")");
}
textWriter.WriteLine();
}
}
[Serializable()]
public class ImageDescriptor
{
# region Static
public static string thumbnail = "thumbnail";
public static string resized = "resized";
#endregion
private int width;
public int ImageWidth { get => width; set => width = value; }
private int height;
public int ImageHeight { get => height; set => height = value; }
private string name;
public string Name { get => name; set => name = value; }
private string folder;
public string Folder { get => folder; set => folder = value; }
[JsonIgnore]
public FileInfo ImageFile { get => file; }
[JsonPropertyName("OriginalFilePath")]
public string JSON_ImageFIle { get => file.FullName.Replace(GitHubRepoRoot.FullName,"");}
protected FileInfo file;
protected FileInfo reSizedFileInfo;
[JsonIgnore]
public string ReSizedFileName { get => reSizedFileName; set => reSizedFileName = value; }
private string reSizedFileName;
[JsonIgnore]
public FileInfo ReSizedFileInfo { get => reSizedFileInfo; set => reSizedFileInfo = value; }
[JsonPropertyName("ResizedFilePath")]
public string JSON_ResizedImage { get => reSizedFileInfo.FullName.Replace(GitHubRepoRoot.FullName,"");}
private string thumbnail_name;
protected System.IO.FileInfo thumbNailFile;
[JsonIgnore]
public string ThumbnailName { get => thumbnail_name; }
[JsonIgnore]
public FileInfo ThumbNailFile { get => thumbNailFile; set => thumbNailFile = value; }
[JsonPropertyName("ThumbnailFilePath")]
public string JSON_ThumbnailFile { get => thumbNailFile.FullName.Replace(GitHubRepoRoot.FullName,"");}
protected DirectoryInfo directory;
private DirectoryInfo GitHubRepoRoot;
public ImageDescriptor(System.IO.DirectoryInfo di, System.IO.FileInfo fi)
{
directory = di;
file = fi;
name = fi.Name;
folder = di.FullName;
thumbnail_name = ImageDescriptor.thumbnail + "-" + directory.Name + "-" + file.Name;
ReSizedFileName = folder + "\\" + ImageDescriptor.resized + "-" + name;
}
public void FillBasicInfo()
{
var inStream = file.OpenRead();
using (Image image = Image.Load(inStream))
{
height = image.Height;
width = image.Width;
}
inStream.Close();
}
public void FixUpPaths(System.IO.DirectoryInfo di)
{
this.GitHubRepoRoot = di;
thumbnail_name = "/" + thumbnail_name.Replace(GitHubRepoRoot.FullName,"").Replace("\\","/");
ReSizedFileName = "/" + ReSizedFileName.Replace(GitHubRepoRoot.FullName,"").Replace("\\","/");
folder = "/" + folder.Replace(GitHubRepoRoot.FullName,"").Replace("\\","/");
}
}
}