Skip to content

Commit 32a6679

Browse files
committed
Fix: sync save uploaded file
1 parent 49bd53e commit 32a6679

32 files changed

+198
-90
lines changed

src/JR.Cms.App/public/assets/base.css

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/JR.Cms/Cms.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@
1616
using JR.Cms.Library.CacheProvider;
1717
using JR.Cms.Library.CacheProvider.CacheComponent;
1818
using JR.Cms.Library.DataAccess.DB;
19-
using JR.Cms.ServiceDto;
19+
using JR.Stand.Abstracts;
2020
using JR.Stand.Core;
21-
using JR.Stand.Core.Framework.IO;
22-
using JR.Stand.Core.Framework.Web;
21+
using JR.Stand.Core.Cache;
2322
using JR.Stand.Core.Framework.Web.UI;
2423
using JR.Stand.Core.PluginKernel;
2524
using JR.Stand.Core.Template.Impl;
26-
using JR.Stand.Core.Utils;
2725
using JR.Stand.Core.Web;
2826
using JR.Stand.Core.Web.Cache;
2927

src/JR.Cms/Conf/LangReader.cs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Web;
44
using System.Xml;
5+
using JR.Stand.Abstracts;
56
using JR.Stand.Core.Utils;
67
using JR.Stand.Core.Web.Cache;
78

src/JR.Cms/Core/CmsTemplate.cs

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Collections.Generic;
1616
using System.Text.RegularExpressions;
1717
using JR.Cms.Conf;
18+
using JR.Stand.Abstracts;
1819
using JR.Stand.Abstracts.Web;
1920
using JR.Stand.Core.Framework.Web;
2021
using JR.Stand.Core.Template.Impl;
@@ -237,6 +238,14 @@ public void Reload()
237238
registry.Reload();
238239
}
239240

241+
/// <summary>
242+
/// 清理页面缓存
243+
/// </summary>
244+
public void CleanPageCache()
245+
{
246+
Cms.Cache.RemoveKeys("site:page");
247+
}
248+
240249
/// <summary>
241250
///
242251
/// </summary>

src/JR.Cms/Library/CacheProvider/CacheComponent/BuiltCacheResultHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ namespace JR.Cms.Library.CacheProvider.CacheComponent
1313
/// 创建缓存数据结果事件
1414
/// </summary>
1515
/// <returns></returns>
16-
public delegate T BuiltCacheResultHandler<T>();
16+
public delegate T BuiltCacheResultHandler<out T>();
1717
}

src/JR.Cms/Library/CacheProvider/CacheComponent/CmsCache.cs

+36-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.IO;
1313
using System.Text;
1414
using JR.Cms.Infrastructure;
15+
using JR.Stand.Abstracts;
1516
using JR.Stand.Abstracts.Web;
1617
using JR.Stand.Core.Framework;
1718
using JR.Stand.Core.Framework.Web.Cache;
@@ -47,10 +48,14 @@ public override void Insert(string key, object value, string filename)
4748

4849
public override void Clear(string keySign)
4950
{
50-
if (keySign != null)
51-
foreach (var key in cache.GetCacheKeys())
52-
if (key.StartsWith(keySign))
53-
cache.Remove(key);
51+
if (keySign == null) return;
52+
foreach (var key in cache.GetCacheKeys())
53+
{
54+
if (key.StartsWith(keySign))
55+
{
56+
cache.Remove(key);
57+
}
58+
}
5459
}
5560

5661
public override object Get(string key)
@@ -99,6 +104,19 @@ public override int GetInt(string key)
99104
{
100105
return this.cache.GetInt(key);
101106
}
107+
108+
/// <summary>
109+
/// 删除指定前缀的键
110+
/// </summary>
111+
/// <param name="prefix"></param>
112+
public override void RemoveKeys(string prefix)
113+
{
114+
var list = this.cache.SearchKeys("^" + prefix);
115+
foreach (var l in list)
116+
{
117+
this.cache.Remove(l);
118+
}
119+
}
102120
}
103121

104122

@@ -182,11 +200,20 @@ public void Insert(string key, object value)
182200
_dependCache.Insert(key, value);
183201
}
184202

203+
/// <summary>
204+
///
205+
/// </summary>
206+
/// <param name="keySign"></param>
185207
public void Clear(string keySign)
186208
{
187209
_dependCache.Clear(keySign);
188210
}
189211

212+
/// <summary>
213+
///
214+
/// </summary>
215+
/// <param name="cacheKey"></param>
216+
/// <returns></returns>
190217
public object Get(string cacheKey)
191218
{
192219
return _dependCache.Get(cacheKey);
@@ -204,5 +231,10 @@ public int GetInt(string key)
204231
{
205232
return this._dependCache.GetInt(key);
206233
}
234+
235+
public void RemoveKeys(string prefix)
236+
{
237+
this._dependCache.RemoveKeys(prefix);
238+
}
207239
}
208240
}

