Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化同步工具 #153

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions SyncTool/Client/SyncOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,33 @@ internal class SyncOptions

public async Task Run()
{
var syncFolder = SyncFolder;
if (string.IsNullOrEmpty(syncFolder))
{
// 没有给明确的文件夹,使用工作文件夹
syncFolder = Environment.CurrentDirectory;

if (OperatingSystem.IsLinux())
{
// 不写 -f 禁止 ~ 路径
var homePath = Environment.GetEnvironmentVariable("HOME");
if (syncFolder == homePath)
{
Console.WriteLine($"禁止在 $HOME='{homePath}' 路径下进行同步,如果确定要在此路径下同步,请使用 -f 参数明确指定路径");
return;
}
}
}

if (OperatingSystem.IsLinux())
{
if (syncFolder == "/")
{
Console.WriteLine($"禁止使用 / 根路径作为同步文件夹");
return;
}
}

if (string.IsNullOrEmpty(Address))
{
Console.WriteLine($@"找不到同步地址,请确保传入正确参数。
Expand All @@ -45,13 +72,6 @@ public async Task Run()
return;
}

var syncFolder = SyncFolder;
if (string.IsNullOrEmpty(syncFolder))
{
// 没有给明确的文件夹,使用工作文件夹
syncFolder = Environment.CurrentDirectory;
}

syncFolder = Path.GetFullPath(syncFolder);
Directory.CreateDirectory(syncFolder);

Expand Down
6 changes: 5 additions & 1 deletion SyncTool/Context/SyncFolderInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace SyncTool.Context;
using System.Text.Json.Serialization;

namespace SyncTool.Context;

/// <summary>
/// 同步的文件夹信息
Expand All @@ -10,6 +12,7 @@ record SyncFolderInfo(ulong Version, List<SyncFileInfo> SyncFileList, List<SyncF
/// <summary>
/// 同步的文件字典,用来给服务端快速获取文件对应
/// </summary>
[JsonIgnore] // 这个属性不需要序列化,只有在服务端使用,用来快速获取文件关系
public Dictionary<string /*RelativePath*/, SyncFileInfo> SyncFileDictionary
{
get
Expand All @@ -19,5 +22,6 @@ record SyncFolderInfo(ulong Version, List<SyncFileInfo> SyncFileList, List<SyncF
}
}

[JsonIgnore]
private Dictionary<string /*RelativePath*/, SyncFileInfo>? _syncFileDictionary;
}
17 changes: 11 additions & 6 deletions SyncTool/Server/ServeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,19 @@ void OnCurrentFolderInfoChanged(object? sender, SyncFolderInfo e)
OutputStatus();
return new SyncCompletedResponse();
});
webApplication.UseStaticFiles(new StaticFileOptions()
webApplication.UseFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(syncFolder, ExclusionFilters.System),
ContentTypeProvider = new ContentTypeProvider(),
ServeUnknownFileTypes = true,
StaticFileOptions =
{
FileProvider = new PhysicalFileProvider(syncFolder, ExclusionFilters.System),
ContentTypeProvider = new ContentTypeProvider(),
ServeUnknownFileTypes = true,
RequestPath = StaticFileConfiguration.RequestPath,
RedirectToAppendTrailingSlash = true,
DefaultContentType = MediaTypeNames.Application.Octet,
},
RequestPath = StaticFileConfiguration.RequestPath,
RedirectToAppendTrailingSlash = true,
DefaultContentType = MediaTypeNames.Application.Octet,
EnableDirectoryBrowsing = true,
});

_ = Task.Run(() =>
Expand Down
Loading