Skip to content

Commit 53211b9

Browse files
Merge pull request #26 from PrasanthMadhaiyan/master
981280: Need to update the Blazor FileManager component AI samples.
2 parents f986c15 + cdfc3bf commit 53211b9

File tree

5 files changed

+108
-119
lines changed

5 files changed

+108
-119
lines changed

blazor/SyncfusionAISamples/Components/Pages/FileManager/FileManagerTagSearch.razor

Lines changed: 46 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -38,68 +38,56 @@
3838
</Description>
3939

4040
<div class="control-container">
41-
<div class="file-manager">
42-
<SfFileManager ShowTooltip="false" TValue="FileManagerDirectoryContent" ID="@FileManagerId" @ref="FileManager" Height="500px">
43-
<FileManagerSearchSettings AllowSearchOnTyping=false></FileManagerSearchSettings>
44-
<FileManagerToolbarSettings ToolbarItems="@Items"></FileManagerToolbarSettings>
45-
<FileManagerContextMenuSettings File="@FileItems" Folder="@FolderItems"></FileManagerContextMenuSettings>
46-
<FileManagerEvents TValue="FileManagerDirectoryContent" OnRead="OnReadAsync" ItemsDeleting="ItemsDeletingAsync" FolderCreating="FolderCreatingAsync" Searching="SearchingAsync" ItemRenaming="ItemRenamingAsync" ItemsMoving="ItemsMovingAsync" ItemsUploaded="ItemsUploadedAsync" BeforeDownload="BeforeDownload" BeforeImageLoad="BeforeImageLoadAsync" MenuOpened="MenuOpened" OnMenuClick="OnMenuClick" FileSelection="CloseTagContainer"></FileManagerEvents>
47-
</SfFileManager>
48-
</div>
49-
@if (isTag)
50-
{
51-
<div id="tags" class="tags">
52-
<div id="tagContainer">
53-
<div class="title-container">
54-
<strong>Existing File Tags:</strong>
55-
<SfButton IconCss="e-icons e-close" CssClass="e-small" OnClick="CloseTagContainer"></SfButton>
56-
</div>
57-
<br />
58-
@if (fileTags.Length == 0)
59-
{
60-
<p>No tags available</p>
61-
}
62-
else
63-
{
64-
<div class="scrollable-container">
65-
<SfChip EnableDelete="true" @ref="Chip">
66-
<ChipEvents Deleted="@Deleted"></ChipEvents>
67-
<ChipItems>
68-
@foreach (var tag in fileTags)
69-
{
70-
<ChipItem Text="@tag"></ChipItem>
71-
}
72-
</ChipItems>
73-
</SfChip>
41+
<div class="file-manager">
42+
<SfFileManager ShowTooltip="false" TValue="FileManagerDirectoryContent" ID="@FileManagerId" @ref="FileManager" Height="500px">
43+
<FileManagerSearchSettings AllowSearchOnTyping=false></FileManagerSearchSettings>
44+
<FileManagerToolbarSettings ToolbarItems="@Items"></FileManagerToolbarSettings>
45+
<FileManagerContextMenuSettings File="@FileItems" Folder="@FolderItems"></FileManagerContextMenuSettings>
46+
<FileManagerEvents TValue="FileManagerDirectoryContent" OnRead="OnReadAsync" ItemsDeleting="ItemsDeletingAsync" FolderCreating="FolderCreatingAsync" Searching="SearchingAsync" ItemRenaming="ItemRenamingAsync" ItemsMoving="ItemsMovingAsync" ItemsUploaded="ItemsUploadedAsync" BeforeDownload="BeforeDownload" BeforeImageLoad="BeforeImageLoadAsync" MenuOpened="MenuOpened" OnMenuClick="OnMenuClick" FileSelection="CloseTagContainer"></FileManagerEvents>
47+
</SfFileManager>
48+
</div>
49+
@if (isTag)
50+
{
51+
<div id="tags" class="tags">
52+
<div id="tagContainer">
53+
<div class="title-container">
54+
<strong>Existing File Tags:</strong>
55+
<SfButton IconCss="e-icons e-close" CssClass="e-small" OnClick="CloseTagContainer"></SfButton>
7456
</div>
75-
}
76-
<br />
77-
<strong style="margin-bottom: 10px;">AI suggested File Tags:</strong>
78-
@if (aiTags.Length == 0)
79-
{
80-
<p>Click <strong>"Generate AI Tags"</strong> button to get suggested tags from AI.</p>
81-
}
82-
else
83-
{
84-
<div class="scrollable-container">
85-
<SfChip EnableDelete="true" @ref="AIChip">
86-
<ChipEvents Deleted="@AIDeleted"></ChipEvents>
87-
<ChipItems>
88-
@foreach (var tag in aiTags)
89-
{
90-
<ChipItem Text="@tag"></ChipItem>
91-
}
92-
</ChipItems>
93-
</SfChip>
57+
<br />
58+
@if (fileTags?.Count == 0)
59+
{
60+
<p>No tags available</p>
61+
}
62+
else
63+
{
64+
<div class="scrollable-container">
65+
<SfChip EnableDelete="true" @ref="Chip" Chips="@fileTags">
66+
<ChipEvents Deleted="@Deleted"></ChipEvents>
67+
</SfChip>
68+
</div>
69+
}
70+
<br />
71+
<strong style="margin-bottom: 10px;">AI suggested File Tags:</strong>
72+
@if (aiTags?.Count == 0)
73+
{
74+
<p>Click <strong>"Generate AI Tags"</strong> button to get suggested tags from AI.</p>
75+
}
76+
else
77+
{
78+
<div class="scrollable-container">
79+
<SfChip EnableDelete="true" @ref="AIChip" Chips="@aiTags">
80+
<ChipEvents Deleted="@AIDeleted"></ChipEvents>
81+
</SfChip>
82+
</div>
83+
}
84+
<div class="button-container">
85+
<SfButton CssClass="e-btn e-outline e-primary" OnClick="SaveTags" Disabled=@isSave>Save AI Tags</SfButton>
86+
<SfButton CssClass="e-btn e-outline e-primary" OnClick="AddTags">Generate AI Tags</SfButton>
9487
</div>
95-
}
96-
<div class="button-container">
97-
<SfButton CssClass="e-btn e-outline e-primary" OnClick="SaveTags" Disabled=@isSave>Save AI Tags</SfButton>
98-
<SfButton CssClass="e-btn e-outline e-primary" OnClick="AddTags">Generate AI Tags</SfButton>
9988
</div>
10089
</div>
101-
</div>
102-
}
90+
}
10391
</div>
10492
</div>
10593

