-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from xhnbzdl/dir
支持按文件夹目录导出
- Loading branch information
Showing
3 changed files
with
112 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters