Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions TelegramDownloader/Controllers/FileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,29 +277,49 @@ public async Task<IActionResult> GetFileStream(string idChannel, string idFile,
var rangeHeader = request.Headers["Range"].ToString();

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

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

long totalLength = 0; // (await _fs.getItemById(idChannel, idFile)).Size;
long totalPartialFileLength = 0;

if(idM.media is MessageMediaDocument { document: Document document })
if (idM.media is MessageMediaDocument { document: Document document })
{
totalLength = document.size;
totalPartialFileLength = document.size;
}

long from = 0;
long initialFrom = 0;
long initialPartFrom = 0;
long to = 0;

if (!string.IsNullOrEmpty(rangeHeader) && rangeHeader.StartsWith("bytes="))
{
var range = rangeHeader.Replace("bytes=", "").Split('-');
from = long.Parse(range[0]);
initialFrom = from;
initialPartFrom = from;
if (range.Length > 1 && !string.IsNullOrEmpty(range[1]))
to = long.Parse(range[1]);
}

if (from >= totalPartialFileLength)
{
Console.WriteLine("Take a file part: from: " + from + ", totalPartialFileLength: " + totalPartialFileLength);
int filePart = 0;
while(from >= totalPartialFileLength)
{
filePart++;
from -= totalPartialFileLength;
}
idM = await _ts.getMessageFile(idChannel, file.ListMessageId[filePart]);
if (idM.media is MessageMediaDocument { document: Document document2 })
{
totalPartialFileLength = document2.size;
}
initialPartFrom = from;
}

if (from > 0)
{
from = (from / 524288) * 524288;
Expand All @@ -315,12 +335,12 @@ public async Task<IActionResult> GetFileStream(string idChannel, string idFile,
else
to = ((to + 524288) / 524288) * 524288;

if (to > totalLength)
if (to > totalPartialFileLength)
{
to = totalLength;
to = totalPartialFileLength;
}

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

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

long skipedBytes = initialFrom - from - (length - data.Length);
long skipedBytes = initialPartFrom - from - (length - data.Length);

if (skipedBytes < 0) skipedBytes = 0;

Expand Down
6 changes: 3 additions & 3 deletions TelegramDownloader/TelegramDownloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
<ItemGroup>
<PackageReference Include="Blazor.Bootstrap" Version="3.4.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="8.0.0" />
<PackageReference Include="MongoDB.Driver" Version="3.4.0" />
<PackageReference Include="MongoDB.Driver" Version="3.4.3" />
<PackageReference Include="QRCoder" Version="1.6.0" />
<PackageReference Include="Syncfusion.Blazor" Version="29.2.11" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.22.1" />
<PackageReference Include="Syncfusion.EJ2.AspNet.Core" Version="29.2.11" />
<PackageReference Include="System.IO.Hashing" Version="9.0.7" />
<PackageReference Include="WTelegramClient" Version="4.3.6" />
<PackageReference Include="System.IO.Hashing" Version="9.0.9" />
<PackageReference Include="WTelegramClient" Version="4.3.10" />
</ItemGroup>

<ItemGroup>
Expand Down