feat:完成导入功能
This commit is contained in:
@@ -161,12 +161,16 @@ namespace Yi.Framework.Bbs.Application.Services
|
||||
|
||||
// 将字节转换成字符串
|
||||
var content = Encoding.UTF8.GetString(bytes);
|
||||
fileObjs.Add(new FileObject() { FileName=item.FileName,Content=content});
|
||||
fileObjs.Add(new FileObject() { FileName = item.FileName, Content = content });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new UserFriendlyException("未选择文件");
|
||||
}
|
||||
//使用简单工厂根据传入的类型进行判断
|
||||
await _forumManager.PostImportAsync(input.DiscussId, fileObjs, input.ImportType);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Bbs.Domain.Entities;
|
||||
using Yi.Framework.Bbs.Domain.Shared.Model;
|
||||
|
||||
namespace Yi.Framework.Bbs.Domain.Managers.ArticleImport
|
||||
{
|
||||
public abstract class AbstractArticleImport
|
||||
{
|
||||
public virtual List<ArticleEntity> Import(Guid discussId, List<FileObject> fileObjs)
|
||||
{
|
||||
var articles = Convert(fileObjs);
|
||||
articles.ForEach(article =>
|
||||
{
|
||||
article.DiscussId = discussId;
|
||||
});
|
||||
return articles;
|
||||
}
|
||||
public abstract List<ArticleEntity> Convert(List<FileObject> fileObjs);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Bbs.Domain.Entities;
|
||||
using Yi.Framework.Bbs.Domain.Shared.Model;
|
||||
|
||||
namespace Yi.Framework.Bbs.Domain.Managers.ArticleImport
|
||||
{
|
||||
internal class DefaultArticleImport : AbstractArticleImport
|
||||
{
|
||||
public override List<ArticleEntity> Convert(List<FileObject> fileObjs)
|
||||
{
|
||||
return fileObjs.OrderBy(x => x.FileName).Select(x => new ArticleEntity { Name = x.FileName, Content = x.Content }).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Bbs.Domain.Entities;
|
||||
using Yi.Framework.Bbs.Domain.Shared.Model;
|
||||
|
||||
namespace Yi.Framework.Bbs.Domain.Managers.ArticleImport
|
||||
{
|
||||
internal class VuePressArticleImport : AbstractArticleImport
|
||||
{
|
||||
public override List<ArticleEntity> Convert(List<FileObject> fileObjs)
|
||||
{
|
||||
//排序及处理目录名称
|
||||
var fileNameHandler = fileObjs.OrderBy(x => x.FileName).Select(x =>
|
||||
{
|
||||
var f = new FileObject { Content = x.Content };
|
||||
|
||||
//除去数字
|
||||
f.FileName = x.FileName.Split('.')[1];
|
||||
return f;
|
||||
});
|
||||
|
||||
|
||||
//处理内容
|
||||
var fileContentHandler= fileNameHandler.Select(x =>
|
||||
{
|
||||
var f = new FileObject { FileName = x.FileName };
|
||||
var lines = x.Content.SplitToLines();
|
||||
|
||||
var num = 0;
|
||||
var startIndex = 0;
|
||||
for (int i = 0; i < lines.Length; i++)
|
||||
{
|
||||
if (lines[i] == "---")
|
||||
{
|
||||
num++;
|
||||
if (num == 2)
|
||||
{
|
||||
startIndex = i;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
lines.ToList().RemoveRange(0, num);
|
||||
var result = string.Join(Environment.NewLine, lines);
|
||||
f.Content = result;
|
||||
return f;
|
||||
});
|
||||
|
||||
var output = fileContentHandler.Select(x => new ArticleEntity() { Content = x.Content, Name = x.FileName }).ToList();
|
||||
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Volo.Abp.Domain.Services;
|
||||
using Yi.Framework.Bbs.Domain.Entities;
|
||||
using Yi.Framework.Bbs.Domain.Managers.ArticleImport;
|
||||
using Yi.Framework.Bbs.Domain.Shared.Enums;
|
||||
using Yi.Framework.Bbs.Domain.Shared.Model;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
@@ -16,11 +17,13 @@ namespace Yi.Framework.Bbs.Domain.Managers
|
||||
public readonly ISqlSugarRepository<DiscussEntity, Guid> _discussRepository;
|
||||
public readonly ISqlSugarRepository<PlateEntity, Guid> _plateEntityRepository;
|
||||
public readonly ISqlSugarRepository<CommentEntity, Guid> _commentRepository;
|
||||
public ForumManager(ISqlSugarRepository<DiscussEntity, Guid> discussRepository, ISqlSugarRepository<PlateEntity, Guid> plateEntityRepository, ISqlSugarRepository<CommentEntity, Guid> commentRepository)
|
||||
public readonly ISqlSugarRepository<ArticleEntity, Guid> _articleRepository;
|
||||
public ForumManager(ISqlSugarRepository<DiscussEntity, Guid> discussRepository, ISqlSugarRepository<PlateEntity, Guid> plateEntityRepository, ISqlSugarRepository<CommentEntity, Guid> commentRepository, ISqlSugarRepository<ArticleEntity, Guid> articleRepository)
|
||||
{
|
||||
_discussRepository = discussRepository;
|
||||
_plateEntityRepository = plateEntityRepository;
|
||||
_commentRepository = commentRepository;
|
||||
_articleRepository = articleRepository;
|
||||
}
|
||||
|
||||
//主题是不能直接创建的,需要由领域服务统一创建
|
||||
@@ -50,6 +53,24 @@ namespace Yi.Framework.Bbs.Domain.Managers
|
||||
/// <returns></returns>
|
||||
public async Task PostImportAsync(Guid discussId, List<FileObject> fileObjs, ArticleImportTypeEnum importType)
|
||||
{
|
||||
AbstractArticleImport abstractArticleImport = default;
|
||||
switch (importType)
|
||||
{
|
||||
case ArticleImportTypeEnum.Defalut:
|
||||
abstractArticleImport = new DefaultArticleImport();
|
||||
|
||||
break;
|
||||
case ArticleImportTypeEnum.VuePress:
|
||||
abstractArticleImport = new VuePressArticleImport();
|
||||
break;
|
||||
|
||||
default: abstractArticleImport = new DefaultArticleImport(); break;
|
||||
}
|
||||
|
||||
var articleHandled = abstractArticleImport.Import(discussId, fileObjs);
|
||||
|
||||
await _articleRepository.InsertManyAsync(articleHandled);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user