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

Refactoring FileProvider #45

Merged
merged 1 commit into from
Jan 21, 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
18 changes: 14 additions & 4 deletions src/c#/GeneralUpdate.Client/MySample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
}

//Receive sample code pushed by the server
private async void GetMessage(string msg)

Check warning on line 31 in src/c#/GeneralUpdate.Client/MySample.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
var isUpdate = true;
if (isUpdate) Upgrade();

Check warning on line 34 in src/c#/GeneralUpdate.Client/MySample.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
}

#endregion 推送功能
Expand Down Expand Up @@ -325,10 +325,20 @@
var targetPath = "D:\\packet\\target";
var resultPath = "D:\\packet\\patchs";

FileProvider fileProvider = new FileProvider();
var list = fileProvider.Handle(sourcePath, targetPath, resultPath, null, FileOperations.Query, SetOperations.Intersection, true, true);
foreach (var item in list)
Console.WriteLine(item.Name);
//FileProvider fileProvider = new FileProvider();
//var list = fileProvider.ExecuteOperation(sourcePath, targetPath,new List<string>(), new List<string>());
//foreach (var item in list) {
// Console.WriteLine(item);
//}
//Console.WriteLine("total" + list.Count());
//Console.WriteLine("--------------------------------------");
//FileProvider fileProvider1 = new FileProvider();
//var list1 = fileProvider1.ExecuteOperation(targetPath, sourcePath, new List<string>(), new List<string>());
//foreach (var item in list1)
//{
// Console.WriteLine(item);
//}
//Console.WriteLine("total" + list1.Count());
}

#endregion 文件管理测试
Expand Down
Empty file.
10 changes: 2 additions & 8 deletions src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<None Remove="CustomAwaiter\.gitkeep" />
<None Remove="Differential\.gitkeep" />
<None Remove="Differential\Binary\.gitkeep" />
<None Remove="Differential\Common\.gitkeep" />
<None Remove="Differential\Config\.gitkeep" />
<None Remove="Differential\Config\Cache\.gitkeep" />
<None Remove="Differential\Config\Handles\.gitkeep" />
Expand Down Expand Up @@ -76,6 +75,8 @@
<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-Comparer.cs" Link="Differential\ContentProvider\FileProvider-Comparer.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Filter.cs" Link="Differential\ContentProvider\FileProvider-Filter.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Manage.cs" Link="Differential\ContentProvider\FileProvider-Manage.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Serialization.cs" Link="Differential\ContentProvider\FileProvider-Serialization.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider.cs" Link="Differential\ContentProvider\FileProvider.cs" />
Expand Down Expand Up @@ -160,8 +161,6 @@
<Compile Include="..\GeneralUpdate.Core\WillMessage\WillMessageManager.cs" Link="WillMessage\WillMessageManager.cs" />
<Compile Include="..\GeneralUpdate.Differential\Binary\BinaryHandle.cs" Link="Differential\Binary\BinaryHandle.cs" />
<Compile Include="..\GeneralUpdate.Differential\Binary\IBinary.cs" Link="Differential\Binary\IBinary.cs" />
<Compile Include="..\GeneralUpdate.Differential\Common\DirectoryComparer.cs" Link="Differential\Common\DirectoryComparer.cs" />
<Compile Include="..\GeneralUpdate.Differential\Common\Filefilter.cs" Link="Differential\Common\Filefilter.cs" />
<Compile Include="..\GeneralUpdate.Differential\Config\Cache\ConfigCache.cs" Link="Differential\Config\Cache\ConfigCache.cs" />
<Compile Include="..\GeneralUpdate.Differential\Config\Cache\ConfigEntity.cs" Link="Differential\Config\Cache\ConfigEntity.cs" />
<Compile Include="..\GeneralUpdate.Differential\Config\Cache\ICache.cs" Link="Differential\Config\Cache\ICache.cs" />
Expand Down Expand Up @@ -201,19 +200,14 @@
<ItemGroup>
<Folder Include="CustomAwaiter\" />
<Folder Include="Differential\Binary\" />
<Folder Include="Differential\Common\" />
<Folder Include="Differential\GStream\" />
<Folder Include="Differential\Binary\" />
<Folder Include="Differential\Common\" />
<Folder Include="Differential\GStream\" />
<Folder Include="Differential\Binary\" />
<Folder Include="Differential\Common\" />
<Folder Include="Differential\GStream\" />
<Folder Include="Differential\Binary\" />
<Folder Include="Differential\Common\" />
<Folder Include="Differential\GStream\" />
<Folder Include="Differential\Binary\" />
<Folder Include="Differential\Common\" />
<Folder Include="Differential\GStream\" />
<Folder Include="Differential\ContentProvider\" />
<Folder Include="Domain\DTO\Assembler\" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@
using System.IO;
using System.Linq;

