favorite & view page & etc.
This commit is contained in:
@@ -125,7 +125,7 @@ namespace Gallery.Util
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<string> DownloadImageAsync(string url, string id, string working, string folder)
|
||||
public static async Task<string> DownloadImageAsync(string url, string id, string working, string folder, Action<(int loc, int size)> action = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -149,7 +149,7 @@ namespace Gallery.Util
|
||||
{
|
||||
Timeout = Config.Timeout
|
||||
};
|
||||
long size;
|
||||
int size;
|
||||
DateTimeOffset lastModified;
|
||||
using (var request = new HttpRequestMessage(HttpMethod.Head, url))
|
||||
{
|
||||
@@ -158,7 +158,7 @@ namespace Gallery.Util
|
||||
headers.Add("Accept-Language", Config.AcceptLanguage);
|
||||
headers.Add("User-Agent", Config.UserAgent);
|
||||
using var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
|
||||
size = response.Content.Headers.ContentLength.Value;
|
||||
size = (int)response.Content.Headers.ContentLength.Value;
|
||||
lastModified = response.Content.Headers.LastModified.Value;
|
||||
#if DEBUG
|
||||
Log.Print($"content length: {size:n0} bytes, last modified: {lastModified}");
|
||||
@@ -167,10 +167,10 @@ namespace Gallery.Util
|
||||
|
||||
// segments
|
||||
const int SIZE = 150000;
|
||||
var list = new List<(long from, long to)>();
|
||||
for (long i = 0; i < size; i += SIZE)
|
||||
var list = new List<(int from, int to)>();
|
||||
for (var i = 0; i < size; i += SIZE)
|
||||
{
|
||||
long to;
|
||||
int to;
|
||||
if (i + SIZE >= size)
|
||||
{
|
||||
to = size - 1;
|
||||
@@ -198,11 +198,12 @@ namespace Gallery.Util
|
||||
headers.Range = new RangeHeaderValue(from, to);
|
||||
headers.Add("User-Agent", Config.UserAgent);
|
||||
using var response = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).Result;
|
||||
using var ms = new MemoryStream(data, (int)from, (int)(to - from + 1));
|
||||
using var ms = new MemoryStream(data, from, to - from + 1);
|
||||
response.Content.CopyToAsync(ms).Wait();
|
||||
#if DEBUG
|
||||
Log.Print($"downloaded range: from({from:n0}) to ({to:n0})");
|
||||
#endif
|
||||
action?.Invoke((to, size));
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user