-
Notifications
You must be signed in to change notification settings - Fork 64
/
PostStoragePathResolver.cs
222 lines (175 loc) · 8.17 KB
/
PostStoragePathResolver.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
// Copyright (c) Source Tree Solutions, LLC. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Author: Joe Audette
// Created: 2016-04-24
// Last Modified: 2018-06-28
//
using cloudscribe.SimpleContent.Models;
using NoDb;
using System;
using System.IO;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
namespace cloudscribe.SimpleContent.Storage.NoDb
{
public class PostStoragePathResolver : IStoragePathResolver<Post>
{
public PostStoragePathResolver(
IStoragePathOptionsResolver storageOptionsResolver)
{
optionsResolver = storageOptionsResolver;
}
private IStoragePathOptionsResolver optionsResolver;
/// <summary>
/// if key is not provided this method will return the post folder path
/// if key is provided it will try to find the file which should be nested in year/month
/// folder based on pubdate, if the file exists it will return the path to the file
/// if not it will return the post folder plus key plus file extension
/// however that is not the correct place to store a new post. The other ResolvePath method
/// which takes an instance of Post should be used for determining where to save a post
/// </summary>
/// <param name="projectId"></param>
/// <param name="key"></param>
/// <param name="fileExtension"></param>
/// <param name="ensureFoldersExist"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<string> ResolvePath(
string projectId,
string key = "",
string fileExtension = ".json",
bool ensureFoldersExist = false,
CancellationToken cancellationToken = default(CancellationToken)
)
{
if (string.IsNullOrWhiteSpace(projectId)) throw new ArgumentException("projectId must be provided");
var pathOptions = await optionsResolver.Resolve(projectId).ConfigureAwait(false);
var firstFolderPath = pathOptions.AppRootFolderPath
+ pathOptions.BaseFolderVPath.Replace("/", pathOptions.FolderSeparator);
if (ensureFoldersExist && !Directory.Exists(firstFolderPath))
{
Directory.CreateDirectory(firstFolderPath);
}
var projectsFolderPath = Path.Combine(firstFolderPath, pathOptions.ProjectsFolderName);
if (ensureFoldersExist && !Directory.Exists(projectsFolderPath))
{
Directory.CreateDirectory(projectsFolderPath);
}
var projectIdFolderPath = Path.Combine(projectsFolderPath, projectId);
if (ensureFoldersExist && !Directory.Exists(projectIdFolderPath))
{
Directory.CreateDirectory(projectIdFolderPath);
}
var type = typeof(Post).Name.ToLowerInvariant();
var typeFolderPath = Path.Combine(projectIdFolderPath, type.ToLowerInvariant().Trim());
if (ensureFoldersExist && !Directory.Exists(typeFolderPath))
{
Directory.CreateDirectory(typeFolderPath);
}
if (string.IsNullOrWhiteSpace(key))
{
return typeFolderPath;
}
var fileName = key + ".xml";
var filePath = Path.Combine(typeFolderPath, key + fileExtension);
if (File.Exists(filePath)) return filePath;
// if the file is not found in the type folder
// we need to check for deeper folders year/month
// if the file is found there then return the path
foreach (string file in Directory.EnumerateFiles(
typeFolderPath,
fileName,
SearchOption.AllDirectories) // this is needed for blog posts which are nested in year/month folders
)
{
return file;
}
fileName = key + ".md";
filePath = Path.Combine(typeFolderPath, key + fileExtension);
if (File.Exists(filePath)) return filePath;
//try again with .md
foreach (string file in Directory.EnumerateFiles(
typeFolderPath,
fileName,
SearchOption.AllDirectories) // this is needed for blog posts which are nested in year/month folders
)
{
return file;
}
// otherwise return the best path calculation based on info provided
// ie to save a file the other ResolvePath method should be used and the
// Post should be passed in to determine the year/month path
return filePath;
}
/// <summary>
/// this method should be used to calculate the path to create or update a post
/// it will use the year/month of the post.PubDate in determining the path
/// </summary>
/// <param name="projectId"></param>
/// <param name="key"></param>
/// <param name="post"></param>
/// <param name="fileExtension"></param>
/// <param name="ensureFoldersExist"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<string> ResolvePath(
string projectId,
string key,
Post post,
string fileExtension = ".json",
bool ensureFoldersExist = false,
CancellationToken cancellationToken = default(CancellationToken)
)
{
if (string.IsNullOrWhiteSpace(projectId)) throw new ArgumentException("projectId must be provided");
var pathOptions = await optionsResolver.Resolve(projectId).ConfigureAwait(false);
var firstFolderPath = pathOptions.AppRootFolderPath
+ pathOptions.BaseFolderVPath.Replace("/", pathOptions.FolderSeparator);
if (ensureFoldersExist && !Directory.Exists(firstFolderPath))
{
Directory.CreateDirectory(firstFolderPath);
}
var projectsFolderPath = Path.Combine(firstFolderPath, pathOptions.ProjectsFolderName);
if (ensureFoldersExist && !Directory.Exists(projectsFolderPath))
{
Directory.CreateDirectory(projectsFolderPath);
}
var projectIdFolderPath = Path.Combine(projectsFolderPath, projectId);
if (ensureFoldersExist && !Directory.Exists(projectIdFolderPath))
{
Directory.CreateDirectory(projectIdFolderPath);
}
var type = typeof(Post).Name.ToLowerInvariant();
var typeFolderPath = Path.Combine(projectIdFolderPath, type.ToLowerInvariant().Trim());
if (ensureFoldersExist && !Directory.Exists(typeFolderPath))
{
Directory.CreateDirectory(typeFolderPath);
}
var pubDate = post.PubDate;
if(!pubDate.HasValue)
{
pubDate = DateTime.UtcNow;
}
var yearFolderName = pubDate.Value.Year.ToString(CultureInfo.InvariantCulture);
var monthFolderName = pubDate.Value.Month.ToString("00", CultureInfo.InvariantCulture);
var yearPath = Path.Combine(typeFolderPath, yearFolderName);
if (ensureFoldersExist && !Directory.Exists(yearPath))
{
Directory.CreateDirectory(yearPath);
}
var monthPath = Path.Combine(yearPath, monthFolderName);
if (ensureFoldersExist && !Directory.Exists(monthPath))
{
Directory.CreateDirectory(monthPath);
}
// we don't care if this file eists
// this method is for calculating where to save a post
if(post.ContentType == "markdown")
{
return Path.Combine(monthPath, key + ".md");
}
return Path.Combine(monthPath, key + ".xml");
}
}
}