Skip to content

Commit f8fc86f

Browse files
authored
Merge pull request #45 from GeneralLibrary/dev
Refactoring FileProvider
2 parents cb04c71 + 84cacbc commit f8fc86f

File tree

12 files changed

+52
-111
lines changed

12 files changed

+52
-111
lines changed

src/c#/GeneralUpdate.Client/MySample.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,20 @@ public void TestFileProvider()
325325
var targetPath = "D:\\packet\\target";
326326
var resultPath = "D:\\packet\\patchs";
327327

328-
FileProvider fileProvider = new FileProvider();
329-
var list = fileProvider.Handle(sourcePath, targetPath, resultPath, null, FileOperations.Query, SetOperations.Intersection, true, true);
330-
foreach (var item in list)
331-
Console.WriteLine(item.Name);
328+
//FileProvider fileProvider = new FileProvider();
329+
//var list = fileProvider.ExecuteOperation(sourcePath, targetPath,new List<string>(), new List<string>());
330+
//foreach (var item in list) {
331+
// Console.WriteLine(item);
332+
//}
333+
//Console.WriteLine("total" + list.Count());
334+
//Console.WriteLine("--------------------------------------");
335+
//FileProvider fileProvider1 = new FileProvider();
336+
//var list1 = fileProvider1.ExecuteOperation(targetPath, sourcePath, new List<string>(), new List<string>());
337+
//foreach (var item in list1)
338+
//{
339+
// Console.WriteLine(item);
340+
//}
341+
//Console.WriteLine("total" + list1.Count());
332342
}
333343

334344
#endregion 文件管理测试

src/c#/GeneralUpdate.ClientCore/Differential/Common/.gitkeep

Whitespace-only changes.

src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
<None Remove="CustomAwaiter\.gitkeep" />
3333
<None Remove="Differential\.gitkeep" />
3434
<None Remove="Differential\Binary\.gitkeep" />
35-
<None Remove="Differential\Common\.gitkeep" />
3635
<None Remove="Differential\Config\.gitkeep" />
3736
<None Remove="Differential\Config\Cache\.gitkeep" />
3837
<None Remove="Differential\Config\Handles\.gitkeep" />
@@ -76,6 +75,8 @@
7675
<Compile Include="..\GeneralUpdate.Core\Bootstrap\UpdateOption.cs" Link="Bootstrap\UpdateOption.cs" />
7776
<Compile Include="..\GeneralUpdate.Core\Bootstrap\UpdateOptionValue.cs" Link="Bootstrap\UpdateOptionValue.cs" />
7877
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileNode.cs" Link="Differential\ContentProvider\FileNode.cs" />
78+
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Comparer.cs" Link="Differential\ContentProvider\FileProvider-Comparer.cs" />
79+
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Filter.cs" Link="Differential\ContentProvider\FileProvider-Filter.cs" />
7980
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Manage.cs" Link="Differential\ContentProvider\FileProvider-Manage.cs" />
8081
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Serialization.cs" Link="Differential\ContentProvider\FileProvider-Serialization.cs" />
8182
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider.cs" Link="Differential\ContentProvider\FileProvider.cs" />
@@ -160,8 +161,6 @@
160161
<Compile Include="..\GeneralUpdate.Core\WillMessage\WillMessageManager.cs" Link="WillMessage\WillMessageManager.cs" />
161162
<Compile Include="..\GeneralUpdate.Differential\Binary\BinaryHandle.cs" Link="Differential\Binary\BinaryHandle.cs" />
162163
<Compile Include="..\GeneralUpdate.Differential\Binary\IBinary.cs" Link="Differential\Binary\IBinary.cs" />
163-
<Compile Include="..\GeneralUpdate.Differential\Common\DirectoryComparer.cs" Link="Differential\Common\DirectoryComparer.cs" />
164-
<Compile Include="..\GeneralUpdate.Differential\Common\Filefilter.cs" Link="Differential\Common\Filefilter.cs" />
165164
<Compile Include="..\GeneralUpdate.Differential\Config\Cache\ConfigCache.cs" Link="Differential\Config\Cache\ConfigCache.cs" />
166165
<Compile Include="..\GeneralUpdate.Differential\Config\Cache\ConfigEntity.cs" Link="Differential\Config\Cache\ConfigEntity.cs" />
167166
<Compile Include="..\GeneralUpdate.Differential\Config\Cache\ICache.cs" Link="Differential\Config\Cache\ICache.cs" />
@@ -201,19 +200,14 @@
201200
<ItemGroup>
202201
<Folder Include="CustomAwaiter\" />
203202
<Folder Include="Differential\Binary\" />
204-
<Folder Include="Differential\Common\" />
205203
<Folder Include="Differential\GStream\" />
206204
<Folder Include="Differential\Binary\" />
207-
<Folder Include="Differential\Common\" />
208205
<Folder Include="Differential\GStream\" />
209206
<Folder Include="Differential\Binary\" />
210-
<Folder Include="Differential\Common\" />
211207
<Folder Include="Differential\GStream\" />
212208
<Folder Include="Differential\Binary\" />
213-
<Folder Include="Differential\Common\" />
214209
<Folder Include="Differential\GStream\" />
215210
<Folder Include="Differential\Binary\" />
216-
<Folder Include="Differential\Common\" />
217211
<Folder Include="Differential\GStream\" />
218212
<Folder Include="Differential\ContentProvider\" />
219213
<Folder Include="Domain\DTO\Assembler\" />

src/c#/GeneralUpdate.Differential/Common/DirectoryComparer.cs renamed to src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Comparer.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@
33
using System.IO;
44
using System.Linq;
55

6-
namespace GeneralUpdate.Differential.Common
6+
namespace GeneralUpdate.Core.ContentProvider
77
{
8-
internal class DirectoryComparer
8+
public partial class FileProvider
99
{
10-
private readonly string _directoryA;
11-
private readonly string _directoryB;
10+
private string _directoryA;
11+
private string _directoryB;
1212

13-
public DirectoryComparer(string directoryA, string directoryB)
14-
{
15-
this._directoryA = directoryA.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
16-
this._directoryB = directoryB.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
17-
}
18-
19-
public List<FileInfo> Comparer()
13+
public List<FileInfo> Comparer(string directoryA, string directoryB)
2014
{
15+
_directoryA = directoryA.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
16+
_directoryB = directoryB.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
2117
var filesInDirectoryA = new HashSet<string>(GetAllFiles(_directoryA).Select(file => file.Substring(_directoryA.Length)), StringComparer.InvariantCultureIgnoreCase);
2218
var missingFilesPath = GetAllFiles(_directoryB).Where(fileB => !filesInDirectoryA.Contains(fileB.Substring(_directoryB.Length))).ToList();
2319
var missingFiles = missingFilesPath.Select(path => new FileInfo(path)).ToList();
@@ -68,4 +64,4 @@ private IEnumerable<string> GetAllFiles(string directoryPath)
6864
}
6965
}
7066
}
71-
}
67+
}

src/c#/GeneralUpdate.Differential/Common/Filefilter.cs renamed to src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Filter.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
24

3-
namespace GeneralUpdate.Differential.Common
5+
namespace GeneralUpdate.Core.ContentProvider
46
{
5-
/// <summary>
6-
/// Used to filter out non-updatable file formats during the update process.
7-
/// </summary>
8-
public class Filefilter
7+
public partial class FileProvider
98
{
109
private static List<string> _blackFiles, _blackFileFormats;
1110

@@ -32,4 +31,4 @@ public static void SetBlacklist(List<string> blackFiles, List<string> blackFileF
3231
/// <returns></returns>
3332
public static List<string> GetBlackFileFormats() => _blackFileFormats ?? new List<string>() { ".patch", ".7z", ".zip", ".rar", ".tar", ".json" };
3433
}
35-
}
34+
}

src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Manage.cs

Lines changed: 5 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,83 +3,23 @@
33
using System;
44
using System.Collections.Generic;
55
using System.IO;
6-
using System.Linq;
76