src/JR.Cms/Library/CacheProvider/CacheComponent/CmsCacheBase.cs

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public virtual string Rebuilt()
5454
}
5555

5656
public abstract int GetInt(string key);
57+
public abstract void RemoveKeys(string prefix);
5758

5859

5960
/// <summary>

src/JR.Cms/Library/CacheProvider/CacheComponent/CmsCacheUtility.cs

+10-3
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,25 @@
99

1010
using System;
1111
using System.Reflection;
12+
using JR.Stand.Abstracts.Cache;
1213

1314
namespace JR.Cms.Library.CacheProvider.CacheComponent
1415
{
1516
/// <summary>
1617
/// Description of CmsCacheUtility.
1718
/// </summary>
18-
public class CmsCacheUtility
19+
public static class CmsCacheUtility
1920
{
20-
public static void EvalCacheUpdate<T>(MethodInfo method) where T : Attribute, ICacheUpdatePolicy
21+
/// <summary>
22+
///
23+
/// </summary>
24+
/// <param name="method"></param>
25+
/// <typeparam name="T"></typeparam>
26+
public static void EvalCacheUpdate<T>(MethodInfo method) where T : Attribute, ICachePolicy
2127
{
2228
var attrs = method.GetCustomAttributes(typeof(T), false);
23-
if (attrs.Length != 0) (attrs[0] as ICacheUpdatePolicy).Clear();
29+
if (attrs.Length == 0) return;
30+
(attrs[0] as ICachePolicy)?.Clean();
2431
}
2532
}
2633
}

src/JR.Cms/Library/CacheProvider/CacheComponent/ICmsCache.cs

+6
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,11 @@ public interface ICmsCache
4848
/// <param name="key">键</param>
4949
/// <returns></returns>
5050
int GetInt(string key);
51+
52+
/// <summary>
53+
/// 移出指定前缀的缓存
54+
/// </summary>
55+
/// <param name="prefix"></param>
56+
void RemoveKeys(string prefix);
5157
}
5258
}

src/JR.Cms/Library/CacheProvider/CacheUpdateAttribute.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,30 @@
99

1010
using System;
1111
using JR.Cms.Library.CacheProvider.CacheComponent;
12+
using JR.Stand.Abstracts.Cache;
1213

