Skip to content

Commit 5295aa5

Browse files
authored
release: v2.0.9 (#35)
* feat: media stream for huge files (#34)
1 parent 11a98a1 commit 5295aa5

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

TelegramDownloader/Controllers/FileController.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,29 +277,49 @@ public async Task<IActionResult> GetFileStream(string idChannel, string idFile,
277277
var rangeHeader = request.Headers["Range"].ToString();
278278

279279
var file = await _fs.getItemById(idChannel, idFile);
280+
long totalLength = file.Size;
280281

281282
TL.Message idM = await _ts.getMessageFile(idChannel, file.MessageId ?? file.ListMessageId.FirstOrDefault());
282283

283-
long totalLength = 0; // (await _fs.getItemById(idChannel, idFile)).Size;
284+
long totalPartialFileLength = 0;
284285

285-
if(idM.media is MessageMediaDocument { document: Document document })
286+
if (idM.media is MessageMediaDocument { document: Document document })
286287
{
287-
totalLength = document.size;
288+
totalPartialFileLength = document.size;
288289
}
289290

290291
long from = 0;
291292
long initialFrom = 0;
293+
long initialPartFrom = 0;
292294
long to = 0;
293295

294296
if (!string.IsNullOrEmpty(rangeHeader) && rangeHeader.StartsWith("bytes="))
295297
{
296298
var range = rangeHeader.Replace("bytes=", "").Split('-');
297299
from = long.Parse(range[0]);
298300
initialFrom = from;
301+
initialPartFrom = from;
299302
if (range.Length > 1 && !string.IsNullOrEmpty(range[1]))
300303
to = long.Parse(range[1]);
301304
}
302305

306+
if (from >= totalPartialFileLength)
307+
{
308+
Console.WriteLine("Take a file part: from: " + from + ", totalPartialFileLength: " + totalPartialFileLength);
309+
int filePart = 0;
310+
while(from >= totalPartialFileLength)
311+
{
312+
filePart++;
313+
from -= totalPartialFileLength;
314+
}
315+
idM = await _ts.getMessageFile(idChannel, file.ListMessageId[filePart]);
316+
if (idM.media is MessageMediaDocument { document: Document document2 })
317+
{
318+
totalPartialFileLength = document2.size;
319+
}
320+
initialPartFrom = from;
321+
}
322+
303323
if (from > 0)
304324
{
305325
from = (from / 524288) * 524288;
@@ -315,12 +335,12 @@ public async Task<IActionResult> GetFileStream(string idChannel, string idFile,
315335
else
316336
to = ((to + 524288) / 524288) * 524288;
317337

318-
if (to > totalLength)
338+
if (to > totalPartialFileLength)
319339
{
320-
to = totalLength;
340+
to = totalPartialFileLength;
321341
}
322342

323-
if (totalLength == initialFrom)
343+
if (totalPartialFileLength == initialPartFrom)
324344
{
325345
Response.StatusCode = 416; // Range Not Satisfiable
326346
Response.Headers["Content-Range"] = $"bytes */{totalLength}";
@@ -335,7 +355,7 @@ public async Task<IActionResult> GetFileStream(string idChannel, string idFile,
335355

336356
//Console.WriteLine("Total download length: " + data.Length);
337357

338-
long skipedBytes = initialFrom - from - (length - data.Length);
358+
long skipedBytes = initialPartFrom - from - (length - data.Length);
339359

340360
if (skipedBytes < 0) skipedBytes = 0;
341361

TelegramDownloader/TelegramDownloader.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
<ItemGroup>
1616
<PackageReference Include="Blazor.Bootstrap" Version="3.4.0" />
1717
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="8.0.0" />
18-
<PackageReference Include="MongoDB.Driver" Version="3.4.0" />
18+
<PackageReference Include="MongoDB.Driver" Version="3.4.3" />
1919
<PackageReference Include="QRCoder" Version="1.6.0" />
2020
<PackageReference Include="Syncfusion.Blazor" Version="29.2.11" />
2121
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.22.1" />
2222
<PackageReference Include="Syncfusion.EJ2.AspNet.Core" Version="29.2.11" />
23-
<PackageReference Include="System.IO.Hashing" Version="9.0.7" />
24-
<PackageReference Include="WTelegramClient" Version="4.3.6" />
23+
<PackageReference Include="System.IO.Hashing" Version="9.0.9" />
24+
<PackageReference Include="WTelegramClient" Version="4.3.10" />
2525
</ItemGroup>
2626

2727
<ItemGroup>

0 commit comments

Comments
 (0)