87
namespace GeneralUpdate.Core.ContentProvider
98
{
10-
public enum FileOperations
11-
{
12-
Query,
13-
Delete,
14-
Update,
15-
Add,
16-
Copy
17-
}
18-
19-
public enum SetOperations
20-
{
21-
Intersection,
22-
Union,
23-
Difference
24-
}
25-
269
public partial class FileProvider
2710
{
28-
public List<FileNode> ExecuteOperation(string sourceDir, string targetDir, List<string> extensionsCondition, List<string> filenamesCondition)
29-
{
30-
if (string.IsNullOrWhiteSpace(sourceDir) || string.IsNullOrWhiteSpace(targetDir) || extensionsCondition == null || filenamesCondition == null)
31-
ThrowExceptionUtility.ThrowIfNull();
32-
33-
var filesInDirA = GetFilesWithSHA256(sourceDir, extensionsCondition, filenamesCondition);
34-
var filesInDirB = GetFilesWithSHA256(targetDir, extensionsCondition, filenamesCondition);
35-
36-
var inBNotInA = InFirstNotInSecond(filesInDirA, filesInDirB);
37-
var inANotInB = InFirstNotInSecond(filesInDirA, filesInDirB);
38-
return new List<FileNode>();
39-
}
40-
41-
bool ShouldSkipFile(string filePath, IList<string> extensionsToSkip, IList<string> filenamesToSkip)
42-
{
43-
var fileInfo = new FileInfo(filePath);
44-
return extensionsToSkip.Contains(fileInfo.Extension) || filenamesToSkip.Contains(fileInfo.Name);
45-
}
46-
47-
Dictionary<string, string> GetFilesWithSHA256(string path, IList<string> extensionsToSkip, IList<string> filenamesToSkip)
48-
{
49-
var result = new Dictionary<string, string>();
50-
foreach (var file in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories))
51-
{
52-
if (!ShouldSkipFile(file, extensionsToSkip, filenamesToSkip))
53-
{
54-
var hashAlgorithm = new Sha256HashAlgorithm();
55-
result[file] = hashAlgorithm.ComputeHash(file);
56-
}
57-
}
58-
return result;
59-
}
60-
61-
IEnumerable<string> InFirstNotInSecond(Dictionary<string, string> first, Dictionary<string, string> second)
62-
{
63-
foreach (var pair in first)
64-
{
65-
string value;
66-
if (!second.TryGetValue(pair.Key, out value) || !value.Equals(pair.Value))
67-
yield return pair.Key;
68-
}
69-
}
70-
7111
public static string GetTempDirectory(string name)
7212
{
73-
var path2 = $"generalupdate_{DateTime.Now.ToString("yyyy-MM-dd")}_{name}";
74-
var tempDir = Path.Combine(Path.GetTempPath(), path2);
13+
var path = $"generalupdate_{DateTime.Now.ToString("yyyy-MM-dd")}_{name}";
14+
var tempDir = Path.Combine(Path.GetTempPath(), path);
7515
if (!Directory.Exists(tempDir))
7616
{
7717
Directory.CreateDirectory(tempDir);
7818
}
7919
return tempDir;
8020
}
8121

82-
public static FileInfo[] GetAllFiles(string path)
22+
public static List<FileInfo> GetAllfiles(string path)
8323
{
8424
try
8525
{
@@ -88,9 +28,9 @@ public static FileInfo[] GetAllFiles(string path)
8828
var tmpDir = new DirectoryInfo(path).GetDirectories();
8929
foreach (var dic in tmpDir)
9030
{
91-
files.AddRange(GetAllFiles(dic.FullName));
31+
files.AddRange(GetAllfiles(dic.FullName));
9232
}
93-
return files.ToArray();
33+
return files;
9434
}
9535
catch (Exception)
9636
{

src/c#/GeneralUpdate.Core/ContentProvider/FileProvider.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using GeneralUpdate.Core.HashAlgorithms;
2-
using GeneralUpdate.Differential.Common;
32
using System;
43
using System.Collections.Generic;
54
using System.IO;
@@ -102,7 +101,7 @@ private IEnumerable<FileNode> Read(string path, string rootPath = null)
102101
/// <returns></returns>
103102
private bool IsMatchBlacklist(string subPath)
104103
{
105-
var blackList = Filefilter.GetBlackFiles();
104+
var blackList = GetBlackFiles();
106105
if (blackList == null) return false;
107106
foreach (var file in blackList)
108107
{

src/c#/GeneralUpdate.Differential/Config/ConfigFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using GeneralUpdate.Core.HashAlgorithms;
2-
using GeneralUpdate.Differential.Common;
1+
using GeneralUpdate.Core.ContentProvider;
2+
using GeneralUpdate.Core.HashAlgorithms;
33
using GeneralUpdate.Differential.Config.Cache;
44
using GeneralUpdate.Differential.Config.Handles;
55
using System;
@@ -158,7 +158,7 @@ private void Find(string rootDirectory, ref List<string> files)
158158
foreach (var file in rootDirectoryInfo.GetFiles())
159159
{
160160
var extensionName = Path.GetExtension(file.Name);
161-
if (!Filefilter.GetBlackFileFormats().Contains(extensionName)) continue;
161+
if (!FileProvider.GetBlackFileFormats().Contains(extensionName)) continue;
162162
var fullName = file.FullName;
163163
files.Add(fullName);
164164
}

src/c#/GeneralUpdate.Differential/DifferentialCore.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using GeneralUpdate.Core.ContentProvider;
22
using GeneralUpdate.Core.HashAlgorithms;
33
using GeneralUpdate.Differential.Binary;
4-
using GeneralUpdate.Differential.Common;
54
using System;
65
using System.Collections.Generic;
76
using System.IO;
@@ -101,7 +100,7 @@ public async Task Clean(string sourcePath, string targetPath, string patchPath =
101100
var oldfile = finOldFile == null ? "" : finOldFile.FullName;
102101
var newfile = file.FullName;
103102
var extensionName = Path.GetExtension(file.FullName);
104-
if (File.Exists(oldfile) && File.Exists(newfile) && !Filefilter.GetBlackFileFormats().Contains(extensionName))
103+
if (File.Exists(oldfile) && File.Exists(newfile) && !FileProvider.GetBlackFileFormats().Contains(extensionName))
105104
{
106105
//Generate the difference file to the difference directory .
107106
await new BinaryHandle().Clean(oldfile, newfile, tempPath0);
@@ -135,8 +134,8 @@ public async Task Dirty(string appPath, string patchPath)
135134
if (!Directory.Exists(appPath) || !Directory.Exists(patchPath)) return;
136135
try
137136
{
138-
var patchFiles = FileProvider.GetAllFiles(patchPath);
139-
var oldFiles = FileProvider.GetAllFiles(appPath);
137+
var patchFiles = FileProvider.GetAllfiles(patchPath);
138+
var oldFiles = FileProvider.GetAllfiles(appPath);
140139

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

186185
#endregion Public Methods
187186

@@ -217,12 +216,12 @@ private Task DirtyUnknow(string appPath, string patchPath)
217216
{
218217
try
219218
{
220-
var dirCompare = new DirectoryComparer(patchPath, appPath);
221-
var listExcept = dirCompare.Comparer();
219+
var fileProvider = new FileProvider();
220+
var listExcept = fileProvider.Comparer(appPath, patchPath);
222221
foreach (var file in listExcept)
223222
{
224223
var extensionName = Path.GetExtension(file.FullName);
225-
if (Filefilter.GetBlackFileFormats().Contains(extensionName)) continue;
224+
if (FileProvider.GetBlackFileFormats().Contains(extensionName)) continue;
226225
var targetFileName = file.FullName.Replace(patchPath, "").TrimStart("\\".ToCharArray());
227226
var targetPath = Path.Combine(appPath, targetFileName);
228227
var parentFolder = Directory.GetParent(targetPath);

src/c#/GeneralUpdate.Differential/GeneralUpdate.Differential.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
<ItemGroup>
2929
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileNode.cs" Link="ContentProvider\FileNode.cs" />
30+
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Comparer.cs" Link="ContentProvider\FileProvider-Comparer.cs" />
31+
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Filter.cs" Link="ContentProvider\FileProvider-Filter.cs" />
3032
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Manage.cs" Link="ContentProvider\FileProvider-Manage.cs" />
3133
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider-Serialization.cs" Link="ContentProvider\FileProvider-Serialization.cs" />
3234
<Compile Include="..\GeneralUpdate.Core\ContentProvider\FileProvider.cs" Link="ContentProvider\FileProvider.cs" />

0 commit comments

Comments
 (0)