Skip to content

Commit 1581ecc

Browse files
authored
Merge from Develop (#12)
* chore: add dropdown button to channel name * feat: deleting shared feat: refresh after import feat: remember tab * chore: improves
1 parent d1d874a commit 1581ecc

15 files changed

Lines changed: 387 additions & 239 deletions

TelegramDownloader/Data/FileService.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,12 @@ public async Task<BsonSharedInfoModel> GetSharedInfoById(string id)
276276
return await _db.getSingleFile(id);
277277
}
278278

279+
public async Task DeleteShared(string id, string collectionId)
280+
{
281+
await _db.DeleteSharedCollection(collectionId);
282+
await _db.DeleteSharedInfo(id);
283+
}
284+
279285
public async Task<MemoryStream> exportAllData(string dbName)
280286
{
281287
var json = System.Text.Json.JsonSerializer.Serialize(await _db.getAllDatabaseData(dbName));
@@ -645,7 +651,7 @@ public async Task downloadFile(string dbName, List<FileManagerDirectoryContent>
645651
if (!itemFile.IsFile)
646652
{
647653
Directory.CreateDirectory(System.IO.Path.Combine(currentFilePath, itemFile.Name));
648-
var filesInDir = collectionId == null ? await _db.getAllFilesInDirectoryPath(dbName, itemFile.FilterPath + itemFile.Name + "/") : await _db.getAllFilesInDirectoryPath(dbName, itemFile.FilterPath + itemFile.Name + "/", collectionId);
654+
var filesInDir = collectionId == null ? await _db.getAllFilesInDirectoryPath(dbName, itemFile.FilterPath == "" ? "/" : itemFile.FilterPath + itemFile.Name + "/") : await _db.getAllFilesInDirectoryPath(dbName, itemFile.FilterPath == "" ? "/" : itemFile.FilterPath + itemFile.Name + "/", collectionId);
649655
if (filesInDir.Count() > 0)
650656
{
651657
await downloadFile(dbName, filesInDir.Select(x => x.toFileManagerContent()).ToList(), System.IO.Path.Combine(currentTargetPath, itemFile.Name).Replace("\\", "/"), collectionId, channelId);

TelegramDownloader/Data/IFileService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public interface IFileService
1212
Task<List<FileManagerDirectoryContent>> createFolder(string dbName, FolderCreateEventArgs<FileManagerDirectoryContent> args);
1313
void cleanTempFolder();
1414
Task<BsonSharedInfoModel> GetSharedInfoById(string id);
15+
Task DeleteShared(string id, string collectionId);
1516
Task downloadFile(string dbName, List<FileManagerDirectoryContent> files, string targetPath, string? collectionId = null, string? channelId = null);
1617
Task downloadFile(string dbName, string path, List<string> files, string targetPath, string? collectionId = null, string? channelId = null);
1718
Task downloadFileToServer(string dbName, string path, string destPath);

TelegramDownloader/Data/ITelegramService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public interface ITelegramService
1010
bool checkChannelExist(string id);
1111
bool checkUserLogin();
1212
Task deleteFile(string chatId, int idMessage);
13+
Task<User> CallQrGenerator(Action<string> func, CancellationToken ct, bool logoutFirst = false);
1314
Task<string> DownloadFile(ChatMessages message, string fileName = null, string folder = null, DownloadModel model = null);
1415
Task<Stream> DownloadFileAndReturn(ChatMessages message, Stream ms = null, string fileName = null, string folder = null, DownloadModel model = null);
1516
Task<List<ChatViewBase>> GetFouriteChannels(bool mustRefresh = true);

TelegramDownloader/Data/TelegramService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ private void newClient()
4848

4949
}
5050

51+
public async Task<User> CallQrGenerator(Action<string> func, CancellationToken ct, bool logoutFirst = false)
52+
{
53+
return await client.LoginWithQRCode(func, logoutFirst: logoutFirst, ct: ct);
54+
}
55+
5156
public async Task<User> GetUser()
5257
{
5358
return client.User;

TelegramDownloader/Data/db/DbService.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,19 @@ public async Task<BsonSharedInfoModel> InsertSharedInfo(BsonSharedInfoModel sim,
110110

111111
}
112112

113+
public async Task DeleteSharedCollection(string collectionId, string dbName = SHARED_DB_NAME)
114+
{
115+
await getDatabase(dbName).DropCollectionAsync(collectionId);
116+
}
117+
118+
public async Task DeleteSharedInfo(string id, string dbName = SHARED_DB_NAME, string collection = "info")
119+
{
120+
await getDatabase(dbName).GetCollection<BsonSharedInfoModel>(collection).DeleteOneAsync(x => x.Id == id);
121+
}
122+
113123
public async Task<List<BsonSharedInfoModel>> getSharedInfoList(string dbName = SHARED_DB_NAME, string collection = "info", string? filter = null)
114124
{
125+
if (collection == null)
115126
if (collection == null)
116127
collection = "info";
117128
List<BsonSharedInfoModel> list = new List<BsonSharedInfoModel>();

TelegramDownloader/Data/db/IDbService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public interface IDbService
1010
Task<BsonSharedInfoModel> InsertSharedInfo(BsonSharedInfoModel sim, string dbName = DbService.SHARED_DB_NAME, string collection = "info");
1111
Task<List<BsonSharedInfoModel>> getSharedInfoList(string dbName = DbService.SHARED_DB_NAME, string collection = "info", string? filter = null);
1212
Task<BsonSharedInfoModel> getSingleFile(string id, string dbName = DbService.SHARED_DB_NAME, string collection = "info");
13+
Task DeleteSharedCollection(string collectionId, string dbName = DbService.SHARED_DB_NAME);
14+
Task DeleteSharedInfo(string id, string dbName = DbService.SHARED_DB_NAME, string collection = "info");
1315
Task addBytesToFolder(string dbName, string folderId, long bytes, string collectionName = "directory");
1416
Task checkAndSetDirectoryHasChild(string dbName, string id, string collectionName = "directory");
1517
Task<BsonFileManagerModel> copyItem(string dbName, string sourceId, FileManagerDirectoryContent target, string targetPath, bool isFile, string collectionName = "directory");

TelegramDownloader/Pages/FileManager.razor

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
@using TelegramDownloader.Models
66
@using TelegramDownloader.Pages.Partials
77
@inject IFileService fs
8+
@inject ITelegramService ts
89
@inject ILogger<FileManager> Logger
910
@inject IJSRuntime JSRuntime;
1011
@inject NavigationManager MyNavigationManager;
@@ -19,10 +20,16 @@
1920
}
2021
</style>
2122

22-
<h3>File Manager <span class="badge bg-secondary">@chatName</span></h3>
23-
24-
<button class="btn" @onclick="exportData"><i class="bi bi-cloud-download"></i> Export</button>
25-
<button class="btn" @onclick="importData"><i class="bi bi-cloud-upload"></i> Import</button>
23+
<h3>
24+
File Manager
25+
<Dropdown Color="DropdownColor.Secondary">
26+
<DropdownToggleButton>@chatName</DropdownToggleButton>
27+
<DropdownMenu>
28+
<DropdownItem @onclick="exportData" Type="DropdownItemType.Button"><i class="bi bi-cloud-download"></i> Export</DropdownItem>
29+
<DropdownItem @onclick="importData" Type="DropdownItemType.Button"><i class="bi bi-cloud-upload"></i> Import</DropdownItem>
30+
</DropdownMenu>
31+
</Dropdown>
32+
</h3>
2633

2734
<ul class="nav nav-tabs">
2835
<li class="nav-item">
@@ -51,8 +58,7 @@
5158
{
5259
try
5360
{
54-
bsi = await fs.GetSharedInfoById(id);
55-
chatName = bsi.Name;
61+
chatName = ts.getChatName(Convert.ToInt64(id));
5662
} catch(Exception ex)
5763
{
5864
Logger.LogError(ex, "Error on GetSharedInfoById");

TelegramDownloader/Pages/Index.razor

Lines changed: 99 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
@layout EmptyLayout
22
@page "/"
3+
@implements IDisposable
34

5+
@using QRCoder
46
@using TelegramDownloader.Data
57
@using TelegramDownloader.Models
68
@inject ITelegramService ts
79
@inject NavigationManager NavManager
810
@inject IJSRuntime JSRuntime;
11+
@inject PreloadService preloadService
912

1013
<style>
1114
.form-signin {
@@ -19,55 +22,66 @@
1922
padding-top: inherit;
2023
}
2124
</style>
25+
<Preload LoadingText="Loading..." />
2226
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/intl-tel-input@23.0.12/build/css/intlTelInput.css">
2327

2428
<PageTitle>Login</PageTitle>
2529

26-
@if(Model.type != null) {
30+
@if (Model != null && Model.type != null)
31+
{
2732
<div class="container form-signin">
2833
<div class="text-center">
2934
<img src="https://static.vecteezy.com/system/resources/previews/023/986/534/large_2x/telegram-logo-telegram-logo-transparent-telegram-icon-transparent-free-free-png.png" alt="Telegram logo" style="max-width: -webkit-fill-available;" />
30-
<EditForm class="form-signin mb-4" Model="Model" OnSubmit="Submit" FormName="LoginModel">
31-
@switch (Model.type)
32-
{
33-
case "phone":
34-
<div class="form-group">
35-
<label for="phone">Teléfono</label>
36-
<InputText class="form-control" id="phone" type="text" @bind-Value="Model!.value" aria-describedby="phone" maxlength="12" data-mask="+00.000.00.00.00" placeholder="Teléfono" />
37-
</div>
38-
break;
39-
case "vc":
40-
<div class="form-group">
41-
<label for="code">Código</label>
42-
<InputText class="form-control" id="code" maxlength="5" type="text" @bind-Value="Model!.value" />
43-
</div>
44-
break;
45-
case "pass":
46-
<div class="form-group">
47-
<label for="pass">Contraseña</label>
48-
<InputText class="form-control" id="pass" type="password" @bind-Value="Model!.value" />
49-
</div>
50-
break;
51-
case "ok":
52-
<span> Authenticado </span>
53-
break;
54-
}
55-
<div class="form-group buttons">
5635

57-
@if (Model.type == "ok")
36+
<EditForm class="form-signin mb-4" Model="Model" OnSubmit="Submit" FormName="LoginModel">
37+
@switch (Model.type)
5838
{
59-
<button class="btn btn-info" @onclick="Salir" type="button">Desconectar</button>
39+
case "phone":
40+
<div class="form-group">
41+
<label for="phone">Teléfono</label>
42+
<InputText class="form-control" id="phone" type="text" @bind-Value="Model!.value" aria-describedby="phone" maxlength="12" data-mask="+00.000.00.00.00" placeholder="Teléfono" />
43+
</div>
44+
break;
45+
case "vc":
46+
<div class="form-group">
47+
<label for="code">Código</label>
48+
<InputText class="form-control" id="code" maxlength="5" type="text" @bind-Value="Model!.value" />
49+
</div>
50+
break;
51+
case "pass":
52+
<div class="form-group">
53+
<label for="pass">Contraseña</label>
54+
<InputText class="form-control" id="pass" type="password" @bind-Value="Model!.value" />
55+
</div>
56+
break;
57+
case "ok":
58+
<span> Authenticado </span>
59+
break;
6060
}
61-
else
62-
{
63-
<button class="btn btn-info" type="submit">Send</button>
64-
@if(Model.type != null && Model.type != "phone")
61+
<div class="form-group buttons">
62+
63+
@if (Model.type == "ok")
64+
{
65+
<button class="btn btn-info" @onclick="Salir" type="button">Desconectar</button>
66+
}
67+
else
68+
{
69+
<button class="btn btn-info" type="submit">Send</button>
70+
@if (Model.type != null && Model.type != "phone")
6571
{
6672
<button class="btn btn-warning" @onclick="Cancel">Cancel</button>
6773
}
74+
}
75+
</div>
76+
@if (!string.IsNullOrEmpty(imageString))
77+
{
78+
<img src="data:image;base64, @imageString" />
6879
}
69-
</div>
70-
</EditForm>
80+
81+
</EditForm>
82+
83+
84+
7185
</div>
7286
</div>
7387
} else
@@ -88,13 +102,17 @@
88102
@code {
89103
// [SupplyParameterFromForm]
90104
public LoginModel? Model { get; set; } = null;
105+
private CancellationToken cTokenQr { get; set; }
106+
CancellationTokenSource source { get; set; }
107+
private string imageString { get; set; }
91108

92109
protected override async Task OnInitializedAsync()
93110
{
94111
Model ??= new();
95112
Model.type = await ts.checkAuth(Model.value);
96113
StateHasChanged();
97-
isLogin();
114+
await isLogin();
115+
98116

99117
}
100118

@@ -121,13 +139,44 @@
121139

122140
Model.value = "";
123141
StateHasChanged();
124-
isLogin();
142+
await isLogin();
125143
// Logger.LogInformation("Id = {Id}", Model?.Id);
126144
}
127145

128-
private void isLogin()
146+
private async Task isLogin()
147+
{
148+
if (Model.type == "ok")
149+
{
150+
if (source != null)
151+
{
152+
if (source.Token.CanBeCanceled)
153+
{
154+
source.Cancel();
155+
}
156+
}
157+
NavManager.NavigateTo("/fetchdata");
158+
} else
159+
{
160+
// source = new CancellationTokenSource();
161+
// cTokenQr = source.Token;
162+
// var user = await ts.CallQrGenerator(GenerateQR, cTokenQr, true);
163+
// if (user != null)
164+
// {
165+
// NavManager.NavigateTo("/fetchdata");
166+
// }
167+
}
168+
}
169+
170+
private void GenerateQR(string data)
129171
{
130-
if (Model.type == "ok") NavManager.NavigateTo("/fetchdata");
172+
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
173+
using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(data, QRCodeGenerator.ECCLevel.Q))
174+
using (PngByteQRCode qrCode = new PngByteQRCode(qrCodeData))
175+
{
176+
byte[] qrCodeImage = qrCode.GetGraphic(20);
177+
imageString = Convert.ToBase64String(qrCodeImage);
178+
StateHasChanged();
179+
}
131180
}
132181

133182
private async void Salir()
@@ -144,4 +193,15 @@
144193
await ts.logOff();
145194
NavManager.NavigateTo("/", true);
146195
}
196+
197+
public void Dispose()
198+
{
199+
if (source != null)
200+
{
201+
if (source.Token.CanBeCanceled)
202+
{
203+
source.Cancel();
204+
}
205+
}
206+
}
147207
}

TelegramDownloader/Pages/Modals/ImportDataModal.razor

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
</div>
5959

6060
@code {
61+
[Parameter]
62+
public EventCallback OnCloseCallback { get; set; }
6163
[Parameter]
6264
public string? id { get; set; }
6365
[Parameter]
@@ -83,10 +85,14 @@
8385
StateHasChanged();
8486
}
8587

86-
public void Close()
88+
public async Task Close()
8789
{
8890
ModalDisplay = "none";
8991
ModalClass = "";
92+
if (OnCloseCallback.HasDelegate)
93+
{
94+
await OnCloseCallback.InvokeAsync(null);
95+
}
9096
StateHasChanged();
9197
}
9298

0 commit comments

Comments
 (0)