From 84cacbc7aefa49a78697f1493a816a78722ba371 Mon Sep 17 00:00:00 2001 From: Juster Zhu Date: Sun, 21 Jan 2024 19:21:42 +0800 Subject: [PATCH] Refactoring FileProvider --- src/c#/GeneralUpdate.Client/MySample.cs | 18 +++-- .../Differential/Common/.gitkeep | 0 .../GeneralUpdate.ClientCore.csproj | 10 +-- .../ContentProvider/FileProvider-Comparer.cs} | 20 +++--- .../ContentProvider/FileProvider-Filter.cs} | 13 ++-- .../ContentProvider/FileProvider-Manage.cs | 70 ++----------------- .../ContentProvider/FileProvider.cs | 3 +- .../Config/ConfigFactory.cs | 6 +- .../DifferentialCore.cs | 15 ++-- .../GeneralUpdate.Differential.csproj | 2 + .../GeneralUpdate.Maui.OSS.csproj | 3 +- .../GeneralUpdate.SystemService.csproj | 3 +- 12 files changed, 52 insertions(+), 111 deletions(-) delete mode 100644 src/c#/GeneralUpdate.ClientCore/Differential/Common/.gitkeep rename src/c#/{GeneralUpdate.Differential/Common/DirectoryComparer.cs => GeneralUpdate.Core/ContentProvider/FileProvider-Comparer.cs} (77%) rename src/c#/{GeneralUpdate.Differential/Common/Filefilter.cs => GeneralUpdate.Core/ContentProvider/FileProvider-Filter.cs} (83%) diff --git a/src/c#/GeneralUpdate.Client/MySample.cs b/src/c#/GeneralUpdate.Client/MySample.cs index d125ce78..72e631da 100644 --- a/src/c#/GeneralUpdate.Client/MySample.cs +++ b/src/c#/GeneralUpdate.Client/MySample.cs @@ -325,10 +325,20 @@ public void TestFileProvider() 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(), new List()); + //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(), new List()); + //foreach (var item in list1) + //{ + // Console.WriteLine(item); + //} + //Console.WriteLine("total" + list1.Count()); } #endregion 文件管理测试 diff --git a/src/c#/GeneralUpdate.ClientCore/Differential/Common/.gitkeep b/src/c#/GeneralUpdate.ClientCore/Differential/Common/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj b/src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj index f2f1d0e7..de6d08b8 100644 --- a/src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj +++ b/src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj @@ -32,7 +32,6 @@ - @@ -76,6 +75,8 @@ + + @@ -160,8 +161,6 @@ - - @@ -201,19 +200,14 @@ - - - - - diff --git a/src/c#/GeneralUpdate.Differential/Common/DirectoryComparer.cs b/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Comparer.cs similarity index 77% rename from src/c#/GeneralUpdate.Differential/Common/DirectoryComparer.cs rename to src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Comparer.cs index 4926bcd1..ae8cfcda 100644 --- a/src/c#/GeneralUpdate.Differential/Common/DirectoryComparer.cs +++ b/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Comparer.cs @@ -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 Comparer() + public List Comparer(string directoryA, string directoryB) { + _directoryA = directoryA.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar; + _directoryB = directoryB.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar; var filesInDirectoryA = new HashSet(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(); @@ -68,4 +64,4 @@ private IEnumerable GetAllFiles(string directoryPath) } } } -} \ No newline at end of file +} diff --git a/src/c#/GeneralUpdate.Differential/Common/Filefilter.cs b/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Filter.cs similarity index 83% rename from src/c#/GeneralUpdate.Differential/Common/Filefilter.cs rename to src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Filter.cs index 919a162f..ba447718 100644 --- a/src/c#/GeneralUpdate.Differential/Common/Filefilter.cs +++ b/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Filter.cs @@ -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 { - /// - /// Used to filter out non-updatable file formats during the update process. - /// - public class Filefilter + public partial class FileProvider { private static List _blackFiles, _blackFileFormats; @@ -32,4 +31,4 @@ public static void SetBlacklist(List blackFiles, List blackFileF /// public static List GetBlackFileFormats() => _blackFileFormats ?? new List() { ".patch", ".7z", ".zip", ".rar", ".tar", ".json" }; } -} \ No newline at end of file +} diff --git a/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Manage.cs b/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Manage.cs index f073acc7..77fa0b86 100644 --- a/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Manage.cs +++ b/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Manage.cs @@ -3,75 +3,15 @@ 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 ExecuteOperation(string sourceDir, string targetDir, List extensionsCondition, List 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(); - } - - bool ShouldSkipFile(string filePath, IList extensionsToSkip, IList filenamesToSkip) - { - var fileInfo = new FileInfo(filePath); - return extensionsToSkip.Contains(fileInfo.Extension) || filenamesToSkip.Contains(fileInfo.Name); - } - - Dictionary GetFilesWithSHA256(string path, IList extensionsToSkip, IList filenamesToSkip) - { - var result = new Dictionary(); - 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 InFirstNotInSecond(Dictionary first, Dictionary 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); @@ -79,7 +19,7 @@ public static string GetTempDirectory(string name) return tempDir; } - public static FileInfo[] GetAllFiles(string path) + public static List GetAllfiles(string path) { try { @@ -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) { diff --git a/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider.cs b/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider.cs index 7767c847..5a89c1d1 100644 --- a/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider.cs +++ b/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider.cs @@ -1,5 +1,4 @@ using GeneralUpdate.Core.HashAlgorithms; -using GeneralUpdate.Differential.Common; using System; using System.Collections.Generic; using System.IO; @@ -102,7 +101,7 @@ private IEnumerable Read(string path, string rootPath = null) /// private bool IsMatchBlacklist(string subPath) { - var blackList = Filefilter.GetBlackFiles(); + var blackList = GetBlackFiles(); if (blackList == null) return false; foreach (var file in blackList) { diff --git a/src/c#/GeneralUpdate.Differential/Config/ConfigFactory.cs b/src/c#/GeneralUpdate.Differential/Config/ConfigFactory.cs index bf33fc8d..5033c1cb 100644 --- a/src/c#/GeneralUpdate.Differential/Config/ConfigFactory.cs +++ b/src/c#/GeneralUpdate.Differential/Config/ConfigFactory.cs @@ -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; @@ -158,7 +158,7 @@ private void Find(string rootDirectory, ref List 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); } diff --git a/src/c#/GeneralUpdate.Differential/DifferentialCore.cs b/src/c#/GeneralUpdate.Differential/DifferentialCore.cs index 1e237a4b..95f4438c 100644 --- a/src/c#/GeneralUpdate.Differential/DifferentialCore.cs +++ b/src/c#/GeneralUpdate.Differential/DifferentialCore.cs @@ -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; @@ -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); @@ -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)); @@ -181,7 +180,7 @@ public async Task Dirty(string appPath, string patchPath) /// /// A collection of blacklist files that are skipped when updated. /// A collection of blacklist file name extensions that are skipped on update. - public void SetBlocklist(List blackFiles, List blackFileFormats) => Filefilter.SetBlacklist(blackFiles, blackFileFormats); + public void SetBlocklist(List blackFiles, List blackFileFormats) => FileProvider.SetBlacklist(blackFiles, blackFileFormats); #endregion Public Methods @@ -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); diff --git a/src/c#/GeneralUpdate.Differential/GeneralUpdate.Differential.csproj b/src/c#/GeneralUpdate.Differential/GeneralUpdate.Differential.csproj index f6fdd388..cb3eba97 100644 --- a/src/c#/GeneralUpdate.Differential/GeneralUpdate.Differential.csproj +++ b/src/c#/GeneralUpdate.Differential/GeneralUpdate.Differential.csproj @@ -27,6 +27,8 @@ + + diff --git a/src/c#/GeneralUpdate.Maui.OSS/GeneralUpdate.Maui.OSS.csproj b/src/c#/GeneralUpdate.Maui.OSS/GeneralUpdate.Maui.OSS.csproj index 3cfd08b8..fde0276d 100644 --- a/src/c#/GeneralUpdate.Maui.OSS/GeneralUpdate.Maui.OSS.csproj +++ b/src/c#/GeneralUpdate.Maui.OSS/GeneralUpdate.Maui.OSS.csproj @@ -51,6 +51,8 @@ + + @@ -71,7 +73,6 @@ - diff --git a/src/c#/GeneralUpdate.SystemService/GeneralUpdate.SystemService.csproj b/src/c#/GeneralUpdate.SystemService/GeneralUpdate.SystemService.csproj index a13f7039..5018bdee 100644 --- a/src/c#/GeneralUpdate.SystemService/GeneralUpdate.SystemService.csproj +++ b/src/c#/GeneralUpdate.SystemService/GeneralUpdate.SystemService.csproj @@ -20,6 +20,8 @@ + + @@ -49,7 +51,6 @@ -