Skip to content

Commit

Permalink
Merge pull request #17 from xhnbzdl/cloud-doc
Browse files Browse the repository at this point in the history
支持个人空间云文档导出
  • Loading branch information
xhnbzdl authored Sep 27, 2023
2 parents f47fedf + d8d3401 commit d7f6e34
Show file tree
Hide file tree
Showing 9 changed files with 551 additions and 116 deletions.
62 changes: 62 additions & 0 deletions src/feishu-doc-export/CloudDocPathGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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 CloudDocPathGenerator
{
/// <summary>
/// 文档token和路径的映射
/// </summary>
private static Dictionary<string, string> documentPaths;

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

foreach (var document in documents)
{
if (!documentPaths.ContainsKey(document.Token))
{
GenerateDocumentPath(document, rootFolderPath, documents);
}
}

}

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

documentPaths[document.Token] = documentFolderPath;

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

private static IEnumerable<CloudDocDto> GetChildDocuments(CloudDocDto document, List<CloudDocDto> documents)
{
return documents.Where(d => d.ParentToken == document.Token);
}

/// <summary>
/// 获取文档的存储路径
/// </summary>
/// <param name="objToken"></param>
/// <returns></returns>
public static string GetDocumentPath(string objToken)
{
documentPaths.TryGetValue(objToken, out string path);
return path;
}
}
}
35 changes: 35 additions & 0 deletions src/feishu-doc-export/Dtos/CloudDocDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace feishu_doc_export.Dtos
{
/// <summary>
/// 个人空间云文档
/// </summary>
public class CloudDocDto
{
[JsonPropertyName("created_time")]
public string CreatedTime { get; set; }

[JsonPropertyName("modified_time")]
public string ModifiedTime { get; set; }

public string Name { get; set; }

[JsonPropertyName("owner_id")]
public string OwnerId { get; set; }

[JsonPropertyName("parent_token")]
public string ParentToken { get; set; }

public string Token { get; set; }

public string Type { get; set; }

public string Url { get; set; }
}
}
28 changes: 28 additions & 0 deletions src/feishu-doc-export/Dtos/CloudDocFolderMeta.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace feishu_doc_export.Dtos
{
/// <summary>
/// 个人空间云文档文件夹信息
/// </summary>
public class CloudDocFolderMeta
{
public string Id { get; set; }

public string Name { get; set; }

public string Token { get; set; }

public string CreateUid { get; set; }

public string EditUid { get; set; }

public string ParentId { get; set; }

public string OwnUid { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/feishu-doc-export/Dtos/PagedResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class PagedResult<T>
{
public List<T> Items { get; set; }

public List<T> Files { get; set; }

[JsonPropertyName("page_token")]
public string PageToken { get; set; }

Expand Down
59 changes: 49 additions & 10 deletions src/feishu-doc-export/GlobalConfig.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
using Aspose.Words;
using feishu_doc_export.Helper;
using System.Security.Cryptography;
using System.Text;

namespace feishu_doc_export
{
public static class GlobalConfig
{
public static string AppId { get; set; }
public static string AppId { get; set; }

public static string AppSecret { get; set; }
public static string AppSecret { get; set; }

public static string ExportPath { get; set; }
public static string ExportPath { get; set; }

public static string WikiSpaceId { get; set; }

public static string CloudDocFolder { get; set; }

public static string Type { get; set; } = "wiki";

private static string _docSaveType = "docx";

public static string DocSaveType {
Expand Down Expand Up @@ -66,6 +71,8 @@ public static void Init(string[] args)
{
AppId = GetCommandLineArg(args, "--appId=");
AppSecret = GetCommandLineArg(args, "--appSecret=");
Type = GetCommandLineArg(args, "--type=", true);
CloudDocFolder = GetCommandLineArg(args, "--folderToken=", true);
WikiSpaceId = GetCommandLineArg(args, "--spaceId=", true);
DocSaveType = GetCommandLineArg(args, "--saveType=", true);
ExportPath = GetCommandLineArg(args, "--exportPath=");
Expand All @@ -75,15 +82,45 @@ public static void Init(string[] args)
//#if !DEBUG
Console.WriteLine("请输入飞书自建应用的AppId:");
AppId = Console.ReadLine();
if (string.IsNullOrWhiteSpace(AppId))
{
LogHelper.LogWarnExit("AppId是必填参数");
}

Console.WriteLine("请输入飞书自建应用的AppSecret:");
AppSecret = Console.ReadLine();
if (string.IsNullOrWhiteSpace(AppSecret))
{
LogHelper.LogWarnExit("AppSecret是必填参数");
}

Console.WriteLine("请输入文档导出的文件类型(可选值:docx、md、pdf,为空或其他非可选值则默认为docx):");
DocSaveType = Console.ReadLine();
Console.WriteLine("请输入要导出的知识库Id(为空代表从所有知识库中选择):");
WikiSpaceId = Console.ReadLine();

Console.WriteLine("请选择云文档类型(可选值:wiki、cloudDoc)");
Type = Console.ReadLine();
if (Type == "cloudDoc")
{
Console.WriteLine("请输入云文档文件夹Token(必填项!)");
CloudDocFolder = Console.ReadLine();
if (string.IsNullOrWhiteSpace(CloudDocFolder))
{
LogHelper.LogWarnExit("文件夹Token是必填参数");
}
}
else
{
Console.WriteLine("请输入要导出的知识库Id(为空代表从所有知识库中选择):");
WikiSpaceId = Console.ReadLine();
}

Console.WriteLine("请输入文档导出的目录位置:");
ExportPath = Console.ReadLine();
//#endif
if (string.IsNullOrWhiteSpace(ExportPath))
{
LogHelper.LogWarnExit("文档导出的目录是必填参数");
}
//#endif
}

InitAsposeLicense();
Expand Down Expand Up @@ -116,11 +153,13 @@ public static string GetCommandLineArg(string[] args, string parameterName, bool
{
Console.WriteLine($"没有找到参数:{parameterName}");
Console.WriteLine("请填写以下所有参数:");
Console.WriteLine(" --appId 飞书自建应用的AppId.");
Console.WriteLine(" --appSecret 飞书自建应用的AppSecret.");
Console.WriteLine(" --spaceId 飞书导出的知识库Id.");
Console.WriteLine(" --appId 飞书自建应用的AppId.【必填项】");
Console.WriteLine(" --appSecret 飞书自建应用的AppSecret.【必填项】");
Console.WriteLine(" --exportPath 文档导出的目录位置.【必填项】");
Console.WriteLine(" --type 知识库(wiki)或个人空间云文档(cloudDoc)(可选值:cloudDoc、wiki,为空则默认为wiki)");
Console.WriteLine(" --saveType 文档导出的文件类型(可选值:docx、md、pdf,为空或其他非可选值则默认为docx).");
Console.WriteLine(" --exportPath 文档导出的目录位置.");
Console.WriteLine(" --folderToken 当type为个人空间云文档时,该项必填");
Console.WriteLine(" --spaceId 飞书导出的知识库Id.");
Environment.Exit(0);
}

Expand Down
9 changes: 9 additions & 0 deletions src/feishu-doc-export/Helper/LogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ public static void LogWarn(string message)
Console.ResetColor();
}

public static void LogWarnExit(string message)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"【WARN】{message} (请按任意键退出!)");
Console.ResetColor();
Console.ReadKey();
Environment.Exit(0);
}

public static void LogError(string message)
{
Console.ForegroundColor = ConsoleColor.Red;
Expand Down
Loading

0 comments on commit d7f6e34

Please sign in to comment.