mirror of
https://github.com/wiz0u/WTelegramClient.git
synced 2025-12-06 06:52:01 +01:00
UploadFileAsync now supports Stream with unknown Length (!CanSeek)
This commit is contained in:
parent
fb8d1c2d07
commit
c059ebf208
|
|
@ -38,19 +38,27 @@ namespace WTelegram
|
||||||
using var md5 = MD5.Create();
|
using var md5 = MD5.Create();
|
||||||
using (stream)
|
using (stream)
|
||||||
{
|
{
|
||||||
long transmitted = 0, length = stream.Length;
|
bool hasLength = stream.CanSeek;
|
||||||
var isBig = length >= 10 * 1024 * 1024;
|
long transmitted = 0, length = hasLength ? stream.Length : -1;
|
||||||
int file_total_parts = (int)((length - 1) / FilePartSize) + 1;
|
bool isBig = hasLength ? length >= 10 * 1024 * 1024 : true;
|
||||||
|
int file_total_parts = hasLength ? (int)((length - 1) / FilePartSize) + 1 : -1;
|
||||||
long file_id = Helpers.RandomLong();
|
long file_id = Helpers.RandomLong();
|
||||||
int file_part = 0, read;
|
int file_part = 0, read;
|
||||||
var tasks = new Dictionary<int, Task>();
|
var tasks = new Dictionary<int, Task>();
|
||||||
bool abort = false;
|
bool abort = false;
|
||||||
for (long bytesLeft = length; !abort && bytesLeft != 0; file_part++)
|
for (long bytesLeft = hasLength ? length : long.MaxValue; !abort && bytesLeft != 0; file_part++)
|
||||||
{
|
{
|
||||||
var bytes = new byte[Math.Min(FilePartSize, bytesLeft)];
|
var bytes = new byte[Math.Min(FilePartSize, bytesLeft)];
|
||||||
read = await stream.FullReadAsync(bytes, bytes.Length, default);
|
read = await stream.FullReadAsync(bytes, bytes.Length, default);
|
||||||
await _parallelTransfers.WaitAsync();
|
await _parallelTransfers.WaitAsync();
|
||||||
bytesLeft -= read;
|
bytesLeft -= read;
|
||||||
|
if (!hasLength && read < bytes.Length)
|
||||||
|
{
|
||||||
|
file_total_parts = file_part;
|
||||||
|
if (read == 0) break; else file_total_parts++;
|
||||||
|
bytes = bytes[..read];
|
||||||
|
bytesLeft = 0;
|
||||||
|
}
|
||||||
var task = SavePart(file_part, bytes);
|
var task = SavePart(file_part, bytes);
|
||||||
lock (tasks) tasks[file_part] = task;
|
lock (tasks) tasks[file_part] = task;
|
||||||
if (!isBig)
|
if (!isBig)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue