Skip to content

Commit

Permalink
Merge pull request #6 from xhnbzdl/dir
Browse files Browse the repository at this point in the history
支持按文件夹目录导出
  • Loading branch information
xhnbzdl authored Jun 30, 2023
2 parents 550b83f + 337fc3b commit 9dfd578
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 8 deletions.
52 changes: 52 additions & 0 deletions src/feishu-doc-export/DocumentPathGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using feishu_doc_export.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace feishu_doc_export
{
public static class DocumentPathGenerator
{
private static Dictionary<string, string> documentPaths;

public static void GenerateDocumentPaths(List<WikiNodeItemDto> documents, string rootFolderPath)
{
documentPaths = new Dictionary<string, string>();

var topDocument = documents.Where(x => string.IsNullOrWhiteSpace(x.ParentNodeToken));
foreach (var document in topDocument)
{
GenerateDocumentPath(document, rootFolderPath, documents);
}

}

private static void GenerateDocumentPath(WikiNodeItemDto document, string parentFolderPath, List<WikiNodeItemDto> documents)
{
// 替换文件名中的非法字符
string title = Regex.Replace(document.Title, @"[\\/:\*\?""<>\|]", "-");
string documentFolderPath = Path.Combine(parentFolderPath, title);

documentPaths[document.ObjToken] = documentFolderPath;

foreach (var childDocument in GetChildDocuments(document, documents))
{
GenerateDocumentPath(childDocument, documentFolderPath, documents);
}
}

private static IEnumerable<WikiNodeItemDto> GetChildDocuments(WikiNodeItemDto document, List<WikiNodeItemDto> documents)
{
return documents.Where(d => d.ParentNodeToken == document.NodeToken);
}

public static string GetDocumentPath(string objToken)
{
documentPaths.TryGetValue(objToken, out string path);
return path;
}
}
}
37 changes: 37 additions & 0 deletions src/feishu-doc-export/Helper/FileHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace feishu_doc_export.Helper
{
public static class FileHelper
{
/// <summary>
/// 保存文件
/// </summary>
/// <param name="path"></param>
/// <param name="content"></param>
public static void Save(this string path, byte[] content)
{
var dir = Path.GetDirectoryName(path);

dir.CreateIfNotExist();

File.WriteAllBytes(path, content);
}

/// <summary>
/// 如果目录不存在,那么创建目录
/// </summary>
/// <param name="path"></param>
public static void CreateIfNotExist(this string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
}
}
31 changes: 23 additions & 8 deletions src/feishu-doc-export/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

using feishu_doc_export.Dtos;
using feishu_doc_export.Helper;
using feishu_doc_export.HttpApi;
using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using WebApiClientCore;
Expand Down Expand Up @@ -68,9 +71,15 @@ static async Task Main(string[] args)

Console.WriteLine($"正在加载知识库【{wikiSpaceInfo.Space.Name}】的所有文档信息,请耐心等待...");

Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();

// 获取知识库下的所有文档
var wikiNodes = await GetAllWikiNode(GlobalConfig.WikiSpaceId);

// 文档路径映射字典
DocumentPathGenerator.GenerateDocumentPaths(wikiNodes, GlobalConfig.ExportPath);

// 不支持导出的文件
List<string> noSupportExportFiles = new List<string>();

Expand Down Expand Up @@ -101,6 +110,7 @@ static async Task Main(string[] args)

await DownLoadDocument(fileExt, item.ObjToken, item.ObjType);
}

Console.WriteLine("—————————————————————————————文档已全部导出—————————————————————————————");
Console.WriteLine(noSupportExportFiles.Any() ? "以下是所有不支持导出的文档" : "");

Expand All @@ -110,6 +120,12 @@ static async Task Main(string[] args)
Console.WriteLine($"{i + 1}.【{noSupportExportFiles[i]}】");
}

stopwatch.Stop();
TimeSpan elapsedTime = stopwatch.Elapsed;
// 输出执行时间(以秒为单位)
double seconds = elapsedTime.TotalSeconds;
Console.WriteLine($"程序执行结束,总耗时{seconds}(秒)");

Console.ReadKey();
}

Expand Down Expand Up @@ -337,26 +353,25 @@ static async Task<byte[]> DownLoad(string fileToken)
/// <param name="token"></param>
/// <param name="type"></param>
/// <returns></returns>
static async Task DownLoadDocument(string fileExtension, string token, string type)
static async Task DownLoadDocument(string fileExtension, string objToken, string type)
{
var exportTaskDto = await CreateExportTask(fileExtension, token, type);
var exportTaskDto = await CreateExportTask(fileExtension, objToken, type);

var exportTaskResult = await QueryExportTaskResult(exportTaskDto.Ticket, token);
var exportTaskResult = await QueryExportTaskResult(exportTaskDto.Ticket, objToken);
var taskInfo = exportTaskResult.Result;

if (taskInfo.JobErrorMsg == "success")
{
var bytes = await DownLoad(taskInfo.FileToken);

var savefileName = taskInfo.FileName + "." + fileExtension;
// 替换文件名中的非法字符
savefileName = Regex.Replace(savefileName, @"[\\/:\*\?""<>\|]", "-");
var saveFileName = DocumentPathGenerator.GetDocumentPath(objToken) + "." + fileExtension;

var filePath = Path.Combine(GlobalConfig.ExportPath, savefileName);
var filePath = Path.Combine(GlobalConfig.ExportPath, saveFileName);

File.WriteAllBytes(filePath, bytes);
filePath.Save(bytes);
}
}
#endregion

}
}

0 comments on commit 9dfd578

Please sign in to comment.