Skip to content

Commit

Permalink
Merge pull request #153 from dotnet-campus/t/lindexi/SyncTool
Browse files Browse the repository at this point in the history
优化同步工具
  • Loading branch information
SeWZC authored Sep 10, 2024
2 parents 72efbb1 + 8ca6e70 commit dd4dfaa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
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

0 comments on commit dd4dfaa

Please sign in to comment.