Skip to content

Commit

Permalink
update: Namespace modification
Browse files Browse the repository at this point in the history
  • Loading branch information
JusterZhu committed Jan 18, 2024
1 parent daab9da commit cd0190f
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 581 deletions.
13 changes: 11 additions & 2 deletions src/c#/GeneralUpdate.Client/MySample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
using GeneralUpdate.Differential;
using System.IO;
using GeneralUpdate.Core.Driver;
using Microsoft.VisualBasic;
using System.Diagnostics;
using GeneralUpdate.Core.WillMessage;
using GeneralUpdate.Core.ContentProvider;

namespace GeneralUpdate.Client
{
Expand Down Expand Up @@ -301,7 +301,6 @@ private bool IsDriverFile(string filePath) =>

#endregion


#region 测试WillMessage

public void TestWillMessage()
Expand All @@ -323,5 +322,15 @@ public void TestWillMessage()
}

#endregion

#region 文件管理测试

public void TestFileProvider()
{
//FileProvider fileProvider = new FileProvider();
//fileProvider.Handle();
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
<Compile Include="..\GeneralUpdate.Core\Bootstrap\AbstractBootstrap.cs" Link="Bootstrap\AbstractBootstrap.cs" />
<Compile Include="..\GeneralUpdate.Core\Bootstrap\UpdateOption.cs" Link="Bootstrap\UpdateOption.cs" />
<Compile Include="..\GeneralUpdate.Core\Bootstrap\UpdateOptionValue.cs" Link="Bootstrap\UpdateOptionValue.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileNode.cs" Link="Differential\ContentProvider\FileNode.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Manage.cs" Link="Differential\ContentProvider\FileProvider-Manage.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider.cs" Link="Differential\ContentProvider\FileProvider.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileTree.cs" Link="Differential\ContentProvider\FileTree.cs" />
<Compile Include="..\GeneralUpdate.Core\CustomAwaiter\IAwaitable.cs" Link="CustomAwaiter\IAwaitable.cs" />
<Compile Include="..\GeneralUpdate.Core\CustomAwaiter\IAwaiter.cs" Link="CustomAwaiter\IAwaiter.cs" />
<Compile Include="..\GeneralUpdate.Core\CustomAwaiter\ICriticalAwaiter.cs" Link="CustomAwaiter\ICriticalAwaiter.cs" />
Expand Down Expand Up @@ -169,9 +173,6 @@
<Compile Include="..\GeneralUpdate.Differential\Config\Handles\IniHandle.cs" Link="Differential\Config\Handles\IniHandle.cs" />
<Compile Include="..\GeneralUpdate.Differential\Config\Handles\JsonHandle.cs" Link="Differential\Config\Handles\JsonHandle.cs" />
<Compile Include="..\GeneralUpdate.Differential\Config\Handles\XmlHandle.cs" Link="Differential\Config\Handles\XmlHandle.cs" />
<Compile Include="..\GeneralUpdate.Differential\ContentProvider\FileNode.cs" Link="Differential\ContentProvider\FileNode.cs" />
<Compile Include="..\GeneralUpdate.Differential\ContentProvider\FileProvider.cs" Link="Differential\ContentProvider\FileProvider.cs" />
<Compile Include="..\GeneralUpdate.Differential\ContentProvider\FileTree.cs" Link="Differential\ContentProvider\FileTree.cs" />
<Compile Include="..\GeneralUpdate.Differential\DifferentialCore.cs" Link="Differential\DifferentialCore.cs" />
<Compile Include="..\GeneralUpdate.Differential\GStream\BZip2Constants.cs" Link="Differential\GStream\BZip2Constants.cs" />
<Compile Include="..\GeneralUpdate.Differential\GStream\BZip2InputStream.cs" Link="Differential\GStream\BZip2InputStream.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/c#/GeneralUpdate.Core/ContentProvider/FileNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace GeneralUpdate.Differential.ContentProvider
namespace GeneralUpdate.Core.ContentProvider
{
public class FileNode
{
Expand Down
143 changes: 143 additions & 0 deletions src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Manage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using GeneralUpdate.Core.HashAlgorithms;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace GeneralUpdate.Core.ContentProvider
{
public enum FileOperations
{
Query,
Delete,
Update,
Add,
Copy
}

public enum SetOperations
{
Intersection,
Union,
Difference
}

public partial class FileProvider
{
/// <summary>
/// 根据参数内容筛选出sourceDir、targetDir两个文件路径中符合要求的文件信息
/// </summary>
/// <param name="sourceDir">源目录</param>
/// <param name="targetDir">目标目录</param>
/// <param name="resultDir">筛选结果保存目录</param>
/// <param name="condition">筛选条件,可以是文件名,可以是文件后缀名</param>
/// <param name="fileOption">文件可执行的操作:增、删、查、改、拷贝</param>
/// <param name="setOperations">集合运算:交集、并集、差集</param>
/// <param name="recursion">整个方法里的操作是否递归执行</param>
/// <param name="integrality">是否保持目录结构的完整性</param>
/// <returns></returns>
public List<FileNode> Handle(string sourceDir, string targetDir, string resultDir, List<string> condition, FileOperations fileOption, SetOperations setOperations, bool recursion, bool integrality)
{
IEnumerable<FileNode> sourceFiles = Directory.EnumerateFiles(sourceDir, "*.*", recursion ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
.Select(path => new FileInfo(path))
.Select(fileInfo => new FileNode
{
Id = fileInfo.GetHashCode(),
Name = fileInfo.Name,
FullName = fileInfo.FullName,
Path = fileInfo.DirectoryName,
Hash = CalculateFileHash(fileInfo)
});
IEnumerable<FileNode> targetFiles = Directory.EnumerateFiles(targetDir, "*.*", recursion ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
.Select(path => new FileInfo(path))
.Select(fileInfo => new FileNode
{
Id = fileInfo.GetHashCode(),
Name = fileInfo.Name,
FullName = fileInfo.FullName,
Path = fileInfo.DirectoryName,
Hash = CalculateFileHash(fileInfo)
});

// 然后根据条件筛选满足要求的文件
IEnumerable<FileNode> filteredSourceFiles = sourceFiles.Where(file => condition.Any(c => file.Name.Contains(c) || file.Name.EndsWith(c)));
IEnumerable<FileNode> filteredTargetFiles = targetFiles.Where(file => condition.Any(c => file.Name.Contains(c) || file.Name.EndsWith(c)));

IEnumerable<FileNode> resultFiles;
switch (setOperations)
{
case SetOperations.Intersection:
resultFiles = filteredSourceFiles.Intersect(filteredTargetFiles);
break;
case SetOperations.Union:
resultFiles = filteredSourceFiles.Union(filteredTargetFiles);
break;
case SetOperations.Difference:
resultFiles = filteredSourceFiles.Except(filteredTargetFiles);
break;
default:
throw new ArgumentException("Invalid operation.", nameof(setOperations));
}

foreach (var file in resultFiles)
{
ExecuteFileOperation(file, fileOption, resultDir, integrality);
}

return resultFiles.ToList();
}

private string CalculateFileHash(FileInfo file)
{
var hashAlgorithm = new Sha256HashAlgorithm();
return hashAlgorithm.ComputeHash(file.FullName);
}

/// <summary>
/// 执行文件操作.
/// </summary>
/// <param name="file">文件对象.</param>
/// <param name="operation">要执行的操作.</param>
/// <param name="resultDir">结果目录.</param>
/// <param name="retainStructure">是否保留结构.</param>
private void ExecuteFileOperation(FileNode file, FileOperations operation, string resultDir, bool retainStructure)
{
string destinationPath = Path.Combine(resultDir, retainStructure ? file.RelativePath : file.Name);

switch (operation)
{
case FileOperations.Add:
// 如果文件已存在,此操作将覆盖现有文件
File.Copy(file.Path, destinationPath, true);
break;
case FileOperations.Delete:
if (File.Exists(destinationPath))
{
File.Delete(destinationPath);
}
break;
case FileOperations.Update:
// 在这里,我们假定更新操作意味着复制源文件到目标位置,覆盖现有文件
File.Copy(file.Path, destinationPath, true);
break;
case FileOperations.Copy:
// 确保目标目录存在
string directoryName = Path.GetDirectoryName(destinationPath);
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
}
// 拷贝文件到新的位置
File.Copy(file.Path, destinationPath, true);
break;
case FileOperations.Query:
// 对于“查询”操作,我们不执行任何文件操作,只在控制台中打印出相关信息
Console.WriteLine($"Found file: {file.FullName}");
break;
default:
throw new ArgumentException("Invalid operation", nameof(operation));
}
}

}
}
136 changes: 2 additions & 134 deletions src/c#/GeneralUpdate.Core/ContentProvider/FileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,9 @@
using System.Threading;
using System.Threading.Tasks;

namespace GeneralUpdate.Differential.ContentProvider
namespace GeneralUpdate.Core.ContentProvider
{
public enum FileOperations
{
Query,
Delete,
Update,
Add,
Copy
}

public enum SetOperations
{
Intersection,
Union,
Difference
}

public class FileProvider
public partial class FileProvider
{
private long _fileCount = 0;

Expand Down Expand Up @@ -128,121 +112,5 @@ private bool IsMatchBlacklist(string subPath)
}

#endregion Private Methods

/// <summary>
/// 根据参数内容筛选出sourceDir、targetDir两个文件路径中符合要求的文件信息
/// </summary>
/// <param name="sourceDir">源目录</param>
/// <param name="targetDir">目标目录</param>
/// <param name="resultDir">筛选结果保存目录</param>
/// <param name="condition">筛选条件,可以是文件名,可以是文件后缀名</param>
/// <param name="fileOption">文件可执行的操作:增、删、查、改、拷贝</param>
/// <param name="setOperations">集合运算:交集、并集、差集</param>
/// <param name="recursion">整个方法里的操作是否递归执行</param>
/// <param name="integrality">是否保持目录结构的完整性</param>
/// <returns></returns>
public List<FileNode> Handle(string sourceDir, string targetDir, string resultDir, List<string> condition, FileOperations fileOption, SetOperations setOperations, bool recursion, bool integrality)
{
IEnumerable<FileNode> sourceFiles = Directory.EnumerateFiles(sourceDir, "*.*", recursion ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
.Select(path => new FileInfo(path))
.Select(fileInfo => new FileNode
{
Id = fileInfo.GetHashCode(),
Name = fileInfo.Name,
FullName = fileInfo.FullName,
Path = fileInfo.DirectoryName,
Hash = CalculateFileHash(fileInfo)
});
IEnumerable<FileNode> targetFiles = Directory.EnumerateFiles(targetDir, "*.*", recursion ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
.Select(path => new FileInfo(path))
.Select(fileInfo => new FileNode
{
Id = fileInfo.GetHashCode(),
Name = fileInfo.Name,
FullName = fileInfo.FullName,
Path = fileInfo.DirectoryName,
Hash = CalculateFileHash(fileInfo)
});

// 然后根据条件筛选满足要求的文件
IEnumerable<FileNode> filteredSourceFiles = sourceFiles.Where(file => condition.Any(c => file.Name.Contains(c) || file.Name.EndsWith(c)));
IEnumerable<FileNode> filteredTargetFiles = targetFiles.Where(file => condition.Any(c => file.Name.Contains(c) || file.Name.EndsWith(c)));

IEnumerable<FileNode> resultFiles;
switch (setOperations)
{
case SetOperations.Intersection:
resultFiles = filteredSourceFiles.Intersect(filteredTargetFiles);
break;
case SetOperations.Union:
resultFiles = filteredSourceFiles.Union(filteredTargetFiles);
break;
case SetOperations.Difference:
resultFiles = filteredSourceFiles.Except(filteredTargetFiles);
break;
default:
throw new ArgumentException("Invalid operation.", nameof(setOperations));
}

foreach (var file in resultFiles)
{
ExecuteFileOperation(file, fileOption, resultDir, integrality);
}

return resultFiles.ToList();
}

private string CalculateFileHash(FileInfo file)
{
var hashAlgorithm = new Sha256HashAlgorithm();
return hashAlgorithm.ComputeHash(file.FullName);
}

/// <summary>
/// 执行文件操作.
/// </summary>
/// <param name="file">文件对象.</param>
/// <param name="operation">要执行的操作.</param>
/// <param name="resultDir">结果目录.</param>
/// <param name="retainStructure">是否保留结构.</param>
private void ExecuteFileOperation(FileNode file, FileOperations operation, string resultDir, bool retainStructure)
{
string destinationPath = Path.Combine(resultDir, retainStructure ? file.RelativePath : file.Name);

switch (operation)
{
case FileOperations.Add:
// 如果文件已存在,此操作将覆盖现有文件
File.Copy(file.Path, destinationPath, true);
break;
case FileOperations.Delete:
if (File.Exists(destinationPath))
{
File.Delete(destinationPath);
}
break;
case FileOperations.Update:
// 在这里,我们假定更新操作意味着复制源文件到目标位置,覆盖现有文件
File.Copy(file.Path, destinationPath, true);
break;
case FileOperations.Copy:
// 确保目标目录存在
string directoryName = Path.GetDirectoryName(destinationPath);
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
}
// 拷贝文件到新的位置
File.Copy(file.Path, destinationPath, true);
break;
case FileOperations.Query:
// 对于“查询”操作,我们不执行任何文件操作,只在控制台中打印出相关信息
Console.WriteLine($"Found file: {file.FullName}");
break;
default:
throw new ArgumentException("Invalid operation", nameof(operation));
}
}

}
}
2 changes: 1 addition & 1 deletion src/c#/GeneralUpdate.Core/ContentProvider/FileTree.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Diagnostics;

namespace GeneralUpdate.Differential.ContentProvider
namespace GeneralUpdate.Core.ContentProvider
{
/// <summary>
/// Simple file binary tree.
Expand Down
Empty file.
Loading

0 comments on commit cd0190f

Please sign in to comment.