blazor/SyncfusionAISamples/Components/Pages/FileManager/FileManagerTagSearch.razor.cs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,28 @@ namespace SyncfusionAISamples.Components.Pages.FileManager
88
public partial class FileManagerTagSearch
99
{
1010
SfFileManager<FileManagerDirectoryContent>? FileManager;
11-
SfChip AIChip;
12-
SfChip Chip;
11+
SfChip? AIChip;
12+
SfChip? Chip;
1313
private string FileManagerId { get; set; } = "FileManager_" + Guid.NewGuid().ToString("N");
1414
private bool isTag { get; set; } = false;
1515
private bool isSave { get; set; } = true;
1616
private string promptQuery = string.Empty;
1717
public List<ToolBarItemModel> Items = new List<ToolBarItemModel>(){
18-
new ToolBarItemModel() { Name = "NewFolder" },
19-
new ToolBarItemModel() { Name = "Cut" },
20-
new ToolBarItemModel() { Name = "Copy" },
21-
new ToolBarItemModel() { Name = "Paste" },
22-
new ToolBarItemModel() { Name = "Delete" },
23-
new ToolBarItemModel() { Name = "Download" },
24-
new ToolBarItemModel() { Name = "Rename" },
25-
new ToolBarItemModel() { Name = "SortBy" },
26-
new ToolBarItemModel() { Name = "Refresh" },
27-
new ToolBarItemModel() { Name = "Selection" },
28-
new ToolBarItemModel() { Name = "View" },
29-
new ToolBarItemModel() { Name = "Details" },
30-
};
31-
private string[] fileTags { get; set; } = new string[] { };
32-
private string[] aiTags { get; set; } = new string[] { };
18+
new ToolBarItemModel() { Name = "NewFolder" },
19+
new ToolBarItemModel() { Name = "Cut" },
20+
new ToolBarItemModel() { Name = "Copy" },
21+
new ToolBarItemModel() { Name = "Paste" },
22+
new ToolBarItemModel() { Name = "Delete" },
23+
new ToolBarItemModel() { Name = "Download" },
24+
new ToolBarItemModel() { Name = "Rename" },
25+
new ToolBarItemModel() { Name = "SortBy" },
26+
new ToolBarItemModel() { Name = "Refresh" },
27+
new ToolBarItemModel() { Name = "Selection" },
28+
new ToolBarItemModel() { Name = "View" },
29+
new ToolBarItemModel() { Name = "Details" },
30+
};
31+
private List<ChipItem> fileTags = new List<ChipItem>();
32+
private List<ChipItem> aiTags = new List<ChipItem>();
3333
public string[] FileItems = new string[] { "Manage Tags", "|", "Cut", "Copy", "|", "Delete", "Rename", "|", "Details" };
3434
public string[] FolderItems = new string[] { "Open", "|", "Cut", "Copy", "Paste", "|", "Delete", "Rename", "|", "Details" };
3535

