Skip to content

Commit c7e1d61

Browse files
committed
feat: add speedChart
chore: add more logs
1 parent 2d85d14 commit c7e1d61

13 files changed

Lines changed: 466 additions & 71 deletions

File tree

TelegramDownloader/Controllers/FileController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ public async Task<IActionResult> GetFileStream(string idChannel, string idFile,
372372

373373
var request = HttpContext.Request;
374374
var rangeHeader = request.Headers["Range"].ToString();
375-
Console.WriteLine("range: ", rangeHeader);
375+
_logger.LogDebug("GetFileStream - Range: {Range}", rangeHeader);
376376

377377
var file = await _fs.getItemById(idChannel, idFile);
378378
long totalLength = file.Size;
@@ -403,7 +403,7 @@ public async Task<IActionResult> GetFileStream(string idChannel, string idFile,
403403

404404
if (from >= totalPartialFileLength)
405405
{
406-
Console.WriteLine("Take a file part: from: " + from + ", totalPartialFileLength: " + totalPartialFileLength);
406+
_logger.LogDebug("Take a file part - From: {From}, TotalPartialFileLength: {TotalPartialFileLength}", from, totalPartialFileLength);
407407
int filePart = 0;
408408
while(from >= totalPartialFileLength)
409409
{
@@ -423,7 +423,7 @@ public async Task<IActionResult> GetFileStream(string idChannel, string idFile,
423423
from = (from / 524288) * 524288;
424424
}
425425

426-
Console.WriteLine("Fom:" + from);
426+
_logger.LogDebug("Stream position - From: {From}", from);
427427

428428
if (to == 0)
429429
if (string.IsNullOrEmpty(rangeHeader) || from == 0)
@@ -501,7 +501,7 @@ public async Task<IActionResult> GetFileStream(string idChannel, string idFile,
501501
}
502502
catch (OperationCanceledException)
503503
{
504-
Console.WriteLine("❌ El cliente cerró la conexión.");
504+
_logger.LogInformation("Client closed connection during stream - File: {FileName}", fileName);
505505
}
506506

507507
return new EmptyResult();

TelegramDownloader/Controllers/WebDavController.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ namespace TelegramDownloader.Controllers
1111
public class WebDavController : ControllerBase
1212
{
1313
IDbService _db { get; set; }
14+
private readonly ILogger<WebDavController> _logger;
1415

15-
public WebDavController(IDbService db)
16+
public WebDavController(IDbService db, ILogger<WebDavController> logger)
1617
{
1718
_db = db;
19+
_logger = logger;
1820
}
1921
[HttpGet]
2022
public async Task<List<WebDavFileModel>> webDavPaths([FromQuery] string path, [FromQuery] string depth)
2123
{
22-
Console.WriteLine("Peticion de webDavPaths: " + path);
24+
_logger.LogDebug("WebDav request - Path: {Path}, Depth: {Depth}", path, depth);
2325
var isFile = Path.HasExtension(path);
2426
if (!path.EndsWith("/") && !isFile)
2527
path = path + "/";
@@ -64,7 +66,7 @@ public async Task<List<WebDavFileModel>> webDavPaths([FromQuery] string path, [F
6466
[HttpGet("meta")]
6567
public async Task<object> webDavMetadata([FromQuery] string path)
6668
{
67-
Console.WriteLine("Peticion de webDavMetadata: " + path);
69+
_logger.LogDebug("WebDav metadata request - Path: {Path}", path);
6870
if (String.IsNullOrEmpty(path))
6971
throw new Exception("Path is null or empty");
7072
string channel = path.Split("/")[1];

TelegramDownloader/Data/FileService.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ public async Task<List<BsonFileManagerModel>> ShareFile(string dbName, string bs
321321

322322
public async Task<FileManagerResponse<Syncfusion.Blazor.FileManager.FileManagerDirectoryContent>> itemDeleteAsync(string dbName, ItemsDeleteEventArgs<Syncfusion.Blazor.FileManager.FileManagerDirectoryContent> args)
323323
{
324+
_logger.LogInformation("Deleting items - DbName: {DbName}, Count: {Count}, Path: {Path}",
325+
dbName, args.Files.Count(), args.Path);
324326
string[] names = args.Files.Select(x => x.Name).ToArray();
325327
bool isMyChannel = _ts.isMyChat(Convert.ToInt64(dbName));
326328
// args.Response = await FileManagerService.Delete(args.Path, names, args.Files.ToArray());
@@ -634,6 +636,8 @@ public async Task<MemoryStream> getImage(string dbName, string path, string file
634636

635637
public async Task downloadFile(string dbName, string path, List<string> files, string targetPath, string? collectionId = null, string? channelId = null)
636638
{
639+
_logger.LogInformation("Starting download - DbName: {DbName}, Path: {Path}, FilesCount: {Count}, TargetPath: {TargetPath}",
640+
dbName, path, files.Count, targetPath);
637641
NotificationModel nm = new NotificationModel();
638642
try
639643
{
@@ -806,6 +810,8 @@ public static void functionCalll(string dbName, int messageId, string destPath)
806810
}
807811
public virtual async Task downloadFromTelegram(string dbName, int messageId, string destPath, BsonFileManagerModel file = null, bool shouldWait = false, string path = null)
808812
{
813+
_logger.LogDebug("Queueing download from Telegram - DbName: {DbName}, MessageId: {MessageId}, DestPath: {DestPath}",
814+
dbName, messageId, destPath);
809815
DownloadModel model = new DownloadModel();
810816
model.path = path ?? destPath;
811817
model.tis = _tis;
@@ -1130,6 +1136,8 @@ public async Task CreateDatabase(string id)
11301136

11311137
public async Task AddUploadFileFromServer(string dbName, string currentPath, List<Syncfusion.Blazor.FileManager.FileManagerDirectoryContent> files, InfoDownloadTaksModel idt = null) // ItemsUploadedEventArgs<FileManagerDirectoryContent> args)
11321138
{
1139+
_logger.LogInformation("Adding upload task from server - DbName: {DbName}, Path: {Path}, FilesCount: {Count}",
1140+
dbName, currentPath, files.Count);
11331141
idt = new InfoDownloadTaksModel();
11341142
idt.tis = _tis;
11351143
idt.id = Guid.NewGuid().ToString();
@@ -1215,7 +1223,8 @@ public async Task<String> CreateStrmFiles(string path, string dbName, string hos
12151223
}
12161224
public async Task UploadFileFromServer(string dbName, string currentPath, List<Syncfusion.Blazor.FileManager.FileManagerDirectoryContent> files, InfoDownloadTaksModel dm = null) // ItemsUploadedEventArgs<FileManagerDirectoryContent> args)
12171225
{
1218-
1226+
_logger.LogInformation("Starting upload from server - DbName: {DbName}, Path: {Path}, FilesCount: {Count}",
1227+
dbName, currentPath, files.Count);
12191228
// string currentPath = args.Path;
12201229
NotificationModel nm = new NotificationModel();
12211230
InfoDownloadTaksModel idt = dm;
@@ -1275,7 +1284,6 @@ public async Task UploadFileFromServer(string dbName, string currentPath, List<S
12751284
um.chatName = _ts.getChatName(Convert.ToInt64(dbName));
12761285
// add upload to task list
12771286
idt.addUpload(um);
1278-
um.thread = Thread.CurrentThread;
12791287
while (attempts != 0 || um.state == StateTask.Canceled)
12801288
try
12811289
{
@@ -1292,7 +1300,8 @@ public async Task UploadFileFromServer(string dbName, string currentPath, List<S
12921300
_logger.LogInformation(e, "Current work cancelled");
12931301
throw e;
12941302
}
1295-
_logger.LogError(e, "Exception sending file to Telegram");
1303+
_logger.LogError(e, "Exception sending file to Telegram - FileName: {FileName}, Attempt: {Attempt}, Remaining: {Remaining}",
1304+
file.Name, 4 - attempts, attempts - 1);
12961305
attempts--;
12971306
// waitForNextAttempt *= 2;
12981307
if (attempts == 0 || um.state == StateTask.Canceled)
@@ -1305,6 +1314,7 @@ public async Task UploadFileFromServer(string dbName, string currentPath, List<S
13051314
um.state = StateTask.Error;
13061315
throw e;
13071316
}
1317+
_logger.LogWarning("Retrying upload in {DelayMs}ms - FileName: {FileName}", waitForNextAttempt, file.Name);
13081318
await Task.Delay(waitForNextAttempt);
13091319
}
13101320

@@ -1363,7 +1373,8 @@ public async Task UploadFileFromServer(string dbName, string currentPath, List<S
13631373
_logger.LogInformation(e, "Current work cancelled");
13641374
throw e;
13651375
}
1366-
_logger.LogError(e, "Exception sending file to Telegram");
1376+
_logger.LogError(e, "Exception sending file to Telegram - FileName: {FileName}, Attempt: {Attempt}, Remaining: {Remaining}",
1377+
file.Name, 4 - attempts, attempts - 1);
13671378
attempts--;
13681379
// waitForNextAttempt *= 2;
13691380
if (attempts == 0 || um.state == StateTask.Canceled)
@@ -1373,11 +1384,11 @@ public async Task UploadFileFromServer(string dbName, string currentPath, List<S
13731384
um.SendNotification();
13741385
return;
13751386
}
1376-
1387+
13771388
um.state = StateTask.Error;
13781389
throw e;
13791390
}
1380-
1391+
_logger.LogWarning("Retrying upload in {DelayMs}ms - FileName: {FileName}", waitForNextAttempt, file.Name);
13811392
await Task.Delay(waitForNextAttempt);
13821393
}
13831394

@@ -1432,9 +1443,8 @@ public async Task UploadFileFromServer(string dbName, string currentPath, List<S
14321443
return;
14331444
}
14341445
idt.state = StateTask.Error;
1435-
_logger.LogError(ex, $"Error on uploadFileFromServer, path: {currentPath}");
1446+
_logger.LogError(ex, "Error on uploadFileFromServer - Path: {Path}, Message: {Message}", currentPath, ex.Message);
14361447
nm.sendEvent(new Notification("Error Uploading files to Telegram", "Telegram Upload", NotificationTypes.Error));
1437-
Console.WriteLine(ex.Message);
14381448
throw ex;
14391449
}
14401450
}
@@ -1631,11 +1641,9 @@ public async Task UploadFile(string dbName, string currentPath, UploadFiles file
16311641
}
16321642
catch (Exception ex)
16331643
{
1634-
_logger.LogError(ex, "Error on uploadFile");
1644+
_logger.LogError(ex, "Error on uploadFile - FileName: {FileName}, Message: {Message}", file.File.Name, ex.Message);
16351645
NotificationModel nm = new NotificationModel();
16361646
nm.sendEvent(new Notification($"Error on UploadFile: {file.File.Name}", "Error", NotificationTypes.Error));
1637-
Console.WriteLine(ex.Message);
1638-
16391647
throw ex;
16401648
}
16411649
}
@@ -1855,6 +1863,8 @@ private async Task SaveToFile(IBrowserFile file, string path)
18551863

18561864
private async Task<List<string>> splitFileStreamAsync(string path, string name, int splitSize)
18571865
{
1866+
_logger.LogInformation("Starting file split - FileName: {FileName}, SizeMB: {SizeMB:F2}, SplitSizeMB: {SplitSizeMB}",
1867+
name, new System.IO.FileInfo(System.IO.Path.Combine(path, name)).Length / (1024.0 * 1024.0), splitSize / (1024 * 1024));
18581868
int _gb = TelegramService.splitSizeGB;
18591869
List<string> listPath = new List<string>();
18601870
byte[] buffer = new byte[splitSize];
@@ -1864,7 +1874,6 @@ private async Task<List<string>> splitFileStreamAsync(string path, string name,
18641874
sm.name = name;
18651875
sm._size = fs.Length;
18661876
sm._transmitted = 0;
1867-
sm.thread = Thread.CurrentThread;
18681877
_tis.addToUploadList(sm);
18691878
int index = 1;
18701879
while (fs.Position < fs.Length)
@@ -1892,12 +1901,13 @@ private async Task<List<string>> splitFileStreamAsync(string path, string name,
18921901
index++;
18931902
}
18941903
}
1895-
1904+
_logger.LogInformation("File split completed - FileName: {FileName}, Parts: {PartsCount}", name, listPath.Count);
18961905
return listPath;
18971906
}
18981907

18991908
private async Task mergeFileStreamAsync(List<string> pathList, string destName)
19001909
{
1910+
_logger.LogInformation("Starting file merge - DestName: {DestName}, PartsCount: {PartsCount}", destName, pathList.Count);
19011911
using (FileStream fileResult = new FileStream(destName, FileMode.Create))
19021912
foreach (string pathSplited in pathList)
19031913
{
@@ -1909,6 +1919,7 @@ private async Task mergeFileStreamAsync(List<string> pathList, string destName)
19091919
//await fileResult.WriteAsync(buffer);
19101920
}
19111921
}
1922+
_logger.LogInformation("File merge completed - DestName: {DestName}", destName);
19121923
}
19131924
}
19141925
}

TelegramDownloader/Data/FileServiceV2.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public override async Task downloadFile(string dbName, List<FileManagerDirectory
9797

9898
public async Task downloadSplitFilesV2(FileManagerDirectoryContent itemFile, BsonFileManagerModel file, string currentFilePath, string dbName)
9999
{
100+
_logger.LogInformation("Starting split file download V2 - FileName: {FileName}, Parts: {Parts}", itemFile.Name, file.ListMessageId.Count);
100101
int i = 1;
101102
string filePathPart = Path.Combine(currentFilePath, itemFile.Name);
102103
if (File.Exists(filePathPart))
@@ -108,10 +109,12 @@ public async Task downloadSplitFilesV2(FileManagerDirectoryContent itemFile, Bso
108109
await downloadFromTelegramV2(dbName, messageId, filePathPart, file, true, Path.Combine(currentFilePath, itemFile.Name), i);
109110
i++;
110111
}
112+
_logger.LogInformation("Split file download V2 completed - FileName: {FileName}", itemFile.Name);
111113
}
112114

113115
public async Task downloadFromTelegramV2(string dbName, int messageId, string destPath, BsonFileManagerModel file = null, bool shouldWait = false, string path = null, Int32 part = 0)
114116
{
117+
_logger.LogDebug("Queueing download V2 - DbName: {DbName}, MessageId: {MessageId}, Part: {Part}", dbName, messageId, part);
115118
DownloadModel model = new DownloadModel();
116119
model.path = path ?? destPath;
117120
model.tis = _tis;

0 commit comments

Comments
 (0)