Skip to content

Commit 7abf2d8

Browse files
authored
Merge pull request #28 from mateof/develop
release: v2.0.6
2 parents bb8c347 + 9b12a0b commit 7abf2d8

19 files changed

Lines changed: 316 additions & 95 deletions

TelegramDownloader/Controllers/FileController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ public async Task<IActionResult> GetFile(string id, string? idChannel, string? i
247247
cm.message = idM;
248248
file = new FileStream(System.IO.Path.Combine(FileService.TEMPDIR, "_temp", $"{idChannel}-{idFile}-{id}"), FileMode.Create, FileAccess.ReadWrite);
249249
DownloadModel dm = new DownloadModel();
250+
dm.tis = _tis;
250251
dm.channelName = _ts.getChatName(Convert.ToInt64(idChannel));
251252
_tis.addToDownloadList(dm);
252253
await _ts.DownloadFileAndReturn(cm, file, model: dm);

TelegramDownloader/Data/FileService.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ public static void functionCalll(string dbName, int messageId, string destPath)
796796
private async Task downloadFromTelegram(string dbName, int messageId, string destPath, BsonFileManagerModel file = null)
797797
{
798798
DownloadModel model = new DownloadModel();
799+
model.tis = _tis;
799800
if (file != null)
800801
{
801802
model.name = file.Name;
@@ -817,7 +818,11 @@ private async Task downloadFromTelegram(string dbName, int messageId, string des
817818
public async Task DownloadFileFromChat(ChatMessages message, string fileName = null, string folder = null, DownloadModel model = null)
818819
{
819820
if (model == null)
821+
{
820822
model = new DownloadModel();
823+
model.tis = _tis;
824+
}
825+
821826
model.name = fileName;
822827
if (message.message.media is MessageMediaDocument { document: Document document })
823828
{
@@ -1099,6 +1104,7 @@ public async Task CreateDatabase(string id)
10991104
public async Task AddUploadFileFromServer(string dbName, string currentPath, List<Syncfusion.Blazor.FileManager.FileManagerDirectoryContent> files, InfoDownloadTaksModel idt = null) // ItemsUploadedEventArgs<FileManagerDirectoryContent> args)
11001105
{
11011106
idt = new InfoDownloadTaksModel();
1107+
idt.tis = _tis;
11021108
idt.id = Guid.NewGuid().ToString();
11031109
idt.total = 0;
11041110
idt.totalSize = 0;
@@ -1135,9 +1141,8 @@ public async Task AddUploadFileFromServer(string dbName, string currentPath, Lis
11351141
}
11361142
idt.callbacks = new Callbacks();
11371143
idt.callbacks.callback = async () => await UploadFileFromServer(dbName, currentPath, files, idt);
1138-
TransactionInfoService ti = new TransactionInfoService();
1139-
ti.addToInfoDownloadTaskList(idt);
1140-
ti.CheckPendingUploadInfoTasks();
1144+
_tis.addToInfoDownloadTaskList(idt);
1145+
_tis.CheckPendingUploadInfoTasks();
11411146
}
11421147

11431148

@@ -1254,6 +1259,7 @@ public async Task UploadFileFromServer(string dbName, string currentPath, List<S
12541259
int attempts = 3;
12551260
int waitForNextAttempt = 1000;
12561261
UploadModel um = new UploadModel();
1262+
um.tis = _tis;
12571263
um.path = currentFilePath;
12581264
um.chatName = _ts.getChatName(Convert.ToInt64(dbName));
12591265
// add upload to task list
@@ -1264,7 +1270,7 @@ public async Task UploadFileFromServer(string dbName, string currentPath, List<S
12641270
{
12651271
wt = new WaitingTime();
12661272
using (FileStream fs = new FileStream(s, FileMode.Open, FileAccess.Read, FileShare.Read))
1267-
m = await _ts.uploadFile(dbName, fs, $"({i} of {filesSplit.Count}) - " + file.Name, um: um);
1273+
m = await _ts.uploadFile(dbName, fs, $"({i} of {filesSplit.Count}) - " + file.Name, um: um, caption: getCaption(filePath));
12681274
attempts = 0;
12691275
await wt.Sleep(); // sleep 1 second to avoid 420 flood_wait_x
12701276
}
@@ -1303,6 +1309,7 @@ public async Task UploadFileFromServer(string dbName, string currentPath, List<S
13031309
int attempts = 3;
13041310
int waitForNextAttempt = 60000;
13051311
UploadModel um = new UploadModel();
1312+
um.tis = _tis;
13061313
um.path = currentFilePath;
13071314
um.chatName = _ts.getChatName(Convert.ToInt64(dbName));
13081315
// add upload to task list
@@ -1317,17 +1324,17 @@ public async Task UploadFileFromServer(string dbName, string currentPath, List<S
13171324
using (FileStream ms = new FileStream(System.IO.Path.Combine(currentFilePath), FileMode.Open))
13181325
if (ImageExtensions.Any(x => file.Name.ToUpper().EndsWith(x)) && file.Size >= MAXIMAGESIZE)
13191326
{
1320-
m = await _ts.uploadFile(dbName, ms, file.Name, "application/octet-stream", um);
1327+
m = await _ts.uploadFile(dbName, ms, file.Name, "application/octet-stream", um, caption: getCaption(filePath));
13211328
}
13221329
else
1323-
m = await _ts.uploadFile(dbName, ms, file.Name, um: um);
1330+
m = await _ts.uploadFile(dbName, ms, file.Name, um: um, caption: getCaption(filePath));
13241331
}
13251332
catch (Exception ex)
13261333
{
13271334
if (new List<string> { "IMAGE", "PHOTO" }.Any(x => ex.Message.Contains(x)))
13281335
{
13291336
using (FileStream ms = new FileStream(System.IO.Path.Combine(currentFilePath), FileMode.Open))
1330-
m = await _ts.uploadFile(dbName, ms, file.Name, "application/octet-stream", um);
1337+
m = await _ts.uploadFile(dbName, ms, file.Name, "application/octet-stream", um, caption: getCaption(filePath));
13311338
}
13321339
else
13331340
{
@@ -1508,7 +1515,7 @@ public async Task UploadFile(string dbName, string currentPath, UploadFiles file
15081515
foreach (string s in files)
15091516
{
15101517
using (FileStream fs = new FileStream(s, FileMode.Open, FileAccess.Read, FileShare.Read))
1511-
m = await _ts.uploadFile(dbName, fs, $"({i} of {files.Count}) - " + file.File.Name);
1518+
m = await _ts.uploadFile(dbName, fs, $"({i} of {files.Count}) - " + file.File.Name, caption: getCaption(currentPath));
15121519
model.ListMessageId.Add(m.ID);
15131520
File.Delete(s);
15141521
i++;
@@ -1519,7 +1526,7 @@ public async Task UploadFile(string dbName, string currentPath, UploadFiles file
15191526
{
15201527
using (FileStream ms = new FileStream(System.IO.Path.Combine(currentFilePath, file.File.Name), FileMode.Open))
15211528
{
1522-
m = await _ts.uploadFile(dbName, ms, file.File.Name);
1529+
m = await _ts.uploadFile(dbName, ms, file.File.Name, caption: getCaption(currentPath));
15231530
}
15241531
model.MessageId = m.ID;
15251532
}
@@ -1751,6 +1758,11 @@ private async Task<List<ExpandoObject>> AddChildRecords(string id, string parent
17511758
return response;
17521759
}
17531760

1761+
private string? getCaption(string filePath)
1762+
{
1763+
return GeneralConfigStatic.config.ShouldShowCaptionPath ? filePath : null;
1764+
}
1765+
17541766
private async Task<MemoryStream> ToMemoryStreamAsync(Stream stream)
17551767
{
17561768
var memoryStream = new MemoryStream();

TelegramDownloader/Data/ITelegramService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public interface ITelegramService
2929
Task<string> getPhotoThumb(ChatBase chat);
3030
Task logOff();
3131
Task sendVerificationCode(string vc);
32-
Task<Message> uploadFile(string chatId, Stream file, string fileName, string mimeType = null, UploadModel um = null);
32+
Task<Message> uploadFile(string chatId, Stream file, string fileName, string mimeType = null, UploadModel um = null, string caption = null);
3333

3434
}
3535
}

TelegramDownloader/Data/TelegramService.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,17 +282,18 @@ public async Task<List<ChatViewBase>> getAllSavedChats()
282282
return allChats;
283283
}
284284

285-
public async Task<Message> uploadFile(string chatId, Stream file, string fileName, string mimeType = null, UploadModel um = null)
285+
public async Task<Message> uploadFile(string chatId, Stream file, string fileName, string mimeType = null, UploadModel um = null, string caption = null)
286286
{
287287
um = um ?? new UploadModel();
288+
um.tis = _tis;
288289

289290
InputPeer peer = chats.chats[Convert.ToInt64(chatId)];
290291
um.name = fileName;
291292
um._size = file.Length;
292293
um._transmitted = 0;
293294
_tis.addToUploadList(um);
294295
var inputFile = await client.UploadFileAsync(file, fileName, um.ProgressCallback);
295-
return await client.SendMediaAsync(peer, fileName, inputFile, mimeType);
296+
return await client.SendMediaAsync(peer, caption ?? fileName, inputFile, mimeType);
296297
}
297298

298299
public async Task deleteFile(string chatId, int idMessage)
@@ -417,7 +418,11 @@ public async Task RemoveFavouriteChannel(long id)
417418
public async Task<Stream> DownloadFileAndReturn(ChatMessages message, Stream ms = null, string fileName = null, string folder = null, DownloadModel model = null)
418419
{
419420
if (model == null)
421+
{
420422
model = new DownloadModel();
423+
model.tis = _tis;
424+
}
425+
421426
model.m = message;
422427
model.channel = message.user;
423428

@@ -466,7 +471,11 @@ public async Task<Stream> DownloadFileAndReturn(ChatMessages message, Stream ms
466471
public async Task<string> DownloadFile(ChatMessages message, string fileName = null, string folder = null, DownloadModel model = null, bool shouldAddToList = false)
467472
{
468473
if (model == null)
474+
{
469475
model = new DownloadModel();
476+
model.tis = _tis;
477+
}
478+
470479
model.m = message;
471480
model.channel = message.user;
472481

TelegramDownloader/Models/DownloadModel.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class InfoDownloadTaksModel
3232
public int progress { get; set; }
3333
public List<UploadModel> currentUploads { get; set; } = new List<UploadModel>();
3434
public List<DownloadModel> currentDownloads { get; set; } = new List<DownloadModel>();
35+
public TransactionInfoService tis { get; set; }
3536

3637
public async void AddOne(long size)
3738
{
@@ -48,7 +49,6 @@ public async void AddOne(long size)
4849
public async void markAsCompleted()
4950
{
5051
state = StateTask.Completed;
51-
TransactionInfoService tis = new TransactionInfoService();
5252
tis.CheckPendingUploadInfoTasks();
5353
EventChanged?.Invoke(this, new InfoTaskEventArgs());
5454

@@ -82,7 +82,6 @@ public void Retry()
8282
{
8383
state = StateTask.Pending;
8484
currentUpload = 0;
85-
TransactionInfoService tis = new TransactionInfoService();
8685
tis.CheckPendingUploadInfoTasks();
8786
}
8887

@@ -107,7 +106,6 @@ public void cancelTask()
107106
um.Cancel();
108107
}
109108
}
110-
TransactionInfoService tis = new TransactionInfoService();
111109
tis.CheckPendingUploadInfoTasks();
112110
}
113111

@@ -119,6 +117,7 @@ private void deleteNotWorkingTasks()
119117
}
120118
public class DownloadModel
121119
{
120+
public Mutex mutex = new Mutex();
122121
public event EventHandler<DownloadEventArgs> EventChanged;
123122
public string id = Guid.NewGuid().ToString();
124123
public string action { get; set; } = "Download";
@@ -129,14 +128,16 @@ public class DownloadModel
129128
public Callbacks callbacks { get; set; }
130129

131130
public long _size { get; set; }
132-
public long _transmitted { get; set; }
131+
public long _transmitted { get; set; } = 0;
133132

134133
public string _sizeString { get; set; }
135134
public string _transmittedString { get; set; }
136135

137136
public IPeerInfo channel { get; set; }
138137
public string channelName { get; set; }
139138
public int progress { get; set; }
139+
public TransactionInfoService tis { get; set; }
140+
140141

141142
public DownloadModel(string? name = null)
142143
{
@@ -150,11 +151,12 @@ public void ProgressCallback(long transmitted, long totalSize)
150151
throw new Exception($"Canceled {name}");
151152
if (state == StateTask.Paused)
152153
{
153-
TransactionInfoService tis = new TransactionInfoService();
154+
154155
state = StateTask.Working;
155156
tis.deleteDownloadInList(this);
156157
throw new Exception($"Paused {name}");
157158
}
159+
tis.addDownloadBytes(transmitted - _transmitted);
158160
_transmitted = transmitted;
159161
_sizeString = HelperService.SizeSuffix(totalSize);
160162
_transmittedString = HelperService.SizeSuffix(transmitted);
@@ -165,7 +167,6 @@ public void ProgressCallback(long transmitted, long totalSize)
165167
state = StateTask.Completed;
166168
NotificationModel nm = new NotificationModel();
167169
nm.sendEvent(new Notification($"Download {name} completed", "Download Completed", NotificationTypes.Success));
168-
TransactionInfoService tis = new TransactionInfoService();
169170
tis.CheckPendingDownloads();
170171
}
171172

@@ -174,7 +175,6 @@ public void ProgressCallback(long transmitted, long totalSize)
174175
public void Cancel()
175176
{
176177
state = StateTask.Canceled;
177-
TransactionInfoService tis = new TransactionInfoService();
178178
tis.CheckPendingDownloads();
179179
EventChanged?.Invoke(this, new DownloadEventArgs());
180180
}
@@ -202,19 +202,21 @@ public class UploadModel
202202
public string path { get; set; }
203203

204204
public long _size { get; set; }
205-
public long _transmitted { get; set; }
205+
public long _transmitted { get; set; } = 0;
206206

207207
public string _sizeString { get; set; }
208208
public string _transmittedString { get; set; }
209209
public string chatName { get; set; }
210210
public IPeerInfo channel { get; set; }
211211
public int progress { get; set; }
212212
public Thread thread { get; set; }
213+
public TransactionInfoService tis { get; set; }
213214

214215
public virtual void ProgressCallback(long transmitted, long totalSize)
215216
{
216217
if (state == StateTask.Canceled)
217218
throw new Exception($"Canceled {name}");
219+
tis.addUploadBytes(transmitted - _transmitted);
218220
_transmitted = transmitted;
219221
_sizeString = HelperService.SizeSuffix(totalSize);
220222
_transmittedString = HelperService.SizeSuffix(transmitted);

TelegramDownloader/Models/GeneralConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class GeneralConfig
7272
public int SplitSize { get; set; } = 0;
7373
public int MaxSimultaneousDownloads = 1;
7474
public bool CheckHash { get; set; } = false;
75+
public bool ShouldShowCaptionPath { get; set; } = false;
7576
public bool ShouldShowLogInTerminal { get; set; } = false;
7677
public List<long> FavouriteChannels { get; set; } = new List<long>();
7778

0 commit comments

Comments
 (0)