XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension

【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension

修复XFE下载器的错误

c9665a7
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

2 个文件 +27 -20
Modified XFE各类拓展.NetCore/ProfileExtension/ProfileInfo.cs +1 -1
@@ -17,7 +17,7 @@ public class ProfileInfo(Type profileType, string path = "", string description
17 17 /// <summary>
18 18 /// 配置文件储存位置
19 19 /// </summary>
20 public string Path { get; init; } = path == "" ? $"{profileType.Name}.xfe" : path;
20 public string Path { get; init; } = path == "" ? $"Profile/{profileType.Name}.xfe" : path;
21 21 /// <summary>
22 22 /// 配置文件描述
23 23 /// </summary>
Modified XFE各类拓展.NetCore/WebExtension/XFEDownloader.cs +26 -19
@@ -8,11 +8,10 @@ namespace XFE各类拓展.NetCore.WebExtension;
8 8 /// <summary>
9 9 /// XFE下载器
10 10 /// </summary>
11 [Obsolete("出现严重漏洞,暂停使用,预计下个版本恢复", true)]
12 11 public class XFEDownloader : IDisposable
13 12 {
14 13 private bool disposedValue;
15 private readonly HttpClient httpClient = new();
14 private readonly List<HttpClient> httpClientList = [];
16 15 private HttpResponseMessage? responseMessage;
17 16 private readonly List<HttpResponseMessage> httpResponseMessages = [];
18 17 private readonly List<Task> downloadTasks = [];
@@ -49,7 +48,8 @@ public class XFEDownloader : IDisposable
49 48 {
50 49 var continueDownload = continueFromLastDownload && File.Exists(SavePath);
51 50 long totalRead = 0;
52 responseMessage ??= await GetHttpResponseMessage();
51 var getInfoClient = new HttpClient();
52 responseMessage ??= await GetHttpResponseMessage(getInfoClient);
53 53 long? totalFileSize = responseMessage.Content.Headers.ContentLength;
54 54 using var createFileStream = new FileStream(SavePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite, 8192, true);
55 55 if (totalFileSize is not null)
@@ -60,31 +60,34 @@ public class XFEDownloader : IDisposable
60 60 downloadTasks.Add(Task.Run(async () =>
61 61 {
62 62 using var fileStream = new FileStream(SavePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, 8192, true);
63 long endPosition = currentSegment == FileSegmentCount - 1 ? fileStream.Length - fileStream.Length / FileSegmentCount * currentSegment : fileStream.Length / FileSegmentCount * (currentSegment + 1);
64 long startBufferIndex = fileStream.Length / FileSegmentCount * currentSegment;
65 long lastBufferDownloadSize = continueDownload ? await fileStream.GetValidPosition(startBufferIndex, endPosition) : startBufferIndex;
66 fileStream.Seek(lastBufferDownloadSize, SeekOrigin.Begin);
67 long currentSegmentDownloadedBuffeSize = lastBufferDownloadSize - startBufferIndex;
63 long currentSegmentTotalBufferSize = fileStream.Length / FileSegmentCount;
64 long endPosition = currentSegment == FileSegmentCount - 1 ? fileStream.Length : currentSegmentTotalBufferSize * (currentSegment + 1);
65 long startBufferIndex = currentSegmentTotalBufferSize * currentSegment;
66 long startPosition = continueDownload ? await fileStream.GetValidPosition(startBufferIndex, endPosition) : startBufferIndex;
67 fileStream.Seek(startPosition, SeekOrigin.Begin);
68 long currentSegmentDownloadedBuffeSize = startPosition - startBufferIndex;
69 var httpClient = new HttpClient();
70 httpClientList.Add(httpClient);
71 httpClient.DefaultRequestHeaders.Range = new System.Net.Http.Headers.RangeHeaderValue(startPosition, endPosition);
68 72 if (continueDownload)
69 {
70 httpClient.DefaultRequestHeaders.Range = new System.Net.Http.Headers.RangeHeaderValue(lastBufferDownloadSize, endPosition);
71 totalRead += lastBufferDownloadSize;
72 }
73 using var contentStream = await (await GetHttpResponseMessage()).Content.ReadAsStreamAsync();
73 totalRead += currentSegmentDownloadedBuffeSize;
74 using var contentStream = await (await GetHttpResponseMessage(httpClient)).Content.ReadAsStreamAsync();
74 75 byte[] buffer = new byte[8192];
75 76 int currentRead;
76 77 while ((currentRead = await contentStream.ReadAsync(buffer)) > 0 && !disposedValue)
77 78 {
78 79 if (IsPaused)
79 while (!IsPaused && !disposedValue) { }
80 while (IsPaused && !disposedValue) { }
80 81 await fileStream.WriteAsync(buffer.AsMemory(0, currentRead));
81 82 totalRead += currentRead;
82 83 currentSegmentDownloadedBuffeSize += currentRead;
83 84 var bufferCopy = new byte[8192];
84 85 bufferCopy.CopyTo(buffer, 0);
85 if (totalRead == totalFileSize)
86 if (Downloaded)
87 return;
88 if (totalRead >= totalFileSize)
86 89 Downloaded = true;
87 BufferDownloaded?.Invoke(this, new FileDownloadedEventArgs(bufferCopy, totalRead, totalFileSize, fileStream.Length / FileSegmentCount, currentSegmentDownloadedBuffeSize, currentSegment, Downloaded));
90 BufferDownloaded?.Invoke(this, new FileDownloadedEventArgs(bufferCopy, totalRead, totalFileSize, currentSegmentTotalBufferSize, currentSegmentDownloadedBuffeSize, currentSegment, Downloaded));
88 91 }
89 92 }));
90 93 }
@@ -105,10 +108,11 @@ public class XFEDownloader : IDisposable
105 108 /// <returns></returns>
106 109 public async Task<(string? fileName, long? fileSize)> GetDownloadInfo()
107 110 {
108 responseMessage ??= await GetHttpResponseMessage();
111 using var httpClient = new HttpClient();
112 responseMessage ??= await GetHttpResponseMessage(httpClient);
109 113 return (Path.GetFileName(responseMessage.RequestMessage?.RequestUri?.AbsolutePath), responseMessage.Content.Headers.ContentLength);
110 114 }
111 private async Task<HttpResponseMessage> GetHttpResponseMessage()
115 private async Task<HttpResponseMessage> GetHttpResponseMessage(HttpClient httpClient)
112 116 {
113 117 var response = await httpClient.GetAsync(DownloadUrl, HttpCompletionOption.ResponseHeadersRead);
114 118 response.EnsureSuccessStatusCode();
@@ -124,8 +128,11 @@ public class XFEDownloader : IDisposable
124 128 {
125 129 if (disposing)
126 130 {
127 httpClient.Dispose();
128 131 responseMessage?.Dispose();
132 foreach (var httpClient in httpClientList)
133 {
134 httpClient.Dispose();
135 }
129 136 foreach (var response in httpResponseMessages)
130 137 {
131 138 response.Dispose();