1314
namespace JR.Cms.Library.CacheProvider
1415
{
1516
/// <summary>
1617
/// Description of CacheUpdateAttribute.
1718
/// </summary>
1819
[AttributeUsage(AttributeTargets.Method)]
19-
public class CacheUpdateAttribute : Attribute, ICacheUpdatePolicy
20+
public class CacheAttribute : Attribute, ICachePolicy
2021
{
21-
public CacheUpdateAttribute(string cacheKey)
22+
public CacheAttribute(string cacheKey)
2223
{
2324
Key = cacheKey;
2425
}
2526

26-
public CacheUpdateAttribute(CacheSign sign)
27+
public CacheAttribute(CacheSign sign)
2728
{
2829
Key = sign.ToString();
2930
}
3031

3132
public string Key { get; private set; }
3233

3334

34-
public void Clear()
35+
public void Clean()
3536
{
3637
CmsCacheFactory.Singleton.Clear(Key);
3738
}

src/JR.Cms/Library/CacheProvider/CmsCacheFactory.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using JR.Cms.Library.CacheProvider.CacheComponent;
2+
using JR.Stand.Abstracts;
23
using JR.Stand.Core.Utils;
34

45
namespace JR.Cms.Library.CacheProvider

src/JR.Cms/Web/Editor/KindEditor.cs

+18-14
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public Task FileManagerRequest(ICompatibleHttpContext context)
6969
String currentDirPath = "";
7070
String moveUpDirPath = "";
7171

72-
String dirPath = EnvUtil.GetBaseDirectory()+this._rootPath;
72+
String dirPath = EnvUtil.GetBaseDirectory() + this._rootPath;
7373
String dirName = context.Request.Query("dir");
7474
if (!String.IsNullOrEmpty(dirName))
7575
{
@@ -199,7 +199,6 @@ public Task FileManagerRequest(ICompatibleHttpContext context)
199199
}
200200

201201

202-
203202
/// <summary>
204203
/// 处理上传
205204
/// </summary>
@@ -225,10 +224,10 @@ public Task UploadRequest(ICompatibleHttpContext context)
225224
ICompatiblePostedFile imgFile = context.Request.File("imgFile");
226225
if (imgFile == null)
227226
{
228-
return this.showError(context,"请选择文件。");
227+
return this.showError(context, "请选择文件。");
229228
}
230229

231-
String dirPath = EnvUtil.GetBaseDirectory()+ this._rootPath;
230+
String dirPath = EnvUtil.GetBaseDirectory() + this._rootPath;
232231
if (!Directory.Exists(dirPath))
233232
{
234233
Directory.CreateDirectory(dirPath).Create();
@@ -243,21 +242,21 @@ public Task UploadRequest(ICompatibleHttpContext context)
243242

244243
if (!extTable.ContainsKey(dirName))
245244
{
246-
return this.showError(context,"目录名不正确。");
245+
return this.showError(context, "目录名不正确。");
247246
}
248247

249248
String fileName = imgFile.GetFileName();
250249
String fileExt = Path.GetExtension(fileName).ToLower();
251250

252251
if (imgFile.GetLength() > maxSize)
253252
{
254-
return this.showError(context,"上传文件大小超过限制。");
253+
return this.showError(context, "上传文件大小超过限制。");
255254
}
256255

257256
if (String.IsNullOrEmpty(fileExt) ||
258257
Array.IndexOf(((String) extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
259258
{
260-
return this.showError(context,"上传文件扩展名是不允许的扩展名。\n只允许" + ((String) extTable[dirName]) + "格式。");
259+
return this.showError(context, "上传文件扩展名是不允许的扩展名。\n只允许" + ((String) extTable[dirName]) + "格式。");
261260
}
262261

263262
//创建文件夹
@@ -290,11 +289,7 @@ public Task UploadRequest(ICompatibleHttpContext context)
290289
targetPath = dirPath + newFileName;
291290
}
292291

293-
using (FileStream fs = new FileStream(targetPath, FileMode.Create))
294-
{
295-
imgFile.CopyToAsync(fs);
296-
fs.Flush();
297-
}
292+
SaveFile(imgFile, targetPath);
298293

299294
String fileUrl = saveUrl + newFileName;
300295

@@ -304,12 +299,21 @@ public Task UploadRequest(ICompatibleHttpContext context)
304299
return context.Response.WriteAsync(JsonAnalyzer.ToJson(hash));
305300
}
306301

307-
private Task showError(ICompatibleHttpContext context,string message)
302+
private async void SaveFile(ICompatiblePostedFile imgFile, string targetPath)
303+
{
304+
using (FileStream fs = new FileStream(targetPath, FileMode.Create))
305+
{
306+
await imgFile.CopyToAsync(fs);
307+
fs.Flush();
308+
}
309+
}
310+
311+
private Task showError(ICompatibleHttpContext context, string message)
308312
{
309313
Hashtable hash = new Hashtable {["error"] = 1, ["message"] = message};
310314
return context.Response.WriteAsync(JsonAnalyzer.ToJson(hash));
311315
}
312-
316+
313317
public class NameSorter : IComparer
314318
{
315319
public int Compare(object x, object y)

src/JR.Cms/Web/Manager/FileExplor.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ private static string CreateFile(string filePath)
236236
return "{}";
237237
}
238238

239-
239+
240240

241241
/// <summary>
242242
///
@@ -251,13 +251,19 @@ public static string Upload(string dir, ICompatiblePostedFile file)
251251

252252
var filePath = dirPath + file.GetFileName();
253253
if (File.Exists(filePath)) return "{\"error\":\"文件已经存在\"}";
254+
255+
SaveFile(file, filePath);
256+
257+
return "{\"url\":\"" + dir + fileName + "\"}";
258+
}
259+
260+
private static async void SaveFile(ICompatiblePostedFile file, string filePath)
261+
{
254262
using (var fs = new FileStream(filePath, FileMode.Create))
255263
{
256-
file.CopyToAsync(fs);
264+
await file.CopyToAsync(fs);
257265
fs.Flush();
258266
}
259-
260-
return "{\"url\":\"" + dir + fileName + "\"}";
261267
}
262268
}
263269
}

src/JR.Cms/Web/Manager/Handle/CategoryHandler.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,10 @@ public void Create()
115115
RenderTemplate(ResourceMap.GetPageContent(ManagementPage.Category_CreateCategory), data);
116116
}
117117

118-
[MCacheUpdate(CacheSign.Category | CacheSign.Link)]
118+
[MCache(CacheSign.Category | CacheSign.Link)]
119119
public string Create_POST()
120120
{
121-
122-
var parentId = 0;
123-
int.TryParse(Request.Form("ParentId"), out parentId);
121+
int.TryParse(Request.Form("ParentId"), out var parentId);
124122

125123
var category = InitCategoryDtoFromHttpPost(Request, new CategoryDto());
126124

@@ -264,7 +262,7 @@ public void Update()
264262
}
265263

266264

267-
[MCacheUpdate(CacheSign.Category | CacheSign.Link)]
265+
[MCache(CacheSign.Category | CacheSign.Link)]
268266
public string Update_POST()
269267
{
270268
var category = ServiceCall.Instance.SiteService.GetCategory(
@@ -286,7 +284,7 @@ public string Update_POST()
286284
/// <summary>
287285
/// 删除栏目
288286
/// </summary>
289-
[MCacheUpdate(CacheSign.Category | CacheSign.Link)]
287+
[MCache(CacheSign.Category | CacheSign.Link)]
290288
public string Delete_POST()
291289
{
292290
var categoryId = int.Parse(Request.Form("category_id"));

0 commit comments

Comments
 (0)