Skip to content

Commit

Permalink
升级到.NET Core 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ldqk committed Oct 20, 2019
1 parent d253f51 commit 861359d
Show file tree
Hide file tree
Showing 32 changed files with 267 additions and 231 deletions.
2 changes: 1 addition & 1 deletion src/Masuit.MyBlogs.Core/Common/CommonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public static string GetIPLocation(this string ip)
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static T Mapper<T>(this object source) where T : class => Startup.AutofacContainer.GetRequiredService<IMapper>().Map<T>(source);
public static T Mapper<T>(this object source) where T : class => Startup.ServiceProvider.GetRequiredService<IMapper>().Map<T>(source);

/// <summary>
/// 发送邮件
Expand Down
19 changes: 19 additions & 0 deletions src/Masuit.MyBlogs.Core/Configs/AutofacModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Autofac;
using Hangfire;
using Masuit.MyBlogs.Core.Extensions;
using Masuit.MyBlogs.Core.Extensions.Hangfire;
using System.Reflection;

namespace Masuit.MyBlogs.Core.Configs
{
public class AutofacModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces().Where(t => t.Name.EndsWith("Repository") || t.Name.EndsWith("Service") || t.Name.EndsWith("Controller") || t.Name.EndsWith("Attribute")).PropertiesAutowired().AsSelf().InstancePerDependency();
builder.RegisterType<BackgroundJobClient>().SingleInstance();
builder.RegisterType<FirewallAttribute>().PropertiesAutowired().AsSelf().InstancePerDependency();
builder.RegisterType<HangfireBackJob>().As<IHangfireBackJob>().PropertiesAutowired(PropertyWiringOptions.PreserveSetValues).InstancePerDependency();
}
}
}
30 changes: 0 additions & 30 deletions src/Masuit.MyBlogs.Core/Configs/GitlabConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,5 @@ public class GitlabConfig
public string AccessToken { get; set; }
public string Branch { get; set; }
public int FileLimitSize { get; set; }

/// <summary>Determines whether the specified object is equal to the current object.</summary>
/// <param name="obj">The object to compare with the current object.</param>
/// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
public override bool Equals(object obj)
{
return AccessToken == ((GitlabConfig)obj).AccessToken;
}

/// <summary>
/// 判等
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public static bool operator ==(GitlabConfig x, GitlabConfig y)
{
return x?.AccessToken == y?.AccessToken;
}