namespace GeneralUpdate.Differential.Common
namespace GeneralUpdate.Core.ContentProvider
{
internal class DirectoryComparer
public partial class FileProvider
{
private readonly string _directoryA;
private readonly string _directoryB;
private string _directoryA;
private string _directoryB;

public DirectoryComparer(string directoryA, string directoryB)
{
this._directoryA = directoryA.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
this._directoryB = directoryB.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
}

public List<FileInfo> Comparer()
public List<FileInfo> Comparer(string directoryA, string directoryB)
{
_directoryA = directoryA.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
_directoryB = directoryB.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
var filesInDirectoryA = new HashSet<string>(GetAllFiles(_directoryA).Select(file => file.Substring(_directoryA.Length)), StringComparer.InvariantCultureIgnoreCase);
var missingFilesPath = GetAllFiles(_directoryB).Where(fileB => !filesInDirectoryA.Contains(fileB.Substring(_directoryB.Length))).ToList();
var missingFiles = missingFilesPath.Select(path => new FileInfo(path)).ToList();
Expand Down Expand Up @@ -68,4 +64,4 @@ private IEnumerable<string> GetAllFiles(string directoryPath)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Text;

namespace GeneralUpdate.Differential.Common
namespace GeneralUpdate.Core.ContentProvider
{
/// <summary>
/// Used to filter out non-updatable file formats during the update process.
/// </summary>
public class Filefilter
public partial class FileProvider
{
private static List<string> _blackFiles, _blackFileFormats;

Expand All @@ -32,4 +31,4 @@ public static void SetBlacklist(List<string> blackFiles, List<string> blackFileF
/// <returns></returns>
public static List<string> GetBlackFileFormats() => _blackFileFormats ?? new List<string>() { ".patch", ".7z", ".zip", ".rar", ".tar", ".json" };
}
}
}
70 changes: 5 additions & 65 deletions src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Manage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,83 +3,23 @@
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
{
public List<FileNode> ExecuteOperation(string sourceDir, string targetDir, List<string> extensionsCondition, List<string> filenamesCondition)
{
if (string.IsNullOrWhiteSpace(sourceDir) || string.IsNullOrWhiteSpace(targetDir) || extensionsCondition == null || filenamesCondition == null)
ThrowExceptionUtility.ThrowIfNull();

var filesInDirA = GetFilesWithSHA256(sourceDir, extensionsCondition, filenamesCondition);
var filesInDirB = GetFilesWithSHA256(targetDir, extensionsCondition, filenamesCondition);

var inBNotInA = InFirstNotInSecond(filesInDirA, filesInDirB);
var inANotInB = InFirstNotInSecond(filesInDirA, filesInDirB);
return new List<FileNode>();
}

bool ShouldSkipFile(string filePath, IList<string> extensionsToSkip, IList<string> filenamesToSkip)
{
var fileInfo = new FileInfo(filePath);
return extensionsToSkip.Contains(fileInfo.Extension) || filenamesToSkip.Contains(fileInfo.Name);
}

Dictionary<string, string> GetFilesWithSHA256(string path, IList<string> extensionsToSkip, IList<string> filenamesToSkip)
{
var result = new Dictionary<string, string>();
foreach (var file in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories))
{
if (!ShouldSkipFile(file, extensionsToSkip, filenamesToSkip))
{
var hashAlgorithm = new Sha256HashAlgorithm();
result[file] = hashAlgorithm.ComputeHash(file);
}
}
return result;
}

IEnumerable<string> InFirstNotInSecond(Dictionary<string, string> first, Dictionary<string, string> second)
{
foreach (var pair in first)
{
string value;
if (!second.TryGetValue(pair.Key, out value) || !value.Equals(pair.Value))
yield return pair.Key;
}
}

public static string GetTempDirectory(string name)
{
var path2 = $"generalupdate_{DateTime.Now.ToString("yyyy-MM-dd")}_{name}";
var tempDir = Path.Combine(Path.GetTempPath(), path2);
var path = $"generalupdate_{DateTime.Now.ToString("yyyy-MM-dd")}_{name}";
var tempDir = Path.Combine(Path.GetTempPath(), path);
if (!Directory.Exists(tempDir))
{
Directory.CreateDirectory(tempDir);
}
return tempDir;
}

public static FileInfo[] GetAllFiles(string path)
public static List<FileInfo> GetAllfiles(string path)
{
try
{
Expand All @@ -88,9 +28,9 @@ public static FileInfo[] GetAllFiles(string path)
var tmpDir = new DirectoryInfo(path).GetDirectories();
foreach (var dic in tmpDir)
{
files.AddRange(GetAllFiles(dic.FullName));
files.AddRange(GetAllfiles(dic.FullName));
}
return files.ToArray();
return files;
}
catch (Exception)
{
Expand Down
3 changes: 1 addition & 2 deletions src/c#/GeneralUpdate.Core/ContentProvider/FileProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using GeneralUpdate.Core.HashAlgorithms;
using GeneralUpdate.Differential.Common;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -64,7 +63,7 @@
/// <param name="path">folder path.</param>
/// <param name="rootPath">folder root path.</param>
/// <returns>different chalders.</returns>
private IEnumerable<FileNode> Read(string path, string rootPath = null)

Check warning on line 66 in src/c#/GeneralUpdate.Core/ContentProvider/FileProvider.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
var resultFiles = new List<FileNode>();
if (string.IsNullOrEmpty(rootPath)) rootPath = path;
Expand Down Expand Up @@ -102,7 +101,7 @@
/// <returns></returns>
private bool IsMatchBlacklist(string subPath)
{
var blackList = Filefilter.GetBlackFiles();
var blackList = GetBlackFiles();
if (blackList == null) return false;
foreach (var file in blackList)
{
Expand Down
6 changes: 3 additions & 3 deletions src/c#/GeneralUpdate.Differential/Config/ConfigFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using GeneralUpdate.Core.HashAlgorithms;
using GeneralUpdate.Differential.Common;
using GeneralUpdate.Core.ContentProvider;
using GeneralUpdate.Core.HashAlgorithms;
using GeneralUpdate.Differential.Config.Cache;
using GeneralUpdate.Differential.Config.Handles;
using System;
Expand Down Expand Up @@ -158,7 +158,7 @@ private void Find(string rootDirectory, ref List<string> files)
foreach (var file in rootDirectoryInfo.GetFiles())
{
var extensionName = Path.GetExtension(file.Name);
if (!Filefilter.GetBlackFileFormats().Contains(extensionName)) continue;
if (!FileProvider.GetBlackFileFormats().Contains(extensionName)) continue;
var fullName = file.FullName;
files.Add(fullName);
}
Expand Down
15 changes: 7 additions & 8 deletions src/c#/GeneralUpdate.Differential/DifferentialCore.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using GeneralUpdate.Core.ContentProvider;
using GeneralUpdate.Core.HashAlgorithms;
using GeneralUpdate.Differential.Binary;
using GeneralUpdate.Differential.Common;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -101,7 +100,7 @@ public async Task Clean(string sourcePath, string targetPath, string patchPath =
var oldfile = finOldFile == null ? "" : finOldFile.FullName;
var newfile = file.FullName;
var extensionName = Path.GetExtension(file.FullName);
if (File.Exists(oldfile) && File.Exists(newfile) && !Filefilter.GetBlackFileFormats().Contains(extensionName))
if (File.Exists(oldfile) && File.Exists(newfile) && !FileProvider.GetBlackFileFormats().Contains(extensionName))
{
//Generate the difference file to the difference directory .
await new BinaryHandle().Clean(oldfile, newfile, tempPath0);
Expand Down Expand Up @@ -135,8 +134,8 @@ public async Task Dirty(string appPath, string patchPath)
if (!Directory.Exists(appPath) || !Directory.Exists(patchPath)) return;
try
{
var patchFiles = FileProvider.GetAllFiles(patchPath);
var oldFiles = FileProvider.GetAllFiles(appPath);
var patchFiles = FileProvider.GetAllfiles(patchPath);
var oldFiles = FileProvider.GetAllfiles(appPath);

//If a JSON file for the deletion list is found in the update package, it will be deleted based on its contents.
var deleteListJson = patchFiles.FirstOrDefault(i => i.Name.Equals(DELETE_FILES_NAME));
Expand Down Expand Up @@ -181,7 +180,7 @@ public async Task Dirty(string appPath, string patchPath)
/// </summary>
/// <param name="blackFiles">A collection of blacklist files that are skipped when updated.</param>
/// <param name="blackFileFormats">A collection of blacklist file name extensions that are skipped on update.</param>
public void SetBlocklist(List<string> blackFiles, List<string> blackFileFormats) => Filefilter.SetBlacklist(blackFiles, blackFileFormats);
public void SetBlocklist(List<string> blackFiles, List<string> blackFileFormats) => FileProvider.SetBlacklist(blackFiles, blackFileFormats);

#endregion Public Methods

Expand Down Expand Up @@ -217,12 +216,12 @@ private Task DirtyUnknow(string appPath, string patchPath)
{
try
{
var dirCompare = new DirectoryComparer(patchPath, appPath);
var listExcept = dirCompare.Comparer();
var fileProvider = new FileProvider();
var listExcept = fileProvider.Comparer(appPath, patchPath);
foreach (var file in listExcept)
{
var extensionName = Path.GetExtension(file.FullName);
if (Filefilter.GetBlackFileFormats().Contains(extensionName)) continue;
if (FileProvider.GetBlackFileFormats().Contains(extensionName)) continue;
var targetFileName = file.FullName.Replace(patchPath, "").TrimStart("\\".ToCharArray());
var targetPath = Path.Combine(appPath, targetFileName);
var parentFolder = Directory.GetParent(targetPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

<ItemGroup>
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileNode.cs" Link="ContentProvider\FileNode.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Comparer.cs" Link="ContentProvider\FileProvider-Comparer.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Filter.cs" Link="ContentProvider\FileProvider-Filter.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Manage.cs" Link="ContentProvider\FileProvider-Manage.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Serialization.cs" Link="ContentProvider\FileProvider-Serialization.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider.cs" Link="ContentProvider\FileProvider.cs" />
Expand Down
3 changes: 2 additions & 1 deletion src/c#/GeneralUpdate.Maui.OSS/GeneralUpdate.Maui.OSS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

<ItemGroup>
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileNode.cs" Link="ContentProvider\FileNode.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Comparer.cs" Link="ContentProvider\FileProvider-Comparer.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Filter.cs" Link="ContentProvider\FileProvider-Filter.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Manage.cs" Link="ContentProvider\FileProvider-Manage.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Serialization.cs" Link="ContentProvider\FileProvider-Serialization.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider.cs" Link="ContentProvider\FileProvider.cs" />
Expand All @@ -71,7 +73,6 @@
<Compile Include="..\GeneralUpdate.Core\Events\EventManager.cs" Link="Events\EventManager.cs" />
<Compile Include="..\GeneralUpdate.Core\Events\IEventManager.cs" Link="Events\IEventManager.cs" />
<Compile Include="..\GeneralUpdate.Core\Events\OSSArgs\OSSDownloadArgs.cs" Link="Events\OSSDownloadArgs.cs" />
<Compile Include="..\GeneralUpdate.Differential\Common\Filefilter.cs" Link="ContentProvider\Filefilter.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

<ItemGroup>
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileNode.cs" Link="ContentProvider\FileNode.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Comparer.cs" Link="ContentProvider\FileProvider-Comparer.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Filter.cs" Link="ContentProvider\FileProvider-Filter.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Manage.cs" Link="ContentProvider\FileProvider-Manage.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Serialization.cs" Link="ContentProvider\FileProvider-Serialization.cs" />
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider.cs" Link="ContentProvider\FileProvider.cs" />
Expand Down Expand Up @@ -49,7 +51,6 @@
<Compile Include="..\GeneralUpdate.Core\HashAlgorithms\Sha256HashAlgorithm.cs" Link="HashAlgorithms\Sha256HashAlgorithm.cs" />
<Compile Include="..\GeneralUpdate.Core\Utils\SerializeUtil.cs" Link="Utrils\SerializeUtil.cs" />
<Compile Include="..\GeneralUpdate.Core\WillMessage\WillMessageManager.cs" Link="Services\WillMessageManager.cs" />
<Compile Include="..\GeneralUpdate.Differential\Common\Filefilter.cs" Link="ContentProvider\Filefilter.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading