77 lines
2.1 KiB
C#
77 lines
2.1 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Logof_Client.Wiki;
|
|
|
|
public class WikiService
|
|
{
|
|
public string WikiRoot { get; }
|
|
public string WikiRootFullPath { get; }
|
|
|
|
public WikiService(string wikiRoot = "wiki")
|
|
{
|
|
// prefer global wiki storage path if configured
|
|
if (Global._instance != null && !string.IsNullOrWhiteSpace(Global._instance.wiki_storage_path))
|
|
{
|
|
var cfg = Global._instance.wiki_storage_path;
|
|
WikiRootFullPath = Path.IsPathRooted(cfg)
|
|
? cfg
|
|
: Path.Combine(Directory.GetCurrentDirectory(), cfg);
|
|
WikiRoot = WikiRootFullPath;
|
|
}
|
|
else
|
|
{
|
|
WikiRoot = wikiRoot;
|
|
WikiRootFullPath = Path.Combine(Directory.GetCurrentDirectory(), wikiRoot);
|
|
}
|
|
}
|
|
|
|
public List<WikiItem> GetRootItems()
|
|
{
|
|
var list = new List<WikiItem>();
|
|
|
|
if (!Directory.Exists(WikiRootFullPath)) return list;
|
|
|
|
var dirInfo = new DirectoryInfo(WikiRootFullPath);
|
|
|
|
// Add folders
|
|
foreach (var dir in dirInfo.GetDirectories())
|
|
{
|
|
list.Add(BuildFolderItem(dir));
|
|
}
|
|
|
|
// Add files in root
|
|
foreach (var file in dirInfo.GetFiles("*.md"))
|
|
{
|
|
list.Add(new WikiItem { Name = file.Name, Path = file.FullName, IsFolder = false });
|
|
}
|
|
|
|
return list.OrderBy(i => i.IsFolder ? 0 : 1).ToList();
|
|
}
|
|
|
|
private WikiItem BuildFolderItem(DirectoryInfo dir)
|
|
{
|
|
var node = new WikiItem { Name = dir.Name, Path = dir.FullName, IsFolder = true };
|
|
|
|
foreach (var subdir in dir.GetDirectories())
|
|
{
|
|
node.Children.Add(BuildFolderItem(subdir));
|
|
}
|
|
|
|
foreach (var file in dir.GetFiles("*.md"))
|
|
{
|
|
node.Children.Add(new WikiItem { Name = file.Name, Path = file.FullName, IsFolder = false });
|
|
}
|
|
|
|
return node;
|
|
}
|
|
|
|
public Task<string?> LoadFileContentAsync(string path)
|
|
{
|
|
if (!File.Exists(path)) return Task.FromResult<string?>(null);
|
|
return File.ReadAllTextAsync(path);
|
|
}
|
|
}
|