/// <summary>
/// 判不等
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public static bool operator !=(GitlabConfig x, GitlabConfig y)
{
return x?.AccessToken != y?.AccessToken;
}
}
}
2 changes: 1 addition & 1 deletion src/Masuit.MyBlogs.Core/Configs/HangfireJobInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class HangfireJobInit
/// </summary>
public static void Start()
{
RecurringJob.AddOrUpdate(() => CheckLinks(), Cron.HourInterval(5)); //每5h检查友链
RecurringJob.AddOrUpdate(() => CheckLinks(), "0 */5 * * *"); //每5h检查友链
RecurringJob.AddOrUpdate(() => EverydayJob(), Cron.Daily(5), TimeZoneInfo.Local); //每天的任务
RecurringJob.AddOrUpdate(() => EveryweekJob(), Cron.Weekly(DayOfWeek.Monday, 5), TimeZoneInfo.Local); //每周的任务
RecurringJob.AddOrUpdate(() => EveryHourJob(), Cron.Hourly); //每小时的任务
Expand Down
6 changes: 2 additions & 4 deletions src/Masuit.MyBlogs.Core/Controllers/BaseController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using AutoMapper;
using AutoMapper.QueryableExtensions;
using EFSecondLevelCache.Core;
using Masuit.MyBlogs.Core.Common;
using Masuit.MyBlogs.Core.Configs;
using Masuit.MyBlogs.Core.Extensions;
Expand Down Expand Up @@ -143,9 +141,9 @@ public override void OnActionExecuted(ActionExecutedContext filterContext)
#region 准备页面数据模型

ViewBag.menus = MenuService.GetQueryFromCache<MenuOutputDto>(m => m.Status == Status.Available).OrderBy(m => m.Sort).ToList(); //菜单
PageFootViewModel model = new PageFootViewModel //页脚
var model = new PageFootViewModel //页脚
{
Links = LinksService.GetQuery(l => l.Status == Status.Available, l => l.Recommend, false).ThenByDescending(l => l.Weight).ThenByDescending(l => new Random().Next()).Take(40).ProjectTo<LinksOutputDto>(MapperConfig).Cacheable().ToList()
Links = LinksService.GetQueryFromCache<LinksOutputDto>(l => l.Status == Status.Available).OrderBy(l => l.Recommend).ThenByDescending(l => l.Weight).ThenByDescending(l => new Random().Next()).Take(40).ToList()
};
ViewBag.Footer = model;

Expand Down
7 changes: 3 additions & 4 deletions src/Masuit.MyBlogs.Core/Controllers/CommentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Masuit.MyBlogs.Core.Models.Entity;
using Masuit.MyBlogs.Core.Models.Enum;
using Masuit.MyBlogs.Core.Models.ViewModel;
using Masuit.Tools;
using Masuit.Tools.Core.Net;
using Masuit.Tools.Html;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -30,7 +29,7 @@ public class CommentController : BaseController
public ICommentService CommentService { get; set; }
public IPostService PostService { get; set; }
public IInternalMessageService MessageService { get; set; }
public IHostingEnvironment HostingEnvironment { get; set; }
public IWebHostEnvironment HostEnvironment { get; set; }

/// <summary>
/// 发表评论
Expand Down Expand Up @@ -95,7 +94,7 @@ public ActionResult Put(CommentInputDto dto)
var emails = new HashSet<string>();
var email = CommonHelper.SystemSettings["ReceiveEmail"]; //站长邮箱
emails.Add(email);
var content = System.IO.File.ReadAllText(HostingEnvironment.WebRootPath + "/template/notify.html")
var content = System.IO.File.ReadAllText(HostEnvironment.WebRootPath + "/template/notify.html")
.Replace("{{title}}", post.Title)
.Replace("{{time}}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
.Replace("{{nickname}}", comment.NickName)
Expand Down Expand Up @@ -237,7 +236,7 @@ public ActionResult Pass(int id)
{
var pid = comment.ParentId == 0 ? comment.Id : CommentService.GetParentCommentIdByChildId(id);
#if !DEBUG
var content = System.IO.File.ReadAllText(Path.Combine(HostingEnvironment.WebRootPath, "template", "notify.html"))
var content = System.IO.File.ReadAllText(Path.Combine(HostEnvironment.WebRootPath, "template", "notify.html"))
.Replace("{{title}}", post.Title)
.Replace("{{time}}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
.Replace("{{nickname}}", comment.NickName)
Expand Down
5 changes: 2 additions & 3 deletions src/Masuit.MyBlogs.Core/Controllers/DashboardController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Masuit.MyBlogs.Core.Models.Enum;
using Masuit.Tools.Logging;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -32,7 +31,7 @@ public class DashboardController : AdminController
/// 控制面板
/// </summary>
/// <returns></returns>
[Route("dashboard"), ResponseCache(Duration = 60, VaryByHeader = HeaderNames.Cookie)]
[Route("dashboard"), ResponseCache(Duration = 60, VaryByHeader = "Cookie")]
public ActionResult Index()
{
return View();
Expand Down Expand Up @@ -119,7 +118,7 @@ public ActionResult DeleteFile(string filename)
/// 资源管理器
/// </summary>
/// <returns></returns>
[Route("filemanager"), ResponseCache(Duration = 60, VaryByHeader = HeaderNames.Cookie)]
[Route("filemanager"), ResponseCache(Duration = 60, VaryByHeader = "Cookie")]
public ActionResult FileManager()
{
return View();
Expand Down
12 changes: 6 additions & 6 deletions src/Masuit.MyBlogs.Core/Controllers/FileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Masuit.MyBlogs.Core.Controllers
[Route("[controller]/[action]")]
public class FileController : AdminController
{
public IHostingEnvironment HostingEnvironment { get; set; }
public IWebHostEnvironment HostEnvironment { get; set; }
/// <summary>
///
/// </summary>
Expand All @@ -31,7 +31,7 @@ public class FileController : AdminController
/// <returns></returns>
public ActionResult GetFiles(string path)
{
var files = Directory.GetFiles(HostingEnvironment.WebRootPath + path).OrderByDescending(s => s).Select(s => new
var files = Directory.GetFiles(HostEnvironment.WebRootPath + path).OrderByDescending(s => s).Select(s => new
{
filename = Path.GetFileName(s),
path = s
Expand Down Expand Up @@ -86,7 +86,7 @@ public ActionResult Upload(string destination)
{
foreach (var t in Request.Form.Files)
{
string path = Path.Combine(HostingEnvironment.ContentRootPath, CommonHelper.SystemSettings["PathRoot"].TrimStart('\\', '/'), destination.TrimStart('\\', '/'), t.FileName);
string path = Path.Combine(HostEnvironment.ContentRootPath, CommonHelper.SystemSettings["PathRoot"].TrimStart('\\', '/'), destination.TrimStart('\\', '/'), t.FileName);
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
t.CopyTo(fs);
Expand All @@ -108,7 +108,7 @@ public ActionResult Upload(string destination)
public ActionResult Handle([FromBody]FileRequest req)
{
List<object> list = new List<object>();
var root = Path.Combine(HostingEnvironment.ContentRootPath, CommonHelper.SystemSettings["PathRoot"].TrimStart('\\', '/'));
var root = Path.Combine(HostEnvironment.ContentRootPath, CommonHelper.SystemSettings["PathRoot"].TrimStart('\\', '/'));
switch (req.Action)
{
case "list":
Expand Down Expand Up @@ -274,7 +274,7 @@ public ActionResult Handle(string path, string[] items, string toFilename)
{
path = path?.TrimStart('\\', '/') ?? "";
var root = CommonHelper.SystemSettings["PathRoot"].TrimStart('\\', '/');
var file = Path.Combine(HostingEnvironment.ContentRootPath, root, path);
var file = Path.Combine(HostEnvironment.ContentRootPath, root, path);
switch (Request.Query["action"])
{
case "download":
Expand All @@ -284,7 +284,7 @@ public ActionResult Handle(string path, string[] items, string toFilename)
}
break;
case "downloadMultiple":
byte[] buffer = SevenZipCompressor.ZipStream(items.Select(s => Path.Combine(HostingEnvironment.ContentRootPath, root, s.TrimStart('\\', '/'))).ToList()).ToArray();
byte[] buffer = SevenZipCompressor.ZipStream(items.Select(s => Path.Combine(HostEnvironment.ContentRootPath, root, s.TrimStart('\\', '/'))).ToList()).ToArray();
return this.ResumeFile(buffer, Path.GetFileName(toFilename));
}
return Content("null");
Expand Down
11 changes: 5 additions & 6 deletions src/Masuit.MyBlogs.Core/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Masuit.MyBlogs.Core.Models.Enum;
using Masuit.MyBlogs.Core.Models.ViewModel;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
Expand Down Expand Up @@ -51,7 +50,7 @@ public class HomeController : BaseController
/// 首页
/// </summary>
/// <returns></returns>
[HttpGet, ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "orderBy" }, VaryByHeader = HeaderNames.Cookie)]
[HttpGet, ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "orderBy" }, VaryByHeader = "Cookie")]
public ActionResult Index()
{
ViewBag.Total = PostService.Count(p => p.Status == Status.Pended || CurrentUser.IsAdmin);
Expand All @@ -70,7 +69,7 @@ public ActionResult Index()
/// <param name="size"></param>
/// <param name="orderBy"></param>
/// <returns></returns>
[Route("p"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "page", "size", "orderBy" }, VaryByHeader = HeaderNames.Cookie)]
[Route("p"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "page", "size", "orderBy" }, VaryByHeader = "Cookie")]
public ActionResult Post([Optional]OrderBy? orderBy, [Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")]int page = 1, [Range(1, int.MaxValue, ErrorMessage = "页大小必须大于0")]int size = 15)
{
ViewBag.Total = PostService.Count(p => p.Status == Status.Pended || CurrentUser.IsAdmin && !p.IsFixedTop);
Expand All @@ -86,7 +85,7 @@ public ActionResult Post([Optional]OrderBy? orderBy, [Range(1, int.MaxValue, Err
/// <param name="size"></param>
/// <param name="orderBy"></param>
/// <returns></returns>
[Route("tag/{id}/{page:int?}/{size:int?}/{orderBy:int?}"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "id", "page", "size", "orderBy" }, VaryByHeader = HeaderNames.Cookie)]
[Route("tag/{id}/{page:int?}/{size:int?}/{orderBy:int?}"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "id", "page", "size", "orderBy" }, VaryByHeader = "Cookie")]
public ActionResult Tag(string id, [Optional]OrderBy? orderBy, [Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")]int page = 1, [Range(1, int.MaxValue, ErrorMessage = "页大小必须大于0")]int size = 15)
{
IList<PostOutputDto> posts;
Expand Down Expand Up @@ -127,7 +126,7 @@ public ActionResult Tag(string id, [Optional]OrderBy? orderBy, [Range(1, int.Max
/// <param name="size"></param>
/// <param name="orderBy"></param>
/// <returns></returns>
[Route("cat/{id:int}"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "id", "page", "size", "orderBy" }, VaryByHeader = HeaderNames.Cookie)]
[Route("cat/{id:int}"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "id", "page", "size", "orderBy" }, VaryByHeader = "Cookie")]
[Route("cat/{id:int}/{page:int?}/{size:int?}/{orderBy:int?}")]
public async Task<ActionResult> Category(int id, [Optional]OrderBy? orderBy, [Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")]int page = 1, [Range(1, int.MaxValue, ErrorMessage = "页大小必须大于0")]int size = 15)
{
Expand Down Expand Up @@ -188,7 +187,7 @@ private IndexPageViewModel GetIndexPageViewModel(int page, int size, OrderBy? or
}
var hot6Post = postsQuery.OrderByDescending(order).Skip(0).Take(5).Cacheable().ToList(); //热门文章
var newdic = new Dictionary<string, int>(); //标签云最终结果
var tagdic = postsQuery.Where(p => !string.IsNullOrEmpty(p.Label)).Select(p => p.Label).Cacheable().SelectMany(s => s.Split(',', ',')).GroupBy(s => s).ToDictionary(g => g.Key, g => g.Count()); //统计标签
var tagdic = postsQuery.Where(p => !string.IsNullOrEmpty(p.Label)).Select(p => p.Label).Cacheable().AsEnumerable().SelectMany(s => s.Split(',', ',')).GroupBy(s => s).ToDictionary(g => g.Key, g => g.Count()); //统计标签

if (tagdic.Any())
{
Expand Down
2 changes: 1 addition & 1 deletion src/Masuit.MyBlogs.Core/Controllers/LinksController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class LinksController : BaseController
/// 友情链接页
/// </summary>
/// <returns></returns>
[Route("links"), ResponseCache(Duration = 600, VaryByHeader = HeaderNames.Cookie)]
[Route("links"), ResponseCache(Duration = 600, VaryByHeader = "Cookie")]
public ActionResult Index()
{
var list = LinksService.GetQueryFromCache<bool, LinksOutputDto>(l => l.Status == Status.Available, l => l.Recommend, false).ToList();
Expand Down
6 changes: 3 additions & 3 deletions src/Masuit.MyBlogs.Core/Controllers/MergeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Masuit.MyBlogs.Core.Controllers
public class MergeController : AdminController
{
public IPostMergeRequestService PostMergeRequestService { get; set; }
public IHostingEnvironment HostingEnvironment { get; set; }
public IWebHostEnvironment HostEnvironment { get; set; }
public MapperConfiguration MapperConfig { get; set; }

/// <summary>
Expand Down Expand Up @@ -96,7 +96,7 @@ public IActionResult Merge(int id)
}

string link = Request.Scheme + "://" + Request.Host + "/" + merge.Post.Id;
string content = System.IO.File.ReadAllText(HostingEnvironment.WebRootPath + "/template/merge-pass.html").Replace("{{link}}", link).Replace("{{title}}", merge.Post.Title);
string content = System.IO.File.ReadAllText(HostEnvironment.WebRootPath + "/template/merge-pass.html").Replace("{{link}}", link).Replace("{{title}}", merge.Post.Title);
BackgroundJob.Enqueue(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Title"] + "博客你提交的修改已通过", content, merge.ModifierEmail));
return ResultData(null, true, "文章合并完成!");

Expand Down Expand Up @@ -139,7 +139,7 @@ public ActionResult Reject(int id, string reason)
}

var link = Request.Scheme + "://" + Request.Host + "/" + merge.Post.Id + "/merge/" + id;
var content = System.IO.File.ReadAllText(HostingEnvironment.WebRootPath + "/template/merge-reject.html").Replace("{{link}}", link).Replace("{{title}}", merge.Post.Title).Replace("{{reason}}", reason);
var content = System.IO.File.ReadAllText(HostEnvironment.WebRootPath + "/template/merge-reject.html").Replace("{{link}}", link).Replace("{{title}}", merge.Post.Title).Replace("{{reason}}", reason);
BackgroundJob.Enqueue(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Title"] + "博客你提交的修改已被拒绝", content, merge.ModifierEmail));
return ResultData(null, true, "合并已拒绝!");
}
Expand Down
10 changes: 5 additions & 5 deletions src/Masuit.MyBlogs.Core/Controllers/MiscController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public class MiscController : BaseController
/// </summary>
public IDonateService DonateService { get; set; }

public IHostingEnvironment HostingEnvironment { get; set; }
public IWebHostEnvironment HostEnvironment { get; set; }
public ImagebedClient ImagebedClient { get; set; }

/// <summary>
/// 杂项页
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[Route("misc/{id:int}"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "id" }, VaryByHeader = HeaderNames.Cookie)]
[Route("misc/{id:int}"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "id" }, VaryByHeader = "Cookie")]
public ActionResult Index(int id)
{
var misc = MiscService.GetFromCache(m => m.Id == id) ?? throw new NotFoundException("页面未找到");
Expand Down Expand Up @@ -82,7 +82,7 @@ public ActionResult DonateList(int page = 1, int size = 10)
/// 关于
/// </summary>
/// <returns></returns>
[Route("about"), ResponseCache(Duration = 600, VaryByHeader = HeaderNames.Cookie)]
[Route("about"), ResponseCache(Duration = 600, VaryByHeader = "Cookie")]
public ActionResult About()
{
return View();
Expand All @@ -92,7 +92,7 @@ public ActionResult About()
/// 声明
/// </summary>
/// <returns></returns>
[Route("disclaimer"), ResponseCache(Duration = 600, VaryByHeader = HeaderNames.Cookie)]
[Route("disclaimer"), ResponseCache(Duration = 600, VaryByHeader = "Cookie")]
public ActionResult Disclaimer()
{
return View();
Expand Down Expand Up @@ -134,7 +134,7 @@ public ActionResult Delete(int id)
{
try
{
System.IO.File.Delete(Path.Combine(HostingEnvironment.WebRootPath + path));
System.IO.File.Delete(Path.Combine(HostEnvironment.WebRootPath + path));
}
catch (IOException)
{
Expand Down
Loading

0 comments on commit 861359d

Please sign in to comment.