@@ -148,12 +148,12 @@ private async Task AddTags()
148148
string tagsString = await GetTagsFromAI(fileContent);
149149
try
150150
{
151-
string[] tagsArray = Regex.Split(tagsString, @"\r\n|\n\n|\n")
151+
var tagsArray = Regex.Split(tagsString, @"\r\n|\n\n|\n")
152152
.Where(tag => !string.IsNullOrWhiteSpace(tag))
153153
.Select(tag => tag.Substring(tag.IndexOf(' ') + 1).Trim())
154-
.ToArray();
155-
aiTags = tagsArray;
156-
isSave = false;
154+
.ToList();
155+
aiTags = tagsArray.Select(tag => new ChipItem { Text = tag }).ToList();
156+
isSave = aiTags.Count == 0 ? true : false;
157157
}
158158
catch (Exception e)
159159
{
@@ -168,24 +168,24 @@ private void SaveTags()
168168
{
169169
var data = FileManager?.GetSelectedFiles();
170170
List<string> tagsList = new List<string>();
171-
foreach (var item in AIChip.Chips)
171+
foreach (var item in AIChip?.Chips)
172172
{
173173
tagsList.Add(item.Text);
174174
}
175-
aiTags = tagsList.ToArray();
175+
aiTags = tagsList.Select(tag => new ChipItem { Text = tag }).ToList();
176176
string filePath = FileManagerService.contentRootPath + data?.FirstOrDefault()?.FilterPath + data?.FirstOrDefault()?.Name;
177177
string fileContent = "File Named as " + data?.FirstOrDefault()?.Name;
178178
FileManagerService.UpdateTagsToFile(filePath, aiTags);
179-
fileTags = FileManagerService.GetTagsFromFile(filePath);
180-
aiTags = new string[] { };
179+
fileTags = FileManagerService.GetTagsFromFile(filePath).Select(tag => new ChipItem { Text = tag }).ToList();
180+
aiTags = null;
181181
isSave = true;
182182
}
183183

184184
private void Deleted(ChipDeletedEventArgs args)
185185
{
186186
if (Chip.Chips.Count == 0)
187187
{
188-
fileTags = new string[] { };
188+
fileTags = null;
189189
isSave = true;
190190
}
191191
var data = FileManager?.GetSelectedFiles();
@@ -197,24 +197,24 @@ private void AIDeleted(ChipDeletedEventArgs args)
197197
{
198198
if (AIChip.Chips.Count == 0)
199199
{
200-
aiTags = new string[] { };
200+
aiTags = null;
201201
isSave = true;
202202
}
203203
}
204204

205205
private void CloseTagContainer()
206206
{
207207
this.isTag = false;
208-
aiTags = new string[] { };
209-
fileTags = new string[] { };
208+
aiTags = null;
209+
fileTags = null;
210210
}
211211

212212
private async Task OnMenuClick(MenuClickEventArgs<FileManagerDirectoryContent> args)
213213
{
214214
if (args.Item.Text == "Manage Tags")
215215
{
216216
string filePath = FileManagerService.contentRootPath + args.FileDetails.FirstOrDefault()?.FilterPath + args.FileDetails.FirstOrDefault()?.Name;
217-
fileTags = FileManagerService.GetTagsFromFile(filePath);
217+
fileTags = FileManagerService.GetTagsFromFile(filePath).Select(tag => new ChipItem { Text = tag }).ToList();
218218
this.isTag = true;
219219
}
220220
}

blazor/SyncfusionAISamples/Components/Pages/FileManager/SmartFileManager.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
@using Syncfusion.Blazor.Spinner;
2121
@using Syncfusion.Blazor.Buttons;
2222
@inject FileManagerService FileManagerService
23-
@inject AzureAIService openAIService
23+
@inject AzureAIService AzureAIService
2424
@inject NavigationManager NavigationManager
2525

2626
<title>Syncfusion - AI Opeartions with FileManager</title>
@@ -48,7 +48,7 @@
4848
</div>
4949
</div>
5050
<div>
51-
<SfDialog ID="Summary" Width="600" Height="90%" ZIndex="1000" EnableResize="true" AllowDragging="true" IsModal=true ShowCloseIcon="true" @bind-Visible="@IsDialogVisible" Target=@("#"+FileManagerId)>
51+
<SfDialog ID="Summary" Width="600" Height="90%" ZIndex="1000" EnableResize="true" AllowDragging="true" IsModal=true ShowCloseIcon="true" @bind-Visible="@IsDialogVisible" Target=@("#" + FileManagerId)>
5252
<DialogTemplates>
5353
<Header>@DialogTitle</Header>
5454
<Content>

blazor/SyncfusionAISamples/Components/Pages/FileManager/SmartFileManager.razor.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,27 @@ public partial class SmartFileManager
2222
private string promptQuery = string.Empty;
2323
private string DialogContent = string.Empty;
2424
private static readonly HashSet<string> AllowedFileTypes = new HashSet<string>
25-
{
26-
".txt",
27-
".docx",
28-
".pdf"
29-
};
25+
{
26+
".txt",
27+
".docx",
28+
".pdf"
29+
};
3030
public List<ToolBarItemModel> Items = new List<ToolBarItemModel>(){
31-
new ToolBarItemModel() { Name = "NewFolder" },
32-
new ToolBarItemModel() { Name = "Cut" },
33-
new ToolBarItemModel() { Name = "Copy" },
34-
new ToolBarItemModel() { Name = "Paste" },
35-
new ToolBarItemModel() { Name = "Delete" },
36-
new ToolBarItemModel() { Name = "Download" },
37-
new ToolBarItemModel() { Name = "Rename" },
38-
new ToolBarItemModel() { Name = "SortBy" },
39-
new ToolBarItemModel() { Name = "Refresh" },
40-
new ToolBarItemModel() { Name = "Organize", Text="Organize", TooltipText="Organize", PrefixIcon="e-icons e-folder", Visible=true },
41-
new ToolBarItemModel() { Name = "Quick Summary", Text="Quick Summary", TooltipText="Get a quick summary of the selected file using AI", PrefixIcon="e-icons e-print-layout", Visible=false },
42-
new ToolBarItemModel() { Name = "Selection" },
43-
new ToolBarItemModel() { Name = "View" },
44-
new ToolBarItemModel() { Name = "Details" },
45-
};
31+
new ToolBarItemModel() { Name = "NewFolder" },
32+
new ToolBarItemModel() { Name = "Cut" },
33+
new ToolBarItemModel() { Name = "Copy" },
34+
new ToolBarItemModel() { Name = "Paste" },
35+
new ToolBarItemModel() { Name = "Delete" },
36+
new ToolBarItemModel() { Name = "Download" },
37+
new ToolBarItemModel() { Name = "Rename" },
38+
new ToolBarItemModel() { Name = "SortBy" },
39+
new ToolBarItemModel() { Name = "Refresh" },
40+
new ToolBarItemModel() { Name = "Organize", Text="Organize", TooltipText="Organize", PrefixIcon="e-icons e-folder", Visible=true },
41+
new ToolBarItemModel() { Name = "Quick Summary", Text="Quick Summary", TooltipText="Get a quick summary of the selected file using AI", PrefixIcon="e-icons e-print-layout", Visible=false },
42+
new ToolBarItemModel() { Name = "Selection" },
43+
new ToolBarItemModel() { Name = "View" },
44+
new ToolBarItemModel() { Name = "Details" },
45+
};
4646

4747
public string[] FileItems = new string[] { "Quick Summary", "Cut", "Copy", "|", "Delete", "Rename", "|", "Details" };
4848
public string[] FolderItems = new string[] { "Organize", "|", "Open", "|", "Cut", "Copy", "Paste", "|", "Delete", "Rename", "|", "Details" };
@@ -301,7 +301,7 @@ public async Task SummarizeAsync(bool isFile, string type, string filterPath, st
301301
string promptQuery = "You are a helpful assistant. Your task is to analyze the provided text and generate a short summary. Provide the summary with highlighted topics in ordered list HTML format and it should be ready for execution :\n\n Do not provide codeblock prefixes or introductory texts such as ``` or html, etc.";
302302
string query = promptQuery + fileContent;
303303

304-
string finalSummary = await openAIService.GetCompletionAsync(query, false);
304+
string finalSummary = await AzureAIService.GetCompletionAsync(query, false);
305305

306306
this.DialogContent = !string.IsNullOrEmpty(finalSummary) ? finalSummary : "Please provide a proper file content to summarize.";
307307
this.isContentGenerating = false;

blazor/SyncfusionAISamples/Components/Service/FileManagerService.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using SyncfusionAISamples.Service;
66
using System.IO.Compression;
77
using System.Text.RegularExpressions;
8+
using Syncfusion.Blazor.Buttons;
89

910
namespace FileManagerAI.Services
1011
{
@@ -24,12 +25,11 @@ public class FileManagerService
2425
public static string DemoDirectoryName = "DemoBaseDirectory";
2526
public string DemoBaseDirectory = "wwwroot\\" + DemoDirectoryName;
2627
public TagService TagService = new TagService();
27-
private AzureAIService openAIService;
28+
public AzureAIService OpenAIService { get; set; }
2829
public Dictionary<string, EmbeddingF32> FileEmbeddings { get; set; } = new Dictionary<string, EmbeddingF32>();
2930

30-
public FileManagerService(IWebHostEnvironment hostingEnvironment, AzureAIService azureAIService)
31+
public FileManagerService(IWebHostEnvironment hostingEnvironment, AzureAIService openAIService)
3132
{
32-
openAIService = azureAIService;
3333
this.basePath = hostingEnvironment.ContentRootPath;
3434
this.DemoBaseDirectory = Path.Combine(this.basePath, this.DemoBaseDirectory);
3535
this.DefaultDirectory = Path.Combine(this.basePath, this.DefaultDirectory);
@@ -41,6 +41,7 @@ public FileManagerService(IWebHostEnvironment hostingEnvironment, AzureAIService
4141
{
4242
_ = Task.Run(() => CleanUpOldDirectories());
4343
}
44+
OpenAIService = openAIService;
4445
}
4546

4647
public void RootFolder(string name)
@@ -210,7 +211,7 @@ public async Task<string> GetCategorizationResponse(string prompt, List<FileMana
210211
{
211212
message += $"File: {file.Name}, Type: {file.Type}\n";
212213
}
213-
var result = await openAIService.GetCompletionAsync((prompt + message), false);
214+
var result = await OpenAIService.GetCompletionAsync(prompt + message, false);
214215
return result;
215216
}
216217

@@ -1340,23 +1341,23 @@ public async Task TagInitialFiles()
13401341
bool hasPermission = parentsHavePermission(fileDetails);
13411342
if (hasPermission)
13421343
{
1343-
string[] tags = TagService.GetTags(fileDetails.Name);
1344+
List<ChipItem> tags = TagService.GetTags(fileDetails.Name).Select(tag => new ChipItem { Text = tag }).ToList();
13441345
string filePath = Path.Combine(this.contentRootPath, file.DirectoryName, file.Name);
13451346
UpdateTagsToFile(filePath, tags);
13461347
}
13471348
}
13481349

13491350
}
13501351

1351-
public void UpdateTagsToFile(string filePath, string[] newTags)
1352+
public void UpdateTagsToFile(string filePath, List<ChipItem> newTags)
13521353
{
13531354
string adsPath = filePath + ":tags";
1354-
string[] existingTags = GetTagsFromFile(filePath);
1355+
List<ChipItem> existingTags = GetTagsFromFile(filePath).Select(tag => new ChipItem { Text = tag }).ToList();
13551356
var combinedTags = existingTags.Union(newTags).ToArray();
13561357
using (FileStream fs = new FileStream(adsPath, FileMode.OpenOrCreate, FileAccess.Write))
13571358
using (StreamWriter writer = new StreamWriter(fs))
13581359
{
1359-
string tagsString = string.Join(";", combinedTags);
1360+
string tagsString = string.Join(";", combinedTags.Select(tag => tag.Text));
13601361
writer.Write(tagsString);
13611362
}
13621363
}

0 commit comments

Comments
 (0)