diff --git a/README.md b/README.md index 2f69c56..2238e3c 100644 --- a/README.md +++ b/README.md @@ -30,65 +30,4 @@ package webpage [ asp.net --+ remoteserver asp.net --+ localserver webpage --+ webtool - -``` - -```plantuml - -entity DirInfo { - String ServerPath - String LocalPath - Arr SpecialFiles - Arr ExcludeFiles -} -entity FileInfo { - DateTime mtime - String Path -} -entity ConfigInfo { - Arr DirInfos - String RemoteAddr - String RemoteName -} -``` - -```plantuml -allowmixing -skinparam classAttributeIconSize 0 -component sqlite -package remoteserver { - class FilesInfoController { - +Arr GetFilesInfo(DirInfo dirinfo) - +UploadFiles(FileInfo) - } - class SyncLogController { - +GetSyncLog() - } - package SyncPersistence { - } - - SyncPersistence --* SyncLogController - SyncPersistence --* FilesInfoController - -} -package localserver { - class FilesConfigController { - - GetLocalFilesInfo() - - CompareLocalRemoteFiles() - -- - + SetDirsConfig(Arr dirs) - } - package ConfigPersistence { - } - ConfigPersistence --* FilesConfigController -} -FilesConfigController <--> FilesInfoController: 文件及信息传递 -sqlite --* SyncPersistence -sqlite --* ConfigPersistence - -Actor Devloper - -Devloper --> FilesConfigController:调用 - -Devloper --> SyncLogController:查看同步信息 ``` diff --git a/Server/Common/Dir.cs b/Server/Common/Dir.cs index f88ec31..73cbe95 100644 --- a/Server/Common/Dir.cs +++ b/Server/Common/Dir.cs @@ -126,7 +126,7 @@ public class Dir(string path, List? children = null, NextOpType? nex /// 是否更新Object对象 /// 是否更新文件目录树 /// - public (bool, string) Combine( + public void Combine( FileDirOp? fileDirOp, Dir diffdir, bool IsUpdateObject = true, @@ -135,7 +135,7 @@ public class Dir(string path, List? children = null, NextOpType? nex { if (this.FormatedPath != diffdir.FormatedPath) { - return (false, "their path is not same"); + throw new ArgumentException("their path is not same"); } else { @@ -215,7 +215,7 @@ public class Dir(string path, List? children = null, NextOpType? nex { if (IsUpdateDirFile) { - fileDirOp?.DirDel(rrdir,false); + fileDirOp?.DirDel(rrdir, false); } if (IsUpdateObject) { @@ -240,84 +240,6 @@ public class Dir(string path, List? children = null, NextOpType? nex } } } - return (true, ""); - } - - public (bool, string) Combine_Old(Dir other) - { - if (this.FormatedPath != other.FormatedPath) - { - return (false, "their path is not same"); - } - else - { - var ldir = this; - var rdir = other; - - foreach (var oc in other.Children) - { - if (oc is File rfile) - { - if (rfile.NextOp != null) - { - if (oc.NextOp == NextOpType.Add) - { - ldir.AddChild(new File(rfile.FormatedPath, rfile.MTime)); - } - else - { - var n = ldir - .Children.Where(x => - x.FormatedPath == oc.FormatedPath && x.Type == DirOrFile.File - ) - .FirstOrDefault(); - if (n is not null) - { - if (oc.NextOp == NextOpType.Del) - { - ldir.Children.Remove(n); - } - else if (oc.NextOp == NextOpType.Modify) - { - if (n is File lfile) - { - lfile.MTime = rfile.MTime; - } - } - } - } - } - } - else if (oc is Dir rrdir) - { - //新增和删除意味着整个文件夹都被新增和删除 - if (rrdir.NextOp == NextOpType.Add) - { - ldir.AddChild(rrdir.Clone(null, true)); - } - else if (rrdir.NextOp == NextOpType.Del) - { - ldir.Children.RemoveAt( - ldir.Children.FindIndex(x => x.FormatedPath == rrdir.FormatedPath) - ); - } - //当子文件夹和文件不确定时 - else - { - var n = ldir - .Children.Where(x => - x.FormatedPath == rrdir.FormatedPath && x.Type == DirOrFile.Dir - ) - .FirstOrDefault(); - if (n is Dir lldir) - { - lldir.Combine_Old(rrdir); - } - } - } - } - } - return (true, ""); } /// @@ -325,9 +247,9 @@ public class Dir(string path, List? children = null, NextOpType? nex /// /// 它的一个clone将被合并的dir,它的NextOp 不应该是空,否则什么都不会发生 /// - public (bool, string) CombineJustObject(Dir other) + public void CombineJustObject(Dir other) { - return Combine(null, other, true, false); + Combine(null, other, true, false); } /// @@ -335,9 +257,9 @@ public class Dir(string path, List? children = null, NextOpType? nex /// /// 它的一个clone将被合并的dir,它的NextOp 不应该是空,否则什么都不会发生 /// - public (bool, string) CombineJustDirFile(FileDirOp fileDirOp, Dir diffDir) + public void CombineJustDirFile(FileDirOp fileDirOp, Dir diffDir) { - return Combine(fileDirOp, diffDir, false, true); + Combine(fileDirOp, diffDir, false, true); } /// @@ -345,11 +267,11 @@ public class Dir(string path, List? children = null, NextOpType? nex /// /// /// / - protected (bool, string) AddChild(AFileOrDir child) + protected void AddChild(AFileOrDir child) { if (child.FormatedPath[..this.FormatedPath.Length] != this.FormatedPath) { - return (false, "their rootpath are not same!"); + throw new ArgumentException("their rootpath are not same!"); } var filtedChildren = this.Children.Where(x => x.Type == child.Type); @@ -359,7 +281,9 @@ public class Dir(string path, List? children = null, NextOpType? nex { if (child is File) { - return (false, "there are same path in the children"); + throw new ArgumentException( + $"there are same path in the children:{child.FormatedPath}" + ); } else if (child is Dir dir) { @@ -377,18 +301,17 @@ public class Dir(string path, List? children = null, NextOpType? nex { this.Children.Add(child); } - return (true, ""); } /// /// 从文件目录结构提起文件信息,注意,此目录文件树不包含文件内容,仅有修改时间mtime /// /// - public (bool, string) ExtractInfo() + public void ExtractInfo() { if (this.Children.Count != 0) { - return (false, "this dir is not empty."); + throw new NotSupportedException("this dir is not empty."); } string[] files = Directory.GetFiles(this.FormatedPath); string[] dirs = Directory.GetDirectories(this.FormatedPath); @@ -402,7 +325,6 @@ public class Dir(string path, List? children = null, NextOpType? nex ndir.ExtractInfo(); this.Children.Add(ndir); } - return (true, ""); } /// @@ -410,9 +332,9 @@ public class Dir(string path, List? children = null, NextOpType? nex /// 文件的修改时间,是否修改文件的修改时间,需要定义文件的写入策略 WriteFileStrageFunc /// /// - public (bool, string) WriteByThisInfo(FileDirOp fileDirOp) + public void WriteByThisInfo(FileDirOp fileDirOp) { - static (bool, string) f(Dir dir, FileDirOp fileDirOp) + static void f(Dir dir, FileDirOp fileDirOp) { foreach (var child in dir.Children) { @@ -420,11 +342,7 @@ public class Dir(string path, List? children = null, NextOpType? nex { if (child is Dir childDir) { - var (IsSuccess, Message) = fileDirOp.DirCreate(childDir, false); - if (!IsSuccess) - { - return (false, Message); - } + fileDirOp.DirCreate(childDir, false); f(childDir, fileDirOp); } } @@ -432,24 +350,16 @@ public class Dir(string path, List? children = null, NextOpType? nex { if (child is File childFile) { - var (IsSuccess, Message) = fileDirOp.FileCreate( - child.FormatedPath, - childFile.MTime - ); - if (!IsSuccess) - { - return (false, Message); - } + fileDirOp.FileCreate(child.FormatedPath, childFile.MTime); } else { - return (false, "child is not File!"); + throw new ArgumentException("child is not File!"); } } } - return (true, ""); } - return f(this, fileDirOp); + f(this, fileDirOp); } /// diff --git a/Server/Common/FileDirOp.cs b/Server/Common/FileDirOp.cs index 584aea9..da89ce5 100644 --- a/Server/Common/FileDirOp.cs +++ b/Server/Common/FileDirOp.cs @@ -4,18 +4,18 @@ namespace Common; public abstract class FileDirOp { - public abstract (bool, string) FileCreate(string absolutePath, DateTime mtime); + public abstract void FileCreate(string absolutePath, DateTime mtime); - public abstract (bool, string) DirCreate(Dir dir, bool IsRecursion = true); + public abstract void DirCreate(Dir dir, bool IsRecursion = true); - public abstract (bool, string) FileModify(string absolutePath, DateTime mtime); - public abstract (bool, string) FileDel(string absolutePath); - public abstract (bool, string) DirDel(Dir dir, bool IsRecursion = true); + public abstract void FileModify(string absolutePath, DateTime mtime); + public abstract void FileDel(string absolutePath); + public abstract void DirDel(Dir dir, bool IsRecursion = true); } public class SimpleFileDirOpForTest : FileDirOp { - public override (bool, string) FileCreate(string absolutePath, DateTime mtime) + public override void FileCreate(string absolutePath, DateTime mtime) { using (FileStream fs = System.IO.File.OpenWrite(absolutePath)) { @@ -23,10 +23,9 @@ public class SimpleFileDirOpForTest : FileDirOp fs.Write(info, 0, info.Length); } System.IO.File.SetLastWriteTime(absolutePath, mtime); - return (true, ""); } - public override (bool, string) FileModify(string absolutePath, DateTime mtime) + public override void FileModify(string absolutePath, DateTime mtime) { using (FileStream fs = System.IO.File.OpenWrite(absolutePath)) { @@ -34,20 +33,18 @@ public class SimpleFileDirOpForTest : FileDirOp fs.Write(info, 0, info.Length); } System.IO.File.SetLastWriteTime(absolutePath, mtime); - return (true, ""); } - public override (bool, string) FileDel(string absolutePath) + public override void FileDel(string absolutePath) { //ToDo 权限检查 if (System.IO.File.Exists(absolutePath)) { System.IO.File.Delete(absolutePath); } - return (true, ""); } - public override (bool, string) DirCreate(Dir dir, bool IsRecursion = true) + public override void DirCreate(Dir dir, bool IsRecursion = true) { //TODO需做权限检查 if (!Directory.Exists(dir.FormatedPath)) @@ -68,10 +65,9 @@ public class SimpleFileDirOpForTest : FileDirOp } } } - return (true, ""); } - public override (bool, string) DirDel(Dir dir, bool IsRecursion = true) + public override void DirDel(Dir dir, bool IsRecursion = true) { //TODO 权限检查 正式徐执行递归 if (!IsRecursion) @@ -80,7 +76,6 @@ public class SimpleFileDirOpForTest : FileDirOp { Directory.Delete(dir.FormatedPath, true); } - return (true, ""); } else { diff --git a/Server/ServerTest/DirFileOpTest.cs b/Server/ServerTest/DirFileOpTest.cs index 6e691af..afdb8e4 100644 --- a/Server/ServerTest/DirFileOpTest.cs +++ b/Server/ServerTest/DirFileOpTest.cs @@ -28,10 +28,8 @@ public class DirFileOpTest(FilesSeed filesSeed) : IClassFixture [Fact, TestPriority(0)] public void FileDirWriteExtract() { - var (IsSuccess, Message) = filesSeed.NewDir.WriteByThisInfo(filesSeed.fileDirOp); - var (IsSuccess2, Message2) = filesSeed.OldDir.WriteByThisInfo(filesSeed.fileDirOp); - Assert.True(IsSuccess, "新文件写入失败!"); - Assert.True(IsSuccess2, "旧文件写入失败!"); + filesSeed.NewDir.WriteByThisInfo(filesSeed.fileDirOp); + filesSeed.OldDir.WriteByThisInfo(filesSeed.fileDirOp); Dir nnd = new(filesSeed.NewDir.FormatedPath); nnd.ExtractInfo(); Assert.True(nnd.IsEqual(filesSeed.NewDir), "新文件提取文件夹的信息与写入信息不一致!"); @@ -62,8 +60,7 @@ public class DirFileOpTest(FilesSeed filesSeed) : IClassFixture [Fact, TestPriority(2)] public void SyncFileDir() { - var (IsSuccess,Messsage) = filesSeed.OldDir.CombineJustDirFile(filesSeed.fileDirOp,filesSeed.DiffDir); - Assert.True(IsSuccess, "文件更新错误!"); + filesSeed.OldDir.CombineJustDirFile(filesSeed.fileDirOp,filesSeed.DiffDir); Dir oldSync = new(filesSeed.OldDir.FormatedPath); oldSync.ExtractInfo(); oldSync.ResetRootPath(filesSeed.OldDir.FormatedPath, filesSeed.NewDir.FormatedPath); @@ -76,8 +73,7 @@ public class DirFileOpTest(FilesSeed filesSeed) : IClassFixture [Fact, TestPriority(3)] public void DirsCombine() { - var (IsSuccess, Message) = filesSeed.OldDir.CombineJustObject(filesSeed.DiffDir); - Assert.True(IsSuccess, "文件合并出错!"); + filesSeed.OldDir.CombineJustObject(filesSeed.DiffDir); //Assert.False(filesSeed.NewDir.IsEqual(filesSeed.OldDir)); filesSeed.OldDir.ResetRootPath("OldDir", "NewDir"); // Console.WriteLine(filesSeed.OldDir.Path);